奇怪的旋转值
问题描述:
我得到了一个对象(叫做tempEnemy),它正在飞行和拍摄。 问题是我无法将tempEnemy.rotate的值保留为正值,即 它应该在0和359度之间。目前rotateTo范围从: rotateTo < 0(bug)& & rotateTo> 0 & & rotateTo> 359(bug)。奇怪的旋转值
tempEnemy.dX = tempEnemy.destX - tempEnemy.x;
tempEnemy.dY = tempEnemy.destY - tempEnemy.y;
//I added 180 because my tempEnemy object was looking and shooting to the wrong direction
tempEnemy.rotateTo = (toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY))) + 180;
if (tempEnemy.rotateTo > tempEnemy.frame + 180) tempEnemy.rotateTo -= 360;
if (tempEnemy.rotateTo < tempEnemy.frame - 180) tempEnemy.rotateTo += 360;
tempEnemy.incFrame = int((tempEnemy.rotateTo - tempEnemy.frame)/tempEnemy.rotateSpeed);
答
您可以随时使用模运算符(%
),以保持一个积极的价值。该模块计算分部的其余部分。
E.g. (例如用整数那里工作为一个除法总是具有遗留下来的。)
19 % 5 = 4
因为在数19 5只适合3次(3 * 5 = 15
,, 4 * 5 = 20
,, 20是太高)的遗留为4 (19 - 15
)。这是模数。
额外的例子:
7 % 3 = 1
15 % 4 = 3
21 % 9 = 3
模操作的输出是永远不会高于右手操作者 - 1那里它是完美的您的问题。
如果您的物体旋转了1234度,那么使用模数360操作它以获得0到360之间的相应数字。
1234 % 360 = 154
其他更容易的例子:
720 % 360 = 0
360 % 360 = 0
540 % 360 = 180
-180 % 360 = 180
-720 % 360 = 0
-540 % 360 = 180
答
听起来像是一个经典的角度平均问题。下面是对平均工作的公式角度
private function averageNums($a:Number, $b:Number):Number {
return = (Math.atan2(Math.sin($a) + Math.sin($b) , Math.cos($a) + Math.cos($b)));
}
+0
非常感谢您!我会检查一下!:) – drpelz 2012-04-06 15:36:19
谢谢!我会尽快尝试!:) – drpelz 2012-04-06 15:35:45