Jump to content
  • Advertisement
Sign in to follow this  
game mercenary

collision detection

This topic is 4879 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 10x10 and a 55x55 box, I just cant get the collision detection to work. how would I do this? if you have any code that would help sitting around it would be nice if you could post it. please dont give me gamedevs tut on this.

Share this post


Link to post
Share on other sites
Advertisement
The simplest way is to take each point in the smaller box and see if it is within the other box. If any one is, you have a collision.

Share this post


Link to post
Share on other sites
if(bulletx <= enemyx+55 && bullety - 55 > enemyy)
the bullet is 10x10 and the tank is 55x55

here is the screen
X ->
----------------------------------
|..............................................|Y
||.............................................||
|..............................................|V
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
| .............................................|
------------------------------------

it should work, y doesnt it?

Share this post


Link to post
Share on other sites
You could do what I did and let Windows handle it for you.
I use the function called 'IntersectRect'. Research it on the MSDN.

Share this post


Link to post
Share on other sites
Quote:
Original post by game mercenary
if(bulletx <= enemyx+55 && bullety - 55 > enemyy)
the bullet is 10x10 and the tank is 55x55

here is the screen
X ->
----------------------------------
|..............................................|Y
||.............................................||
|..............................................|V
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
| .............................................|
------------------------------------

it should work, y doesnt it?

Do this for each point in the bullet:

if bulletpointx >= rectx && bulletpointx <= rectx+rectwidth &&
bulletpointy >= recty && bulletpointy <= recty+rectheight:
//this point is colliding
collisionFlag = True

Share this post


Link to post
Share on other sites
Quote:
Original post by game mercenary
I dont like windows specific programming, I like my programs to be cross-platform.


Well, I guess you could use somthing like this:


bool point_in_rect(const rect& r, int x, int y)
{
return (x >= r.left && x < rect.right && y >= rect.top && y < rect.bottom);
}

bool rect_intersect_rect(const rect& r1, const rect& r2)
{
rect smaller, bigger;
if (r1.area() >= r2.area()) {
bigger = r1;
smaller = r2;
} else {
bigger = r2;
smaller = r1;
}
if (point_in_rect(bigger, smaller.top, smaller.left) ||
point_in_rect(bigger, smaller.top, smaller.right) ||
point_in_rect(bigger, smaller.bottom, smaller.left) ||
point_in_rect(bigger, smaller.bottom, smaller.right)) {
return true;
}
return false;
}


Quote:
Original post by game mercenary
if you have any code that would help sitting around it would be nice if you could post it. please dont give me gamedevs tut on this.


Give one some bread, and you'll nourish him one day. Teach one how to make bread and you'll nourish him until then end of his life.

Or something like that.

Regards,

Share this post


Link to post
Share on other sites
thanks, but I figured it out..... some people pull out their sketh pad and doodle during school, due to the fact I homeschool on my laptop, I just open dev-c++. 1/3 through english I got it.

Share this post


Link to post
Share on other sites
You know, sometimes for this kind of thing it's better to "pull out your sketchpad and doodle"... in this case, draw the rectangles in intersecting and non-intersecting cases, label the coordinates, stare and think... [smile]

Share this post


Link to post
Share on other sites
Here's another method:


BOOL RectIntersect(const RECT& r1, const RECT& r2)
{
if (r1.right < r2.left ||
r1.left > r2.right ||
r1.bottom < r2.top ||
r1.top > r2.bottom)
{
return FALSE;
}

return TRUE;
}



It simply compares the opposite sides of each rectangle. ie; If the right side of one rectangle is outside the left side of the other, then they obviously aren't intersecting. It does this for each side of a rectangle. Simple and efficient.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!