javajbox2d

JBox2D Why is gravity reversed?


Good evening everyone. I'm requesting your help ! I'm discovering Box2D but I'm already struggling with a few things :

My gravity is reversed and I don't understand why. And my second problem is that I can't manage to draw it in a debugged way. I had to manually draw it to actually see something.

Here is a code sample to play with !

 package tests;

 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;

 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.Timer;

 import engine.jbox2d.collision.shapes.CircleShape;
 import engine.jbox2d.collision.shapes.PolygonShape;
 import engine.jbox2d.common.Vec2;
 import engine.jbox2d.dynamics.Body;
 import engine.jbox2d.dynamics.BodyDef;
 import engine.jbox2d.dynamics.BodyType;
 import engine.jbox2d.dynamics.FixtureDef;
 import engine.jbox2d.dynamics.World;

 public class JBox2DTest extends JPanel
 {
    private static final long serialVersionUID = 7173873195721096625L;

    // model
    private World            world;
    private Ball             ball;

    public JBox2DTest()
    {
        // Création du monde
        world = new World(new Vec2(0.0f, -9.81f));

        // Création d'un sol
        new Ground(world);

        // Création d'une balle
        this.ball = new Ball(world);

        //Création de la frame
        JFrame frame = new JFrame();
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setVisible(true);

        //Loop
        Timer simulationTimer = new Timer(10, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                world.step(1.0f / 60.0f, 6, 2);
                repaint();
            }
        });
        simulationTimer.start();
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        ball.draw((Graphics2D) g);
    }

    public static void main(String[] args)
    {
        new JBox2DTest();
    }
 }

 class Ground
 {
    Body body;

    public Ground(World world)
    {
        BodyDef groundBodyDef = new BodyDef();
        groundBodyDef.position.set(0.0f, -10.0f);
        body = world.createBody(groundBodyDef);
        PolygonShape groundBox = new PolygonShape();
        groundBox.setAsBox(800.0f, 10);
        body.createFixture(groundBox, 0.0f);
    }
 }

 class Ball
 {
    private Body body;

    public Ball(World world)
    {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DYNAMIC; // Sujet aux forces
        bodyDef.position.set(800/2, 600/2);
        this.body = world.createBody(bodyDef);
        CircleShape dynamicCirle = new CircleShape();
        dynamicCirle.setRadius(10f);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = dynamicCirle;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        fixtureDef.restitution = 0.3f;
        body.createFixture(fixtureDef);
    }

    public void draw(Graphics2D g2d)
    {
        g2d.setColor(Color.RED);

        Vec2 ballPos = body.getPosition();
        float ballRadius = body.getFixtureList().getShape().getRadius();

        g2d.drawOval(
            (int) (ballPos.x - ballRadius),
            (int) (ballPos.y - ballRadius),
            (int) (ballRadius * 2),
            (int) (ballRadius * 2)
        );
    }
 }

Thanks a lot for your help ! Best regards, Alann


Solution

  • It apparently depends on the implementation of Box2D you are using. Gravity may been reversed in your version:

    world = new World(new Vec2(0.0f, -9.81f));
    

    should be without the minus:

    world = new World(new Vec2(0.0f, 9.81f));