Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pre collision velocity

Discussion in 'Physics' started by TheDelhiDuck, Aug 2, 2018.

  1. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    35
    Hi I'm trying to get the velocity of a given point on a Rigidbody prior to a collision taking place. I'm using GetPointVelocity inside OnCollisionEnter my understanding is that, in OnCollisionEnter the velocity of the body has already been updated in response to the collision. I can't use Collision.relativeVelocity because it doesn't take into account the rotational velocity of the body.
    The only hackish way I've managed to get something close is to record the velocity & angularVelocity at the end of each FixedUpdate. Then temporarily plug these value back into the Rigidbody, prior to calling GetPointVelocity, as in the following:

    Code (CSharp):
    1.         private void FixedUpdate()
    2.         {
    3.             m_LastVelocity = m_Rigidbody.velocity;
    4.             m_LastAngularVelocity = m_Rigidbody.angularVelocity;
    5.         }
    6.  
    7.         public Vector3 GetPreCollisionPointVelocity(Vector3 _point)
    8.         {
    9.             var currentVelocity = m_Rigidbody.velocity;
    10.             var currentAngularVelocity = m_Rigidbody.angularVelocity;
    11.             m_Rigidbody.velocity = m_LastVelocity;
    12.             m_Rigidbody.angularVelocity = m_LastAngularVelocity;
    13.             var pointVelocity = m_Rigidbody.GetPointVelocity(_point);
    14.             m_Rigidbody.velocity = currentVelocity;
    15.             m_Rigidbody.angularVelocity = currentAngularVelocity;
    16.             return pointVelocity;
    17.         }
    18.  
    19.         private void OnCollisionEnter(Collision _collision)
    20.         {
    21.             var pointVelocity = GetPreCollisionPointVelocity(_collision.contacts[0].point);
    22.         }



    Am I missing something, is there any other way around this?
    Thanks
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    GetPreCollisionPointVelocity may be rewritten to perform the calculation using vector math instead of modifying the rigidbody. Actually it's a pretty simple operation:

    Vpoint = Vcom + angularVelocity x r​

    Where:
    • Vpoint is the resulting instant velocity of the point
    • Vcom is the velocity of the center of mass (m_LastVelocity in your case)
    • angularVelocity is the angular velocity vector of the rigidbody (m_LastAngularVelocity in your case)
    • r is the vector from the center of mass to the point
    • x is the cross-product vector operation (Vector3.Cross)
    Source
     
    TheDelhiDuck likes this.
  3. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    35
    Thanks that worked perfectly.
     
    Edy likes this.