LIBGDX Box2d的最新测试平台

时间:2013-02-11 07:31:16

标签: java box2d libgdx jbox2d testbed

我正在使用Libgdx测试最新的Box2d Testbed。看起来他们没有工作,需要知道是否有其他人有同样的问题。第一个叫做传送带,https://github.com/ansman/box2d/blob/master/Testbed/Tests/ConveyorBelt.h

我转换为:

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;*/


}

这是通过将其与其他Box2D测试结合使用,但我注意到了一些事情。

Libgdx在其Contact.java类中没有setTangentSpeed方法

设置必须使用org.jbox2d.common导入

无法将测试解析为类型

我也尝试过使用Breakable,https://github.com/ansman/box2d/blob/master/Testbed/Tests/Breakable.h

已转换为

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;

        }

我注意到了:

Libgdx在Manifold.java中没有pointCount。一个快速解决方法是更改​​为getWorldManifold但没有做任何好的

Vector2.java不包含crs(float x,Vector2 v),其中m_velocity的Vectors不允许

设置不存在,除非我使用com.jbox2d.common import

如果不包含这些方法,如何让它们在Libgdx中运行? Libgdx不再更新了吗?我想使用这些,但它似乎落后于移植。我甚至在Contact.java中注意到他在setTangentSpeed的代码之前停止了。我已经在我的测试中添加了gwt罐子而没有任何好处。

1 个答案:

答案 0 :(得分:0)

如果您需要2.2.1版本的jbox2d,可以从trunk获取版本。它几乎已经准备好发布了,我只需要做一些优化和内联。

编辑:看起来libgdx使用它自己的包装器来实现Box2D的本机C ++实现。这在性能方面绝对是可取的,希望他们会更新他们的库。