Skip to main content
2 of 2
deleted 8 characters in body
kzidane
  • 17.7k
  • 3
  • 28
  • 100

This needs a bit of physics knowledge. It should be basically easy though! Let's imagine how gravity affects the ball in real life!

I'm not sure the following will be 100% correct since I haven't studied physics in years

As I hit the ball up when it's going down, it starts moving up. It's velocity as I hit it is 0. It then increases, let's say (supposing I'm a superman) by 100 m/s and as the gravity affects the ball with some force in the opposite direction, then velocity starts decreasing until it gets to 0 again (in the air this time), then the ball starts moving down with acceleration of 9.8 m/s squared.

How to simulate this?

The key to the solution is definitely variables. You should create a variable that stores the velocity of the ball on the y-axis. We'll assume that when the velocity of the ball on the y-axis is negative, this means that the ball is moving up and when it's positive, then the ball is moving down.

We could have assumed the opposite (i.e., positive means up and negative means down) and in fact, that would be more familiar, but unfortunately, this is the way computer graphics work so not to be confused.

enter image description here

As the player hits the ball, the velocity should become negative and starts increasing until it becomes 0 then it keeps increasing until it hits the a player or the ground. At this moment, it should become negative again and the process repeats!

The amount with which the velocity of the ball is decreased/increased may be stored in another variable and this amount itself may be increased/decreased depending on how much you want your game to be realistic! ;)

kzidane
  • 17.7k
  • 3
  • 28
  • 100