--Collision Detection over a range--
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.--
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]
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"
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
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement