1
\$\begingroup\$

I am currently devloping my first javascript program ever which is a table tennis game. I am trying to create trajectory for the ball to follow when hit - just a very basic parabola. However im not very good at math, and I have no idea how to do this!

I want to have the parabola start wherever the ball was hit. The X position will be either 100 or 110, since that is the only two positions where it is possible to hit the ball. And the Y position will completely vary. So I need to create a parabola that - if your looking at a 2D table - starts from the right side of the net and ends up on the right side, as if you did an under arm hit. I have no ides how to do this since there will be no minus positions! - Maybe I should have two arcs one coming up and the other one coming down? I also have no idea what equation would make an trajectory in the shape of an arc.

\$\endgroup\$
4
  • \$\begingroup\$ It sounds like you want to hit the ball upwards and across the table, and let gravity pull it back down right? Do you also want the ball to ever bounce off the table? \$\endgroup\$
    – Alan Wolfe
    Aug 16 '15 at 0:56
  • \$\begingroup\$ @Alan Wolfe, thats exactly right! \$\endgroup\$
    – Manu
    Aug 16 '15 at 7:52
  • \$\begingroup\$ I don't have time to make a full answer at the moment but you don't have to calculate the parabola. You just keep track of the ball's velocity, and add gravity to it each frame. So your ball might have a velocity vector of (-0.5, 0.1) if moving to the left and slightly upwards. Move the ball that much that frame then subtract some amount from the y component to simulate gravity for next frame. When it hits a paddle you adjust the velocity accordingly. Rinse and repeat! \$\endgroup\$
    – Alan Wolfe
    Aug 17 '15 at 0:25
  • \$\begingroup\$ @Alan Wolfe Thanks! Do you know any tutorials / links / source code for this? \$\endgroup\$
    – Manu
    Aug 17 '15 at 9:46
2
\$\begingroup\$

You don't necessarily need a parabola formula for this.
The best way, in my opinion, to do this is to update the velocity of the ball each frame. I don't know how you organized your code, but I assume you have an update function that is executed multiples times per frame.

To make the trajectory of a parabola, you simply need to substract a constant to the velocity each frame.

ball.velocity.y = -1; // The initial velocity of the ball, negative because it is going upward
ball.velocity.x = 1; // The constant velocity of the ball on the X axis

var gravity = 9.81; // The acceleration due to gravity

function update(){

    ball.velocity.y += gravity; // Notice the + sign because the gravity goes downward

    // Process the velocity
    ball.position.x += ball.velocity.x;
    ball.position.y += ball.velocity.y;

}

I didn't include it in this snippet, but you should use a delta-time variable to handle FPS-variation (you can look it up on Google)

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.