当给定角度和闪光速度时,计算物体的轨迹

时间:2012-09-16 05:28:39

标签: actionscript-3 flash game-physics flash-cs6

我正试图从大炮发射一个炮弹并让它遵循一条现实的道路。火焰的角度根据大炮的方向而变化(自动定向到鼠标指针)。所以我想弄清楚的是,当给定角度和设定速度时,如何沿着抛物线路径移动炮弹。

我已经读过,这可以在没有复杂三角学的情况下完成(从未在高中学习过),并且可以通过在每个刻度上为yVelocity添加重力来简单计算。但是,此刻,我不知道如何计算初始yVelocity(再次,取决于炮弹方向)。

您可以在此处查看当前动画:http://kate.ict.op.ac.nz/~welfajw1/portfolio/videos/task3-assignment2.swf

这一切都在AS3中完成,我的代码如下:

主要时间轴代码:

import flash.display.*;
import flash.events.*;
import flash.geom.*;

var cannonball:ball_mc;
var angleDegree;

myCannon.addEventListener(Event.ENTER_FRAME, cannonEnterFrame);

function cannonEnterFrame(pEvt)
{
    var mc = myCannon;
    var mg = myCannon.myGun;

    //find angle for orientation
    var angleRadian = Math.atan2(mouseY - mc.y, mouseX - mc.x);

    //convert to degrees
    angleDegree = angleRadian * 180 / Math.PI;

    //limit rotation
    if(angleDegree > -63 && angleDegree < 20)
        mg.rotation = angleDegree;
}

stage.addEventListener(Event.ENTER_FRAME, stageRefresh);

function stageRefresh(pEvt)
{
    if (cannonball)
    {
        //move every "tick"
        cannonball.move();
    }
}

stage.addEventListener(MouseEvent.CLICK, mouseClicked);

function mouseClicked(pEvt)
{
    //starting position of the ball
    cannonball = new ball_mc(100, 475);

    //SEND IN INITIAL x, y VELOCITIES
    cannonball.fire(20, angleDegree);

    //add to stage
    stage.addChild(cannonball);
}

ball_mc代码:

package 
{

    import flash.display.MovieClip;
    import flash.sensors.Accelerometer;
    import flashx.textLayout.formats.Float;


    public class ball_mc extends MovieClip
    {
        //constant gravity
        public static const g:Number = 2;

        //starting velocities
        private var ux:Number;
        private var uy:Number;

        public function ball_mc(startX:int, startY:int)
        {
            x = startX;
            y = startY;
        }

        public function fire(vx:Number, vy:Number):void
        {
            ux = vx;
            uy = vy;
        }

        public function move():void
        {
            //distance moved in x dir
            var sx:Number = ux; 
            //new velocity in y dir
            var vy:Number = uy + g;
            //distance moved in y dir
            var sy:Number = uy + g/2;

            //apply movement
            x += sx;
            y += sy;

            //save new y velocity
            uy = vy;
        }
    }
}

0 个答案:

没有答案