why cylinder collider doesn't exist ?!

hi folks,
Instead of using a mesh collider for cylindrical object like Barrels … why there’s no cylindre collider optimized by unity jst like the box spherical one :s

Mathematically (with respect to collision detection at least), cylinders are much harder to work with than triangles, rectangles, spheres, capsules, or boxes. Algorithms involving cylinders are generally less optimal (considerably, in some cases) than similar algorithms involving other simple primitives, so it makes sense that cylinders would not be supported as a basic primitive.

I don’t know how PhysX handles intersection testing, but in general there are a few different options. Shapes such as spheres and capsules can generally be dealt with pretty easily. They can be viewed as the Minkowski sum of a sphere and a simple primitive that serves as the medial structure for the shape; for a sphere the medial structure is a point, and for a capsule it’s a segment. Many of the queries that commonly need to be performed can then be reduced to simple queries against those medial structures, which is generally fairly efficient.

Another commonly used algorithm is the SAT (separating axis test, or theorem is you prefer). The SAT is a fairly optimal solution for boxes in particular.

Cylinders don’t work very well (or at all, depending) with either of these methods. There are general solutions that treat all convex objects uniformly, such as the GJK algorithm, and (AFAIK, at least) some physics engines do use such algorithms. But, these algorithms are in general more complex, less stable, and harder to implement than the alternatives (IMX at least), and in the case of curved objects such as a cylinder may reduce to an iterative solution for the narrow phase, which I imagine you’d want to avoid if possible in a real-time physics simulation.

So, long story short, cylinders just aren’t generally an optimal choice for real-time intersection testing, so usually it’s better to instead use a reasonable approximation such as a capsule or regular polygonal prism.

[Edit: Just to cover my bases, I don’t actually know why cylinders aren’t included as a basic primitive in Unity; I’m just speculating. But, I think it’s probably a reasonable bet that the fact that cylinders are somewhat harder to work with than other primitives might have something to do with it.]

7 Likes

@Jesse Anders It is the pleasure to have such well-informed unity user ;-). While i can only speculate myself (i’m not physX developer), this is the real story. Only one small correction - GJK is actually quite stable, but anyway cylinder is very special case for it - so everyone basically skip that :wink:

Well, unfortunately I’m not always well informed - only sometimes :slight_smile:

I may have overstated that aspect of things. From my own efforts with GJK (which were a while ago) I seem to remember some thresholds and tolerances being involved, perhaps more so than with simple tests such as, say, sphere vs. capsule. But, as I said, it’s been a while, and I may be misremembering.

Interesting… im facing the same problem. Im searching a better way to simulate a tire than using the raycast method, cause with raycast thers only one contact point. in unity 3 finally has been added the sphere cast, thats a good step forward, but its not perfect (doesnt finds all contacts with the ground). The ideal solution seems to be using a convex cast like in this very well done newton example

http://newtondynamics.com/forum/viewtopic.php?f=14&t=5999

there is also a donwloadable demo where you can see the difference between
raycast (cast 3 rays horizontally) (picture 1)
spherecast (circular raycast - casts 3 rays horizontally and 8 circular around the tire) (picture 2)
convex cast (casts cylinder the size of the tire towards under the car, finds all contacts with the ground) (picture 3)

Unfortunately unity doesnt support convex cast, so im evaluating using a cylinder with a mesh collider, but after reading this im not sure anymore that its a good solution…

A bullet developer adviced about using a cylinder collision shape:
http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=&f=&t=1817

So for you wich is the best solution to this problem using unity? casting several rays around the tire like in the newton example can be a good solution or is eavy to process?



thx guys for these informations

but i still think that cylinder collider is a standar geometry that should be added to the standard colliders …

Cylinder is base on the radius system that the sphere has and the linear projection on the Y -axies of that cylinder that already has the Box too …

So i think that it really should be added in unities colliders .

3 Likes

Well, did you read the earlier posts? There was considerable detail provided as to why it’s fairly common to prefer other shapes over cylinders for collision detection.

The best way to understand what’s really involved would probably be to write some collision detection code yourself; without that experience, it can be difficult to make reasonable determinations about what ‘should’ be supported in a collision detection or physics engine.

