Best ways to test for collision detection.

Started by
6 comments, last by DevLiquidKnight 21 years, 8 months ago
[removed] [Edited by - DevLiquidKnight on February 14, 2007 9:21:15 AM]
Advertisement
If you want a working implementation of a collision detection system to see what you would like, try Paul Nettle''s ellipsoid collision scheme.

http://www.fluidstudios.com/publications.html

That seems to be very effective. Spheres are easier to implement though.
try coldet:

www.photoneffect.com/coldet
Just use the distance equation,

Private Function GetDist(intX1 As Single, intY1 As Single, intX2 As Single, intY2 As Single) As Single

DIST = Sqr((intX1 - intX2) ^ 2 + (intY1 - intY2) ^ 2)

End Function

Private Sub Colision()
If Dist(Object1, Ojbect2) >= 0 Then

'' perform collision repsonse code

Else
Exit Sub
End If

If you are using 3D, the dist equation is

Sqr((intX1 - intX2) ^ 2 + (intY1 - intY2) ^ 2) + (intZ1 - intZ2) ^ 2)

-megaman
Two problems with distance equasion technique: a) works only if you''re testing if two points collide. Might be better if you could check for a range. That would give you a point within a sphere test, which is a little better. b) it''s slow. You''ve got 2 multiplications two substractions and a square root. For normal loads that''s probably fine, but not when you''ve got a good deal of collidable objects on screen.

A simple InRect call is fine for a simple 2D engine, especially if your sprites take up an entire rect. This can be most simply implemented as (in C):

  inline bool IsInRect(int x, int y, RECT rect){     return x > rect.left && x < rect.right &&            y > rect.top && y < rect.bottom;}  


Also, if you''re working with a tile engine with unmovable tiles, flaging tiles you can''t walk on is a simple check. e.g. map[x][y].isWalkable
inline bool IsInRect(int x, int y, RECT rect)
{ return x > rect.left && x < rect.right && y > rect.top && y < rect.bottom;
}
is somthing i never seen before quite intersting
Most engines when you collide with a wall at an angle you move slowly parralel to the wall. If you get what i mean - in my engine you stop dead - i wanted to have that slow parralel movement - what algoritm is that.

YOU''''LL NEVER BEAT THE IRISH!
You'll Never Beat The Irish!
quote:Original post by Cuchulainn
Most engines when you collide with a wall at an angle you move slowly parralel to the wall. If you get what i mean - in my engine you stop dead - i wanted to have that slow parralel movement - what algoritm is that.


Just project the velocity vector onto the surface. My linear algebra is a bit rusty bit I think it''s:

v is the velocity vector, w1 and w2 are any two non-parallel vectors on the plane. The projection of v onto the plane is (v dot w1)/(w1 dot w1)*w1 + (v dot w2)/(w2 dot w2)*w2

This topic is closed to new replies.

Advertisement