- Home /
Rigidbody sticks to walls. Or, how the h*ll do you force elastic collisions?
There's at least a dozen posts on this topic, none of which seem to answer anything conclusively. I have four cubes to which I've attached rigidbody2d and box collider 2d components. I have a single sphere, to which I've assigned a rigidbody2d and a circle collider.
If the sphere is moving too slowly when it approaches the wall, any motion perpendicular to the surface is removed and all that remains is motion parallel to the surface.
I really only want to simulate a circle bounding around in a square. Surely this is possible with Unity?
Additional info: Friction and drag are abolished on the all objects in the scene via physics2d materials. There is no drag.
Answer by eriophora · Aug 16, 2017 at 08:53 PM
It's actually a configurable setting.
In the 2D physics setting, Velocity Threshold
dictates the cutoff x where collisions with relative velocity lower than x will be treated as inelastic.
Ultimately, you can't force Unity to treat all collisions as elastic, but by setting Velocity Threshold
sufficiently low you can cause nearly all collisions to be treated elastically. If you don't have complex colliders and relatively few colliding bodies, you won't incur a substantial performance hit.
Answer by TheSOULDev · Aug 16, 2017 at 06:03 PM
This is not as trivial as you think it is. You will need to create a function that calculates the new vector on collision. There are plenty of formulae for perfect elastic collisions on the internet (it's classic physics). Using those you can determine a new vector based on your entry vector. Don't think there is anything already implemented in Unity that does this innately, so you'll have to implement it yourself. For a perfectly elastic collision, you will also have to set the mass of the squares to be far larger than the mass of the object you want to bounce off the squares.
Thanks! As it turns out, Unity treats all sufficiently fast collisions as elastic, and the cutoff is configurable. No doubt this is for efficiency's sake. I just had to set the parameter to a sufficiently small number (see my answer).
Also, a few notes: - There are indeed formulas available, but I suspect that Unity's internal implementation of elastic collisions is more efficient. As in my original post, Unity treats sufficiently fast collisions as elastic so Unity can at least represent that type of interaction. - Since the squares (i.e., the walls) are static, I think their mass is treated as infinitely large already.
Finally, you could argue that implementing fully elastic collisions is more trivial than the system that Unity already has implemented: transitioning from elastic to inelastic collisions as a function of the relative velocity of the objects colliding. While I get why they did it, and I'm glad they've gone ahead and introduced these optimizations "under-the-hood," it'd be nice if it were somehow made more explicit. $$anonymous$$akes me wonder how many other optimizations are lying in wait for me to trip over them.
Your answer
Follow this Question
Related Questions
How do I solve the Box Collider (3D) Edge / Corner Collisions problem? 5 Answers
Rigidbody is behaving abnormally when transformed manually 0 Answers
Character Controller meets Rigidbody 1 Answer
How to get collisions on Character controller? 1 Answer
Rigidbody.MovePosition/MoveRotation Hits far away colliders 2 Answers