Search code examples
unity-game-enginemathvector

How to make a Vector2 Direction to became a Vector3 Direction along Binormal or whatever it calls?


Currently i have a 2D Vector2 Direction, if i cast it to (Vector3)Direction, it would be a direction vector rotate alone V3.forward if i am not wrong right?

I've already successfully got all 3 vectors, green is the normal, red is the tangent of the curved, blue is just the binormal. What i want is making the 2D Vector2 Direction rotate along blue and start from red in world space.

I would like to know both unity function base solution and the additionally pure math base solution if anyone can help.


Solution

  • So to my understanding what you get is a Vector2 direction.

    Now you want to convert it into a Vector3 but align it with a coordinate system composed of the known up and right vectors.

    So what you want to do is basically take the right vector and rotate it around the forward vector by the given angle.

    So I would first get that angle

    var angle = Vector2.SignedAngle(Vector2.right, direction);
    

    Then get the forward axis we want to rotate around

    var forward = Vector3.Cross(right, up);
    

    Calculate the rotation to be applied

    var rotation = Quaternion.AngleAxis(angle, forward);
    

    and finally apply that rotation to the existing right vector

    var result = rotation * right;