Box2D Forums

It is currently Sat May 25, 2013 8:29 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Fri Aug 19, 2011 9:11 pm 
Offline

Joined: Fri Aug 19, 2011 8:58 pm
Posts: 5
Hi.

I'm trying to set up JBox2d. I'm basing my code off the box2d tutorials (http://www.box2d.org/manual.html#_Toc258082968"box2d%20tutorials). This is my code:

Code:
package test;

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.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

public class Game {
    static final float timeStep = 1.0f / 60.0f;
    static final int velocityIterations = 6;
    static final int positionIterations = 2;
    static int debugInt = 0;
   
   World world;
   BodyDef bodyDef;
   Body body;
   PolygonShape groundBox;
   
   BodyDef dynamicBodyDef;
   Body dynamicBody;
   PolygonShape dynamicBox;
   FixtureDef dynamicFixDef;
   
   public Game() {
        // Create the ground box.
        world = new World(new Vec2(0, -10.0f), true);
        bodyDef = new BodyDef();
        bodyDef.position.set(0, -10.0f);
        body = new Body(bodyDef, world);
        groundBox = new PolygonShape();
        groundBox.setAsBox(50.0f, 10.0f);
        body.createFixture(groundBox, 1.0f);
       
        // Create the dynamic body.
        dynamicBodyDef = new BodyDef();
        dynamicBodyDef.type = BodyType.DYNAMIC;
        dynamicBodyDef.position = new Vec2(0, 4.0f);
        dynamicBody = world.createBody(dynamicBodyDef);
        dynamicBox = new PolygonShape();
        dynamicBox.setAsBox(1, 1);
        dynamicFixDef = new FixtureDef();
        dynamicFixDef.shape = dynamicBox;
        dynamicFixDef.density = 1.0f;
        dynamicFixDef.friction = 0.3f;
        dynamicBody.createFixture(dynamicFixDef);
    }
   
   public void run() {
       for (int i = 0; i < 60; ++i) {
           world.step(timeStep, velocityIterations, positionIterations);
           world.clearForces();
          
           Vec2 position = body.getPosition();
           float angle = body.getAngle();
           System.out.println("iteration = " + i + " pos.x = " + position.x + " pos.y = " + position.y + " angle = " + angle);
       }
   }
   
    public static void main(String[] args) {
        Game game = new Game();
        game.run();       
    }
}


Here is the console output:
Quote:
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.
iteration = 0 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 1 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 2 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 3 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 4 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 5 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 6 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 7 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 8 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 9 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 10 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 11 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 12 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 13 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 14 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 15 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 16 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 17 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 18 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 19 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 20 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 21 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 22 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 23 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 24 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 25 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 26 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 27 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 28 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 29 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 30 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 31 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 32 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 33 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 34 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 35 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 36 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 37 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 38 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 39 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 40 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 41 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 42 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 43 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 44 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 45 pos.x = 0.0 pos.y = -10.0 angle = 0.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.jbox2d.dynamics.Island.add(Island.java:413)
at org.jbox2d.dynamics.World.solve(World.java:916)
at org.jbox2d.dynamics.World.step(World.java:560)
at test.Game.run(Game.java:53)
at test.Game.main(Game.java:65)


I have no idea why an exception is being thrown - I'm most likely screwing something up somewhere, but I'm unsure where as I'm still new to box2d.

Cheers.


Top
 Profile  
 
PostPosted: Wed Aug 24, 2011 4:22 pm 
Offline

Joined: Fri Aug 19, 2011 8:58 pm
Posts: 5
*tumbleweed*


Top
 Profile  
 
PostPosted: Fri Aug 26, 2011 9:44 am 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
catbreath wrote:
*tumbleweed*


Sorry about that, been a bit busy. It looks like you're right, I'll look into this some more


Top
 Profile  
 
PostPosted: Fri Aug 26, 2011 9:50 am 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
Ok, so you shouldn't be creating a body outside of the world. This might be something that has changed recently with box2d 2.1.3, but in 2.1.2 if you construct a new body then it won't be added to the world's body list. Change the body instantiation to world.createBody call and you'll be good.


Top
 Profile  
 
PostPosted: Sat Aug 27, 2011 9:27 pm 
Offline

Joined: Fri Aug 19, 2011 8:58 pm
Posts: 5
toucansam wrote:
catbreath wrote:
*tumbleweed*


Sorry about that, been a bit busy. It looks like you're right, I'll look into this some more

Haha sweet, I got a reply.

toucansam wrote:
Ok, so you shouldn't be creating a body outside of the world. This might be something that has changed recently with box2d 2.1.3, but in 2.1.2 if you construct a new body then it won't be added to the world's body list. Change the body instantiation to world.createBody call and you'll be good.

Cheers. :) For some reason, however, my object isn't falling according to the tutorial:

