0

我正在使用 Libgdx 测试最新的 Box2d 测试平台。他们似乎没有工作,需要知道其他人是否有同样的问题。第一个叫Conveyor Belt,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 的向量不允许

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

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

4

1 回答 1

0

如果你想要 jbox2d 的 2.2.1 版本,你可以从trunk获取构建。它几乎准备好发布了,我只需要做更多的优化和内联。

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

于 2013-03-07T18:00:43.893 回答