Два тела не сталкиваются в физике Farseer 3.3.2 - PullRequest
0 голосов
/ 27 февраля 2012

В настоящее время я работаю над игровым проектом, использующим Farseer Physics для XNA. Прямо сейчас у меня есть два класса, расширяющих класс Body, который поставляется с Farseer. Ниже мой код для их столкновения.

Указанные ниже классы должны быть немного понятны. По сути, я хочу, чтобы игрок мог сталкиваться со всеми плитками в мире.

    public Player(World gameWorld, GameWindow Window, int playernum, Texture2D sprite) : base(gameWorld)
    {
        //place the player in the center of the screen - this whole method can be changed
        Position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
        playerSprite = sprite;
        playerNum = (PlayerIndex)playernum;

        //Fixture stuff
        playerFixture = FixtureFactory.AttachRectangle(sprite.Width, sprite.Height, 1, new Vector2(), this);
        playerFixture.CollisionCategories = Category.Cat2;
        playerFixture.CollidesWith = Category.Cat1;
        playerFixture.OnCollision += playerOnCollision;
        //initialize the melee weapon
        //initialize the ranged weapon
    }

    public Tile(World gameWorld, Vector2 location, Game1 game, Vector2 offset) : base(gameWorld)
    {
        //Loading content in the constructor for simplicity's sake because the content manager is initialized by the time the stage is created
        health = 100;
        prevhealth = health;
        maxhealth = health;

        this.game = game;
        contentName = game.random.NextDouble() > 0.5 ? "Images/Tiles/MarbleTilesBreak" : "Images/Tiles/MarbleTiles1Break";
        tileTex = game.Content.Load<Texture2D>(contentName + "0");
        //breakSound = game.Content.Load<SoundEffect>("Tiles/FloorBreaking");
        location.X *= tileTex.Width;
        location.Y *= tileTex.Height;
        location += offset;
        Position = location;
        tileFixture = FixtureFactory.AttachRectangle(tileTex.Width, tileTex.Height, 1, new Vector2(), this);
        tileFixture.CollisionCategories = Category.Cat1;
        tileFixture.CollidesWith = Category.Cat2;
        tileFixture.OnCollision += _OnCollision;
    }

Мое _OnCollision выглядит так:

    public bool _OnCollision(Fixture fix1, Fixture fix2, Contact con)
    {
        if (fix2.CollisionCategories == Category.Cat2)
        {
            health -= 10f;
        }
      return false;
    }

Тем не менее, когда я запускаю код, нет никаких признаков столкновения. Если у плитки 0 здоровья, она должна быть удалена, но никакая плитка никогда не удаляется.

1 Ответ

0 голосов
/ 27 июля 2012

Я обнаружил, что для перемещения своего спрайта я должен был шагнуть по миру, как они упоминали в комментариях, а затем назначить позицию спрайта позиции тела, что обновило мир, обновленный для меня.

world.step(TIME_SPEED);
sprite.Position = body.Position;
...