我怎样才能确定是否它的速度更快面对的对象顺时针或逆时针旋转? [英] How can I determine whether it's faster to face an object rotating clockwise or counter clockwise?

查看:163
本文介绍了我怎样才能确定是否它的速度更快面对的对象顺时针或逆时针旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想这个没有用了一些日子了,但基本上我有一些生物和屏幕上的播放器。我希望发生的是敌人转向面向玩家在变速,而不是锁定到适当位置,并立即面对的球员。

I've been trying this to no avail for some days now, but basically I have some creatures and the player on the screen. What I want to happen is for the enemies to turn to face the player at a variable speed, rather than 'lock' into position and face the player immediately.

我所要做的是制定出是否是更快地为一个给定的敌人转动顺时针或逆时针面对玩家,但它证明是超出了我的能力与三角。

What I am trying to do is work out whether it is faster for a given enemy to rotate clockwise or counter clockwise to face the player, but it's proving to be beyond my capabilities with trigonometry.

例如:

X 在这些数字再presents我想在每一种情况下旋转'短'的路径和方向。

x in these figures represents the 'shorter' path and the direction I want to rotate in each situation.

什么是制定出既顺时针或逆时针在这种情况下,最简单的方式,使用任何如下:

What is the simplest way to work out either 'clockwise' or 'counter-clockwise' in this situation, using any of the following:

  • 敌人面对的方向。
  • 敌方玩家之间的角度,和敌人的球员。

推荐答案

不需要以计算的角度,或在这里使用三角函数,假设你有一个方向向量。

There is no need to calculate angles or use trigonometric functions here, assuming you have a direction vector.

var pos_x, pos_y, dir_x, dir_y, target_x, target_y;
if ((pos_x - target_x) * dir_y > (pos_y - target_y) * dir_x) {
    // Target lies clockwise
} else {
    // Target lies anticlockwise
}

此简单地绘制通过在它的面对的方向的物体的假想线,并计算出该该行的一侧的目标是上。这是基本的线性代数,所以你不应该需要使用罪() COS()等任何地方此功能,除非你需要计算从角度的方向矢量

This simply draws an imaginary line through the object in the direction it's facing, and figures out which side of that line the target is on. This is basic linear algebra, so you should not need to use sin() or cos() etc. anywhere in this function, unless you need to calculate the direction vector from the angle.

这还采用了右手坐标系,这将是倒退,如果你使用的是左手坐标系 - 该公式将是相同的,但顺时针和逆时针将被交换

This also uses a right-handed coordinate system, it will be backwards if you are using a left-handed coordinate system -- the formulas will be the same, but "clockwise" and "anticlockwise" will be swapped.

深入的解释:的函数计算前向矢量的外积(dir_x,dir_y)和向量目标,(target_x - POS_X,target_y - POS_Y)。所得外产物是赝它是正或负,这取决于靶是否是顺时针或逆时针。

Deeper explanation: The function computes the outer product of the forward vector (dir_x, dir_y) and the vector to the target, (target_x - pos_x, target_y - pos_y). The resulting outer product is a pseudoscalar which is positive or negative, depending on whether the target is clockwise or anticlockwise.

一个载体是幅度方向,例如,北3公里,距离或6厘米下来。您可以使用直角坐标系(X,Y)重新present一个载体,也可以重新它使用的极坐标present (R,θ)。双方重新presentations给你的一样的载体,但它们使用不同的号码和不同的公式。一般情况下,你应该坚持用直角坐标系,而不是极坐标。如果你正在编写一个游戏,极坐标吸庄严 - 他们垃圾你的code与罪() COS()随处可见。

A vector is a magnitude and direction, e.g., 3 km north, or 6 centimeters down. You can represent a vector using cartesian coordinates (x, y), or you can represent it using polar coordinates (r,θ). Both representations give you the same vectors, but they use different numbers and different formulas. In general, you should stick with cartesian coordinates instead of polar coordinates. If you're writing a game, polar coordinates suck royally — they litter your code with sin() and cos() everywhere.

在code有三个矢量在里面:

The code has three vectors in it:

  • 向量(POS_X,POS_Y)是对象的位置,相对于原点。

  • The vector (pos_x, pos_y) is the position of the object, relative to the origin.

向量(target_x,target_y)是目标的位置,相对于原点。

The vector (target_x, target_y) is the position of the target, relative to the origin.

向量(dir_x,dir_y)是对象面对的方向。

The vector (dir_x, dir_y) is the direction that the object is facing.

这篇关于我怎样才能确定是否它的速度更快面对的对象顺时针或逆时针旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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