-1

我一直在尝试制作一个塔防游戏,其中暴徒被赋予以给定速度移动的目的地,而现在它的设置方式发生了一些奇怪的事情,我不确定 updateAngle() 方法.

    // Destination is represented by Coordinates[index]

    public void Update(GameTime time)
    {
        Position.Y -= (float)(time.ElapsedGameTime.TotalSeconds * velocity * Math.Cos(angle));
        Position.X += (float)(time.ElapsedGameTime.TotalSeconds * velocity * Math.Sin(angle));

        float remainingX = Math.Abs(Destination.X - Position.Y);
        float remainingY = Math.Abs(Destination.Y - Position.Y);

        if (remainingX < 2 && remainingY < 2)
        {
            index++;

            if (index == Coordinates.Count - 1)
            {
                End = true;
            }
            else
            {
                updateAngle();
            }
        }

    }

    private void updateAngle()
    {
        angle = (float)Math.Atan((Position.Y - Destination.Y) / (Position.X - Destination.X));
    }
4

1 回答 1

0

如果位置和目的地正确,则给出直角:

angle = Math.Atan2(Destination.Y-Position.Y, Destination.X-Position.X) 
于 2013-08-26T18:37:41.890 回答