Sign in to follow this  
SyncViews

2D Collision Detection Implementation Design Questions

Recommended Posts

I'm working on code my own collision detection code from scratch (mainly as a learning exercise, might use it in a game if I like the end result).

I'm planning on using points, lines, circles, AABB's (for fast boundary box testing) and convex polygons. With the exception of the AABB rects (and circles :) ) everything will support rotation. Complex objects will be represented as a list of different parts.

I'm fairly happy with the checking for collision between 2 of these shapes (using seperating axis theorm for the polygons, and simple stuff like distance for circle-circle/point) however I'm not sure on the best way to get this data. At present each shape uses a simple struct, e.g.:
[source lang="cpp"]
struct Circle : public Shape
{
Vector2F centre;
float r;
};
struct Polygon : public Shape
{
unsigned count;
Vector2F *p;
};
...
struct Hull
{
unsigned count;
Shape *parts;
RectF bbox;//essentailyl a square, since needs to be large enough for all rotations
};
...
bool test(Circle a,Circle b)
return distance(a,b) < a.r + b.r
bool test(Circle,Point)
bool test(Hull,Hull)
[/source]

This is fine if the objects never move, but since that doesnt happen, the issue is taking a Hull, and applying a rotation and translation to it to get all the shapes into world coordinates (or at least right relativeto the other object). The two options Ive come up with are:

1) The test etc methods take a position and rotation for each of the two objects, and transform the shapes vertices on the fly. The downside to this is that in a given frame, many vertices get transformed many times. Also rotating vertices each require a fair bit of trig or matrix multiplication, which seems expensive.
2) When an object instance is created, it gets its own copy of its Hull. Each time it moves it then updates the vertices using the origenal hull as a template. Doesnt seem to solve the expensive transform issue though.

Is there a better way to store the collision data, or to use the data during detection and response?

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