Jump to content
  • Advertisement
Sign in to follow this  
LamerGamer

Collision Detection Suggestions

This topic is 5458 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

Just wondering how each of you have implemented collision detection of two sprites in your games. I started out using the one in "Game Programming All In One 2nd Ed." where you compare the central point of the source sprite with the entire bitmap area of the colliding sprite. Although it wasn't precise, it was sufficient for being simple and easy to implement. Now I've developed my own sprite collision detection function: for each sprite, you may define x amount of rectangles on the fly via a createCollisionRectangle() function such that you create enough rectangles to encompass the entire sprite without including a lot of "empty space" or leaving out odd-shaped parts of the sprite. Then the collide() function compares all the rectangles of the source sprite with all the rectangles of the colliding sprite. It's seems very accurate. I was just curious on how each of you use collision detection.

Share this post


Link to post
Share on other sites
Advertisement
I am trying to use bounding-volumes in my first attempt at game-making, crude but effective.

Share this post


Link to post
Share on other sites
I set up a structure for my different items such as spaceship, bullets,aliens and have a collision radius for each one when they confict i carry out the actions, such as kill alien and remove bullet.............i got this from another guy who was a tad smart!

void Collision( Actor *a, Actor *b )
{
if( a->active && b->active )
{
float r=(float)sqrt((a->x-b->x)*(a->x-b->x)+(a->y-b->y)*(a->y-b->y));
if( r<(a->colr+b->colr) )
{
if( b->type==ALIEN )
{
if( a->type==BULLET )

Share this post


Link to post
Share on other sites
I got the book Game Programming All In One. And the collision detection for bounding rectangles is bad, because its slow and it will sometimes say theres no collision when there is. I dunno if its the same in 2nd ed. What u say, is indeed very accurate but may become CPU expensive if u have alot of rectangles. Id just use this:
if ((rect1.x < rect2.x + rect1.width/2 + rect2.width/2) &&
(rect1.x > rect2.x - rect1.width/2 - rect2.width/2) &&
(rect1.y < rect2.y + rect1.height/2 + rect2.height/2) &&
(rect1.y > rect2.y - rect1.height/2 - rect2.height/2))
{return true;}
Not very precise, but fast

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!