Hi guys,
I am trying to make square, JBox2d in combination with JavaFX. I found on the internet manual for making this work with balls (thanks to that guy), but I am not able to make this with squares. I have no clue which coordinates and heights I have to convert and when.
I know JavaFX uses coordinate system with point starting at the top left corner (pixels) and JBox2D uses it from the centre of the screen (metres). I am also aware of fact, than when we are making square, it takes only half of the height/width.
So my problem is, I dont know yet, how to make object in JBox2D and JavaFX on the same spot on screen.
There is code I use:
This is copy pasted from the guy that made manual for balls.
Class for Ball
Square class
and main class
Balls are there for seeing where is the invisible body of square made in JBox2D, as they bounce around it.
Thanks in advance.
I am trying to make square, JBox2d in combination with JavaFX. I found on the internet manual for making this work with balls (thanks to that guy), but I am not able to make this with squares. I have no clue which coordinates and heights I have to convert and when.
I know JavaFX uses coordinate system with point starting at the top left corner (pixels) and JBox2D uses it from the centre of the screen (metres). I am also aware of fact, than when we are making square, it takes only half of the height/width.
So my problem is, I dont know yet, how to make object in JBox2D and JavaFX on the same spot on screen.
There is code I use:
This is copy pasted from the guy that made manual for balls.
Code: Select all
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
public class Utils {
//Create a JBox2D world.
public static final World world = new World(new Vec2(0.0f, -5.0f), true);
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int BALL_SIZE = 10;
public static final int NO_OF_BALLS = 300;
//Ball gradient
private final static LinearGradient BALL_GRADIENT = new LinearGradient(0.0, 0.0, 1.0, 0.0, true, CycleMethod.NO_CYCLE, new Stop[]{new Stop(0, Color.WHITE), new Stop(1, Color.RED)});
//This method adds a ground to the screen.
public static void addGround(float width, float height) {
PolygonShape ps = new PolygonShape();
ps.setAsBox(width, height);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
BodyDef bd = new BodyDef();
bd.position = new Vec2(0.0f, -10f);
world.createBody(bd).createFixture(fd);
}
//This method creates a walls.
public static void addWall(float posX, float posY, float width, float height) {
PolygonShape ps = new PolygonShape();
ps.setAsBox(width, height);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 1.0f;
fd.friction = 1.0f;
BodyDef bd = new BodyDef();
bd.position.set(posX, posY);
Utils.world.createBody(bd).createFixture(fd);
}
//This gives a look and feel to balls
public static LinearGradient getBallGradient(Color color) {
if (color.equals(Color.RED)) {
return BALL_GRADIENT;
} else {
return new LinearGradient(0.0, 0.0, 1.0, 0.0, true, CycleMethod.NO_CYCLE, new Stop[]{new Stop(0, Color.WHITE), new Stop(1, color)});
}
}
//Convert a JBox2D x coordinate to a JavaFX pixel x coordinate
public static float jBoxToFxPosX(float posX) {
float x = WIDTH*posX / 100.0f;
return x;
}
//Convert a JavaFX pixel x coordinate to a JBox2D x coordinate
public static float fxToJboxPosX(float posX) {
float x = (posX*100.0f*1.0f)/WIDTH;
return x;
}
//Convert a JBox2D y coordinate to a JavaFX pixel y coordinate
public static float jBoxToFxPosY(float posY) {
float y = HEIGHT - (1.0f*HEIGHT) * posY / 100.0f;
return y;
}
//Convert a JavaFX pixel y coordinate to a JBox2D y coordinate
public static float fxToJboxPosY(float posY) {
float y = 100.0f - ((posY * 100*1.0f) /HEIGHT) ;
return y;
}
//Convert a JBox2D width to pixel width
public static float jBoxtoPixelWidth(float width) {
return WIDTH*width / 100.0f;
}
//Convert a JBox2D height to pixel height
public static float jBoxtoPixelHeight(float height) {
return HEIGHT*height/100.0f;
}
}Class for Ball
Code: Select all
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.shape.Circle;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
public class Ball {
//JavaFX UI for ball
public Node node;
//X and Y position of the ball in JBox2D world
private float posX;
private float posY;
// private Color color;
//Ball radius in pixels
private int radius;
/**
* There are three types bodies in JBox2D – Static, Kinematic and dynamic In
* this application static bodies (BodyType.STATIC – non movable bodies) are
* used for drawing hurdles and dynamic bodies (BodyType.DYNAMIC–movable
* bodies) are used for falling balls
*/
private BodyType bodyType;
//Gradient effects for balls
private LinearGradient gradient;
public Ball(float posX, float posY) {
this(posX, posY, Utils.BALL_SIZE, BodyType.DYNAMIC, Color.RED);
this.posX = posX;
this.posY = posY;
}
public Ball(float posX, float posY, int radius, BodyType bodyType, Color color) {
this.posX = posX;
this.posY = posY;
this.radius = radius;
this.bodyType = bodyType;
this.gradient = Utils.getBallGradient(color);
node = create();
}
/**
* This method creates a ball by using Circle object from JavaFX and
* CircleShape from JBox2D
*/
private Node create() {
//Create an UI for ball - JavaFX code
Circle ball = new Circle();
ball.setRadius(radius);
ball.setFill(gradient); //set look and feel
/**
* Set ball position on JavaFX scene. We need to convert JBox2D
* coordinates to JavaFX coordinates which are in pixels.
*/
ball.setLayoutX(Utils.jBoxToFxPosX(posX));
ball.setLayoutY(Utils.jBoxToFxPosY(posY));
ball.setCache(true); //Cache this object for better performance
//Create an JBox2D body defination for ball.
BodyDef bd = new BodyDef();
bd.type = bodyType;
bd.position.set(posX, posY);
CircleShape cs = new CircleShape();
cs.m_radius = radius * 0.1f; //We need to convert radius to JBox2D equivalent
// Create a fixture for ball
FixtureDef fd = new FixtureDef();
fd.shape = cs;
fd.density = 0.9f;
fd.friction = 0.3f;
fd.restitution = 0.7f;
/**
* Virtual invisible JBox2D body of ball. Bodies have velocity and
* position. Forces, torques, and impulses can be applied to these
* bodies.
*/
Body body = Utils.world.createBody(bd);
body.createFixture(fd);
ball.setUserData(body);
return ball;
}
}Square class
Code: Select all
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
public class Square {
//JavaFX UI for square
/**
* 1 define body 2 create body 3 create shape 4 create fixture 5 attach
* shape to body
*/
public Node node;
private float posX;
private float posY;
private float sWidth;
private float sHeight;
private static Color color;
// private BodyType bodyType;
private static Rectangle rc;
// private float angle;
// private BodyDef bd;
public Square(float posX, float posY, float sWidth, float sHeight, Color color) {
this.posX = posX;
this.posY = posY;
this.sWidth = sWidth;
this.sHeight = sHeight;
this.color = color;
// this.bodyType = bodyType;
node = create();
}
public Node create() {
//UI for square in JavaFX
rc = RectangleBuilder.create()
.x(posX)
.y(posY)
.width(sWidth)
.height(sHeight)
.fill(Color.AQUA)
// .rotate(45)
.build();
/**
* Set square position on JavaFX scene. We need to convert JBox2D
* coordinates to JavaFX coordinates which are in pixels.
*/
rc.setLayoutX(Utils.jBoxToFxPosX(posX));
rc.setLayoutY(Utils.jBoxToFxPosY(posY)); //also missing methods for converting height and width of square
//Create an JBox2D body definition for square.
BodyDef bd = new BodyDef();
bd.type = BodyType.STATIC;
bd.position.set(posX, posY); //do i have to convert these??
PolygonShape ps = new PolygonShape();
ps.setAsBox(Utils.jBoxtoPixelWidth(sWidth/80),Utils.jBoxtoPixelHeight(sHeight/80)); //wrong!! we need to convert to Jbox equiualent
//fixture for polygon, in this case square
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 0.6f;
fd.friction = 0.3f;
fd.isSensor = false;
Body body = Utils.world.createBody(bd);
body.createFixture(fd);
rc.setUserData(body);
return rc;
}
}
and main class
Code: Select all
import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.jbox2d.dynamics.Body;
public class FallingSquares extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("squares DO SOMETHING");
primaryStage.setFullScreen(false);
primaryStage.setResizable(false);
final Group root = new Group();
final Scene scene = new Scene(root, Utils.WIDTH, Utils.HEIGHT, Color.BLACK);
final Ball[] ball = new Ball[Utils.NO_OF_BALLS];
final Square square = new Square(50.0f, 20.0f, 30.0f, 30.0f, Color.AQUA);
Random r = new Random(System.currentTimeMillis() + 100);
for (int i = 0; i < Utils.NO_OF_BALLS; i++) {
ball[i] = new Ball(r.nextInt(80) + 10, r.nextInt(50) + 120);
}
Utils.addGround(100, 15);
Utils.addWall(1, 100, 1, 100);
Utils.addWall(99, 100, 1, 100);
final Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
Duration duration = Duration.seconds(1.0 / 60.0);
EventHandler<ActionEvent> ae = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
Utils.world.step(2.0f / 60.f, 8, 3);
for (int i = 0; i < Utils.NO_OF_BALLS; i++) {
Body body = (Body) ball[i].node.getUserData();
float xpos = Utils.jBoxToFxPosX(body.getPosition().x);
float ypos = Utils.jBoxToFxPosY(body.getPosition().y);
ball[i].node.setLayoutX(xpos);
ball[i].node.setLayoutY(ypos);
}
}
};
/**
* Set ActionEvent and duration to the KeyFrame. The ActionEvent is
* trigged when KeyFrame execution is over.
*/
KeyFrame frame = new KeyFrame(duration, ae, null, null);
timeline.getKeyFrames().add(frame);
//Create button to start simulation.
final Button startBtn = new Button();
startBtn.setLayoutX((Utils.WIDTH) - Utils.WIDTH + 30);
startBtn.setLayoutY((Utils.HEIGHT - Utils.HEIGHT + 10));
startBtn.setText("Start");
startBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
timeline.playFromStart();
startBtn.setVisible(true);
}
});
final Button pauseBtn = new Button();
pauseBtn.setLayoutX((Utils.WIDTH) - Utils.WIDTH + 80);
pauseBtn.setLayoutY((Utils.HEIGHT - Utils.HEIGHT + 10));
pauseBtn.setText("Pause");
pauseBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
timeline.pause();
pauseBtn.setVisible(true);
}
});
root.getChildren().add(startBtn);
root.getChildren().add(pauseBtn);
root.getChildren().add(square.node);
for (int i = 0; i < Utils.NO_OF_BALLS; i++) {
root.getChildren().add(ball[i].node);
}
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}Balls are there for seeing where is the invisible body of square made in JBox2D, as they bounce around it.
Thanks in advance.