Help Getting STarted

Hello!
<--Im noob to forum but not new to programming.

I have been having trouble using the jbox2d library (in jar form) in netbeans. I managed to link ( import ) the library ok and that all seems to be working, but I really struggled to find any tutorials to get me started. I did however find a handy manual called 'Box2D v2.2.0 User Manual' ( http://download.putfile2.com/download/3037/xcnpea ) which was a nice start. Unfortunately its written for C++ and I believe there are one or two clashes that I haven't managed to figure out. Please see if you can spot the errors in the print screen below:

Image

Also if anyone could point me in the right direction for more tutorials. That would be appreciated.
I usually program in C++ but am trying to see what I can come up with in Java in less than 1000 lines of code at an 80 character width line limit. (not including 3rd party libs ofc)

Stu
You should be able to walk though the c++ tutorials without too much of a problem. http://www.box2d.org/manual.html. Don't give up before you've really given it an earnest try 🙂 The reason there aren't many, if any, Java tutorials is because the Java implementation is "close-enough" to the C++ implementation (baring class name differences. i.e. b2Body is Body, b2Vec2 is Vec2, etc.).

If you are using netbeans (or any other IDE eclipse is a good one) you will most definitely have auto-complete support. Use this to find the matching Java methods or member variable names. In eclipse this is accessed using "ctrl+space" not sure about netbeans...

If you are really in need of some Java source. There is this one http://www.4feets.com/2009/03/2d-physics-on-android-using-box2d/ that I found with a quick google search. It is for android but the box2d portion is what you are interested in anyway.

But just in case here is a very very simple example of jbox2d.

Code: Select all

import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.collision.shapes.CircleShape;
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;
import org.jbox2d.dynamics.contacts.Contact;

public class Main
{
    public static void main( String[] args )
    {
        // Define the gravity vector.
        Vec2 gravity = new Vec2( 0, -10 );

        // Allow objects to sleep.
        boolean doSleep = true;

        // Initialise the World.
        World world = new World( gravity, doSleep );

        // Set a contact listener.
        world.setContactListener( new ContactListener()
        {
            @Override
            public void preSolve( Contact arg0, Manifold arg1 )
            {
            }

            @Override
            public void postSolve( Contact arg0, ContactImpulse arg1 )
            {
            }

            @Override
            public void endContact( Contact arg0 )
            {
            }

            @Override
            public void beginContact( Contact arg0 )
            {
                // Ball collided with container!
                System.out.println( "Bounce" );
            }
        } );

        // Create the ground (Something for dynamic bodies to collide with).
        {
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.position.set( 0, 0 );
            groundBodyDef.type = BodyType.STATIC;

            // Create the Body in the World.
            Body ground = world.createBody( groundBodyDef );

            // Create the fixtures (physical aspects) of the ground body.
            FixtureDef groundEdgeFixtureDef = new FixtureDef();
            groundEdgeFixtureDef.density = 1.0f;
            groundEdgeFixtureDef.friction = 1.0f;
            groundEdgeFixtureDef.restitution = 0.4f;

            PolygonShape groundEdge = new PolygonShape();
            groundEdgeFixtureDef.shape = groundEdge;

            // We will create the ground as a box. Creating each edge of the box
            // in turn. The reason we don't use groundEdge.setAsBox() method is
            // because this creates a solid fixture which does not allow other
            // bodies to exist within it. When we create the ground with edges
            // this will give us a nice container to have our bodies bounce
            // around in.

            // Bottom Edge.
            groundEdge.setAsEdge( new Vec2( 0, 0 ), new Vec2( 10, 0 ) );
            ground.createFixture( groundEdgeFixtureDef );

            // Reuse the PolygonShape and FixtureDef objects since they are only
            // used to create objects in the World. Think of them as being a
            // cookie cutter. Once you make cut a cookie it does matter what
            // happens to the cookie cutter after.

            // Right Edge.
            groundEdge.setAsEdge( new Vec2( 10, 0 ), new Vec2( 10, 10 ) );
            ground.createFixture( groundEdgeFixtureDef );

            // Top Edge.
            groundEdge.setAsEdge( new Vec2( 10, 10 ), new Vec2( 0, 10 ) );
            ground.createFixture( groundEdgeFixtureDef );

            // Left Edge.
            groundEdge.setAsEdge( new Vec2( 0, 10 ), new Vec2( 0, 0 ) );
            ground.createFixture( groundEdgeFixtureDef );
        }

        // Create a Ball.
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyType.DYNAMIC;
        ballBodyDef.position.set( 5, 5 ); // Centre of the ground box.

        // Create the body for the ball within the World.
        Body ball = world.createBody( ballBodyDef );

        // Create the actual fixture representing the box.
        CircleShape ballShape = new CircleShape();
        ballShape.m_radius = 0.5f; // Diameter of 1m.
        // ballShape.m_p is the offset relative to body. Default of (0,0)

        FixtureDef ballFixtureDef = new FixtureDef();
        ballFixtureDef.density = 1.0f; // Must have a density or else it won't
                                       // be affected by gravity.
        ballFixtureDef.restitution = 0.8f; // Define how bouncy the ball is.
        ballFixtureDef.friction = 0.2f;
        ballFixtureDef.shape = ballShape;

        // Add the fixture to the ball body.
        ball.createFixture( ballFixtureDef );

        // Do a few steps of simulation.
        for( int i = 0; i < 10; ++i )
        {
            System.out.println( ball.getPosition() );

            // Advance the world 1/6 of a second into the future.
            // Typically you should use 1/60 of a second for simulating physics
            // though...
            world.step( 1 / 6f, 8, 3 );
        }
    }
}

Output will look like this.

Code: Select all

(5.0,5.0)
(5.0,4.7222223)
(5.0,4.166667)
(5.0,3.3333335)
(5.0,2.2222223)
(5.0,0.83333325)
Bounce
(5.0,0.504375)
(5.0,2.0599308)
(5.0,3.3377087)
(5.0,4.3377085)


As you can see the ball is falling and bounces up when it hits the bottom edge of the container.

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