简体   繁体   English

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

[英]averaging pixel locations to find a surface normal. (2D)

I Am working on a 2D game in XNA that needs some physics. 我正在XNA上开发2D游戏,需要一些物理技巧。 I have found that physics engines such as Farseer will not cut it due to some of the ways they work. 我发现诸如Farseer之类的物理引擎由于它们的某些工作方式而不会削减它。 (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. (这是一个漫长的故事,涉及3次重写代码以最终弄清楚我需要自己编写物理学。)因此,根据我使用的参考资料,我现在唯一需要的数据是表面的法线。碰撞。

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. 谢谢。

Considered storing normals for each point in your sprites (ie 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). 这样,您将能够调整法线,以使碰撞看起来“自然”(就您的游戏而言,从物理的角度来看,不一定是正确的)。

[Edited] - I like Alexei's sprite normals idea better than my table-driven approach, but still would add ... [编辑]-我比表驱动方法更喜欢Alexei的Sprite Normals思想,但仍然会增加...

You may want to consider just having the ability to attach a 2D geometry to each sprite. 您可能只想考虑能够将2D几何图形附加到每个精灵上的能力。 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. 确定精灵碰撞时,请查看两个精灵的多边形,然后将标准碰撞物理应用于它们。

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 freq)(ratio)量以获得所需的效果。 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. 响应频率是您希望重叠校正自身的速度,“阻尼比”是一个正数,可以小于或大于一个正数,以便您可以微调阻尼响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM