I have a problem to code the path of a projectile from a gun as it moves forward and upward. This needs to be done in p5.js.

Although the Angry Birds method has been tried by me, I am looking for a more physics derived function involving the sine and cosine of angle of the gun.


1 Answer 1


Presumably you started by reading our extensive library of existing Q&A on , and constructing a vector from an angle.

Putting those together, you should have no difficulty deriving:

launchVelocity.x = launchSpeed * Math.cos(launchAngle);
launchVelocity.y = launchSpeed * Math.sin(launchAngle);

positionAtTimeT.x = launchPosition.x + t * launchVelocity.x
positionAtTimeT.y = launchPosition.y + t * launchVelocity.y + t * t * gravity/2.0;

Where gravity is your acceleration due to gravity, eg. \$-9.81 \frac m {s^2}\$


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .