【问题标题】:How to rotate a Vector2?如何旋转 Vector2?
【发布时间】:2011-04-01 07:48:43
【问题描述】:


我正在尝试旋转 Vector2,但没有任何效果。
我已经尝试了以下 -> 没有用:

x' = cos(angle)*x - sin(angle)*y & y' = sin(angle)*x + cos(angle)*y

我试过使用旋转矩阵 -> 没用

我做错了什么? :/

angle = MathHelper.Pi;
direction.X = (int)((direction.X) * Math.Cos(angle) - direction.Y * Math.Sin(angle));
direction.Y = (int)((direction.X) * Math.Sin(angle) + direction.Y * Math.Cos(angle));

float angle = MathHelper.PiOver2;
Vector2 dir = new Vector2(direction.X, direction.Y);
Vector2.Transform(dir, Matrix.CreateRotationX(angle));
direction = new Point((int)dir.X, (int)dir.Y);

【问题讨论】:

    标签: c# .net xna


    【解决方案1】:

    Vector2.Transform() 返回一个结果,而不是就地应用更改。

    var transformed = Vector2.Transform(dir, Matrix.CreateRotationX(angle));
    direction = new Point((int) dir.X, (int) dir.Y);
    

    【讨论】:

    • 不要对自己太苛刻。这是一类错误,你会发现自己在接下来的编程日子里每年都会重复一次(每次都有相同的困惑)。
    • 我想应该是CreateRotationZ,因为你想绕Z轴旋转。
    【解决方案2】:

    您编写的第一个方法应该可以工作,这里也显示了:http://www.oocities.org/davidvwilliamson/rotpoint.jpg

    记住存储原始值,并使用它们来确定新值,不要使用新的 x 值来计算 y。或者将产品存储在单独的变量中。

    【讨论】:

    • 我做到了(粘贴的代码是旧版本)但它仍然不起作用:/
    • +1 表示“记住存储原始值,并使用这些值来确定新值”。我花了大约 30 分钟尝试调试我的代码。我现在感觉很笨
    猜你喜欢
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多