Spherical collision is great performance-wise, but some meshes may be hard to sum up as a mere sphere and produce silly collisions depending on the radius you apply to them. The key to maximizing performance is skipping the sqrt on your distance call, and instead squaring the other side of the comparison equation... So that

[source lang="cpp"]typedef struct{ float x, y, z;} vec3_t;typedef struct{ vec3_t origin; float radius; // etc properties...} object_t;// returns 1 if colliding, returns 0 if not..int ObjectsColliding(object_t *a, object_t *b){ vec3_t delta; float dist_sqr, sizes_sqr;; delta.x = a->origin.x - b->origin.x; delta.y = a->origin.y - b->origin.y; delta.z = a->origin.z - b->origin.z; dist_sqr = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z; sizes_sqr = a->radius + b->radius; sizes_sqr *= sizes_sqr; // this is faster than sqrt(dist_sqr) if(dist_sqr < sizes_sqr) return 1; return 0;}[/source]

Alternatively you could use axis-aligned bounding boxes but then everything collides as rectangular prisms (ala Quake1/2).

Or a combination of a vertical axis-aligned bounding 'space' (eg: a height) combined with an XY pythag collision to produce cylindrical collisions, which is better suited for bipedal type character collisions.. Or you can even implement all three options to be used with specific objects that each is better suited for, requiring that you implement detection for each possible type of collision (sphere/sphere, sphere/bbox, sphere/cylinder, bbox/bbox, bbox/cylinder, cylinder/cylinder).

### Show differencesHistory of post edits

### #1radioteeth

Posted 14 November 2012 - 10:14 AM

Spherical collision is great performance-wise, but some meshes may be hard to sum up as a mere sphere and produce silly collisions depending on the radius you apply to them. The key to maximizing performance is skipping the sqrt on your distance call, and instead squaring the other side of the comparison equation... So that

[source lang="cpp"]typedef struct{ float x, y, z;} vec3_t;typedef struct{ vec3_t origin; float radius; // etc properties...} object_t;// returns 1 if colliding, returns 0 if not..int ObjectsColliding(object_t *a, object_t *b){ vec3_t delta; float dist_sqr, sizes_sqr;; delta.x = a->origin.x - b->origin.x; delta.y = a->origin.y - b->origin.y; delta.z = a->origin.z - b->origin.z; dist_sqr = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z; sizes_sqr = a->radius + b->radius; sizes_sqr *= sizes_sqr; // this is faster than sqrt(dist_sqr) if(dist_sqr < sizes_sqr) return 1; return 0;}[/source]

Alternatively you could use axis-aligned bounding boxes but then everything collides as rectangular prisms (ala Quake1/2).

Or a combination of a vertical axis-aligned bounding 'space' (eg: a height) combined with an XY pythag collision to produce cylindrical collisions, which is better suited for bipedal type character collisions.. Or you can even implement all three options to be used with specific objects that they each is better suited for, requiring that you implement detection for each possible type of collision (sphere/sphere, sphere/bbox, sphere/cylinder, bbox/bbox, bbox/cylinder, cylinder/cylinder).

[source lang="cpp"]typedef struct{ float x, y, z;} vec3_t;typedef struct{ vec3_t origin; float radius; // etc properties...} object_t;// returns 1 if colliding, returns 0 if not..int ObjectsColliding(object_t *a, object_t *b){ vec3_t delta; float dist_sqr, sizes_sqr;; delta.x = a->origin.x - b->origin.x; delta.y = a->origin.y - b->origin.y; delta.z = a->origin.z - b->origin.z; dist_sqr = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z; sizes_sqr = a->radius + b->radius; sizes_sqr *= sizes_sqr; // this is faster than sqrt(dist_sqr) if(dist_sqr < sizes_sqr) return 1; return 0;}[/source]

Alternatively you could use axis-aligned bounding boxes but then everything collides as rectangular prisms (ala Quake1/2).

Or a combination of a vertical axis-aligned bounding 'space' (eg: a height) combined with an XY pythag collision to produce cylindrical collisions, which is better suited for bipedal type character collisions.. Or you can even implement all three options to be used with specific objects that they each is better suited for, requiring that you implement detection for each possible type of collision (sphere/sphere, sphere/bbox, sphere/cylinder, bbox/bbox, bbox/cylinder, cylinder/cylinder).