Sign in to follow this  
game mercenary

collision detection

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this