petey
July 6, 2019, 1:00am
1
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.
Can someone help me with that?
Thanks,
Pete
Look into vector projection.
In case of 3D, projection on surface, but can be also on other vector.
Reflect the inDirection by the normal, then average the indirection and the result of the reflection. I think that’s right.
Vector3 reflection = Vector3.Reflect( inDirection, normal );
Vector3 alongTheWall = (inDirection + reflection) * 0.5f;
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:
Vector3 reflection = Vector3.Reflect( inDirection, normal );
Vector3 alongTheWall = (inDirection + reflection) * 0.5f;
Vector3 maintainSpeedAlongWall = alongTheWall.normalized * inDirection.magnitude;
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.
Edy
July 6, 2019, 2:05am
4
Assuming inNormal points upwards in the picture:
Vector3 blueVector = inDirection + inNormal * Mathf.Dot(inNormal, inDirection);
1 Like
Edy
July 6, 2019, 2:09am
5
If you want blueVector to be the same magnitude as inDirection:
Vector3 projection = inDirection + inNormal * Mathf.Dot(inNormal, inDirection);
Vector3 tangent = projection.normalized;
Vector3 blueVector = tangent * inDirection.magnitude;
Edy
July 6, 2019, 2:13am
7
No, it assumes inDirection has any magnitude and inNormal is an unit vector.
1 Like
Oh, my mistake. Thanks for the super math knowledge.
(I got my bedmas wrong when reading).
1 Like
Edy
July 6, 2019, 2:26am
9
No super math, just vectors (I use them too much xD)
petey
July 6, 2019, 2:57am
10
Wow, thanks for all the help!
1 Like