1

I'm having some problems with JBox2D. I've created a ball and world and everything as far as I can tell correctly, but when I call the world.step() method it dosen't make the ball fall from gravity.

Here is my object creation:

package objects;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.lwjgl.opengl.GL11;

import util.GLColor;
import util.Location;
import util.Size;

public class PlayerCircle extends DefaultObject{


GLColor c = new GLColor(25,255,255);



public PlayerCircle(Location l,Size s){
  super(l,s);
  load(l.getX(),l.getY());
}

BodyDef bd = new BodyDef();
FixtureDef fd = new FixtureDef();
public static CircleShape  cd = new CircleShape();

public void load(double x, double y){


     bd.type = BodyType.DYNAMIC;
    // bd.position = new Vec2(0.0f, -10.0f);
     cd.m_radius = s.getRadius()/Main.Main.scaleX;

     //cd.m_p.set((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
     bd.position = new Vec2((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
     fd.friction = .1f;
     fd.density = 2f;
     fd.shape = cd;

     Main.Main.getWorld().createBody(bd).createFixture(fd);
    // bd.allowSleep = false;
}
}

Heres the code where the main loop is:

public void run() throws LWJGLException, InterruptedException{
    Vec2 gravity = new Vec2(2f, -.1f);
    world = new World(gravity, true);
    Display.setDisplayMode(new DisplayMode(900,700));
    Display.create();
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, scaleX, 0, scaleY, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    level = new Level1();
    level.load();

    level.start();


    float tempStep = 1.0f/60.0f;
    int VI = 6;
    int POSI = 2;
    int a = 0;

    while(!done){
        //System.out.println(world.getContactCount() + "  "+world.getGravity().x);

        world.step(tempStep, VI, POSI);

         Display.update();
         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

        logic();

        if (Display.isVisible() || Display.isDirty()) {
             render();
        }


        if(Display.isCloseRequested()){
            done = true;
        }
        Thread.sleep(1);
    }
}
}

Test Class

package test;

import objects.PlayerCircle;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;

import util.Location;
import util.Size;

public class Test {
static Vec2 gravity = new Vec2(2f, -.5f);

static float tempStep = 1.0f/60.0f;
static int VI = 6;
static int POSI = 2;
public static World world = new World(gravity,true);
public static void main(String args[]){

    PlayerCircle pc = new PlayerCircle(new Location(400,5000),new Size(10));
    while(true){
        world.step(tempStep, VI, POSI);
        try{Thread.sleep(10);}catch(Exception e){}
        System.out.println(pc.bd.position.y);       

    }

}
}

Result from print

7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
7.142857
LOZ
  • 1,169
  • 2
  • 16
  • 43
  • Do you actually create a `PlayerCircle` instance`? Where in the above code does that happen? – Thomas Oct 10 '11 at 16:32
  • Its being created in the level code ( a seperate class) `public PlayerCircle player = new PlayerCircle(new Location(10,300), new Size(20));` – Double_0_negative Oct 10 '11 at 17:01
  • Ok, and where is that code called? I assume `World` is a JBox2D class, so where in between world creation and calling `step(...)` is that done? – Thomas Oct 10 '11 at 19:42
  • I updated my second code block to include the entire block of code. – Double_0_negative Oct 10 '11 at 20:45
  • Ok, I have just been skimming over the documentation and your code looks good so far. Did you verify that it is not the display that is not updating correctly? I don't know LWJGL that well, but normally you'd clear the buffers before calling update. Also, what does `render()` do? – Thomas Oct 10 '11 at 22:59
  • Render calls the draw() function on each object. I did verify that its not a problem with just the display part because i did a system.out.println on the location var – Double_0_negative Oct 11 '11 at 15:04
  • I made a test class to test it without doing any drawing, just adding it to a world and simulating it. i added the code and output above – Double_0_negative Oct 13 '11 at 14:36

1 Answers1

0

Get the y position from the Body object, not from the BodyDef object.

To do this, store the Body object returned by Main.Main.getWorld().createBody(bd) and access its position by pc.yourBodyVariableNameHere.m_xf.position.y

Mayoneez
  • 441
  • 5
  • 19