Search code examples
androidbox2djbox2d

JBox2d: Negative gravity results in object going up?


I am a newbie learner to JBox2D. I was just trying JBox2D for the first time on Android(I know Android development and I'm good in it) because my project needed physics.

Now, the tutorials and "Official User Manual" of Box2D said that negative gravity would result in objects being attracted downwards. But, in my case the object is being attracted upwards when I set Vec2's second parameter to be negative! Weird.

Here's the code which results in a circle shape going up on its own:

The Gravity:

Vec2 gravity = new Vec2(0.0f, -50.0f);
boolean doSleep = true;
world = new World(gravity, doSleep);

The circle shape is being made by following code:

    //body definition
    BodyDef bd = new BodyDef();
    bd.position.set(200, 500);  
    bd.type = BodyType.DYNAMIC;

    //define shape of the body.
    CircleShape cs = new CircleShape();
    cs.m_radius = 10f;  

    //define fixture of the body.
    FixtureDef fd = new FixtureDef();
    fd.shape = cs;
    fd.density = 1f;
    fd.friction = 0.2f;        
    fd.restitution = 0.8f;

    //create the body and add fixture to it
    body =  world.createBody(bd);
    body.createFixture(fd);

And I'm using SurfaceView canvas to draw:

canvas.drawCircle(body.getPosition().x, body.getPosition().y, 10, paint);

And stepp-ing as follows:

float timeStep = 1.0f / 60.f;
int velocityIterations = 6;
int positionIterations = 2;

world.step(timeStep, velocityIterations, positionIterations);

So, what's wrong within my code? I am unable to identify the mistake I've done.

Also, I'm making a tennis-like 2D game on Android for which I'll be using JBox2D. So, can anybody tell me a tutorial/book on JBox2D? Though I googled vigorously, I couldn't find a good tutorial on it. (Though Box2D seems to be much popular instead of JBox2D)

I would be extremely grateful if someone could help me out here. Thank you.


Solution

  • In Box2D there is standard coordinate system: Y directed up, X to the right. In the graphic systems, usual, coordinate system has Y directed down, because window has static top-left corner. Looks like at your graphic system all the same. So, what in Box2D is moving down, you see as moving up.

    It is the irritable problem, and directing gravity up is not the best solution. If you change only gravity, then you will need think about up-down problem in many other cases, for example, when define bodies, apply forces and so on. The most irritable, that it is not easy to understand, how physic coordinates conform graphic (for example, in one of my projects I had to draw points on paper, then turn paper back, rotate on 180 grades and look on the light :).

    You can't change Box2D coordinate system, but, most likely, you can easy change coordinate system of graphic system by changing translation matrix. For example, in OpenGL it looks like this:

    glScalef(1.0, -1.0, 1.0);
    

    But take attention, after this, all that have positive Y coordinate will be not visible on the screen (it will be above the top edge of the window). So, you will need work with negative coordinates. If you don't want this, you can translate matrix down like this:

    glTranslatef(0.0, -windowHeight.0, 0.0)
    

    But before, think what to do if window would be resized.

    About second question. I doubt whether you can find anywhere tutorial or book for JBox2D. JBox2D is port of Box2D (that means, it is exact copy of Box2D), and writing special book for it looks strange. Learn Box2D, and you will have no problem with JBox2D. For example, you can look there.