0

I'm testing the newest Box2d Testbed using Libgdx. It appears they aren't working and need to know if anyone else is having the same issue(s). The first one is called Conveyor Belt, https://github.com/ansman/box2d/blob/master/Testbed/Tests/ConveyorBelt.h

which I converted to:

package com.badlogic.gdx.tests.box2d;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.joints.FrictionJointDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Transform;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.tests.utils.GdxTest;


public class ConveyorBelt extends Box2DTest {
    Body m_body;    
    Fixture platform;

@Override
protected void createWorld (World world) {
    world.setGravity(new Vector2(0, -10));
    /** amount of bounce when colliding */
    float k_restitution = 0.4f;
    Body ground;

    {       /** x,y of entire shapes */
        BodyDef bd = new BodyDef();
        bd.position.set(0, 20);
        ground = world.createBody(bd);         

     PolygonShape shape = new PolygonShape();
     shape.setAsEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
     ground.createFixture(shape, 0.0f);

     shape.dispose();
 }

          // Platform
          {
                    BodyDef bd = new BodyDef();
                  bd.position.set(-5.0f, 5.0f);
                  Body body = world.createBody(bd);

                  PolygonShape shape = new PolygonShape();
                  shape.setAsBox(10.0f, 0.5f);

                  FixtureDef fd = new FixtureDef();
                  fd.shape = shape;
                  fd.friction = 0.8f;
                  platform = body.createFixture(fd);
          }

          // Boxes
          for (int i = 0; i < 5; ++i)
          {
                    BodyDef bd = new BodyDef();
                  bd.type = BodyType.DynamicBody;
                  bd.position.set(-10.0f + 2.0f * i, 7.0f);
                  Body body = world.createBody(bd);

                  PolygonShape shape = new PolygonShape();
                  shape.setAsBox(0.5f, 0.5f);
                  body.createFixture(shape, 20.0f);
          }
  }

 /*      void PreSolve(Contact contact, Manifold oldManifold)
  {
          Test extends PreSolve(contact, oldManifold);

          Fixture fixtureA = contact.getFixtureA();
          Fixture fixtureB = contact.getFixtureB();

          if (fixtureA == platform)
          {
                  contact.setTangentSpeed(5.0f);
          }

          if (fixtureB == platform)
          {
                  contact.setTangentSpeed(-5.0f);
          }
  }

  void Step(Settings settings)
  {
          Test extends Step(settings);
  }

  static Test Create()
  {
          return new ConveyorBelt;
  }

  Fixture platform;*/


}

This is working by putting it in with the other Box2D tests but I noticed a few things.

Libgdx doesn't have a setTangentSpeed method in its Contact.java class

Settings has to use the org.jbox2d.common import

Test can't be resolved to a type

I also tried using Breakable, https://github.com/ansman/box2d/blob/master/Testbed/Tests/Breakable.h

Which was converted to

package com.badlogic.gdx.tests.box2d;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
import com.badlogic.gdx.backends.gwt.*;

public class Breakable extends Box2DTest {

        Body body1;
        Vector2 m_velocity;
        float m_angularVelocity;
        PolygonShape m_shape1;
        PolygonShape m_shape2;
        Fixture m_piece1;
        Fixture m_piece2;

        boolean m_broke;
        boolean m_break;

        /**
         * @see org.jbox2d.testbed.framework.TestbedTest#initTest()
         */
        @Override
    protected void createWorld (World world) {
    world.setGravity(new Vector2(0, -10));
    Body ground;
            // Ground body
                {
                        BodyDef bd = new BodyDef();
                        ground = world.createBody(bd);

                        PolygonShape shape = new PolygonShape();
                        shape.setAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                        ground.createFixture(shape, 0.0f);

                        shape.dispose();
                }

                // Breakable dynamic body
                {
                        BodyDef bd = new BodyDef();
                        bd.type = BodyType.DynamicBody;
                        bd.position.set(0.0f, 40.0f);
                        bd.angle = 0.25f * MathUtils.PI;
                        body1 = world.createBody(bd);

                        m_shape1 = new PolygonShape();
                        m_shape1.setAsBox(0.5f, 0.5f, new Vector2(-0.5f, 0.0f), 0.0f);
                        m_piece1 = body1.createFixture(m_shape1, 1.0f);

                        m_shape2 = new PolygonShape();
                        m_shape2.setAsBox(0.5f, 0.5f, new Vector2(0.5f, 0.0f), 0.0f);
                        m_piece2 = body1.createFixture(m_shape2, 1.0f);
                }

                m_break = false;
                m_broke = false;
        }


