Determining the Angle of Spin
The CalculateSpinAngle
function takes two parameters: playerPosition
and anchorPosition
, both of type Vector2
. These parameters represent the positions of the player and the anchor point respectively. The function aims to calculate the spin angle, which indicates the rotational orientation of the player relative to the anchor.
private float CalculateSpinAngle(Vector2 playerPosition, Vector2 anchorPosition) {
Vector2 anchorToPlayer = playerPosition - anchorPosition;
return Mathf.Atan2(anchorToPlayer.y, anchorToPlayer.x) - Mathf.PI / 2;
}
Let’s break down the steps involved:
- Anchor to Player Vector: The first step is to compute the vector from the anchor position to the player’s position. This is done by subtracting the anchor position vector from the player position vector. The resulting vector, known as
anchorToPlayer
, represents the direction from the anchor point towards the player.
- Atan2 Calculation: To calculate the spin angle, the Mathf.Atan2 function is used. This function returns the angle between the positive x-axis and the vector specified by its y and x components. In this case, we pass the y and x components of the anchorToPlayer vector to Mathf.Atan2.
- Offset Adjustment:
To align the spin angle correctly with the desired orientation, an offset is applied to the calculated angle. In this case, the offset is
Mathf.PI / 2
, which corresponds to a90-degree
rotation. Subtracting this offset ensures that the spin angle is correctly adjusted.
- final calculated angle: The angle aligned within Y-axis is returned.
Understanding the swing direction calculation
The CalculateSwingDirection
function accepts three parameters: playerPosition
, anchorPosition
, and playerVelocity
, all of type Vector2
. This function’s primary purpose is to calculate the swing direction, which indicates whether the player should swing clockwise
, counterclockwise
, or not swing at all.
private SpinDirection CalculateSwingDirection(Vector2 playerPosition, Vector2 anchorPosition, Vector2 playerVelocity) {
Vector2 anchorToPlayer = playerPosition - anchorPosition;
Vector2 perpendicularPlayerVelocity = new Vector2(-playerVelocity.y, playerVelocity.x);
float crossProduct = Vector2.Dot(anchorToPlayer, perpendicularPlayerVelocity);
return crossProduct switch
{
> 0 => SpinDirection.Clockwise,
< 0 => SpinDirection.CounterClockwise,
_ => SpinDirection.None
};
}
- Perpendicular Player Velocity Vector:
A vector perpendicular to the player’s velocity vector is created. To achieve this, the Vector2 constructor is used,
swapping the x and y components of the player velocity vector and negating the new y component
. This vector is perpendicular to the player’s velocity and helps us determine if they are moving towards or away from the anchor.
Let’s consider a vector X = (x, y)
. To obtain a vector perpendicular to X
, we need to rotate it counterclockwise by 90 degrees
.
The rotation of a vector by an angle θ counterclockwise can be represented by a 2x2 rotation matrix R(θ):
R(θ) = | cos(θ) -sin(θ) |
| sin(θ) cos(θ) |
For a 90-degree counterclockwise rotation, we have θ = π/2. Substituting this value into the rotation matrix:
R(π/2) = | cos(π/2) -sin(π/2) | = | 0 -1 |
| sin(π/2) cos(π/2) | | 1 0 |
To rotate the vector X = (x, y) by 90 degrees counterclockwise, we can multiply it by the rotation matrix R(π/2):
R(π/2) * X = | 0 -1 | * | x | = | -y |
| 1 0 | | y | | x |
The result is the vector (-y, x), which is perpendicular to the original vector X = (x, y).
- Cross Product Calculation:
The
dot product
ofanchorToPlayer
andperpendicularPlayerVelocity
vectors is calculated usingVector2.Dot
. This operation yields a value representing the cosine of the angle between the two vectors. In this context, the resulting value is calledcrossProduct
.
Final result:
References