mention rotate extension
continuous-integration/drone/push Build is passing Details

main
Evan Hemsley 2020-07-15 18:32:06 -07:00
parent 5b4c9d10c1
commit 913839edde
1 changed files with 13 additions and 2 deletions

View File

@ -66,6 +66,17 @@ While we're at it, let's add a few more math helper functions.
}
```
We need one last utility function. Bizarrely, **Vector2** does not provide a built-in rotation function. Let's fix that.
In **Vector2Extensions.cs**:
```cs
public static Vector2 Rotate(this Vector2 vector, float radians)
{
return Vector2.Transform(vector, Matrix3x2.CreateRotation(radians));
}
```
Now we can construct a formula for our random serve direction. First, let's change the BallSpawner to take a speed value instead of a specific velocity.
{{% notice tip %}}
@ -90,8 +101,8 @@ The difference between speed and velocity is that velocity contains directional
Now, in **BallSpawner.cs**:
```cs
var direction = (float)MathHelper.RandomDouble(-System.Math.PI / 4.0, System.Math.PI / 4.0);
var velocity = new Vector2(message.Speed * (MathHelper.CoinFlip() ? -1 : 1), 0).Rotate(direction);
var direction = MathHelper.RandomDouble(-System.Math.PI / 4.0, System.Math.PI / 4.0);
var velocity = new Vector2(message.Speed * (MathHelper.CoinFlip() ? -1 : 1), 0).Rotate((float)direction);
AddComponent(ball, new VelocityComponent(velocity));
```