                void PostSolve(Contact contact, ContactImpulse impulse)
                {
                        if (m_broke)
                        {
                                // The body already broke.
                                return;
                        }

                        // Should the body break?
                        int count = contact.getManifold().pointCount;

                        float maxImpulse = 0.0f;
                        for (int i = 0; i < count; ++i)
                        {
                                maxImpulse = MathUtils.max(maxImpulse, impulse.normalImpulses[i]);
                        }

                        if (maxImpulse > 40.0f)
                        {
                                // Flag the body for breaking.
                                m_break = true;
                        }
                }

                void Break()
                {
                        // Create two bodies from one.
                        Body body1 = m_piece1.getBody();
                        Vector2 center = body1.getWorldCenter();

                        body1.destroyFixture(m_piece2);
                        m_piece2 = null;

                        BodyDef bd = new BodyDef();
                        bd.position = body1.getPosition();
                        bd.angle = body1.getAngle();

                        Body body2 = world.createBody(bd);
                        m_piece2 = body2.createFixture(m_shape2, 1.0f);
                }

}
                        // Compute consistent velocities for new bodies based on
                        // cached velocity.
                        Vector2 center1 = body1.getWorldCenter();
                        Vector2 center2 = body2.getWorldCenter();

                        Vector2 velocity1 = m_velocity.add(Vector2.crs(m_angularVelocity, center1.sub(center)));
                        Vector2 velocity2 = m_velocity.add(Vector2.crs(m_angularVelocity, center2.sub(center)));

                        body1.setAngularVelocity(m_angularVelocity);
                        body1.setLinearVelocity(velocity1);

                        body2.setAngularVelocity(m_angularVelocity);
                        body2.setLinearVelocity(velocity2);
                }

                void Step(Settings settings)
                {
                        if (m_break)
                        {
                                Break();
                                m_broke = true;
                                m_break = false;
                        }

                        // Cache velocities to improve movement on breakage.
                        if (m_broke == false)
                        {
                                m_velocity = body1.getLinearVelocity();
                                m_angularVelocity = body1.getAngularVelocity();
                        }

                        Test = Step(settings);
                }

                static Test Create()
                {
                        return new Breakable;
                }

                Body m_body1;

        }

I Noticed:

Libgdx doesn't have pointCount in Manifold.java. One quick fix was to change to getWorldManifold but didn't do any good

Vector2.java doesn't contain a crs( float x, Vector2 v) which the Vectors for m_velocity won't allow

Settings doesn't exist, unless I use the com.jbox2d.common import

How do I get these to work in Libgdx if these methods aren't included? Is Libgdx not updated anymore? I want to use these but it seems they are behind with porting. I even noticed in Contact.java he stopped just before the code for setTangentSpeed. I have added gwt jars to my tests with no benefit.

  • 1
    What is your question? Java port did not updated yet to the last Box2D version, it is obvious. Only way to fix this - make your best, and do it yourself, or wait while someone will update jbox2d. – Pavel Feb 11 '13 at 14:35
  • From what I recall reading Box2D v2.1 was released in 2011...why is it taking so long? There's no way I can do it myself but I did ask the creator and he never wrote back so I'm guessing he doesn't know either – nemofiendr Mar 05 '13 at 19:18

1 Answers1

0

If you want the 2.2.1 version of jbox2d, you can grab a build from the trunk. It's almost ready for a release, I just need to do a bit more optimization and inlining.

edit: It looks like libgdx uses it's own wrappers to a the native C++ implementation of Box2D. This is definitely preferable performance-wise, hopefully they will update their library.

Daniel Murphy
  • 852
  • 7
  • 14