【问题标题】:Adobe Flash cs4 rotation mathAdobe Flash cs4 旋转数学
【发布时间】:2011-09-25 15:21:23
【问题描述】:

目前我的对象旋转面对另一个对象时遇到问题。目前它工作正常,就像这张图片一样,我在原点的对象将能够旋转并移动到 3 个象限中的对象,除了带有正的象限90度到180度,我的物体在移动到物体时会旋转几个完整的旋转一段时间,有人知道为什么吗?我用来旋转的句子是rotation += (foodLocationDegrees - (rotation - 90)) * .2;,其中食物是使用

var angle:Number = Math.atan2(foodTarget.y - y, foodTarget.x - x); 
            foodLocationDegrees =Math.floor((angle  * 180 / Math.PI));

y 和 x 是鱼的对象,foodtarget 是食物的对象。

 public function moveToFood():void
        {   

            var dx:Number = x - _destinationX;
            var dy:Number = y - _destinationY;

            trace("Food location degree relative to fish mouth "+foodLocationDegrees);
            var targetRotation:Number = 0;
            if (foodLocationDegrees > 0 && foodLocationDegrees < 90)
            {
            trace("food is on 1st quadrant of the fish mount");
                this.x -= dx / 18;
                this.y -= dy / 18;
            }else if (foodLocationDegrees > 90 && foodLocationDegrees < 180)
            {
            trace("food is on 2nd quadrant of the fish mount"); 

                this.x -= dx / 18;
                this.y -= dy / 18;
            }else if (foodLocationDegrees > -180 && foodLocationDegrees < -90)
            {
                trace("food is on 3nd quadrant of the fish mount");

                this.x -= dx / 18;
                this.y -= dy / 18;
            }else if (foodLocationDegrees < 0 && foodLocationDegrees > -90)
            {
                trace("food is on 4nd quadrant of the fish mount");

                this.x -= dx / 18;
                this.y -= dy / 18;
            }
            trace("Before adding Rotation " + rotation);
            var number:Number = (foodLocationDegrees - (rotation - 90)) * .1;
            trace("rotation to add "+number);
            rotation += (foodLocationDegrees - (rotation - 90)) * .2;

            trace("After adding Rotation " + rotation);

            //removing food when both hit boxes hit
            if (hit.hitTestObject(foodTarget.hit))
            {
                foodInPond--;
                foodTarget.removeSelf();
            }

        }

【问题讨论】:

  • PS 我的数学知识很薄弱,因为我没有努力学习数学,公式是从这里提出的答案拼凑而成的,我的数字有点乱,请原谅我。

标签: flash actionscript-3 math flash-cs4


【解决方案1】:
public function moveToFood(food:Food):void
{
    var speed:Number = 6.5;

    var a:Number = food.x - x;
    var b:Number = food.y - y;
    var ang:Number = Math.atan2(b, a);

    rotation = ang * 180 / Math.PI;

    x += Math.cos(ang) * speed;
    y += Math.sin(ang) * speed;
}

你不能只使用sincos 朝你所面对的方向移动吗?

【讨论】:

  • 我试过你的函数,但是鱼离开了食物,函数在 Enter_frame 处理程序中处理,使值反复变化。
  • 我能够让移动工作,但鱼头旋转面对食物是我遇到的麻烦。使用您的方法,鱼会侧向移动。是因为我设置鱼符号的注册点的方式吗?
  • 您需要做的是旋转库中的图形,使其朝右,而不是朝上。
  • 谢谢,我可以知道在哪里可以了解公式的工作原理吗?
  • en.wikipedia.org/wiki/File:Sine_and_Cosine.svg sin 和 cos 取一个弧度值,等于 -1 和 1 之间的值。例如,cos(0) 等于 1,sin(0) 等于 0 . 所以如果你看公式,当你的角度是 0 弧度时,你沿着 x 轴移动 1 * 速度,并穿过 y 轴 0 * 速度 (0)。
猜你喜欢
  • 2010-11-06
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
相关资源
最近更新 更多