使用余弦定律计算角度,给出2个点并计算1个点

时间:2011-12-27 23:07:45

标签: android actionscript-3 math mobile air

我正在AIR(as3)开发移动游戏。我为这个游戏创造了一些机器人,最初只是四处闲逛。但是当奖金进入舞台时,他们必须转移到奖金。

新奖金的检测和机器人的移动工作正常,但是当新奖金进入舞台时,操纵有问题。

基本上,这就是它的工作原理(或者我试着让它起作用):

我的机器人随着他的轮换作为参数移动。 (工作正常)

private function moveToNextLocation():void
{
    var angle:Number = _rotation * 0.0174532925; // 1 degree = 0.0174532925 radians
    var speedX:Number = Math.sin(angle) * _speed; 
    var speedY:Number = -Math.cos(angle) * _speed;

    if (_turnLeft && !_moveToBonus){
        _rotation -= _rotationStep * (_speed / _maxSpeed);
    }
    else if (_turnRight && !_moveToBonus){
        _rotation += _rotationStep * (_speed / _maxSpeed);
    }

    this.x += speedX; this.y += speedY;
}

所以,要知道是否有新的奖金出现我首先抓住一个事件:

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点(nextLocation)。 我想通过余弦定律来解决这个问题,因为我需要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);
}

我已经搜索了这个问题很长一段时间了,但找不到答案......有人看到我做错了什么吗? - [已修复]

1 个答案:

答案 0 :(得分:1)

你这里有一个错字:

var cos:Number = (Math.pow(lengthB, 2) + Math.pow(lengthC, 2) - Math.pow(lengthC, 2))

要减去的数量应为Math.pow(lengthA, 2)。按原样,您计算(模数浮点不准确)lengthB^2/(2*lengthB*lengthC)。这会产生NaN if lengthB == 0,我怀疑是会发生什么。这意味着nextLocation相同或至少非常接近currentLocation

另一方面,normalNextLocation currentLocation + timeStep*velocity不应该velocity?就我所见,您将其设置为{{1}}。