围绕中心向量点旋转对象

Jus*_*opa 7 c# math xna xna-4.0

我应该在这个前言,我不是一个数学人.我在另一个问题中找到的代码似乎有点工作......除了它导致我放置的对象大部分在屏幕外旋转.

这是代码:

public void Update(GameTime gameTime)
{
    Location = RotateAboutOrigin(Center, Origin, 0.01f);
}

public Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation)
{
    var u = point - origin; //point relative to origin  

    if (u == Vector2.Zero)
         return point;

    var a = (float)Math.Atan2(u.Y, u.X); //angle relative to origin  
    a += rotation; //rotate  

    //u is now the new point relative to origin  
    u = u.Length() * new Vector2((float)Math.Cos(a), (float)Math.Sin(a));
    return u + origin;
} 
Run Code Online (Sandbox Code Playgroud)

通过鼠标单击中心向量周围的任意位置来设置位置.

当我点击时,中心就是我所放置的物体的中心(你猜对了).它是通过简单地划分纹理的高度和宽度来确定的.

Origin是我试图旋转的vector2.它静态设置为384,384.


我可以说,它是两个向量之间的距离,然后使用atan2来确定角度.其余的后来对我来说是一个谜.我知道我应该知道一切都做了什么,而且我打算在春季开始上大学(仅晚了十年).我试过读它们,但我迷路了.任何帮助,将不胜感激.

此外,如果你有一个很好的触发傻瓜网站,你可以推荐,我会很高兴阅读它.

Ste*_*e H 10

这是一个替代方案:

public Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation)
{
    return Vector2.Transform(point - origin, Matrix.CreateRotationZ(rotation)) + origin;
} 
Run Code Online (Sandbox Code Playgroud)

它确实"转化为世界起源,旋转,然后平移回来"位.

无论原点相对于世界原点的位置如何,这都会使"原点"围绕"旋转"弧度旋转"点".触发发生在内置函数的'CreateRotationZ()'中.要了解它如何应用trig,请在框架中反映该方法.

编辑:修复变量名称