What is the cost of using of capsule , Box and Sphere collision performance wise?

What is the cost of using of capsule , Box and Sphere collision performance wise?

Technically, sphere collider will be the cheapest one, followed by box and capsule.

I don’t think this is correct, I think it is sphere > capsule > box

Okay, how about this for a follow up: what would be more performant, one box or two capsules?

Also, what are you basing that on; are there any references anywhere on what makes boxes more expensive?

I think it’s about mathematics : a sphere is easy to define with maths and the engine have less trouble figuring it out. A capsule is a little bit more complex, then a box even more…

I’d also like to know the answer to this question, what the costier between two capsules and a box?

You can estimate the expense by how many numbers it takes to define each shape (not counting the transform):

A sphere takes one number: Radius.

A capsule takes two numbers: Radius and half-height.

A box takes three numbers: length, width, height.

So two capsules will need four numbers, and a box will still need three numbers, so a box will be cheaper.

1 Like

Cheaper for memory, not for pefromance.

Performance of sphere requires computing with PI which sure, its quick, but possibly mote expensive depending on pi digits.

As far as collision cost/sweep the only answer is that you have to use whatever works for your project and bench test along the way.
You can get some obvious cost reduction by reducing the number of primitives involved in a specific object, or outright changing the primitive.
But the only way to know is to check the specific situation.

Sphere collision actually doesn’t require calculating Pi (which doesn’t get ‘calculated’ in Unreal, anyway. It’s defined as 3.1415926535897932f in UnrealMathUtility. Calculating an irrational number at runtime would be insane).

A sphere collider is a visual abstraction of a straight linear distance calculation (y’know, Pythagorean Theorem. sqrt(a^2+b^2+c^2)) for ease of use . I haven’t looked too closely at it but it’s also probable that Radius is getting squared invisibly to avoid the sqrt operation. If distance between Vector A (the root of the sphere collision) and Vector B (the closest point on the colliding object’s collider) is less than or equal to the Radius, a collision has occurred.

Capsules and boxes become more expensive because the orientation of the collider relative to the collidee has to be taken into account. Capsule is cheaper because it’s still mostly a linear distance check. Box is more expensive because the length of the line varies a lot more (i.e. if the colliding object is coming in at a corner instead of in a cardinal direction relative to the box).

Maybe you are insane for reading plain English and thinking that’s what was written?
A fact practically proven by the rest of that diatribe…

Look Just stop while you are way behind already. I’ll try and explain it in simpler terms for you

It’s pretty simple: More Math = MORE COST.

radius * 2 is math. radius * pi is math - and that’s not the formula for a sphere - that’s a 2d circle.
NOT similarly:
length * height * depth - is math - and gives you a volume.

Further, the proper formula of a sphere’s volume being
4.0/3.0 - a division - which does produce an irrational number

  • pi * r * r * r

This alone makes it evident that if checking VOLUMES, then a sphere isn’t the fastest way to go…

The assertion that Boxes and Capsule have orientation which affects things is also insane;
What you probably meant is that a sphere being spherical you have no need for an orientation WITHIN the calculations, leading to no branching - see the end.

Assume this much:

  • ALL COLLISION - in anything that checks it - is done with math.
  • ALL COLLISION - in anything that uses any sort of computing is done with MULTIPLICATION instead of POWER or Division - because the operation is faster.
  • ALL COLLISION - is affected by the number of digits and precision required by the engine (take this as Pi with 3 digits, vs Pi with 15, and the use of simple float vs double).
  • ALL COLLISION - is either done mathematically by you - say like initiating a line or box or sphere trace (in engine is part of kismet) OR by a physics engine.

Given this, and because of this.

With CHAOS - you really need to benchmark; The performance has been mangled by well over 60% on the same scenarios between PhysX and Chaos.

This means that EVERY collider is more expensive - probably because the built in math is about as good as the math and ideas expressed above. Confused to say the least.

This one part is also just sheer misinformed assumption.

Capsule is cheaper because when you do the math you can simplify it A LOT - like not having to do sqrt, or being able to have an early prediction that’s accurate based on center-point and radius without having to necessarily perform the rest of the calculation.
You almost had the right idea - almost.
You can’t rely on the assumption that a point at a distance less than a radius from the center point is a definite hit - and it also wouldn’t provide you with any meaningful information as to the impact point - which is normally required by all systems as their output.
It’s an early check at best, which makes your follow up check much more meaningful and targeted.

