The ball will move upwards in the direction you threw it. At some point it will start to slow down, stop, and then start falling down. The path the ball follows is called a parabola. This is what we expect of gravity. It's always around us, so we don't pay too much attention to it. If you would throw the ball in space, which has no gravity, the ball would move on forever in a straight line, as it does now in our game. On Earth, the ball will move up for a while, then slow down, and eventually fall down again. This movement path is described in the following diagram:
In this case, the cannon and cannonball should be on the ground, so we want to simulate some gravity. Not only will the parabola trajectory look more interesting, it will also make the game more challenging. You could try hitting a target by shooting the cannonball at a steep angle and have it hit a target as it drops down. It is very useful to shoot over the hills, which we will do in the last stage of this project.
To simulate gravity, you could use a realistic mathematical formula. But it can be hard to figure out, and in a simple game, it often isn't needed. We are going to create a good-looking parabola trajectory with just a simple calculation and some trickery.
To simulate the pull of gravity, we are going to use the built-in timer variable. This timer will start counting seconds when the game is started, but we can reset it to start counting again. We will be using the increasing number as a constantly increasing pull, which will eventually start dragging the cannonball downwards.
The steps to create the trajectory are as follows:
In my opinion, the number -5 has the best result. But if you disagree, you're free to choose another number. Just make sure that the number is negative, because a positive number will cause the cannonball to float ever faster upwards.
3.21.93.20