What kind of collision detection?
I''m making a top down game where you drive a tank.
1) What kind of collision detection would be best if i''m going to collide with other tanks, bullets, and other misc things like walls.
I don''t think I can use rectangle detection unless somehow you can rotate that.
2) Are there any other choices besides pixel perfect?
3)Wouldn''t it be slow?
To test a point against a rectangle, you could test it against each edge of the rectangle. If it is on the 'correct' side of each edge of the rectangle, it is inside.
An easier , but probably slower way of doing it would be to take the point, perform the inverse of the opperations you did on the rectangle on it, then use the rectangle colision detection you're probably used to.
To test 2 rectangles against eachother, test all 4 points of rect A against rect B, then all 4 points of rect B against rect A.
[edited by - Bagel Man on October 13, 2002 9:58:26 PM]
An easier , but probably slower way of doing it would be to take the point, perform the inverse of the opperations you did on the rectangle on it, then use the rectangle colision detection you're probably used to.
To test 2 rectangles against eachother, test all 4 points of rect A against rect B, then all 4 points of rect B against rect A.
[edited by - Bagel Man on October 13, 2002 9:58:26 PM]
I can''t do that because if the tank is rotated 45 degrees, there will be a big area that will test true, but doesn''t actually hit the tank.
Rotate the bounding box with the tank. You can then do a simple polygon-point or polygon-polygon collision test. I''m not 100% sure, but there could be some sort of optimized method out there for rectangular polygons in any orientation.
Some time ago, I coded a routine for rapidly detecting a collision between two rotated rectangles. My routine covers all cases of collision, including the infamous "crossing rectangles" case. Some low level optimizing might do it some good.
If you find it useful or you actually code it *well* in assembly, please let me know.
Here it is:
http://qsoft.ragestorm.net
[edited by - SkatMan on October 14, 2002 12:43:56 PM]
If you find it useful or you actually code it *well* in assembly, please let me know.
Here it is:
// Rotated Rectangles Collision Detection Routine// Written by Oren Becker, 2001// email: skatman AT ragestorm DOT net// website: http://qsoft.ragestorm.netstruct _Vector2D{ float x, y;};struct _RotRect{ _Vector2D C; _Vector2D S; float ang;};inline void AddVectors2D(_Vector2D * v1, _Vector2D * v2){ v1->x += v2->x; v1->y += v2->y; }inline void SubVectors2D(_Vector2D * v1, _Vector2D * v2){ v1->x -= v2->x; v1->y -= v2->y; }inline void RotateVector2DClockwise(_Vector2D * v, float ang){ float t, cosa = cos(ang), sina = sin(ang); t = v->x; v->x = t*cosa + v->y*sina; v->y = -t*sina + v->y*cosa;}// Rotated Rectangle Collision Detection, Oren Becker, 2001int CDRotRects(_RotRect * rr1, _RotRect * rr2){ _Vector2D A, B, // verteces of the rotated C, // center of rr2 BL, TR; // verteces of rr2 (bottom-left, top-right) float ang = rr1->ang - rr2->ang, // orientation of rotated rr1 cosa = cos(ang), // precalculated trigonometic - sina = sin(ang); // - values for repeated use float t, x, a; // temporary variables for various uses float dx; // deltaX for linear equations float vert1, vert2; // min/max vertical values // move rr2 to make rr1 cannonic C = rr2->C; SubVectors2D(&C, &rr1->C); // rotate rr2 clockwise by rr2->ang to make rr2 axis-aligned RotateVector2DClockwise(&C, rr2->ang); // calculate vertices of (moved and axis-aligned := 'ma') rr2 BL = TR = C; SubVectors2D(&BL, &rr2->S); AddVectors2D(&TR, &rr2->S); // calculate vertices of (rotated := 'r') rr1 A.x = -rr1->S.y*sina; B.x = A.x; t = rr1->S.x*cosa; A.x += t; B.x -= t; A.y = rr1->S.y*cosa; B.y = A.y; t = rr1->S.x*sina; A.y += t; B.y -= t; t = sina*cosa; // verify that A is vertical min/max, B is horizontal min/max if (t < 0) { t = A.x; A.x = B.x; B.x = t; t = A.y; A.y = B.y; B.y = t; } // verify that B is horizontal minimum (leftest-vertex) if (sina < 0) { B.x = -B.x; B.y = -B.y; } // if rr2(ma) isn't in the horizontal range of // colliding with rr1(r), collision is impossible if (B.x > TR.x || B.x > -BL.x) return 0; // if rr1(r) is axis-aligned, vertical min/max are easy to get if (t == 0) {vert1 = A.y; vert2 = -vert1; } // else, find vertical min/max in the range [BL.x, TR.x] else { x = BL.x-A.x; a = TR.x-A.x; vert1 = A.y; // if the first vertical min/max isn't in (BL.x, TR.x), then // find the vertical min/max on BL.x or on TR.x if (a*x > 0) { dx = A.x; if (x < 0) { dx -= B.x; vert1 -= B.y; x = a; } else { dx += B.x; vert1 += B.y; } vert1 *= x; vert1 /= dx; vert1 += A.y; } x = BL.x+A.x; a = TR.x+A.x; vert2 = -A.y; // if the second vertical min/max isn't in (BL.x, TR.x), then // find the local vertical min/max on BL.x or on TR.x if (a*x > 0) { dx = -A.x; if (x < 0) { dx -= B.x; vert2 -= B.y; x = a; } else { dx += B.x; vert2 += B.y; } vert2 *= x; vert2 /= dx; vert2 -= A.y; } } // check whether rr2(ma) is in the vertical range of colliding with rr1(r) // (for the horizontal range of rr2) return !((vert1 < BL.y && vert2 < BL.y) || (vert1 > TR.y && vert2 > TR.y));}
http://qsoft.ragestorm.net
[edited by - SkatMan on October 14, 2002 12:43:56 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement