Search Unity

AddForce() causing objects to intersect for one frame

Discussion in 'Physics' started by rstern, Dec 5, 2018.

  1. rstern

    rstern

    Joined:
    Nov 26, 2015
    Posts:
    4
    I made a simple project to test different ways of moving rigid bodies and found some odd results:

    I have a cube that the player can move around with a Box Collider and a Rigid Body, set to Continuous Dynamic.
    I have a wall that has only a Box Collider on it.

    My movement code uses this in the fixed update:
    m_RigidBody.AddForce(m_m3Velocity * Time.fixedDeltaTime, ForceMode.Impulse);

    When I move the cube at high speed, it visibly penetrates half way into the wall for one frame before being pushed back. I can pause Unity just before the moment of impact and step forward frame by frame and actually catch the frame where it intersects.

    My understanding was that using AddForce() with a Continuous Dynamic rigid body should not penetrate at all: it uses swept collision doesn't it and should resolve the penetration before rendering.
    How do you prevent this bug?
     
  2. rstern

    rstern

    Joined:
    Nov 26, 2015
    Posts:
    4
    With further testing, I can reproduce this most accurately using this code in the fixed update:

    Code (CSharp):
    1. Vector3 currentVelocity = m_RigidBody.velocity;
    2. currentVelocity.x = 30;
    3. m_RigidBody.velocity = currentVelocity;
    Using the editors Pause and Step buttons, I can freeze it on the frame where a 1m cube penetrates to half its depth for one frame before being pushed back out.
     
  3. Xcrypt1991

    Xcrypt1991

    Joined:
    May 20, 2020
    Posts:
    23
    Seeing the same results. Confused by this as well.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Look up the difference between Discrete and Continuous collision detection for a Rigidbody(2D). That'll explain it.
     
  5. Xcrypt1991

    Xcrypt1991

    Joined:
    May 20, 2020
    Posts:
    23
    But OP stated he is using continuous collision detection
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    But you didn't state that you were using it too. No way of knowing that.
     
  7. Xcrypt1991

    Xcrypt1991

    Joined:
    May 20, 2020
    Posts:
    23
    Well, I meant to imply that I am using the same settings by saying that I am seeing the same results. There is a frame of intersection, and I'm using continuous collision detection against a static collider.
     
  8. Xcrypt1991

    Xcrypt1991

    Joined:
    May 20, 2020
    Posts:
    23
    With further testing, it seems like continuous collision detection prevents the rigidbody from going THROUGH the other collider, but it doesn't prevent it from going INSIDE (afterwards the physics engine depenetration pulls it back out over several frames). Weird stuff.

    So apparently "PhysX3 features an algorithm used to detect whether the expensive CCD simulation is actually needed given the current body velocity or a default discreet would do just fine. It’s activated once you enable the CCD."

    Which is extremely annoying because this means discrete collision detection is activated at lower speeds that allow objects to interpenetrate...

    @MelvMay Can you give us an option to disable this algorithm?
     
    Last edited: May 12, 2021
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I am not a 3D physics dev so unfortunately I am not the person to speak to.