平均像素位置以找到表面法线。 (二维)

发布于 2024-11-07 05:56:07 字数 321 浏览 0 评论 0原文

我正在 XNA 中开发一款 2D 游戏,需要一些物理知识。我发现像 Farseer 这样的物理引擎由于其某些工作方式而不会削减它。 (这是一个很长的故事,涉及重写代码 3 次,最终发现我需要自己编写物理原理。) 因此,根据我正在使用的参考材料,我现在唯一需要的数据是碰撞表面的法线。

这是使用精灵的每像素碰撞检测。 我目前有一个所有重叠像素及其位置的数组。我想使用这些数据来平均重叠像素的位置,以找出该表面的法线。这应该允许我与不规则形状的物体发生碰撞,但仍然保留真实的物理相互作用。

如果有人对我如何使用此像素信息来计算出表面法线或足够接近的东西提出建议,那就太好了。

谢谢。

I Am working on a 2D game in XNA that needs some physics. I have found that physics engines such as Farseer will not cut it due to some of the ways they work. (thats a long story involving rewriting the code 3 times to finally figure out that I needed to write the physics myself.)
So according to the reference material I'm using, the only data that I need now is a normal for the surface of collision.

This is using per-pixel collision detection using sprites.
I currently have an array of all the overlapping pixels and their locations. I would like to use that data to some how average the locations of the overlapping pixels to figure out the normal for that surface. this should then allow me to have collisions with objects of irregular shape but still retain realistic physics interaction.

If anyone has a suggestion on how I could use this pixel info to figure out the surface normal or something close enough, that would be great.

Thanks.

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

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

发布评论

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

评论(3

琉璃繁缕 2024-11-14 05:56:07

考虑存储精灵中每个点的法线(即在单独的纹理中),而不是尝试计算它们。这样您就可以调整法线,使碰撞看起来“自然”(从游戏的某种意义上来说,从物理角度来看不一定正确)。

Considered storing normals for each point in your sprites (i.e. in separate texture) instead trying to compute them. This way you'll be able to adjust normals so your collisions look "natural" (in a sense of your game, not necessary correct from physics point of view).

伴梦长久 2024-11-14 05:56:07

[已编辑] - 与我的表格驱动方法相比,我更喜欢 Alexei 的精灵法线想法,但仍然会添加...

您可能需要考虑将 2D 几何体附加到每个精灵的能力。它将使您能够进行真正的多边形碰撞。当您确定精灵碰撞时,请查看两个精灵的多边形并对它们应用标准碰撞物理原理。

[Edited] - I like Alexei's sprite normals idea better than my table-driven approach, but still would add ...

You may want to consider just having the ability to attach a 2D geometry to each sprite. It will give you the ability to do real polygon collision. When you determine a sprite collision, look at the polygon of the two sprites and apply standard collision physics to them.

帅冕 2024-11-14 05:56:07

这可能会起作用:找到两个最有问题的像素(每个身体一个)并在它们之间应用临时弹簧以将它们分开。基本上,无论哪个像素组合具有最大的重叠(与分离相反),并施加与重叠量成比例的力。这将为您的系统增加能量,因此还可以考虑添加阻尼器(力与相对速度成正比)。我经常使用的方程是:

(reduced mass) = 1/( 1/(mass 1) + 1/(mass 2) )
(stiffness) = (reduced mass) * (response freq)^2
(damping) = (ratio)*(reduced mass) * (response freq)

我控制(响应频率)(比率) 数量以获得所需的效果。响应频率是指您希望重叠自行纠正的速度,“阻尼比”是一个正数,可以小于或大于 1,这样您就可以微调阻尼响应。

This might work: Find your two worst offending pixels (one for each body) and apply a temporary spring between them to drive them apart. Basically whichever pixel combo has the largest overlap (opposite of separation) and apply a force proportional to the overlap amount. This will add energy to your system so consider adding a damper (force proportional to relative velocity) also. The equations I use often are:

(reduced mass) = 1/( 1/(mass 1) + 1/(mass 2) )
(stiffness) = (reduced mass) * (response freq)^2
(damping) = (ratio)*(reduced mass) * (response freq)

and I control the (response freq) and (ratio) amount to get the desired effect. Response frequency is how fast you want the overlap to correct itself, and "damping ratio" is a positive number than can be less or more than one that will let you fine tune your damping response.

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