-1

I've been trying to implement projectile motion in Javascript and I'm stuck at figuring out the angle (from which to derive the x and y velocity)

var v = 1;
var d = 10;
var g = -1;
var angle = 0.5 * Math.asin((g*d)/(v*v));

I would've expected someting like this to work, since it's coming from here Here.
The values I seem to get are either NaN or a very small number.

To give a bit more context; The projectile has to go from point A to point B (the distance is d in my code) where A and B are at the same height. Later on I would like to randomize the distance and angle a bit, but I assume that's not going to be an issue once this angle problem is solved.

EDIT:
As for a better example:

var v = 100;
var d = 100;
var g = 1;  // I've made this positive now
var angle = 0.5 * Math.asin((g*d)/(v*v));

This says that angle is now 0.005

Dries
  • 995
  • 2
  • 16
  • 45

2 Answers2

0

Im not really good at this physics problrm but i'll try

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin

  • the arg for Math.asin() should be between -1 and 1. Otherwise it returns NaN
  • from your example you run Math.asin(-10/1) maybe the velocity has max distance that it could cover. No matter the angle, 1m/s wont reach 500m distance for example
  • in your link there is a formula to count the max distance from given velocity and angle. Use that to confirm your variables are relevant.
  • angles are represented in values between -1 to 1 in cos, sin, tan. It makes sense that NaN (or values outside the range) means no angle can cover the distance

Hope it helps a bit

Nikko Khresna
  • 1,024
  • 8
  • 10
  • Thanks, I'll try to see if I can incorporate some of those bulletpoints in my code to get a better understanding of what's happening – Dries Nov 09 '18 at 19:02
0

Note from the same wikipedia page you linked d_max = v*v/g. Given your inputs this evaluates to 1. Therefore a distance of 10 is impossible.

Another way to notice this is the range of sin is (-1,1). Therefore asin of any number outside of this range is undefined. (g*d)/(v*v) is 10, so Math.asin returns NaN

wilkben
  • 657
  • 3
  • 12