Java 需要一些帮助来学习jbox2d吗

Java 需要一些帮助来学习jbox2d吗,java,box2d,playn,jbox2d,Java,Box2d,Playn,Jbox2d,首先,非常感谢您抽出时间:) 我目前正试图了解jbox2d是如何工作的,我遇到了一些问题。我写的代码对我来说很有意义,但应该有一些我根本不懂的东西。基本上我现在想做的是让主角(由玩家控制)与墙壁碰撞 不需要太多细节,我有一个名为Player的动态实体类和一个名为Wall的静态实体类。我还有一个名为Map的类,它处理级别。 实体的坐标由屏幕中的像素表示 现在是关于jbox2d的部分 在课程地图中,我有: // ... other fields private static final float

首先,非常感谢您抽出时间:)

我目前正试图了解jbox2d是如何工作的,我遇到了一些问题。我写的代码对我来说很有意义,但应该有一些我根本不懂的东西。基本上我现在想做的是让主角(由玩家控制)与墙壁碰撞

不需要太多细节,我有一个名为Player的动态实体类和一个名为Wall的静态实体类。我还有一个名为Map的类,它处理级别。 实体的坐标由屏幕中的像素表示

现在是关于jbox2d的部分

在课程地图中,我有:

// ... other fields
private static final float TIME_STEP = 1.0f / 60.f;
private static final int VELOCITY_ITERATIONS = 6;
private static final int POSITION_ITERATIONS = 3;
private World world;

// constructor
public Map(int startPosX, int startPosY)
{
        // ...other stuffs
    Vec2 gravity = new Vec2(0, -10f);
    world = new World(gravity);
        // ...other stuffs
}

// update method that is called every 30 ms
public void update(int delta)
{
        // ...other stuffs
    world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}
下面是静态实体的外观:

private Map map;
private Body body;
private Fixture fixture;
private PolygonShape shape;

public Wall(int x, int y, Map map)
{
    super(x, y);
    this.map = map;
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x, y);
    bodyDef.type = BodyType.STATIC;
    shape = new PolygonShape();
    shape.setAsBox(CELL_HEIGHT, CELL_WIDTH);
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape;
    body = map.getWorld().createBody(bodyDef);
    fixture = body.createFixture(fixtureDef);
}
最后是玩家:

private Map map;
private PolygonShape shape;
private Body body;
private Fixture fixture;

public MovingEntity(float x, float y, Map map)
{
    super.setX(x);
    super.setY(y);
    animation = Animation.IDLE;
    layer = graphics().createImmediateLayer(new EntityRenderer(this));
    layer.setVisible(false);
    graphics().rootLayer().add(layer);

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x, y);
    bodyDef.type = BodyType.DYNAMIC;
    shape = new PolygonShape();
    shape.setAsBox(getFrameSize().height(), getFrameSize().width());
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape;
    body = map.getWorld().createBody(bodyDef);
    fixture = body.createFixture(shape, 2.0f);
}
你们知道我做错了什么吗?这些实体根本不会发生碰撞。此外,如果我尝试在更新方法中打印玩家身体的当前位置,即使我不移动,坐标也会发生变化(我想它会因为重力而下降,我在游戏中不需要重力)


再次非常感谢

我认为您的实体不会发生碰撞,因为您使用的是多边形形状

shape = new PolygonShape();
您必须定义Polygone形状的点,以便jbox可以测试形状的碰撞。大概是这样的:

Vec2[] vertices = {
                new Vec2(0.0f, - 10.0f),
                new Vec2(+ 10.0f, + 10.0f),
                new Vec2(- 10.0f, + 10.0f)
        };

        PolygonShape shape = new PolygonShape();
        shape.set(vertices, vertices.length);
此外,如果不需要重力,则只需将重力向量设置为0,0

Vec2重力=新的Vec2(0f,0f)