【问题标题】:Calculating an angle using the Law of Cosines, given 2 points and calculating 1 point使用余弦定律计算角度,给定 2 个点并计算 1 个点
【发布时间】:2011-12-28 10:47:56
【问题描述】:

我正在用 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);
}

我已经在这个问题上搜索了很长一段时间,但找不到答案......有人知道我做错了什么吗? - [固定]

【问题讨论】:

  • 您能否发布一个正在发生的事情的演示,或者一个关于所需行为的说明?我不确定我是否理解您要执行的操作。如果机器人不再朝它前进,为什么机器人应该使用“normalNextLocation”,这对我来说似乎很奇怪。如果您只需要两点之间的角度,请使用Math.atan2(target.y-bot.y, target.x-bot.x)
  • 我需要两条线之间的角度,我有 2 个点,估计是第 3 个点
  • 因为我没有超过 10 个代表,给你:imgur.com/Bkl27

标签: android actionscript-3 math mobile air


【解决方案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)。如果lengthB == 0,那将产生NaN,我怀疑这是发生了什么。这意味着nextLocation 相同或至少非常接近currentLocation

另一方面,normalNextLocation 不应该是currentLocation + timeStep*velocity 吗?据我所知,您将其设置为velocity

【讨论】:

  • 谢谢,NaN解决了!将看看 normalNextLocation 的东西,因为它还不能正常工作......谢谢你的注意! :-)
猜你喜欢
  • 2023-03-07
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
相关资源
最近更新 更多