Newbie question

Hello All,

Please forgive if this question or something like this has been asked before.....

I'm having a bit of trouble getting this simple example to work. It seems as though the shape falls, but it's linear velocity (y) does not increase or decrease when it hits the ground. The result is a box that falls but not very realistically (velocity is constant) and it just jitters when it hits the ground. Please advise....

Here is the class:

Code:


import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;


public class Box2dTest {
   
      public int targetFPS = 40; 
       public int timeStep = (1000 / targetFPS); 


 
       private World world; 
       private BodyDef groundBodyDef; 
       private PolygonShape groundShapeDef; 
       private Body actorBody;
    
       public void create() { 
          
            
           Vec2 gravity = new Vec2(0, -10); 
           boolean doSleep = true; 
           world = new World(gravity, doSleep); 
          
               
          
           groundBodyDef = new BodyDef(); 
           groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0)); 
           Body groundBody = world.createBody(groundBodyDef); 
           groundShapeDef = new PolygonShape(); 
           groundShapeDef.setAsBox((float) 50.0, (float) 1); 
           groundBody.createFixture(groundShapeDef, 0); 
       } 
    
       public void addBox() { 
          
          BodyDef actorBodyDef = new BodyDef();
         actorBodyDef.type = org.jbox2d.dynamics.BodyType.DYNAMIC;
   
         
         actorBodyDef.position.set(0, 50);
         actorBody = world.createBody(actorBodyDef);
         
         PolygonShape actorBox = new PolygonShape();
         
         
         
         actorBox.setAsBox(1, 1);
         
         org.jbox2d.dynamics.FixtureDef fixtureDef = new org.jbox2d.dynamics.FixtureDef();
         fixtureDef.shape = actorBox;
         fixtureDef.density = 1;
         fixtureDef.friction = 0.3f;
         fixtureDef.restitution =1f;
         
         actorBody.createFixture(fixtureDef);


       } 
    
       public void update() { 
   
           world.step(timeStep, 8, 3); 

            Vec2 position = actorBody.getPosition(); 
            float angle = actorBody.getAngle(); 
            Vec2 velocity = actorBody.getLinearVelocity();
            System.out.println("Pos: (" + position.x + ", " + position.y + "), vel: " + velocity.y); 
            
       } 
   
   

}


and here is the main loop:

Code:

public static void main(String[] args) {
      
      
      Box2dTest t = new Box2dTest();
      t.create();
      t.addBox();
      
      for(int i=0; i < 60; i++) {
         
         t.update();
         
      }

   }



some output:

Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Pos: (0.0, 48.0), vel: -0.08
Pos: (0.0, 46.0), vel: -0.08
Pos: (0.0, 44.0), vel: -0.08
Pos: (0.0, 42.0), vel: -0.08
Pos: (0.0, 40.0), vel: -0.08
Pos: (0.0, 38.0), vel: -0.08
Pos: (0.0, 36.0), vel: -0.08
Pos: (0.0, 34.0), vel: -0.08
Pos: (0.0, 32.0), vel: -0.08
Pos: (0.0, 30.0), vel: -0.08
Pos: (0.0, 28.0), vel: -0.08
Pos: (0.0, 26.0), vel: -0.08
Pos: (0.0, 24.0), vel: -0.08
Pos: (0.0, 22.0), vel: -0.08
Pos: (0.0, 20.0), vel: -0.08
Pos: (0.0, 18.0), vel: -0.08
Pos: (0.0, 16.0), vel: -0.08
Pos: (0.0, 14.0), vel: -0.08
Pos: (0.0, 12.0), vel: -0.08
Pos: (0.0, 10.0), vel: -0.08
Pos: (0.0, 8.0), vel: -0.08
Pos: (0.0, 6.0), vel: -0.08
Pos: (0.0, 4.0), vel: -0.08
Pos: (0.0, 2.0), vel: -0.08
Pos: (0.0, 0.0), vel: -0.08
Pos: (0.0, -2.0), vel: -0.08
Pos: (0.0, -4.0), vel: -0.08
Pos: (0.0, -6.0), vel: -0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08
Pos: (0.0, -7.9854183), vel: -0.08
Pos: (0.0, -5.9854183), vel: 0.08




Thanks in advance!
Try a smaller timestep. Currently your timestep size is 25 seconds:
Code:
public int targetFPS = 40; 
public int timeStep = (1000 / targetFPS);
Hi there,

I tried

Code:

public int targetFPS = 80;
public int timeStep = (1000 / targetFPS);



as well as a few different extremes and what happens is the velocity becomes larger OR smaller from the get go but it's still a constant that NEVER changes. See output below:

