美文网首页
2024-12-11【Math】Vector2 Local XY

2024-12-11【Math】Vector2 Local XY

作者: 持刀的要迟到了 | 来源:发表于2024-12-10 16:59 被阅读0次
    // Returns the local direction of V2 relative to V1
    public static Vector2 GetLocalDirection(Vector2 V1, Vector2 V2)
    {
        // Normalize V1 to ensure it represents forward direction
        Vector2 forward = V1.normalized;

        // Calculate the right-hand perpendicular vector
        Vector2 right = new Vector2(forward.y, -forward.x);

        // Compute the difference vector from V1 to V2
        Vector2 difference = V2 - V1;

        // Project the difference onto the forward and right vectors
        float localY = Vector2.Dot(difference, forward); // Projection onto forward
        float localX = Vector2.Dot(difference, right);   // Projection onto right

        // Return the local direction
        return new Vector2(localX, localY);
    }
image.png

相关文章

网友评论

      本文标题:2024-12-11【Math】Vector2 Local XY

      本文链接:https://www.haomeiwen.com/subject/xnvasjtx.html