Farseer 物理:碰撞检测问题
我有三个矩形块:地面块、蓝色块、英雄块。 地面放置在屏幕底部,蓝色块放在地面块上,英雄块掉落到蓝色块上。我有一个侦听器,可以检测英雄何时触地。 有两种情况: 1)当英雄从低处跌落到蓝色块时,它的 ok 侦听器会通知英雄仅接触蓝色块。 2)当英雄从稍高的高度跌落到蓝色方块时,监听器通知英雄接触地面!如何解决这个问题?
这是英雄 OnCollision 监听器:
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"))
{
OnHeroTouchedBlueBlock();
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;
}
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"))
{
OnHeroTouchedBlueBlock();
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,它根据掉落的高度而不同,这表明英雄正在穿透蓝色方块接触地面。尝试将英雄块设置为子弹体,如果问题解决则确实是这种情况。您还可以尝试蓝色块的高度,看看它如何影响事物。
我建议使用调试绘制来检查您所看到的是否确实是物理引擎中发生的情况。
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.
我找到了解决问题的方法:
1)我将 Farseer 项目插入到我的解决方案中
2)在Settings.cs中找到行
公共常量浮点AABBExtension = 0.1f;
3) 将值更改为“0.01f”。它有效!
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 !!!