Code:
Pos: (0.0, 48.0), vel: -0.16666667
Pos: (0.0, 46.0), vel: -0.16666667
Pos: (0.0, 44.0), vel: -0.16666667
Pos: (0.0, 42.0), vel: -0.16666667
Pos: (0.0, 40.0), vel: -0.16666667
Pos: (0.0, 38.0), vel: -0.16666667
Pos: (0.0, 36.0), vel: -0.16666667
Pos: (0.0, 34.0), vel: -0.16666667
Pos: (0.0, 32.0), vel: -0.16666667
Pos: (0.0, 30.0), vel: -0.16666667
Pos: (0.0, 28.0), vel: -0.16666667
Pos: (0.0, 26.0), vel: -0.16666667
Pos: (0.0, 24.0), vel: -0.16666667
Pos: (0.0, 22.0), vel: -0.16666667
Pos: (0.0, 20.0), vel: -0.16666667
Pos: (0.0, 18.0), vel: -0.16666667
Pos: (0.0, 16.0), vel: -0.16666667
Pos: (0.0, 14.0), vel: -0.16666667
Pos: (0.0, 12.0), vel: -0.16666667
Pos: (0.0, 10.0), vel: -0.16666667
Pos: (0.0, 8.0), vel: -0.16666667
Pos: (0.0, 6.0), vel: -0.16666667
Pos: (0.0, 4.0), vel: -0.16666667
Pos: (0.0, 2.0), vel: -0.16666667
Pos: (0.0, 0.0), vel: -0.16666667
Pos: (0.0, -2.0), vel: -0.16666667
Pos: (0.0, -4.0), vel: -0.16666667
Pos: (0.0, -6.0), vel: -0.16666667
Pos: (0.0, -7.9854183), vel: -0.16666667
Pos: (-7.603796E-8, -5.9854183), vel: 0.16666667
Pos: (-7.614372E-8, -7.9854183), vel: -0.16666667
Pos: (-2.5846055E-7, -5.9854183), vel: 0.16666667
Pos: (-2.5871412E-7, -7.9854183), vel: -0.16666667
Pos: (-2.549122E-7, -5.9854183), vel: 0.16666667
Pos: (-2.5490692E-7, -7.9854183), vel: -0.16666667
Pos: (-4.055684E-7, -5.9854183), vel: 0.16666667
Pos: (-4.0577794E-7, -7.9854183), vel: -0.16666667
Pos: (-9.114416E-9, -5.9854183), vel: 0.16666667
Pos: (-8.562728E-9, -7.9854183), vel: -0.16666667
Pos: (-4.6006042E-7, -5.9854183), vel: 0.16666667
Pos: (-4.6068837E-7, -7.9854183), vel: -0.16666667
Pos: (-3.1053162E-8, -5.9854183), vel: 0.16666667
Pos: (-3.0455617E-8, -7.9854183), vel: -0.16666667
Pos: (-5.447658E-7, -5.9854183), vel: 0.16666667
Pos: (-5.454811E-7, -7.9854183), vel: -0.16666667
Pos: (-1.4074305E-7, -5.9854183), vel: 0.16666667
Pos: (-1.4018013E-7, -7.9854183), vel: -0.16666667
Pos: (-3.6011647E-7, -5.9854183), vel: 0.16666667
Pos: (-3.6042238E-7, -7.9854183), vel: -0.16666667
Pos: (-3.7153583E-7, -5.9854183), vel: 0.16666667
Pos: (-3.715513E-7, -7.9854183), vel: -0.16666667
Pos: (-4.5203745E-7, -5.9854183), vel: 0.16666667
Pos: (-4.521494E-7, -7.9854183), vel: -0.16666667
Pos: (-3.3188724E-7, -5.9854183), vel: 0.16666667
Pos: (-3.3171997E-7, -7.9854183), vel: -0.16666667
Pos: (-4.3615427E-7, -5.9854183), vel: 0.16666667
Pos: (-4.3629953E-7, -7.9854183), vel: -0.16666667
Pos: (-3.2553035E-7, -5.9854183), vel: 0.16666667
Pos: (-3.2537628E-7, -7.9854183), vel: -0.16666667
Pos: (-5.8575245E-7, -5.9854183), vel: 0.16666667
Pos: (-5.861146E-7, -7.9854183), vel: -0.16666667
Pos: (-3.8501915E-7, -5.9854183), vel: 0.16666667
Pos: (-3.8473945E-7, -7.9854183), vel: -0.16666667
Pos: (-3.0500823E-7, -5.9854183), vel: 0.16666667
Pos: (-3.0489733E-7, -7.9854183), vel: -0.16666667
Pos: (-5.0150834E-7, -5.9854183), vel: 0.16666667
Pos: (-5.017818E-7, -7.9854183), vel: -0.16666667
Pos: (-1.9941152E-7, -5.9854183), vel: 0.16666667
Pos: (-1.9899097E-7, -7.9854183), vel: -0.16666667
Pos: (-6.878152E-7, -5.9854183), vel: 0.16666667
Pos: (-6.8849505E-7, -7.9854183), vel: -0.16666667
Pos: (-5.0189726E-7, -5.9854183), vel: 0.16666667
Pos: (-5.016377E-7, -7.9854183), vel: -0.16666667
Pos: (-7.3193803E-7, -5.9854183), vel: 0.16666669
Pos: (-7.3225834E-7, -7.9854183), vel: -0.16666667
Pos: (-5.953764E-7, -5.9854183), vel: 0.16666667
Pos: (-5.95186E-7, -7.9854183), vel: -0.16666667
Pos: (-7.690866E-7, -5.9854183), vel: 0.16666667
Pos: (-7.693285E-7, -7.9854183), vel: -0.16666667
Pos: (-5.340357E-7, -5.9854183), vel: 0.16666667
A time step of 12.5 seconds is still way too large. Take a look at some of the examples - a typical time step size is about 1/60 seconds.
You were absolutely right.

I had tried 1/60 before but i was getting a velocity of 0, so I had assume that the java port was using MS (hence why I saw the 1000 in some examples). The problem was that like always I'm an idiot!.

I had used the following code:

Code:

public int targetFPS = 40;
public int timeStep = (1/targetFPS);



duh! what int in the known world works with 1/40??? when i changed it to floats all was well.....

Thanks again for you help on this!!!!

Original thread: https://box2d.org/forum/viewtopic.php?t=7816