JBox2d V2.1.2 - ArrayIndexOutOfBoundsException

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.
*tumbleweed*
catbreath wrote:
*tumbleweed*


Sorry about that, been a bit busy. It looks like you're right, I'll look into this some more
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.
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
Got any ideas? 🙁
It looks like you're creating the second body inside of your ground body, check the dimensions of each.
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!
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.
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.

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