Search code examples
actionscript-3mathgame-physics

find angle and velocity for a parabola that meet specific range


i'm a little ashamed to ask this, but i have tried a lot of different things and can't make it work.

i have a game that shots a bullet, i have made the code that calculates the parabola trajectory given a an angle and a velocity, but i'm trying to make the calculus needed to get the angle and velocity needed to reach X point (the user enemy tank) and i'm unable to make it work as i need.

my current code is:

  var startingPointX:Number = globalCoord.x;
  var startingPointY:Number = globalCoord.y;                
  var targetX:Number = tankPlayer.x;
  var targetY:Number = tankPlayer.y;
  //distance between user and enemy tank
  var distanceTarget = Math.sqrt(( startingPointX - targetX ) * ( startingPointX - targetX ) + ( startingPointY - targetY ) * ( startingPointY - targetY ));
  var fixedVel = (distanceTarget/10)*2;
  var fixedG = bullet.g;
  // launch angle
  var o:Number = -(Math.asin((0.5 * Math.atan(fixedG * distanceTarget / fixedVel *   fixedVel))) * 180 / Math.PI); 
  bullet.init(startingPointX, startingPointY, o, fixedVel);

and the functions in the bullet object that actually position the bullet in the parabola trajectory is:

 public function init(x, y:Number, rot:Number, speed:Number) {
        // set the start position
        var initialMove:Number = 35.0;
        this.x = x + initialMove * Math.cos(2 * Math.PI * rot / 360);
        this.y = y + initialMove * Math.sin(2 * Math.PI * rot / 360);
        this.rotation = rot;

        //get speed
        dx = speed * Math.cos(2 * Math.PI * rot / 360);
        dy = speed * Math.sin(2 * Math.PI * rot / 360);

        //animation  
        lastTime = getTimer();
        addEventListener(Event.ENTER_FRAME,moveBullet);


    }
    public function moveBullet(event:Event)
    {
        //get the time passed
        var timePassed:int = getTimer() - lastTime;
        lastTime +=  timePassed;

        //move bullet
        dy += g * timePassed / 1000;
        this.x +=  dx * timePassed / 1000;
        this.y +=  dy * timePassed / 1000;

        //bullet past the top of the screen
        if (this.y < 0)
        {
            deleteBullet();
        }
    }

any help would be really useful, thanks ! :D

Regards, Shadow.


Solution

  • Based on the suggestion from @mathematician1975 i resolved the code to this and works perfectly :D

                var distanceTarget =  startingPointX - targetX ;
                var fixedVel = 100;
                var fixedG = tmpB.g;
                var o:Number = (0.5 * Math.atan((fixedG * distanceTarget / (fixedVel * fixedVel)))) * 180 / Math.PI;
                //this is only necessary why the enemy tank is facing left
                o -= 180;
    

    what i made is:

    • set a fixed velocity as @mathematician1975 said, a lot bigger than before
    • the distance between starting and ending point is lineal and not using Pythagoras.
    • the -180 is just why the enemy tanks is facing left.

    i hope someone would find it useful in the future :D

    Regards, Shadow.