Box is not “more expensive because the length varies” :rofl:
No.
It’s more expensive because you do not have the nicety of a prediction
and more importantly
It’s more expensive because no matter WHAT engine you use the calculation for it is always going to lead to some branching - IF / Else - that sort of thing - on the various axis (x, then y, then z - order being irrelevant, but one having to run before the next).
Having to wait for one true/false to return is more expensive than the process a sphere calculation would take - where there COULD be no x/y/y branching involved.

This really has no bearing on anything. You are misconstruing how things work and believing that the orientation of an object has any bearing what so ever on how the math is performed…
IT DOES NOT.
You still have to check each cardinality for a yes or no answer to the “is this point within this range” question. Regardless of the rotation of said object.

The Reason BOX can be more expensive than Capsule Is again the fact that one uses a circle and that you can have an early guess as to something hitting within that cylindrical shape.
An early check which will some-times actually result in a false positive - at the drooping end of the capsule.

That’s basically why
Sphere > Capsule > Box

BUT, in practice, this is NOT true for physics engines.
Particularly Chaos.
EI: Go bench different scenarios against Mixed scenarios.
On different days you may even get nonsensical results - that’s probably why they called it Chaos I guess !? :stuck_out_tongue:

On the other hand of the spectrum - physX benching gives you about a .1ms difference between 1000 sphere and 1000 box.
Making the whole question essentially irrelevant if not for an academical knowledge or the ability to run something on a potato…

4 Likes

Sphere collision doesn’t check volume, or area, or surface area. Pi is irrelevant.

The way to check if a point is at the surface or within the volume of a sphere (thus COLLIDING WITH IT) is to check if the distance between the point you’re checking and the center point of the sphere is less than the radius of the sphere. You don’t need to know what the volume or surface area of the sphere is, so you never need to multiply by pi, or square or cube the radius. It’s a simple vector length check. (non-Unreal example: the Jolt physics library doesn’t use pi at all in its sphere collider class)

I mentioned squaring the radius because using SquaredLength is computationally cheaper than Length (because there’s no sqrt operation), and squaring the radius (or radius * radius if you’re going to be a ■■■■ about it) is basically free.

The impact point is easily derived from the direction vector which you get from knowing the location of the colliding point. And the impact normal is the normalized direction vector.

For physics sim instead of pure collision you need volume because that’s necessary to calculate the mass of the sphere, but neither Naminuz or Goon Amusements’ question about whether a box or two capsules was cheaper, nor the original post about which collision type is cheapest, were about simulation, they were about collision. And if they were about simulation, the answer would still be that a box is cheaper than two capsules.

Look, stop posting.
You are still as confused as it gets and obviously either unable to understand simple English or unwalling to.

This isn’t “your opinion” either, its just misinformation at this point.
Verging (not quite) on trolling too.

Actually, I was wrong. Two capsules are probably cheaper than a box.

Sphere is computationally cheaper than capsule which is computationally cheaper than box.

If you want to get real deep into the nitty gritty of this, you can take a look at Chapter 4 of Christer Ericson’s ‘Real-Time Collision Detection’. (For clarification: Unreal’s box colliders are Oriented Bounding Boxes, not Axis Aligned Bounding Boxes. And Capsules are the ‘Sphere Swept Volumes’ in Chapter 4.5). There’s also a bunch of stuff in that chapter about the cost of different methods of computing the volumes, but that’s largely irrelevant because neither Unreal nor any other game engine that I’m aware of recompute the volumes in real-time (except at the highest levels of the collision hierarchy, but unless you’re completely re-writing the collision system that’s not something you need to worry about). The most relevant parts are where he talks about the intersection tests for the volumes. AABBs and Spheres have the simplest intersection tests (simple coordinate check for AABB, radius check for Sphere), Capsules have the next simplest (find the closest point on the long axis of the capsule followed by a radius check centered on that point), OBBs have the most complex (a pretty massive 15 step check to get a positive intersection result).

Literally NONE of that matters.

Chaos doesn’t play ball with it.
And Chaos is in charge of getting you hit results.

Maybe if the people working on chaos had read this or just about anything on the subject, who knows, performance wouldn’t be so ■■■■ poor…

But it is. So the academic portion is almost entierly irrelevant to the practical result(s).

Still, now that you somewhat understand better, maybe you can see how theoretically checking 3 capsules for collision could net around the same time the brancing process eforces on each axis of a box.

Nvidia did things smarter.
Including splitting off the calcs to parallel processes via CUDA

This would often result in a box costing about the same as a capsule - because the 3 branches are checked simultaniously.

Again, for the nth time.
You are never going to know the real cost of anything until you perform benchamerks on whatever it is you need to get the baseline performance of.
(Particulalry in unreal engine, where performance is literally ignored by the engine developers at all times.)

1 Like