2012-02-27 85 views
0

我目前正在使用Farseer物理XNA的遊戲項目。現在我有兩個類擴展了Farseer自帶的Body類。下面是我的代碼,讓他們碰撞。兩個物體不會在Farseer物理碰撞3.3.2

下面的類應該有點不言自明。基本上,我希望玩家能夠與世界上所有的瓷磚碰撞。

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健康狀態時,應該刪除它,但不會刪除瓦片。

+0

您是踩着世界級的世界嗎? – ClassicThunder 2012-02-27 17:45:11

+0

使用world.Step(1)確實解決了碰撞問題。然而,現在,夾具並沒有移動。 – 2012-02-28 00:53:53

+0

嘗試步驟((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f); – ClassicThunder 2012-02-28 01:14:04

回答

0

我發現的是,爲了移動我的精靈,我不得不像他們在評論中提到的那樣走向世界,然後將精靈的位置分配給身體的位置,從而爲我更新世界。

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