#### Archived

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

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

## 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 on other sites
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 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

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633368
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×