簡體   English   中英

Box2D重塑身體

[英]Box2D recreating a body

在我的游戲中,我有一個Bullet類,該類負責在每次開槍時制作新的子彈。 創建后,項目符號將添加到“項目Bullets類中,該類負責跟蹤所述項目符號以及其余項目符號。 我遇到了一個奇怪的行為:
殺死敵人然后再次射擊后,新子彈具有以下特征:

  1. 子彈與殺死敵人的子彈是相同的子彈(在相同的代碼ID中)。 (即,如果ID是: com.badlogic.gdx.physics.box2d.Body@fc7157f ,則它將是完全相同的ID。)
  2. 子彈似乎卡在原地,它的子畫面不會移動,但是根據游戲,它會具有一定的速度,但是位置保持不變。 唯一可見的動作是啟用Box2DDebugRenderer ,您可以看到身體向下移動,直到撞到地面為止,此時他“傳送”回去,然后緩慢回落。
  3. 卡住的子彈數量等於被殺死的敵人數量。

這是項目符號類:

public class Bullet {

private Body bullet;

public Bullet(final float force, final int bulletDmg, final Weapon weapon,
        final World world) {
    System.out.println("Position " + weapon.getPosition() + ", Angle: "
            + weapon.getAngle());
    final BodyDef bulletDef = new BodyDef();
    bulletDef.type = BodyDef.BodyType.DynamicBody;
    bulletDef.angle = weapon.getAngle();
    bulletDef.position.set(
            weapon.getPosition().x
                    + (float) (2.5 * MathUtils.cos(weapon.getAngle())),
            weapon.getPosition().y
                    + (float) (2.5 * MathUtils.sin(weapon.getAngle())));
    bulletDef.angle = weapon.getAngle();
    PolygonShape bulletShape_1 = new PolygonShape();
    bulletShape_1.setAsBox(0.34375f, 0.34375f);
    CircleShape bulletShape_2 = new CircleShape();
    bulletShape_2.setPosition(new Vector2(0.34375f, 0));
    bulletShape_2.setRadius(0.34375f);
    final FixtureDef bulletFixture_1 = new FixtureDef();
    bulletFixture_1.density = 1f;
    bulletFixture_1.shape = bulletShape_1;
    bulletFixture_1.friction = 0.25f;
    bulletFixture_1.restitution = 0.75f;
    final FixtureDef bulletFixture_2 = new FixtureDef();
    bulletFixture_2.density = 1;
    bulletFixture_2.shape = bulletShape_2;
    bulletFixture_2.friction = 0.25f;
    bulletFixture_2.restitution = 0.75f;
    final Timer creationTimer = new Timer();
    creationTimer.scheduleTask(new Task() {

        @Override
        public void run() {
            if (!world.isLocked()) {
                System.out.println(bullet);
                bullet = world.createBody(bulletDef);
                bullet.createFixture(bulletFixture_1);
                bullet.createFixture(bulletFixture_2);
                System.out.println(bullet);
                bullet.applyForceToCenter(
                        force * MathUtils.cos(weapon.getAngle()), force
                                * MathUtils.sin(weapon.getAngle()), true);
                Sprite sprite = new Sprite(new Texture(
                        "sprites\\Weapon\\bullet_standard.png"));
                sprite.setSize(1.03125f, 0.6875f);
                sprite.setOrigin((float) (sprite.getWidth() / 2 - 0.12f),
                        (float) (sprite.getHeight() / 2));
                bullet.setUserData(sprite);
                Bullets bullets = Bullets.getInstance(world);
                bullets.addBullet(bullet);
                bullets.setDmg(bulletDmg);
                System.out.println("Create bullet number: " + bullet);
                creationTimer.stop();
            }
        }

    }, 0, 1);
    creationTimer.start();
}

}

我已經面對這個問題已經有一段時間了,無法解決問題,我很樂意為您提供幫助。 提前致謝!


更新1:
我不會重復使用所創建的任何項目符號。
這是處理與敵人碰撞的代碼:

public void onCollision(final Body collidedBody, final String bodyHit,
        final int index) {
    assert instance != null;
    final Timer timer = new Timer();
    timer.scheduleTask(new Task() {
        @Override
        public void run() {
            if (!world.isLocked()) {
                Circles circles = Circles.getInstance();
                if (bodyHit.equalsIgnoreCase("ground")) {
                    if (bulletGroundCollision.get(index) == 5) {
                        if (bullets.get(index) != null) {
                            world.destroyBody(bullets.get(index));
                            bullets.removeIndex(index);
                            bulletGroundCollision.removeIndex(index);
                        }
                    } else
                        bulletGroundCollision.set(index,
                                (bulletGroundCollision.get(index) + 1));
                } else if (bodyHit.equalsIgnoreCase("enemy")) {
                    Circle enemy = circles
                            .findAccordingToCode(collidedBody);
                    enemy.damaged(bulletDmg);
                    System.out.println("Hit at: "
                            + bullets.get(index).getPosition());
                    if (bullets.get(index) != null) {
                        world.destroyBody(bullets.get(index));
                        bullets.removeIndex(index);
                        bulletGroundCollision.removeIndex(index);
                    }
                } else if (bodyHit.equalsIgnoreCase("player")) {
                    if (bullets.get(index) != null) {
                        world.destroyBody(bullets.get(index));
                        bullets.removeIndex(index);
                        bulletGroundCollision.removeIndex(index);
                    }
                    Square square = Square.getInstance(world);
                    square.damaged(bulletDmg);
                }
                timer.stop();
            }
        }

    }, 0, 1);
    timer.start();
}

創建項目符號的代碼已經發布為bullet類。
bullets -是作為項目bulletsArray
bulletGroundCollision是一個整數Array ,用於跟蹤子彈在i位置(即索引)擊中地面的次數。

更新2
我已經注意到,子彈卡住后,就不會根據主體發生與子彈的碰撞,而是僅在與精靈碰撞時才觸發碰撞。

您的代碼有些難以閱讀。 但是,您需要確保在敵人死亡之后,需要使用子彈類中的update方法再次更新子彈類。

boolean enemyDead;

if(damage<=0)
{
 bulletClass.updateBulletLocation();
}

在我看來,殺死敵人后您正在更新子彈等級,因此,子彈不會移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM