• Create Account

## Collisions: should I use triangles, cubes or spheres?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1RollerSimmer  Members

104
Like
0Likes
Like

Posted 12 November 2012 - 09:00 PM

I am involved in the development of an open source game featuring theme park construction and operation. For this game I want to make a quadtree map for collisions to prevent two solid objects from going in the same space. ( I have considered octrees, but they don't seem useful in relatively flat space. Quadtrees would be sufficient.) That part is mostly figured out, except for the core collision testing for each object in the tree. Triangles are very precise, and would correspond to the object's graphics almost exactly. The problem is that the algorithm is a little "involved" - both for me and the processor. For spheres I easily do a single-line function based off of distance-squared. Boxes, as long as they are axis-aligned, would be a simple case of comparing bounds. I've looked over a few texts explaining triangle intersection, and they involve multiple steps and a lot of linear algebra. I generally understand the math and geometry behind it, but it seems too complex for my needs.

I would like a certain degree of precision (perhaps within a third of a meter or so), but with small radii and more points in the "cloud", I could make collisions somewhat accurate with variable-radius spheres. Axis-aligned boxes would require more comparisons, but not as many as triangles would. Plus I would still need a lot of "grains" to make the map accurate. Should I go for simple computations and high memory usage, or multiple step matrix and vector processing with lower memory usage? I would like graphical collision to be close to physical collision, but not exact. The polygon counts for large roller coaster track go into the 100K+ range. The number of objects on a map will be huge. Think along the lines of RollerCoaster Tycoon or SimCity. This game aspires to something of that scale, if not bigger.

I am using Irrlicht game library for this game. I know there are some collision checks in that engine, but they seem tied to graphics scenes. I may want to make the collision geometry for certain objects (like ride track) simpler than their graphical mesh geometry. I figured a custom tree would be needed for this task. With STL dynamic structures, that should be a piece of cake. But if someone can point me to a better way, I would be glad. I don't like reinventing the wheel if I don't need to.

Any general advice on collisions in 3D would be appreciated.

### #2kd7tck  Members

735
Like
0Likes
Like

Posted 12 November 2012 - 09:55 PM

Personally I would go with whatever gave the tightest fit to the object. You don't want to use a sphere bounding radius when the object is cube shaped. Just have a couple predefined bouding shapes and pick or choose for each object. Your could use bounding meshes for complex objects, but this would be memory and cpu intensive.

### #3RollerSimmer  Members

104
Like
0Likes
Like

Posted 12 November 2012 - 10:30 PM

I was thinking about that. Each occupancy in the tree could have a tag giving it's shape type. I think vertical cylinders may be easy to include as well. Any shape with simple bound checking would work.

### #4Yrjö P.  Members

1416
Like
0Likes
Like

Posted 13 November 2012 - 04:42 AM

Frankly, I don't understand why you'd need accurate clash checking for a theme park sim. What are the objects whose clashes you need to prevent?

### #5larspensjo  Members

1561
Like
0Likes
Like

Posted 13 November 2012 - 05:30 AM

What are your performance requirements? Are you going to have LOTS of objects, or just a few? Do you need to test for collision frequently, in real-time?

That is, how many collision evaluations do you expect to do per second?

Are you sure the player will care about the difference between circle tests and triangle tests?

One approximation that can be uses is to represent every object with more than once circle. Maybe 3-4 of them if your objects are generally formed like triangles.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #6MrDaaark  Members

3556
Like
0Likes
Like

Posted 13 November 2012 - 06:13 AM

You can have many bounding volumes and do different tests.

As someone pointed out, a sphere isn't a good fit with most objects. However, a Sphere can contain any object, and it's a cheap test to perform. If two object's bounding spheres do not intersect, then there is no need to proceed onto more expensive tests, because they can't possibly be colliding. If they do intersect, then you can loop through the objects hierarchy of other bounding volumes to get a more specific result.

eg:
You can have a character that has a bounding volume for every limb. And then a bounding sphere that surrounds the whole character. Then you can have a projectile, and test the projectile against that sphere.

-If it doesn't the sphere, it can't possibly intersect the other volumes, because they are in the sphere. So you save yourself the trouble of having to transform all the other volumes and do those extra tests.

-If it does intersect, it may still be a miss, but at least now you know that the more specific tests are worth running. Transform those volumes along with the character's current animation frame, and then do the tests.

The big spheres are a cheap first pass at collision. In a big scene it lets you quickly skip hundreds or thousands (or even more) of needless tests, and then focus only a small list of possible collisions.

### #7deftware  Prime Members

1670
Like
0Likes
Like

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 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).

Edited by radioteeth, 14 November 2012 - 10:15 AM.

### #8MaxDZ8  Members

4976
Like
0Likes
Like

Posted 15 November 2012 - 03:08 AM

There's something definetely wrong with this thread, as with most threads involving physics and CD.
No matter how fast a narrowphase is (in the case of hierarchical structures), it is not enough.
So, I'm sure I'll get bashed again for writing this, but everyone considering writing a CD library should
• Reconsider their decision
• After consideration, they might want to look at Bullet for good practices (as I consider it an excellent library given it has been used in movie industry as well).
That said,

• I would like graphical collision to be close to physical collision, but not exact. The polygon counts for large roller coaster track go into the 100K+ range.
• The number of objects on a map will be huge.
• With STL dynamic structures, that should be a piece of cake.
• But if someone can point me to a better way...

• As you note a few lines later, you totally must use special collision geometry. For models counting 100k+ polys, I'd be rather surprised if the corresponding collision geometry would take more than 5K. You might also consider building the geometry as an assembly of rigid bodies.
• Barely relevant if you use a broadphase. In most of my tests, doubling the amount of collidable objects incurs in a 5% performance penalty. Those are only my tests, on my dataset, yours could be different and likely will... but I think it's good scaling.
• Unfortunately not, as they lack the proper management for alignment requirements and such. Ok, I guess you could write your own std::allocator to make them work. Bullet does not use them, possibly because of portability issues. I don't know.
• Yes, consider using a CD library. You also get dynamics "for free" (in effort terms). Think about it.

Previously "Krohm"

### #9RollerSimmer  Members

104
Like
0Likes
Like

Posted 19 November 2012 - 07:09 PM

I'm going to try Bullet. I didn't really want a full physics simulator, just something for space occupancy to prevent overlapping items. I'll see if Bullet has something to that effect.

### #10RollerSimmer  Members

104
Like
0Likes
Like

Posted 19 November 2012 - 07:13 PM

Frankly, I don't understand why you'd need accurate clash checking for a theme park sim. What are the objects whose clashes you need to prevent?

Maybe "collision" is the wrong word. I meant overlap avoidance of objects. Although there may be mini games involving free bodies that need physics and collisions. All I need is a simple boolean function that tells me if there is something where I want to place an object. Location of contact and force are irrelevant.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.