碰撞检测委托方案

发布于 2024-09-29 16:13:34 字数 495 浏览 8 评论 0原文

嘿伙计们!我的物理引擎进展顺利(感谢您的提问!),我已经准备好开始研究一些更高级的垃圾了。举个例子,我正在尝试设置碰撞引擎,以便在发生碰撞时可以通知任意委托。让我为您设置一个场景:

假设我们在物理模拟中有对象 A、对象 B 和对象 C。我希望能够通知代表 A 和 B 之间的冲突,并通知可能不同的代表 A 和 C 之间的冲突。

一些背景信息:我有一个已知的代表接口,我有可能为我的碰撞检测器存储状态(但不是 atm),并且能够在对象本身中存储状态。同样,我使用此委托模型来处理碰撞解决,只需将物理引擎默认设置为所有对象的委托,允许用户根据需要更改委托。

现在,我已经尝试让每个对象存储它自己的碰撞委托,当发生碰撞时会通知该委托。这不起作用,因为当对象具有相同的碰撞委托时,相同的碰撞会被处理两次。当我切换到仅使用第一个对象的委托时(无论如何决定),模拟的顺序成为一个问题。我想使用字典,但这会带来大量开销。然而,这似乎是我需要前进的方向。

那么问题来了:为找到合适的解决方案而进行殊死搏斗。您将如何解决这个问题?

Hey guys! My physics engine is coming along quite nicely (thanks for asking!) and I'm ready to start working on some even more advanced junk. Case in point, I'm trying to set up my collision engine so that an arbitrary delegate can be notified when a collision occurs. Let me set up a scenario for you:

Say we have object A, object B, and object C in the physics simulation. I want to be able to inform a delegate about a collision between A and B, AND inform a potentially DIFFERENT delegate about a collision between A and C.

A little background information: I have a known interface for the delegate, I have the potential of storing state for my collision detector (but don't atm), and have the ability to store state in the objects themselves. Similarly, I use this delegate model to handle collision resolution, simply setting the physics engine as the delegate for all objects by default, allowing the user to change the delegate if desired.

Now, I already tried having each object store it's own collision delegate that would be informed when a collision occurred. This didn't work because when the objects had the same collision delegate, the same collision was handled twice. When I switched to only using the delegate of the first object (however that was decided), the order of simulation became an issue. I want to use a dictionary, but that introduces a significant amount of overhead. However, that seems like the direction I need to be heading.

So here's the question: fight to the death over a suitable solution. How would YOU solve this problem?

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

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

发布评论

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

评论(1

站稳脚跟 2024-10-06 16:13:34

我必须说,两个对象可以有不同的委托(在碰撞时)有点奇怪,但如果两个相同的委托在碰撞时触发,那仍然会很糟糕。我觉得他们应该一直开火或者只有其中一个应该开火。一致性是这里困扰我的地方。
解释一下这会有所帮助。

其次,如果您使用简单的版本,为每个对象持有一个委托,然后调节激活其功能(“if(!某个布尔值指示该委托已被触发){做某事}”),这可以通过一个非常小的解决方案来解决开销。
它有效,但我不喜欢这种代码。

我的建议(有点复杂,所以在处理之前请考虑一下)是尝试专注于一个管理器对象,该对象将遍历所有委托并调用与碰撞相关的两个委托。
例如,A 和 B 发生碰撞,并且以它们作为参数来调用管理器。现在,您可以循环遍历系统已知的所有委托(假设它们很少)并触发匹配“delegate == a.del 或 delegate == b.del”的委托。
这会带来更大的开销,但如果我们谈论的是少数代表,则影响很小。另一方面,这将允许您将来在该领域进一步扩展碰撞检测引擎(例如每个对象存在多个委托)。

I must say that it's a bit odd that two objects can have different delegates (at a collision) and still it would be bad if two identical delegates fired at a collision. I seems like they should both fire all the time or only one of them should. Consistency is what bothers me here.
Explaining that would help some more.

Second, if you use the naive version of holding a delegate for each object and then conditioning activating its functionality ("if (!some boolean indicating this delegate was fired already) {do something}"), this could be solved with a very small overhead.
It works, but I don't like this kind of code.

My suggestion (a bit complex, so think about it before working on it) is to try to focus on a manager object which would go over all the delegates and invoke the two that where relevant to the collision.
For instance, A and B collide, and the manager is invoked with them as parameters. You now can cycle through all the delegates known to the system (assuming they are few) and fire the ones that match "delegate == a.del or delegate == b.del".
This comes at a greater overhead, but if we are talking about a small number of delegates, if will make very little difference. On the other side this will allow you to expend your collision detection engine in this area further more in the future (like the existence of more then one delegate per object).

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