Search Unity

Trying to calculate this collision vector (without physics system)

Discussion in 'Physics' started by petey, Jul 6, 2019.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hi all!

    I'm trying to figure out how to calculate the blue vector as if a collision has occurred and the colliders have zero bounce. I have the incoming vector and surface normal. I thought maybe to lerp the incoming vector and reflection vector but I feel the length might not be correct.
    Screen Shot 2019-07-05 at 4.46.19 pm.png Can someone help me with that?

    Thanks,
    Pete
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Look into vector projection.
    In case of 3D, projection on surface, but can be also on other vector.
     
  3. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Reflect the inDirection by the normal, then average the indirection and the result of the reflection. I think that's right.

    Code (csharp):
    1.  
    2. Vector3 reflection = Vector3.Reflect( inDirection, normal );
    3. Vector3 alongTheWall = (inDirection + reflection) * 0.5f;
    4.  
    the reflection vector in this code is equal to 'Result' in your diagram.

    If you want it to maintain the same speed then you may need to do this:

    Code (csharp):
    1.  
    2. Vector3 reflection = Vector3.Reflect( inDirection, normal );
    3. Vector3 alongTheWall = (inDirection + reflection) * 0.5f;
    4.  
    5. Vector3 maintainSpeedAlongWall = alongTheWall.normalized * inDirection.magnitude;
    6.  
    Maintaining the speed may get a funny result though as a ball hitting the wall at near or at the same angle as the normal vector will do odd things.
     
    Last edited: Jul 6, 2019
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    Assuming inNormal points upwards in the picture:
    Code (CSharp):
    1. Vector3 blueVector = inDirection + inNormal * Mathf.Dot(inNormal, inDirection);
     
    Antony-Blackett likes this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    If you want blueVector to be the same magnitude as inDirection:
    Code (CSharp):
    1. Vector3 projection = inDirection + inNormal * Mathf.Dot(inNormal, inDirection);
    2. Vector3 tangent = projection.normalized;
    3. Vector3 blueVector = tangent * inDirection.magnitude;
     
    Last edited: Jul 6, 2019
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    This also assumes inDirection is a unit vector. Given it's called 'direction' that's a fair assumption.

    Edit: Disregard.
     
    Last edited: Jul 6, 2019
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    No, it assumes inDirection has any magnitude and inNormal is an unit vector.
     
    Antony-Blackett likes this.
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Oh, my mistake. Thanks for the super math knowledge.

    (I got my bedmas wrong when reading).
     
    Edy likes this.
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    No super math, just vectors (I use them too much xD)
     
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Wow, thanks for all the help!
     
    Edy likes this.