对象的旋转围绕中心vector2点 [英] Rotation of an object around a central vector2 point

查看:1308
本文介绍了对象的旋转围绕中心vector2点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该和我是附近的一个数学的人没有在任何地方前言本。我在另一个问题中发现的代码似乎有所工作...但它使对象我放在一个大圈大多是关闭屏幕旋转。

I should preface this with I am not anywhere near a math person. The code I've found in another question seems to be working somewhat... except that it causes the object I place to rotate in a large circle mostly off screen.

下面的代码:

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;
} 



位置是由在围绕中心向量的任意位置上的鼠标点击设置。

Location is set by a mouse click in an arbitrary position around the central vector.

中心是对象I'地方'的(你猜对了)中心当我点击。它是由简单地除以纹理的高度和宽度来确定。

Center is the (you guessed it) center of the object I 'place' when I click. It's determined by simply dividing the height and width of the texture.

来源是我试图绕所述vector2。它是静态设置为384384。

Origin is the vector2 I am attempting to rotate around. It's statically set at 384,384.

作为最好的,我可以告诉,它越来越两个向量之间的距离,然后用ATAN2以确定的角度。其余的以后是一个谜给我。我知道,我应该学什么做的一切,我也打算当我去上大学(仅仅十年下旬)在春天开始。我试着对他们的阅读,但我迷路了。任何帮助,将不胜感激。

As best I can tell, it's getting the distance between the two vectors and then using atan2 to determine the angle. The rest afterward is a mystery to me. I'm aware that I should learn what everything does, and I do intend to when I go to college(only a decade late) starting in the spring. I've tried reading up on them, but I'm lost. Any help would be appreciated.

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

Also, if you have a good trig for dummies site you can recommend, I would be delighted to read through it.

推荐答案

下面是一个另类:

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



它的翻译成世界坐标系原点,旋转,再转换回位。

It does the "translate to the world origin, rotate, then translate back" bit.

这旋转通过旋转弧度点周围的出身无论在哪里起源是相对于世界原点。该触发发生在CreateRotationZ()内置函数。要看看它是如何应用触发,反映了框架,方法

This rotates 'point' around 'origin' by 'rotation' radians regardless of where origin is relative to the world origin. The trig happens in the 'CreateRotationZ()' built in function. To see how it applies the trig, reflect that method in the framework.

编辑:固定变量名

这篇关于对象的旋转围绕中心vector2点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