Part 5: Shooting Cannon Balls
The Sea Gunner
What goes into firing a cannon ball in Pirate Sea Jam? Shooting a naval gun in a video game might seem simple – A matter of responding to the player hitting the fire button by hurtling a projectile into the air.
However, programming cannon shots in a video game can be a matter of intricate details, just as there were more to it than simply lightning a fuse when naval guns roared on the high seas hundreds of years ago.
Shooting a cannon during the golden age of piracy
Back in the 17th century large crews of gunners were responsible for handling cannons on the gun deck, including preparations, loading, and firing.
To go in to some detail, between each shoot the bore had to be carefully cleaned to remove any debris or residual gunpowder, swabbed out and dried up, ready for loading the next round. The gun powder was usually pre wrapped in bags of cloth and pushed down the end of the barrel using a a long wooden handle. The gun then had to be secured in the proper position before being fired.
Early cannons had no triggers to fire shots. The gun powder had to be directly ignited at the end of the barrel through a small hole filled with finely grounded gunpowder.
Everything needed to be timed right when opening fire. The helmsman had to turn and align the broadside of the ship towards the target, and the gunner had to wait while the ship was rolling side to side in the waves until it tilted just the right amount. Too high and the cannon ball would fly through the air above the enemy. Too low and the shot would simply splash into the sea.
Shooting a cannon in Pirate Sea Jam
Just as in real piracy life the video game requires the helmsman to aim the cannons sideways by turning the entire ship. The guns only shoot straight ahead with the possibility to loft the cannon ball by keeping the fire button pressed for an extended amount of time.
The ship has cannons on both sides, but only the cannons on the side facing the closest target are fired. There is some math involved in calculating which cannons are on the side that points towards the target. The vector (think "arrow with a direction and length") pointing from the ship towards the target are compared with the front facing vectors of each cannon. An algebraic operation called the dot product can efficiently calculate to what extent two vectors are aligned and if they are pointing roughly in the same direction, without having to resort to trigonometric functions and angles. Two vectors pointing in the same direction have a positive dot product and vectors pointing in the opposite direction have a negative dot product. Two perpendicular vectors have a dot product of zero.
The two vectors in our case represent the cannon direction and the target direction. Checking if the dot product is positive or negative quickly determines which cannons are pointing towards the target.
A push in the right direction
When the fire button is released a cannon ball is inserted at the location of the cannons that are on the target side of the ship and given the same velocity as the vessel. If that was all, the impression would be that the cannon ball was just heaved overboard. To make is shoot off, the projectile is given a strong push in the same direction as the cannon is pointing. Note that the direction is a combination of the heading of the ship, how the ship heels in the rolling waves and how much the barrel has been tilted upwards. When soaring through the air, the trajectory of the cannon ball is affected by the gravitational force that accelerates the projectile downwards until it reaches the water level and is removed from the game world. With some skill and luck the cannon ball might have hit a target on its way.
Conclusion
The art of accurately firing a cannon aboard a moving ship lay largely in the timing, but also in paying attention to the finer details, both back in the the 17th century and today in Pirate Sea Jam.
Now, off to sea and good luck target practising!
Next up
Next up is trying to make the game mobile web browser friendly. Keep your fingers crossed and you might end up being able to control Pirate Ship Jam using your thumbs.
Ship ahoy!
Get Pirate Sea Jam
Pirate Sea Jam
Jam-sized pirate game prototype in the making. Written in Rust and Bevy. Made with ❤️
More posts
- Part 10: Sailing Physics SimulationMay 31, 2024
- Part 9: Total rewriteFeb 07, 2024
- Part 8: Creating an Infinite OceanNov 20, 2023
- Part 7: Dynamic Orbiting CameraNov 13, 2023
- Part 6: Adjusting for Mobile DevicesNov 02, 2023
- Part 4: Player control and Game FeelOct 04, 2023
- Part 3: Adding Low Poly ModelsSep 02, 2023
- Part 2: Buoyancy and Water DynamicsAug 26, 2023
- Part 1: Creating Ocean WavesAug 22, 2023
Leave a comment
Log in with itch.io to leave a comment.