为什么新的子弹隧道,当旧的子弹没有?

时间:2015-01-05 00:51:14

标签: java jbox2d

我使用JBox2D在Java中创建游戏,允许玩家拍摄小型高速子弹。当我制造子弹并向墙壁方向施加力时,子弹有时会穿过墙壁。这似乎取决于子弹在最初创建时与墙壁的接近程度。

例如,假设我有一个x = 5的墙。如果我创建一个子弹,将其放置在x = 0,并施加1000牛顿力,那么墙将成功阻止子弹。但是,如果我在x = 4处创建子弹,那么墙不会阻止子弹。

这是一个适合所有人的SSCCE。它被编写为TestbedTest,使测试更简单。

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.testbed.framework.TestbedTest;

public class BulletTest extends TestbedTest
{
    @Override
    public String getTestName()
    {
        return "Bullet Test";
    }

    @Override
    public void initTest(boolean deserialized)
    {
        getWorld().setGravity(new Vec2());

        BodyDef wallBodyDef = new BodyDef();
        wallBodyDef.type = BodyType.STATIC;
        wallBodyDef.position.x = 5.0f;
        Body wallBody = getWorld().createBody(wallBodyDef);

        PolygonShape wallShape = new PolygonShape();
        wallShape.setAsBox(0.25f, 10.0f);
        wallBody.createFixture(wallShape, 1.0f);
    }

    @Override
    public void keyPressed(char keyChar, int keyCode)
    {
        switch (keyChar)
        {
        case '1':
            createBullet(0.0f);
            break;
        case '2':
            createBullet(4.0f);
            break;
        }
    }

    private void createBullet(float xPosition)
    {
        BodyDef bulletBodyDef = new BodyDef();
        bulletBodyDef.type = BodyType.DYNAMIC;
        bulletBodyDef.position.x = xPosition;
        bulletBodyDef.bullet = true; // shouldn't matter since the wall is static
        Body bulletBody = getWorld().createBody(bulletBodyDef);

        CircleShape bulletShape = new CircleShape();
        bulletShape.setRadius(0.125f);
        bulletBody.createFixture(bulletShape, 1.0f);

        bulletBody.applyForceToCenter(new Vec2(1000.0f, 0.0f));
    }
}

0 个答案:

没有答案