Box2d - 高速射球

时间:2014-02-24 12:54:58

标签: libgdx box2d

我的问题是,无论调整我都没有达到快速移动的球。它漂浮在空中而不是通过它射击。

在下面的方法中,我创建了一个半径为5个单位的球。将半径设置为0.01不会产生结果差异。

在创作之后我应用了LinearImpulse个荒谬的比例,球只漂浮了。 (另外,我正在使用DebugRender。这不应该用于处理单位转换吗?)

Ball class

public Body createBody(float x, float y, float w, float h) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(x,y);
    Body body = this.getWorld().createBody(bodyDef);

    CircleShape shape = new CircleShape();
    shape.setRadius(5);
    shape.setPosition(new Vector2(0, 0));  // relative to body position

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 3f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0.8f;
    body.createFixture(fixtureDef);

    shape.dispose();
    // note the massive impulse vector
    body.applyLinearImpulse(new Vector2(10000, 100000), body.getWorldCenter(), true);

    return body;
}

render method in Game class

@Override
public void render(){

    Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    debugRenderer.render(world, camera.combined);

    // time step, velocityIterations, positionIterations
    world.step(1/40.0f, 6, 2);

    // scheduler takes care of creating game objects on the fly
}

2 个答案:

答案 0 :(得分:3)

好的,连同我对这个问题的回答Is there an upper limit on velocity when using box2d?我会尝试解释你的问题。

Box2D对速度有一个硬限制,每步时间为2米。这意味着您每秒执行的次数越多,您的Bodies就能越快移动。但是,由于您实际上希望每秒具有固定的时间步长,因此通常不能选择更多的时间步。

这意味着box2d中的1米应以屏幕为单位转换为16或128像素。这将提高整体精度,因为Box2D在较小范围内更精确。如果您的比例为1:1,则意味着这些天的标准屏幕将跨越1980m x 1080m的范围,这定义太多了。你还可以看到,60fps = 60次步长=每秒60 * 2m的最大距离,在最大速度下每秒只需120pixels。这就是你目前作为漂浮体所经历的事情。

例如,如果你将1m翻译成16px,你的最大速度将增加到120px * 16 = 7200px每秒。在我猜的情况下,这已经足够了。

此外,您还应将body.bullet设置为true,以提高快速移动的物体的碰撞精度。

答案 1 :(得分:1)

其中一个原因是世界规模,如果它很大,你可能会有一个非常大的世界规模。

它建议为了更好的准确性,使用set bullet为true,但需要更多的计算

body.bullet = true;
相关问题