Skip to main content
GameDev.net gamedev.net
🔒 Locked

Collision Testing

Started by compfanatic Jun 21, 2000 at 11:06 PM 1 replies 500+ views
Original Post
compfanatic
compfanatic
I have a function that tests for collision, but it only tests for collision in the center of the object. Anybody know of a great function that tests for collision with the borders of a sprite? Thanks! -Alex +
I looooove programming! Even at my young age.
aDasTRa
aDasTRa
this might be overly complicated, but it works.

1-get the "radius" of your sprites (i am assuming you are using aome kind of 2-d bitmap; if not the rest of this post will be meaningless to you). the radius would be the distance from the centre of the sprite to the farthest point out.

2-check the distances between the center of one sprite and every other sprite on the screen (or something), and repeat for each sprite. if the distance between centers is less than the sum of the radii of the two sprites, then a collision may have occured.

3-at this point you may be satisfied that a collision did occur (which is not always a good idea but works for the most part). you could go on to check if the sprites actually did hit by a couple of ways. one that works well (but is slow) is this:
-find the area of the sprites that overlaps.
-pick one sprite and find the location of every non-alpha value (ie not transparent)
-under each of these non-transparent pixels, check to see if under it there is a non-transparent value on the other sprite.

as i said, this may seem too complicated, but it works beautifully.



<(o)>
<(o)>
djsteffey
djsteffey
bitmaps are square.........
the cheap and easy way is to check bounding box collision.....
your sprite has four corners, which is actually a point......each point has an x and y value....so just check to see if any one of those four points are inside of the sprite you are checking for collision against
    
int x = oneCornerOfSpriteXvalue;
int y = oneCornerOfSpriteYvalue;

if (x > otherSpriteX && x < otherSpriteX + otherSpriteWidth &&
y > otherSpriteY && y < otherSpriteY + otherSpriteHeight)
{
// you have a collision

}
else
{
// you dont have a collision

}


this is very basic collision detection.....it doesn''t look too good if you are using sprites that have alot of "empty space" (black usually) in them, especially on the edges like a circle would be.......for those you need to do pixel perfect collision which I am not going to get into on this post because it is tough to get implemented correctly but when you do, it is well worth it


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.