开发者

Farseer physics: Collision detection issues

开发者 https://www.devze.com 2023-03-28 15:06 出处:网络
I have three rectangular blocks: ground block, blue block, hero block. Ground is placed at the bottom of screen, blue block lay on ground block and hero block is falling down to blue block. I have lis

I have three rectangular blocks: ground block, blue block, hero block. Ground is placed at the bottom of screen, blue block lay on ground block and hero block is falling down to blue block. I have listener which detects when hero is touched ground. There are two situations: 1) When hero falls down from low height to blue block its ok listener notify that hero contacts only blue block. 2) When hero falls down from a bit higher height to blue block listener notify that hero touches ground !!! How to solve this issue ?

This is hero OnCollision listener:

bool heroBody_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
{

  Texture2D textureB = (Texture2D)fixtureB.UserData;
  string textureBName = ((string)textureB.Tag).ToLower();

  if (textureBName == "ground")
  { 
    OnHeroTouchedGround();
    return true;
   }
   else if (textureBName.Contains("blue"))
   {       
     OnHeroTouchedBlu开发者_如何学编程eBlock();
     return true;
   }

   return true;
}


    public HeroState GetHeroState()
    {
        ContactEdge contactEdge = null;

        if (heroBody != null) contactEdge = heroBody.ContactList;

        while (contactEdge != null)
        {
            if (heroBody.LinearVelocity == Vector2.Zero)
            {
                Texture2D textureA = (Texture2D)contactEdge.Contact.FixtureA.UserData;
                string textureAName = ((string)textureA.Tag).ToLower();
                Texture2D textureB = (Texture2D)contactEdge.Contact.FixtureB.UserData;
                string textureBName = ((string)textureB.Tag).ToLower();

                if (textureAName == "ground" || textureBName == "ground")
                    return HeroState.OnGroud;
                 else if (textureAName.Contains("blue") ||    textureBName.Contains("blue"))
                    return HeroState.OnHome;
            }

            contactEdge = contactEdge.Next;
        }

        return HeroState.Playing;
    }


The fact that it's different depending on the height dropped from suggests that the hero is penetrating right through the blue block to touch the ground. Try setting the hero block to be a bullet body, if the problem is fixed then this was indeed the case. You could also experiment with the height of the blue block to see how it affects things.

I recommend using debug draw to check that what you are seeing is really what's happening in the physics engine.


I found how to solve the issue: 1) I plugged Farseer project to my solution 2) in Settings.cs found line public const float AABBExtension = 0.1f; 3) Changed value to '0.01f'. And it works !!!

0

精彩评论

暂无评论...
验证码 换一张
取 消