Android 使用余弦定律计算角度,给定2个点,计算1个点
我正在开发一款空中移动游戏(as3)。我已经为这个游戏创建了一些最初只是四处游荡的机器人。但当奖金进入阶段时,他们必须转移到奖金上 新奖金的检测和机器人的移动工作正常,但当新奖金进入阶段时,操作有问题 基本上,这就是它的工作原理(或者我试图让它工作): 我的机器人作为一个参数随着他的旋转而移动。(工程罚款) 因此,要想知道是否出现了新的奖金,我首先要参加一个活动:Android 使用余弦定律计算角度,给定2个点,计算1个点,android,actionscript-3,math,mobile,air,Android,Actionscript 3,Math,Mobile,Air,我正在开发一款空中移动游戏(as3)。我已经为这个游戏创建了一些最初只是四处游荡的机器人。但当奖金进入阶段时,他们必须转移到奖金上 新奖金的检测和机器人的移动工作正常,但当新奖金进入阶段时,操作有问题 基本上,这就是它的工作原理(或者我试图让它工作): 我的机器人作为一个参数随着他的旋转而移动。(工程罚款) 因此,要想知道是否出现了新的奖金,我首先要参加一个活动: protected function onNewBonusAppeared(event:BonusEvent):void {
protected function onNewBonusAppeared(event:BonusEvent):void
{
trace("new bonus appeared!");
_moveToBonus = true;
_rotation = getRotation(new Point(event.bonus.x, event.bonus.y));
trace("Heading to " + event.bonus.type + " with new rotation: " + _rotation);
}
我的机器人现在位于A点,通常会转到B点(normalNextLocation)。
然后,新的奖金出现在C点(下一个位置)。
我想用余弦定律来解决这个问题,因为我需要A的角度来知道我机器人的新旋转。
这就是我如何计算的:
// calculate new rotation
private function getRotation(nextLocation:Point):Number
{
//sources:
//http://www.teacherschoice.com.au/maths_library/trigonometry/triangle_given_3_points.htm
//http://en.wikipedia.org/wiki/Law_of_cosines
//http://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
//Calculate current angle and corners
var angle:Number = _rotation * 0.0174532925;
var currentLocation:Point = new Point(this.x, this.y);
var normalNextLocation:Point = new Point(Math.sin(angle) * _speed, -Math.cos(angle) * _speed);
//Calculate lengths of the 3 sides of the triangle
var lengthA:Number = calculateLength(normalNextLocation, nextLocation);
var lengthB:Number = calculateLength(currentLocation, nextLocation);
var lengthC:Number = calculateLength(currentLocation, normalNextLocation);
//Calculate the difference in rotation
//-------------THIS IS WHERE IT GOES WRONG-----------
var deltaRotation:Number = calculateAndInverseCosineRule(lengthA, lengthB, lengthC);
//positive or negative rotation difference
if (normalNextLocation.y < nextLocation.y)
return _rotation - deltaRotation;
else
return _rotation + deltaRotation;
}
private function calculateLength(a:Point, b:Point):Number
{
//SQRT((x2 - x1)² + (y2 - y1)²)
return Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
}
private function calculateAndInverseCosineRule(lengthA:Number, lengthB:Number, lengthC:Number):Number
{
//a² = b² + c² - 2*b*c*cos(alpha)
//cos(alpha) = (b² + c ² - a²) / (2 * b * c)
//alpha = cos^-1(cos(alpha))
var cos:Number = (Math.pow(lengthB, 2) + Math.pow(lengthC, 2) - Math.pow(lengthA, 2))
/ (2 * lengthB * lengthC);
trace("cos: ", cos); //returns NAN at some point... don't know why
return Math.acos(cos);
}
//计算新的旋转
私有函数getRotation(nextLocation:Point):编号
{
//资料来源:
//http://www.teacherschoice.com.au/maths_library/trigonometry/triangle_given_3_points.htm
//http://en.wikipedia.org/wiki/Law_of_cosines
//http://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
//计算当前角度和角点
变量角度:数字=_旋转*0.0174532925;
var currentLocation:点=新点(此.x,此.y);
var normalNextLocation:点=新点(数学sin(角度)*\u速度,-数学cos(角度)*\u速度);
//计算三角形三条边的长度
变量长度:数值=计算长度(normalNextLocation,nextLocation);
var lengthB:Number=计算长度(当前位置,下一个位置);
var lengthC:Number=计算长度(当前位置,正常下一个位置);
//计算旋转的差值
//-------------这就是问题所在-----------
var deltaRotation:Number=calculateAndInverseCosineRule(lengthA、lengthB、lengthC);
//正旋转差或负旋转差
if(normalNextLocation.y
我已经在这件事上找了很长时间了,但是找不到答案。。。有人知道我做错了什么吗[已修复]您在此处输入错误:
var cos:Number = (Math.pow(lengthB, 2) + Math.pow(lengthC, 2) - Math.pow(lengthC, 2))
要减去的量应该是Math.pow(lengthA,2)
。按原样计算(模浮点精度)lengthB^2/(2*lengthB*lengthC)
。如果lengthB==0
,就会产生一个NaN
,我怀疑就是这样。这意味着nextLocation
相同或至少非常接近currentLocation
另一方面,
normalNextLocation
不应该是currentLocation+timeStep*velocity
?据我所知,您将其设置为velocity
。您是否可以发布一个正在发生的事情的演示,或者一个关于所需行为的说明?我不确定我是否明白你想做什么。我觉得奇怪的是,如果机器人不再向它前进,为什么它需要使用“normalNextLocation”。如果您只需要两点之间的角度,请使用Math.atan2(target.y-bot.y,target.x-bot.x)
我需要两条线之间的角度,我有两个点,估计是第三个点,因为我的重复次数不超过10次,给您:谢谢,NaN问题解决了!将查看normalNextLocation的内容,因为它还不能正常工作。。。感谢您的关注!:-)
var cos:Number = (Math.pow(lengthB, 2) + Math.pow(lengthC, 2) - Math.pow(lengthC, 2))