[LWJGL] Box2D Ground Collision not working anymore?

Hi,

I am learning JBox2D using Oskar’s tutorials (http://www.youtube.com/watch?v=ZKJC2cloIqc),

and I’ve come across an issue (or maybe I’m a bit thick and I’ve missed something obvious)

In the tutorial Oskar sets the code up like this:

glOrtho(0, 320, 0, 240, 1, -1);

but I need to use it like this

glOrtho(0, 320, 240, 0, 1, -1);

So I’ve managed to flip everything up the right way I believe, except for the ground. It doesnt seem to render in the right place or the box doesnt collide with it.

Here is the render code for the ground:

        BodyDef groundDef = new BodyDef();
        groundDef.position.set(0, height);
        groundDef.type = BodyType.STATIC;
        PolygonShape groundShape = new PolygonShape();
        groundShape.setAsBox(1000, 0);
        Body ground = world.createBody(groundDef);
        FixtureDef groundFixture = new FixtureDef();
        groundFixture.density = 1;
        groundFixture.restitution = 0.3f;
        groundFixture.shape = groundShape;
        ground.createFixture(groundFixture);
        bodies.add(ground);

and here is a link to the rest of the code if you want:
http://pastebin.java-gaming.org/e8f9408558e

Thanks,

  • Dan

Well first let’s get the obvious out of the way. What is “height” (the variable you pass into the groundDef)?

Oh wait, I just dug into the class and found height is set to 480. Now, I am assuming you want to ground to render on the “bottom” of the screen. First off, why is your glOrtho call setting the matrix up to only take up around half the screen? I don’t see why you do that. Change glOrtho to this:


glOrtho(0, width, height, 0, 1, -1);

Second, (if I remember this correctly), you are setting up the origin to be in the bottom left hand corner. I think. Your height variable is set to 480, so of course it will render at the top of the screen. Change that to be 0. Again, I can’t remember if you are using a bottom or top origin, so I might be completely wrong!

Also note I’ve never used Box2D, so I can’t help with your collision code.

This is the original specification of [icode]glOrtho[/icode] function.


void glOrtho(GLdouble  left,     GLdouble  right,
             GLdouble  bottom,   GLdouble  top,
             GLdouble  nearVal,  GLdouble  farVal)

The LWJGL’s specification will be like this.


/**
 * glOrtho describes a transformation that produces a parallel projection.
 * The current matrix (see glMatrixMode) is multiplied by this matrix
 * and the result replaces the current matrix
 *
 * @param left    Left most coordinate of the window
 * @param right   Right most coordinate of the window
 * @param bottom  Bottom most coordinate of the window
 * @param top     Top most coordinate of the window
 * @param nearVal The near clipping plane
 * @param farVal  The far clipping plane
 */
void glOrtho(double left,    double right,
             double bottom,  double top,
             double nearVal, double farVal)

The documentation is added by me from here.

Great, but when I fix it so that it follows that pattern

glOrtho(0, width, height, 0, 1, -1);

The box falls up on to the platform at the top, so I change the gravity so it falls down, and I tried to move the platform to the bottom, but that didnt work? The box just falls down.

        BodyDef groundDef = new BodyDef();
        groundDef.position.set(0, height);
        groundDef.type = BodyType.STATIC;
        PolygonShape groundShape = new PolygonShape();
        groundShape.setAsBox(1000, 0);
        Body ground = world.createBody(groundDef);
        FixtureDef groundFixture = new FixtureDef();
        groundFixture.density = 1;
        groundFixture.restitution = 0.3f;
        groundFixture.shape = groundShape;
        ground.createFixture(groundFixture);
        bodies.add(ground);

Ideas?
Thanks for the Ortho documentation :),

  • Dan

The code you just posted is for the ground correct? Not for the box? We need the code for the platform and the box then if you want help!

Well using the ground as a platform was working, and the rest of the code is in the first post

  • Dan

I don’t entirely understand your issue then, do you not want the box to move? What happens that you don’t want to happen?

The issue is, when the logic is inverted, and the ceiling is the floor, the box lands on the ground fine and you can move it around,

But when I fix the logic so that the ceiling is 0 as opposed to the window height, and I make the box fall down, the ground acts as if it isnt there.

Sorry if that doesnt make too much sense, I’m trying to explain it in the best way possible

  • Dan

groundDef.position.set(0, height);

This is the line of code that stands out to me the most. Again, I’ve never used box2D, so I’m just reading documentation trying to figure this out, so I may be wrong. But, if your origin is at the bottom left hand corner, then setting the position to the height of the window wouldn’t work, you need to set the position to be 0, or wherever your ground body is.

Well if I make it 0, 0 then it places on the ceiling :confused:

How should I get it on the bottom?

  • Dan

Ah, never mind your origin is in the top left. Hmm… ok I’ll take a look over your code again and edit this reply if I come up with something.

Think it depends on your coordinate system. If your ortho call is


glOrtho(0, width, height, 0, 1, -1);

it sets the coordinate system in a way that (0,0) will be top-left and (width,height) will be bottom-right. So


groundDef.position.set(0, height);

should move the ground to the bottom. You can set the width of the ground to the width of the window and this fixes the problem, I think. No JBox2D experience for me too.

Thats what I did, which is why I’m trying to figure out why it doesnt work.

Thanks,

  • Dan

groundShape.setAsBox(1000, 0);

Maybe this is it? From the docs:


public void setAsBox(float hx,
            float hy)
Build vertices to represent an axis-aligned box.
Parameters:
hx - the half-width.
hy - the half-height.

So, logically you should set the height to at least be 1 to have a height “thickness”. I imagine this should do it, this seems to be how Box2D creates it’s collision meshes.

Set the box position back to 0, height. You were correct with that. But definitely change the setAsBox function, that should do it!

Well it works if I make the position 0, 0 and keep the size as 1000, 0 but not if I make the position 0, height

Thanks,

  • Dan

I still think you need to change that method. Having 0 for height is telling Box2D you don’t want the object to collide on the y axis. Try specifying a negative y variable maybe? Can you post the code for when you change the setAsBox function?

Nope both

groundShape.setAsBox(1000, 1);

or this

groundShape.setAsBox(1000, -1);

dont work :frowning:

Thanks,

  • Dan

OK I just read that Box2D won’t detect collisions if you don’t apply gravity to your scene, which makes sense because why would you waste processor time calculating collisions when the meshes “shouldn’t” move? Do you apply gravity to your scene at all?

Edit, never mind you do… I’ll keep looking.

I’m not sure, but I think giving it a try is worth it. Maybe this.


BodyDef groundDef = new BodyDef();
@@groundDef.position.set(0, height); // Here
groundDef.type = BodyType.STATIC;
PolygonShape groundShape = new PolygonShape();
@@groundShape.setAsBox(1000, height); // And Here
Body ground = world.createBody(groundDef);
FixtureDef groundFixture = new FixtureDef();
groundFixture.density = 1;
groundFixture.restitution = 0.3f;
groundFixture.shape = groundShape;
ground.createFixture(groundFixture);

Should fix it. Just changed the coordinates to move to top-left as origin from his source code. Say if it works.

But wouldn’t that just set the ground to have a collision box in the y axis equal to the entire height of the window?