开发者

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

开发者 https://www.devze.com 2023-03-06 02:05 出处:网络
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 t

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.


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).


[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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消