The fact that both a cylinder and a sphere have a radius is insignificant (for the most part). The important characteristic of spheres and capsules is not that they have a radius, but that their surface is formed from all points that are exactly radius units from the medial structure. It is this characteristic that makes intersection tests involving spheres and capsules fairly straightforward, and it is this characteristic that a cylinder lacks.

Here’s one way you might be able to convince yourself. Implement a boolean sphere-box test (by ‘boolean’ I mean that the test only determines whether an intersection occurred - no other intersection information is computed). Then, implement a boolean cylinder-box test. That should give you some insight into why other shapes might be preferred over cylinders.

Again, I don’t know for sure why cylinders aren’t supported in Unity; maybe other physics engines support them (I’m not sure off the top of my head), and maybe Unity will support them in the future.

But, it’s important to understand that just because something makes sense from an intuitive point or view or because it would be convenient for the user doesn’t mean that it would be easy to implement or practical to support. In our intuitive day-to-day world, a cylinder is indeed a basic shape and we might not think of it as being qualitatively different from a sphere or box. Mathematically though, a cylinder is very different from either of those shapes, and as far as collision detection in a computer simulation goes, it’s the mathematics that matters, not the intuition.

Does that help clear things up at all?

ok and what about convex cast like in the example i put in my post? quote from bullet forum:

that “new shape that approximates it better” is commonly capsule though as it has the benefits of the sphere without the problem that the single point of contact normally creates

as for faking a cylinder collider: a combined collider basing on subs using capsule + 2 wheel colliders for top / bottom rings might work out too, never tried that

Unity uses PhysX, so if there’s no cylinder collider in PhysX (which there isn’t), then there’s no cylinder collider in Unity. In many cases you can use a capsule collider + box collider (works great for barrels as long as they’re not too short).

–Eric

1 Like

hi dreamora,
thank you for your reply.
i dont think that the shape they were talking in the bullet forum is a capsule, cause the height of a capsule cant be less than the diameter (in that case the capsule becomes a sphere)

hi Eric5h5,
do you know if the convex cast are supported by physx? if you have a look at the example i put in my previous post, you can see that its the best way if you want to find all contact points of a wheel.
if not, which is for you the best way to find all contact points of a wheel? i tried raycast (only one contact point) and sphere cast (better but doesnt find all contact points, probably cause it casts several circular rays around the tire, but not enough in order to find all contact points)

that indeed is true.
But that does not mean you can’t use it, it depends on the explicit requirements. and in case of a barrel which has totally different requirements to a wheel there is even less speaking against it

but in unity you wouldn’t use primitive colliders anyway as PhysX has a special collider for wheel type usage, which in Unity is exposed as Wheel Collider.

and yes convex colliders can be used for moving objects. use mesh collider and mark them as convex
They would not “cast” in the sense of expanding outwards over time but they would collide fine like primitive colliders.

Hi dreamora,
yes i tried to use convex collider as trigger but it doesnt seem to work.
Here what i tried:
I have a terrain with mesh colliders. I create a cylinder gameobject and i add a mesh collider to it.
I check istrigger and convex options, and i add a kinematic rigidbody.

with this configuration no trigger event are fired, instead the same situation with a “basic” collider (cube, sphere, capsule) it works.

So Its not possible to use a mesh collider as a trigger?

actually , i think that two mesh colliders don’t collide between them ::s
that’s why creating a terrain using unity3d terrain tool is better …

mesh colliders can collide with each other (or technically move, because those who don’t do never move internally → not register collisions. they teleport), but the moving ones must be convex (and marked as such).

stationary ones don’t need that.

my tests say another thing: two mesh collider dont fire trigger event. a basic collider (sphere, cube etc) and a mesh collider do fire trigger event.

did you ever solve this Newlife ?

@Jesse_Anders Umm, wouldn’t a collision check for a cylinder collider be just a combination of the math used for capsules and boxes? If anything it should be simpler than boxes.

I mean, you’d perform a capsule collision check at first, but then perform different handling for the caps.

If we just consider checking if a single point is within the volume of a capsule, it’d just be a matter of finding the closest point on a line segment to the point in question. This can be done by imagining the line segment is, rather, an infinite line, but then clamping the point within the start and end of the line segment. Then, with the closest point acquired, perform a distance check.

In the case of a cylinder, if the point needs to be clamped then the point is not within the volume of the cylinder. The distance check can be omitted altogether.