shadow_of__soul
shadow_of__soul

Reputation: 427

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.

Upvotes: 3

Views: 2433

Answers (2)

mathematician1975
mathematician1975

Reputation: 21351

If this is a ballistics problem in the sense that you project a particle from point A with velocity v at an angle theta and you want it to hit a point T where the y coordinates of A and T match (ie they lie on a plane perpendicular to the vector of gravitational force vector) then you can calculate the required angle and velocity from this equation (See your wiki link where this is defined):

 R = (v * v * sin(2 * theta))/g

Here R is the distance travelled in the x direction from your start point A . The problem you are facing is you are trying to interpolate a parabola through just 2 points. There are an infinite amount of parabolas that will interpolate 2 points while the parabola through 3 points is unique. Essentially there are an infinite amount of choices for velocity and angle such that you can hit your target.

You will either need to fix the angle, or the velocity of the bullet in order to use the above equation to find the value you require. If not, you have an infinite number of parabolas that can hit your target.

The above assumes that air resistance is ignored.

EDIT : Thus if you know velocity v already you can get theta from simple rearrangement of the above :

( asin(g * R / (v * v)) ) / 2 = theta

Upvotes: 4

shadow_of__soul
shadow_of__soul

Reputation: 427

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.

Upvotes: 0

Related Questions