1

I am making a Javascript program that includes physics but the projectile formula is very difficult for me to get right. I saw a link with the formula https://math.stackexchange.com/questions/82934/how-to-find-the-parabola-of-a-flying-object?newreg=e730be6696794b3f9ef8404df475c9c8 but this is quite hard to visualize with the way javascript does math functions. My code is the following.

//speed
var speed = 0;

//angle
var angle = 0;

//previous coord log
var pballx = 0;
var pbally = 0;

//ball fall counters
var bffasttime = 0;
var bftime = bffasttime/60;
//log counter
var lftime = 0;
var ltime = 0;

speed = sqrt(sq(abs(bally-pbally))+sq(abs(ballx-bally)))/0.2;
angle = Math.atan((pbally-bally)-(pballx-ballx))*180/Math.PI;
bally = ballx*Math.tan(angle)-[(1/2)*(9.8/sq(bftime))*sq(bftime)]/sq(speed);

It allows you to drag a ball around and every .2 seconds it is dragged it logs the coords which is what pbally and pballx and when released it calculates angle speed and the parabola but instead it just teleports to the top. I'm almost positive my formulas are correct but if they aren't any correction would be nice. Thanks!!!

Community
  • 1
  • 1
  • What exactly is this code supposed to do? What does it do instead? Which line behaves differently from what you expect? Can you narrow it down to a [mcve] instead of your entire sketch? – Kevin Workman Feb 26 '17 at 19:25
  • Its supposed to make a ball that can be thrown and picked up with a mouse. And I want it to behave in a manner that resembles realistic physics and instead it just glitches out and teleports to the very top of the screen. – Patrick Jones Feb 27 '17 at 03:00
  • Its the lines with the "projectile formula" – Patrick Jones Feb 27 '17 at 03:01
  • @KevinWorkman so thats it. – Patrick Jones Feb 27 '17 at 03:19
  • You need to go through the same process I outlined in [your other question](http://stackoverflow.com/questions/42494313/javscript-animation-will-not-perform-as-intended). You need to start working in smaller chunks and breaking things down into smaller pieces. You can't try to code 5 different things and then ask why none of them work. You have to break them down into smaller individual steps. For example, can you just get a ball moving across the screen first? Then try to make it more realistic from there. – Kevin Workman Feb 28 '17 at 00:39
  • @KevinWorkman Is that Better? – Patrick Jones Mar 01 '17 at 15:48

1 Answers1

0

angle = Math.atan((pbally-bally)-(pballx-ballx))*180/Math.PI;

Probably should be:

angle = Math.atan((pbally-bally)/(pballx-ballx))*180/Math.PI;