如何检测与 Chipmunk 物理引擎的永久碰撞

发布于 2024-08-08 07:18:27 字数 219 浏览 7 评论 0 原文

当球形状击中任何其他形状时,我试图发出“轰鸣”声。这有效。但效果有点太好了……

当球静止或开始滚动时,它会与它所接触的任何物体发生永久碰撞,因此会不断发出“轰鸣”声。

我在花栗鼠文档中找不到任何内容来告诉我何时两件事永久碰撞。所以我想我必须自己想办法解决这个问题,可能是使用某种计时器来比较上次碰撞与当前碰撞。但这对我来说听起来很老套。

有人解决过这个问题吗?你是怎么解决的?

I'm trying to play a "boing" sound when a ball shape hit's any other kind of shape. Which works. But works a little too well....

When the ball comes to rest, or starts to roll, it's in a permanent collision with whatever it's touching, so the "boing" sound fires constantly.

I can't find anything in the chipmunk documentation to tell me when two things are permanently colliding. So I'm thinking I will have to somehow figure it out myself, probably with some sort of timer that compare the last collision against the current collision. But that sounds hacky to me.

Has anyone tackled this issue? How did you solve it?

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

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

发布评论

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

评论(3

虚拟世界 2024-08-15 07:18:27

当接触中断时,你不能只播放“boing”声吗?
在chipmunk中有一个回调, typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data)

这样,每当它弹起时,你都会收到boings,但当它只是滚动。当然,当它从你的形状上滚下来时,你也会得到一个,但这可能是一个功能,具体取决于你如何看待它。

Couldn't you just play your "boing" sound when contact is BROKEN?
There is a callback for that in chipmunk, typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data)

That way you get boings whenever it bounces, but not when it's just rolling along. 'course, you'll also get one when it rolls off of your shape, but that could be a feature depending on how you look at it.

梅倚清风 2024-08-15 07:18:27

我不认为我要说的是一个好的做法,但我相信它会解决你的问题:

  1. 对于每个对象,保留两个状态变量:(1)最后一个对象碰撞, (2) 最后一次碰撞时间。

  2. 碰撞时,仅当碰撞对象不同(和球)或自上次碰撞以来已经过了一定的“增量时间”时才播放声音。然后记录最后一次碰撞统计数据。

这非常简单而且非常有效:

// In your ball interface
id lastCollisionObject;
double lastCollisionTime;


// When a collision occurs...
double now = GetTime();
id other = GetCollisionObject();
if ((now - lastCollisionTime) > 0.3 || other != lastCollisionObject) {
    PlaySound(kBoingSound);
}
lastCollisionObject = other;
lastCollisionTime = now;

I don't think that what I'm about to say is a good practice, but I'm sure it will solve your problem:

  1. For each object, keep two state variables: (1) The last object collided with, (2) The last collision time.

  2. Upon collision, only play a sound if the collided object is different (and a ball) OR a certain "delta time" has elapsed since the last collision. Then record the last collision stats.

This is pretty simple and very effective:

// In your ball interface
id lastCollisionObject;
double lastCollisionTime;


// When a collision occurs...
double now = GetTime();
id other = GetCollisionObject();
if ((now - lastCollisionTime) > 0.3 || other != lastCollisionObject) {
    PlaySound(kBoingSound);
}
lastCollisionObject = other;
lastCollisionTime = now;
梦途 2024-08-15 07:18:27

花栗鼠的形状没有速度。他们所附着的身体拥有它。您可以像这样访问速度:“myShape.body->v”。我同意您应该能够检查速度是否超过某个阈值,以了解何时发生“影响”。您还可以检查旋转速度以查看球是否在滚动。

Shapes don't have velocity in chipmunk. The bodies they are attached to have it. You can access velocity like this: "myShape.body->v". I agree that you should just be able to check if the velocity is over a certain threshold to know when an 'impact' occurs. You can also check the rotational velocity to see if the ball is rolling.

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