Skip to main content
gamedev.net
🔒 Locked

RECT collision detection.

Started by maxd gaming 2 replies 300+ views
Original Post
maxd gaming
maxd gaming
I have the corrod''s of my 2 objects stored in RECTS (they are 2d) and I want to see if they have collided (via a 2d bounding box in rects) please help (source would be nice) doesn''t matter which language cause it is easy to figure out... HELP!

It''s Maxd Gaming, put in an underscore and I will beat you with a rubber ducky! { My Site! - Banner } { My Site! - One time popup }{ My .Net Information }{ A upcoming space RTS codenamed Gruntacktica . }{ . }
Not following the new trend: Looky here mommy! No cow pic!
Phoe''s Forums
oliii
oliii

bool collided(RECT* r1, RECT* r2)
{
if (r1->bottom < r2->top || r2->bottom < r1->top) return false;
if (r1->right < r2->left || r2->right < r1->left) return false;
return true;
}
Everything is better with Metal.
maxd gaming
maxd gaming
quote:
Original post by oliii

bool collided(RECT* r1, RECT* r2)
{
if (r1->bottom < r2->top || r2->bottom < r1->top) return false;
if (r1->right < r2->left || r2->right < r1->left) return false;
return true;
}



Whoa... perfect thanks you are my god





It''s Maxd Gaming, put in an underscore and I will beat you with a rubber ducky!
{ My Site! - Banner } { My Site! - One time popup }{ My .Net Information }{ A upcoming space RTS codenamed Gruntacktica . }{ . }

Not following the new trend:
Looky here mommy! No cow pic!


Phoe''s Forums
oliii
oliii
I might as well tell you how you can find the intersection depth, then you can push the player away from the tile.


// r1 = player, r2 = tile, push = direction to push the player

bool collided(RECT* r1, RECT* r2, POINT* push)
{
float y0 = r1->top - r2->bottom;
float y1 = r2->top - r1->bottom;
if (y0 > 0.0f || y1 > 0.0f) return false;

if (abs(y1) < abs(y0))
push->y = y1;
else
push->y = -y0;

float x0 = r1->left - r2->right;
float x1 = r2->left - r1->right;
if (x0 > 0.0f || x1 > 0.0f) return false;

if (abs(x1) < abs(x0))
push->x = x1;
else
push->x = -x0;

if (abs(push->x) < abs(push->y))
push->y = 0;
else if (abs(push->y) < abs(push->x))
push->x = 0;

return true;
}
Everything is better with Metal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.