BOX2D - 与怪物的正确碰撞

发布于 2024-12-28 08:44:07 字数 890 浏览 1 评论 0原文

我正在开发 2D 侧卷轴 Android 游戏,使用 AndEngine 及其 BOX2D 扩展。

我有玩家身体,有 2 个用于“脚”和“头”的传感器,因此我可能确切地知道玩家的哪一侧接触了不同的物体等。下面的图像显示了它是如何工作的:

在此处输入图像描述

它可以很好地检查玩家当前是否用脚接触地面,以便他可以跳跃等。现在我正在尝试执行之后执行的操作与怪物身体接触。

在我的接触传感器中,我正在检查

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("foot"))
                {
                    jump();
                }

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("player"))
                {
                    GameManager.playSound(lostSound);
                    handleDie();
                }

但每次我跳到怪物的“头部”上(所以基本上我用脚传感器触摸它)都会执行死亡动作。因为这两个接触都会被接触监听器注意到,所以如果我将脚传感器设置得更高,从玩家身体突出更多(玩家身体是玩家精灵纹理的精确形状),它就会起作用,这样看起来玩家和怪物之间没有接触根本不。

预先感谢您提供如何正确处理的任何提示。

I'm developing 2D Side scroll Android Game, using AndEngine and its BOX2D extension.

I have player body, with 2 sensors for 'feet' and 'head' so I might know exactly which side of the player touched different object etc. Here's image showing how does it work:

enter image description here

It works well for checking if player is currently touching ground with feet, so he can jump for example, etc. Now I'm trying to implement actions executed after contact with monster body.

In my contact sensor, I'm checking

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("foot"))
                {
                    jump();
                }

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("player"))
                {
                    GameManager.playSound(lostSound);
                    handleDie();
                }

But every time I jump on the 'head' of the monster (So basically I'm touching it with feet sensor) died action is executed. Because both contacts are noticed by contact listener, it works if I would make feet sensor higher, to protrude more from player body (player body is exact shape of player's sprite texture) so it would look like there wasn't contact between player and monster at all.

Thanks in advance for any tips how to handle it properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2025-01-04 08:44:07

我认为最简单的方法是让你的“脚”传感器成为一个坚固的固定装置。也让玩家的身体变小。实际上,您可以制作具有 3 个固定装置的玩家:头部、身体、脚。碰撞仍然会以正确的方式处理,但如果你用脚接触怪物,由于固体固定装置,无法用身体接触他

The easiest way i think is to make your 'Feet' Sensor a solid fixture. Also make players'body smaller. You can actually make the player of 3 solid fixtures: head, body, feet. The collision will still be handled the right way, but if you touch the monster with feet there is no way to touch him with the body because of the solid fixture

笨死的猪 2025-01-04 08:44:07

在您的更新方法中检查这样的联系:

for (b2ContactEdge* ce = feet->GetContactList(); ce; ce = ce->next)
{

     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if one of them is a monster, mark him for being ignored this frame.
}

for (b2ContactEdge* ce = player->GetContactList(); ce; ce = ce->next)
{
     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if the monster is ignored, jump(), else die() 
}

您可能还应该检查怪物是否在玩家下方。

这是 C++,但您应该能够在 Java 中执行类似的操作。

希望有帮助。

Check contact like this on your update method:

for (b2ContactEdge* ce = feet->GetContactList(); ce; ce = ce->next)
{

     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if one of them is a monster, mark him for being ignored this frame.
}

for (b2ContactEdge* ce = player->GetContactList(); ce; ce = ce->next)
{
     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if the monster is ignored, jump(), else die() 
}

You probably should also check if the monster is below the player.

This is C++, but you should be able to do something similar in Java.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文