问题描述
我有一个飞船,有一个位置,目的地和旋转。当有一个新的目标向前移动时所有顺时针旋转,直到它正面临其目的地的时间。
I have a spaceship that has a location, destination and a rotation. When it has a new destination it moves forward all the while rotating clockwise until it is facing its destination.
code:
public void Move()
{
Vector requiredDirection = destination - origin;
requiredDirection.Normalize();
Vector directionNow = new Vector((float)Math.Cos(rotation), (float)Math.Sin(rotation));
float x = Math.Abs(requiredDirection.X - directionNow.X);
float y = Math.Abs(requiredDirection.Y - directionNow.Y);
if ((x > rotationSpeed) || (y > rotationSpeed))
{
rotation += rotationSpeed;
}
shipPosition += directionNow * speed;
}
我的问题是,这艘船只在旋转在一个的方向,直到它正面临着目标,我需要它,这将是最短路线的方向转动。
My problem is that the ship will only rotate in the one direction until it is facing its target, I need it to rotate in the direction that would be the shortest route.
我真的很茫然,以从哪里开始,这是矢量我第一次真正尝试。
I'm really at a loss as to where to begin, this is my first real attempt at Vectors.
推荐答案
从 directionNow
的夹角 requiredDirection
是通过 Math.Atan2(requiredDirection.Y,requiredDirection.X)鉴于 - Math.Atan2(directionNow.Y,directionNow.X)
。那将是积极的逆时针转动,负向顺时针方向转动。
The angle from directionNow
to requiredDirection
is given by Math.Atan2(requiredDirection.Y,requiredDirection.X) - Math.Atan2(directionNow.Y,directionNow.X)
. That will be positive to rotate counterclockwise, negative to rotate clockwise.
这篇关于C#载体,角度和旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!