Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

compfanatic

Collision Testing

This topic is 6720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 +

Share this post


Link to post
Share on other sites
Advertisement
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)>

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!