Jump to content
  • Advertisement

Archived

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

Phorbin

--Collision Detection over a range--

This topic is 5283 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''m writing a game for my game programming class and I am having trouble with my collision detection. I am programming in C++ of course. I just can''t figure a way to check two ranges against each other. I have an upper left cordinate of the player. Which is 300 pixels wide and 200 pixels tall. Then I have a rock also with an upper left cordinate and that is 100 pixels square. I am looking for the best and fastest way to see if the two objects have hit eachother. --You can have everything if you let yourself be.--

Share this post


Link to post
Share on other sites
Advertisement

virtual BOOL Collides (Sprite *s)
{
// bounding rectangles method
if (y + height <= s->y)
return FALSE;
if (y >= s->y + s->height)
return FALSE;
if (x + width <= s->x)
return FALSE;
if (x >= s->x + s->width)
return FALSE;

return TRUE;
}


is that what you're looking for?

[edited by - JasonA on June 1, 2004 11:18:36 PM]

Share this post


Link to post
Share on other sites
you may have to better define what you mean by "range" if the simple box test above did not answer your question. To me, range means, "an object''s bounding volume over a segment of time"

Share this post


Link to post
Share on other sites
so you want some box/box collision/distance.

the distance between the two can be tricky to find. but in general, you find intersections of two box (min0, max0), (min1, max1) like that


struct vector
{
float x, y;
};

bool box_box_intersect(vector min0, vector max0, vector min1, vector max1)
{
if (min1.x > max0.x || min0.x > max1.x) return false;
if (min1.y > max0.y || min0.y > max1.y) return false;
return true;
}

for you,
min0 = vector(player_position.x-300/2, player_position.y-200/2);
max0 = vector(player_position.x+300/2, player_position.y+200/2);
min1 = vector(rock_position.x-100/2, rock_position.y-100/2);
max1 = vector(rock_position.x+100/2, rock_position.y+100/2);


the best way to deal with the actual collision simply, is to still work with intervals, along the X and Y axes, and select the best possible separation vector so you can push the boxes around so they stop intersecting.

this is the code I''ve got



bool Interval_Intersect(float min0, float max0, float min1, float max1, float& d)
{
float d0 = max1 - min0;
float d1 = max0 - min1;

if (d0 < 0.0f || d1 < 0.0f)
return false;

if (d0 < d1)
d = d0;
else
d = -d1;

return true;
}

bool Intersect_AABox_AABox(const Vector& xMin0, const Vector& xMax0, const Vector& xMin1, const Vector& xMax1, Vector& N)
{
N = Vector(0, 0);

//---------------------------------------------------------------------------

// test intersection along x axis

//---------------------------------------------------------------------------

if (!Interval_Intersect(xMin0.x, xMax0.x, xMin1.x, xMax1.x, N.x))
return false;

//---------------------------------------------------------------------------

// test intersection along y axis

//---------------------------------------------------------------------------

if (!Interval_Intersect(xMin0.y, xMax0.y, xMin1.y, xMax1.y, N.y))
return false;

//---------------------------------------------------------------------------

// select the axis with the minimum of separation as the collision axis

//---------------------------------------------------------------------------

if (fabs(N.y) < fabs(N.x))
{
N.x = 0.0f;
}
else
{
N.y = 0.0f;
}

return true;
}


it returns false if the box do not intersect, true is they intersect. it returns the MTD (the minimum translation distance), a vector that you can use to separate the box so they stop intersecting, by pushing the boxes along that vector.

Share this post


Link to post
Share on other sites
thanks guys
but I am a little confused by your answers.
I am just testing to see if the rock hit the player.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!