平均像素位置以找到表面法线。 (二维)
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑存储精灵中每个点的法线(即在单独的纹理中),而不是尝试计算它们。这样您就可以调整法线,使碰撞看起来“自然”(从游戏的某种意义上来说,从物理角度来看不一定正确)。
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).
[已编辑] - 与我的表格驱动方法相比,我更喜欢 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.
这可能会起作用:找到两个最有问题的像素(每个身体一个)并在它们之间应用临时弹簧以将它们分开。基本上,无论哪个像素组合具有最大的重叠(与分离相反),并施加与重叠量成比例的力。这将为您的系统增加能量,因此还可以考虑添加阻尼器(力与相对速度成正比)。我经常使用的方程是:
我控制
(响应频率)
和(比率)
数量以获得所需的效果。响应频率是指您希望重叠自行纠正的速度,“阻尼比”是一个正数,可以小于或大于 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:
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.