如何旋转矢量

How to Rotate vector

我一直在大量的论坛上搜索,但没有任何东西可以使用(或理解)。 所以我有这个

public void OnFinishCasting(Champion owner, Spell spell, Unit target)
    {
        var current = new Vector2(owner.X, owner.Y);
        var to = Vector2.Normalize(new Vector2(spell.X, spell.Y) - current);
        var range = to * 1150;
        var trueCoords = current + range;

我会向您解释它的作用。 当我按下 Q 按钮时,它会朝鼠标方向发射一个弹丸,我需要做的是制作另一个弹丸并朝另一个方向发射它,比如圆锥形或其他东西,无论它去哪里,只要按照我喜欢的方式旋转它。 怎么做 ?

根据您所说的:

make another projectile and shot it in another direction ..., no matter where it goes

我将在

上为 X 和 Y 添加一些随机值
new Vector2(spell.X + intRndX, spell.Y + intRndY)


根据跟进评论更新

试试这个:

public static class Vector2Extensions
{
    public static Vector2 Rotate(this Vector2 v, double degrees)
    {
        return new Vector2(
            (float)(v.X * Math.Cos(degrees) - v.Y * Math.Sin(degrees)),
            (float)(v.X * Math.Sin(degrees) + v.Y * Math.Cos(degrees))
        );
    }
}

这将为 Vector2 添加扩展 class,您可以调用:

trueCoords.Rotate(Math.PI/90)

获取新向量