c# - 转换后如何旋转表示某些射弹的矢量?

标签 c#

我有这个小代码,用于播放器和从所有者(播放器)到鼠标光标的射弹。我需要解决其他射弹转换成“锥形”形状的问题。 现在,我有这个

//this is used for rotation
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))
        );
    }
}


//this is when I cast the spell
public void OnFinishCasting(IChampion owner, ISpell spell, IAttackableUnit 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;

        spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords.X, trueCoords.Y);
        spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords.Rotate(Math.PI , 15).X, trueCoords.Rotate(Math.PI, 15).Y);
        spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords.Rotate(Math.PI , 45).X, trueCoords.Rotate(Math.PI, 45).Y);
        spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords.Rotate(Math.PI , 90).X, trueCoords.Rotate(Math.PI, 90).Y);

    }

让我稍微解释一下代码:

owner.X and owner.Y are the player's position , from where the spell is going to cast.

trueCoords.X and trueCoords.Y are the mouse's position , to where the spell is supposed to cast.

使用这种方法,第一个射弹按照预期的方式(到鼠标位置),其余的都非常错误,有时(在某些未知位置,按预期进行)并且在某些位置它们是去随机位置。 例如,我正在向屏幕的最右侧施放我的火球,其他人正在施放,可能是上侧,可能是下侧,也可能是玩家位置的后侧。 我该如何解决这个问题,以便每次都能将它们转换成这种“圆锥”形状?

最佳答案

你旋转的不是方向(“到”/“范围”)而是目标位置,你想要做的是将旋转应用于方向(“到”向量或“范围”向量都应该起作用), 然后将原点位置应用于该方向向量(否则你不仅仅是在旋转火球的方向,你基本上是在整个地方旋转鼠标位置)。 一个小抛光的东西:也许你想使用角度:-45,-15,15,45 所以圆锥体的中间瞄准鼠标位置,而不是圆锥体的侧面(当然取决于你的游戏/法术等

    var to = Vector2.Normalize(new Vector2(spell.X, spell.Y) - current);
    var range = to * 1150;

    var trueCoords0 = current + range
    var trueCoords1 =  current + range.Rotate(Math.PI, 15);
    var trueCoords2 =  current + range.Rotate(Math.PI, 45);
    var trueCoords3 =  current + range.Rotate(Math.PI, 90);

    spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords.X, trueCoords.Y);
    spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords1.X, trueCoords1.Y);
    spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords2.X, trueCoords2.Y);
    spell.AddProjectile("Fireball", owner.X, owner.Y, trueCoords3.X, trueCoords3.Y);

关于c# - 转换后如何旋转表示某些射弹的矢量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57709047/

相关文章:

c# - 1000000000 * 3 = -1294967296?

c# - 属性支持字段 - 它有什么用?

c# - 静态 WCF 代理类对象

c# - 在 C# 中处理可变集合键

c# - 如何确保 Autofac 在 EF6 DbContext 上调用 Dispose()

c# - 比较两个图像并使用 emgu cv 库提取差异

c# - 如何使用 ASP.NET MVC 5.0 的 ASP.NET Identity 实现密码重置?

c# - 如何使用属性表示Nest中的关键字数组

c# - 在 ASP.NET 中插入时缺少 SELECT 关键字

c# - 如何防止 HTML 进入 ASP.NET Web 窗体文本框