2
\$\begingroup\$

I am having what is probably a newbie problem with Unity, but for the life of me, I can't find a decent solution to it. In short: my rigid bodies collide, but sometimes, they interpenetrate and stay stuck.

I am looking for a solution that would either 1- stop the interpenetration from even happening 2- or at least eject the bodies to 'unstuck' the collision.

By making the physics fixed step 0.01 instead of the default 0.02, I can avoid some of the interpenetration. However, that hardly sound like the proper way to fix this problem.

Here is a screen shot of the bodies colliding. The car on the right is shown completely and you can notice that the ones behind have penetrated the others to some extent.

alt text

To give more information that could be relevant:

  • There is no gravity

  • All the objects have rigid bodies

  • Their sizes are 3.5 x 6.8 and they are moving at 8 to 50 m/s

  • Each object has a conpounded collider made up of 3 box colliders

  • If I change the fixed step to 0.01 there is a lot less more interpenetration (as I mentioned). Other physics settings do not seem to have any effect

  • The object is moved by setting its velocity in the FixedUpdate function with rigidbody.velocity = XYZ until there is a collision, then there is no more processing done in FixedUpdate at all. I detect the collision with OnCollisionEnter and set a flag.

  • I haven't set any collision material, but I can't see that being a factor?

Solved

Thanks to everyone who helped me with the issue.

There were a few factors in play. First, there was the scale. What I did to figure this one is create an empty scene and put 2 cubes in there. I varied the size and position of the cubes (from 68x35 to 0.65x0.35 for size and the position by factors or 10 as well) and attached a script to move them, with variation in speed by factors or 10 as well. So all else being equal (same size on the screen and time to cross it), the size of the objects elected a different response to collision: big object got interpenetration, small ones none. I am not familiar enough with Unity to explain the cause of that, but it is reproductible.

A possible second factor was the shape of my colliders and how it may be possible that they screwed up the ejecting part. The colliders I had were not overlapping, so I simplified them to 2 boxes in the shape of a cross.

Finally I was setting the position of the objects manually in FixedUpdate and that caused some problems with interpenetration at lower scale (at higher scale, it didn't appear to matter). I changed that to work with the body velocity and it helped.

\$\endgroup\$
4
  • \$\begingroup\$ Does this mostly, or always occur with high-speed collisions? Your image shows some objects interpenetrated from opposite sides, which could be squishing the object in the middle. Does this occur with 2 objects, or does it require more than 2? \$\endgroup\$
    – Nic Foster
    Commented Feb 27, 2012 at 15:46
  • \$\begingroup\$ It occurs between 2 objects, but once interpenetrated, they won't come unstuck, so if a third one happen to smash into them, it can become stuck as well. For the speed, I mention it above, but to clarify, it is so that the car crosses the screen in 2-3 seconds. So it is relatively high, but nothing shocking. We are not talking of a bullet here. \$\endgroup\$
    – ADB
    Commented Feb 27, 2012 at 15:55
  • \$\begingroup\$ The fact that the interpenetrations become more rare, or less deep, when the step times are reduced suggest that it has to do somewhat with the speed of the colliding objects. Unfortunately I don't have experience with Unity's physics engine, but many physics engines will allow you to increase the priority of collisions with certain objects. You may also be able to change the way collisions are handled through some global setting that calculates collisions with a TOI (time of impact). Using a TOI should prevent this kind of issue, but is a more expensive way of handling collision detection. \$\endgroup\$
    – Nic Foster
    Commented Feb 27, 2012 at 16:35
  • 1
    \$\begingroup\$ You should put your solution as an answer and mark it as accepted. \$\endgroup\$
    – Tetrad
    Commented Feb 28, 2012 at 21:22

1 Answer 1

2
\$\begingroup\$

Any general rigid-body physics engine should be able to eject interpenetrating bodies given good collision data. Try changing the collision shape of your cars; it sounds like they might be getting stuck due to inward-facing collision points. For example, if your 3 boxes are arranged as front-middle-back then you could have a problem like this:

enter image description here

Note that the rear box of the front car (whichever one that is) is behind the front box of the rear car, so the contact points generated will be facing the wrong way. On the other hand, if you make them out of two intersecting boxes, then there are no such inward faces:

enter image description here

(Similar problems can happen when trying to collide against meshes, since meshes specify surface rather than volume.)

You should also consider whether you really need more than one box to get the gameplay effects you're looking for.

This is all largely speculation since you haven't specified what your collision shape is in detail.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .