NOTE THAT THIS IS NOT ENTIRELY ACCURATE!! This game runs with the physics at 60 FPS. Therefore, adjustments may need to be made if you intend to use this feature. Also, this is a work in progress example. Not everything is set in stone, stuff are subject to change, there may be errors in the math, yada yada yada.
Use WASD to control Lucky. Use the Arrows to control Hope.
Press SPACEBAR to throw the ball. Either Lucky or Hope will throw the ball at the other, and hopefully one of the two will end up catching it.
Pres O to toggle visualizations (see where the characters are aiming for, the area in which the ball travels, etc.)
So how does this work? By nature, when we throw something upwards, it will travel in a parabolic shape. As the object flies further, gravity will begin to bring the object back towards the ground. Eventually the object will stop moving vertically, and then travel back down to the ground, increasing its velocity as it falls. This motion creates a parabola. Check it out yourself! Either use this game's simulation, or lob an object in the air to another person in real life and you'll see the parabola. It's just basic physics.
So what this game does is, when you prepare to throw the ball, the character will calculate the angle in which it needs to throw the ball in order for the ball to reach the other character while traveling in a parabola shape.
- STEP 1: First, the player takes the distance between itself and its target and cuts it in half. You'll see what this is for soon...
- STEP 2: The player then finds the distance between itself and a point in the air to aim for. This point is located at the midpoint between the player and its target. It has a fixed y-value, which in this case is 32.
- STEP 3: The player then finds the angle between the itself and the point in the air to aim for. We use our results from STEPS 1 and 2 to use cosine. The reason for this is because, assuming that gravity did not exist, then the ball would move in a straight line and not a parabola.
- STEP 4: The player makes sure that the angle it found in STEP 3 is not negative, which can happen if the target is to the left of the player. If the angle is negative, then we convert it to positive. Alternatively, we could just find the absolute value of the angle we received in STEP 3, but I just wanted to create this 4th step as a precaution...
Once the angle has been determined, it's time to send that puppy flying!
KNOWN ISSUES:
- This really only works if the player and target are at the same y-level. If they are not, the projectile might miss. Additional modifications may be needed to ensure that the point in the air increases/decreases in height depending on the difference in height to the player and target.
- Because you cannot currently change the emit force of the Emit behavior while the game is running, sometimes the emit force may be too strong or too weak to reach the target, depending on how far away they are.
- If the player and target are too far away, then the throw will guarantee to miss.
- STEP 4 is not really necessary if you take the absolute value of the angle you get in STEP 3.
- The physics run at 60 FPS. As such, everything moves a little faster than "normal." Adjustments may be needed for games that do not have their physics run at 60 FPS.
- To accommodate for the 60 FPS physics in this game, the gravity has also been reduced from 45 to 25. As usual, adjustments may be needed for games that have greater/less gravity.
There may be more issues that I am not aware of.