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
Generally speaking, the best quality 2D collision detection is either pixel perfect or uses "outlines". The best speed uses boxes instead. You can find middle ground by defining a "collision is possible" box and a "collision is definite" box, then using either the outline or pixel perfect detection if both objects intersect each other's "collision is possible" box but both intersect each other's "collision is definite" box. Obviously if both objects intersect each other's "collision is definite" box, then there's a collision.

If you want rotation, pixel perfect is a bad option and using an outline is better. If you can sacrifice quality, it might be wise to create a group of rectangles that define a "fuzzy" outline. Rectangles can be rotated around a point somewhat efficiently with basic trigonometry

Math:
let theta be the angle of rotation, (xr, xy) be the cartesian point rotating around, and (xn, yn) be a corner of the rectangle.

newX=(xn-xr)*cos(theta) - (yn-yr)*sin(theta) + xr
newY=(yn-yr)*cos(theta) + (xn-xr)*sin(theta) + yr
Generally I guess you'd be rotating from the origin (0, 0) so you can use a simpler form:
newX = xn*cos(theta) - yn*sin(theta)
newY = yn*cos(theta) + xn*sin(theta)

Share this post


Link to post
Share on other sites
You were on the right track, actually. And circles might actually be more efficient than rectangles since you only need to rotate one point, but quality would be lost for non-circular objects or parts.

but I added the math required, so hopefully that's helpful.

As for the actual design, it's up to your programming style ultimately. ClanLib offers outline-based collision detection, so you might like to reference that. Generally speaking, though, you'd only need three things:

A struct or class containing outline information
A function to create a collision outline (doing this manually is a PITA, though so is programming an outline creator).
A function to detect a collision given the collision information of two objects, their positions in world space, their angle of rotation, and their scale (if applicable).

Share this post


Link to post
Share on other sites
Is there some info for what there using? Seems to me (from the docs I found) it is taking an image, creating a set of convex shapes for it and then collision testing with those? So more or less what I have, with auto generation of the collision data?

Share this post


Link to post
Share on other sites
[quote name='SyncViews' timestamp='1318329864' post='4871406']
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.
[/quote]

Rather than storing an objects rotation and it's non-rotated hull, you could just rotate the hull as the actual object rotates. That way you only apply transforms when things are actually moving/rotating. Objects that are at rest won't need to be transformed.

If you want, I think the farseer physics engine for C#/xna is open source so you can look to see what they do. I think they do a combination of both of the above, but their system is fairly complex due to the amount of features they support. Their code might also be hard to read, so I'm not sure quite how helpful it will be. Still, you might find something you find interesting.

Share this post


Link to post
Share on other sites
So anyway, I still need to efficiently store and transform this data. Somthing I just thought of is that storing a magnitude + direction seems faster to transform than cy coords, giving me somthing like:
[source lang="cpp"]
//I'm leaning towards not liking this. Perhaps a struct, union and type enum?
class Shape;
class Circle;
class Polygon;
class Shape
{
public:
/**Objects vectors currently hold x = magnitude, y = direction*/
virtual void translateFromMagDir(float rotate, const Vector2F &translate)=0;
/**Makes vectors into magnitude/direction format.*/
virtual void createMagDir()=0;
//not sure i like this either
virtual bool test(const Shape *other)const=0;
virtual bool test(const Circle *other)const=0;
virtual bool test(const Polygon *other)const=0;
//or these...
virtual void assign(const Shape *other)=0;
virtual Shape *copy()const=0;
};
class Circle : public Shape
{
public:
virtual void translateFromMagDir(float rotate, const Vector2F &translate)
{
p = Vector2F::fromMagDir(p.x, p.y + rotate);
}
virtual void createMagDir()
{
p = p.magDir();
}
virtual void assign(const Shape *other)
{
P = ((const Circle*)other)->p;
r = ((const Circle*)other)->r;
}
virtual Shape *copy()const
{
return new Circle(*this);
}
virtual bool test(const Shape *other)const
{
return other.test(this);
}
virtual bool test(const Circle *other)const;
virtual bool test(const Polygon *other)const;

Vector2F p;
float r;
};
class Polygon : public Shape
{
public:
virtual void translateFromMagDir(float rotate, const Vector2F &translate)
{
for(unsigned i = 0; i < count; ++i)
p[i] = Vector2F::fromMagDir(p[i].x, p[i].y + rotate);
}
virtual void createMagDir()
{
for(unsigned i = 0; i < count; ++i)
p[i] = p[i].magDir();
}
virtual void assign(const Shape *sother)
{
const Polygon *other = (const Polygon *)sother;
assert(count == other.count);
for(unsigned i = 0; i < count; ++i)
p[i] = other.p[i];
}
virtual Shape *copy()const
{
Polygon *p = new Polygon();
p->count = count;
p->p = new Vector2F[count];
p->assign(this);
return p;
}
virtual bool test(const Shape *other)const
{
return other.test(this);
}
virtual bool test(const Circle *other)const;
virtual bool test(const Polygon *other)const;

unsigned count;
Vector2F *p;
};
struct Hull
{
public:
Hull();
Hull(const Hull &hull);
Hull& operator = (const Hull &rhs);
void transform(float rotation, const Vector2F &translate);
static bool test(const Hull &a, const Hull &b);
private:
unsigned count;
Shape *parts;
RectF bbox;
};
...
[source]
struct SomeObjectType
{
collision::Hull collisionHull;
};
class SomeObject
{
const SomeObjectType *type;
collision::Hull collisionHull;
};
[/source]

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