1

I'm doing some initial experiments for a CS course, where I'm trying to use JBox2D as a physics library and drawing using JavaFX. I have had some luck draw circles that fall from the top of the screen, but now I have issues doing the same with rectangles (or any other shape).

I'm able to both draw and simulate, but I cannot figure out how to rotate the drawn nodes in JavaFX using values from the Body objects.

Here's my code for the bodies using JBox2D:

    @Override
    public Body createBody() {

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        bd.position.set(centerX, centerY);

        PolygonShape ps = new PolygonShape();
        ps.setAsBox(2.0f,4.0f);

        FixtureDef fd = new FixtureDef(); 
        fd.shape = ps;
        fd.density = 0.9f;
        fd.friction = 0.3f; 
        fd.restitution = 0.5f;

        Body b = PhysicalScene.world.createBody(bd);  

        b.createFixture(fd);

        return b;
    }
}

This is drawn using nodes in JavaFX - as written here:

    @Override
    public Node create() {
        Rectangle r = new Rectangle();
        r.setHeight(20);
        r.setWidth(40);
        r.setStroke(Color.RED);
        r.setLayoutX(Physics.toPixelX(centerX)); 
        r.setLayoutY(Physics.toPixelY(centerY));

        // THIS IS WHAT I'M MISSING
        // r.setRotate(/* some value from Body */);

        return r;
    }

The code isn't pretty, but I hope you get the idea. I am able to rotate the nodes by inserting arguments in the method, but do not know how to get these values from the Body objects themselves.

I hope you can help, thanks!

Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
freddy
  • 17
  • 8

0 Answers0