Quote:
iteration = 0 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 1 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 2 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 3 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 4 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 5 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 6 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 7 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 8 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 9 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 10 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 11 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 12 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 13 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 14 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 15 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 16 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 17 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 18 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 19 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 20 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 21 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 22 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 23 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 24 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 25 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 26 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 27 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 28 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 29 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 30 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 31 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 32 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 33 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 34 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 35 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 36 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 37 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 38 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 39 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 40 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 41 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 42 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 43 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 44 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 45 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 46 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 47 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 48 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 49 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 50 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 51 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 52 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 53 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 54 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 55 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 56 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 57 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 58 pos.x = 0.0 pos.y = -10.0 angle = 0.0
iteration = 59 pos.x = 0.0 pos.y = -10.0 angle = 0.0


Top
 Profile  
 
PostPosted: Thu Sep 01, 2011 4:31 am 
Offline

Joined: Fri Aug 19, 2011 8:58 pm
Posts: 5
Got any ideas? :(


Top
 Profile  
 
PostPosted: Sat Oct 22, 2011 7:27 am 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
It looks like you're creating the second body inside of your ground body, check the dimensions of each.


Top
 Profile  
 
PostPosted: Sun Oct 23, 2011 1:21 am 
Offline

Joined: Fri Sep 30, 2011 11:33 am
Posts: 6
Hi catbreath,

As toucansam says you shouldn't create a Body outside of the World. It will not be added to the World and thus will not be altered by the world physics. I'm surprised that they don't set the access restriction to protected for the Body constructor (and all other World dependent classes) ... maybe they have good reasons for this I am unaware of.

The other problem you are seeing is because you are trying to get the position of the static Body (body) which of course won't change its location because it is static. You need to check the location of the dynamicBody instead.

The update code is below.

I have embedded some minor comments.

Code:
package test;

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.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

public class Game
{
    static final float timeStep = 1.0f / 60.0f;
    static final int velocityIterations = 6;
    static final int positionIterations = 2;
    static int debugInt = 0;

    World world;
    BodyDef bodyDef;
    Body body;
    PolygonShape groundBox;

    BodyDef dynamicBodyDef;
    Body dynamicBody;
    PolygonShape dynamicBox;
    FixtureDef dynamicFixDef;

    public Game()
    {
        // Create the ground box.
        world = new World( new Vec2( 0, -10.0f ), true );
        bodyDef = new BodyDef();
        bodyDef.position.set( 0, -10.0f );
        // Create Body in World.
        body = world.createBody( bodyDef );
        groundBox = new PolygonShape();
        groundBox.setAsBox( 50.0f, 10.0f );
        body.createFixture( groundBox, 1.0f );

        // Create the dynamic body.
        dynamicBodyDef = new BodyDef();
        dynamicBodyDef.type = BodyType.DYNAMIC;
        dynamicBodyDef.position = new Vec2( 0, 4.0f );
        dynamicBody = world.createBody( dynamicBodyDef );
        dynamicBox = new PolygonShape();
        dynamicBox.setAsBox( 1, 1 );
        dynamicFixDef = new FixtureDef();
        dynamicFixDef.shape = dynamicBox;
        dynamicFixDef.density = 1.0f;
        dynamicFixDef.friction = 0.3f;
        dynamicBody.createFixture( dynamicFixDef );
    }

    public void run()
    {
        for( int i = 0; i < 60; ++i )
        {
            world.step( timeStep, velocityIterations, positionIterations );
            world.clearForces();

            // Get position of dynamicBody not body.
            Vec2 position = dynamicBody.getPosition();
            float angle = dynamicBody.getAngle();
            System.out.println( "iteration = " + i + " pos.x = " + position.x
                    + " pos.y = " + position.y + " angle = " + angle );
        }
    }

    public static void main( String[] args )
    {
        Game game = new Game();
        game.run();
    }
}


I have run the updated code and obtained the following output snippet:

Code:
iteration = 0 pos.x = 0.0 pos.y = 3.9972222 angle = 0.0
iteration = 1 pos.x = 0.0 pos.y = 3.9916666 angle = 0.0
iteration = 2 pos.x = 0.0 pos.y = 3.983333 angle = 0.0
...
iteration = 58 pos.x = 2.2070685E-7 pos.y = 1.0149592 angle = 6.209061E-6
iteration = 59 pos.x = 2.2089253E-7 pos.y = 1.0149659 angle = 4.9359E-6


Hope that helps!


Top
 Profile  
 
PostPosted: Fri Nov 04, 2011 5:59 am 
Offline

Joined: Wed Nov 02, 2011 5:55 am
Posts: 6
Even I copy KostBot Code there is the Error:

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.


Top
 Profile  
 
PostPosted: Thu Nov 10, 2011 9:33 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
That happens because there isn't a logging framework for SLF4J to use. It's just telling you it can't log anything, which is fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group