Posted July 20, 2025 by Kehvarl
Now we've added thrust and controls to the Blue ship. There are 2 parts to making this work.
Part 1: Rotation. DragonRuby allows us to apply a rotation to our sprites using the angle parameter. So we'll tweak that in 1-degree increments when the player pressed the left or right arrow keys.
Part 2: Vectored Thrust. When the player presses the up arrow, we want to apply thrust in the direction the ship is currently pointed
def thrust_vector ship a_rad = (ship.angle + 90) / 180 * 3.14 ship.vx += Math.cos(a_rad) * ship.thrust ship.vy += Math.sin(a_rad) * ship.thrust end
First off we need to correct our ship's angle. The sprites are drawn pointed UP, but our formula expects and unrotated ship to be pointed RIGHT. To resolve that we add 90 degrees to our ship angle to line up the math.
We can calculate the VX and VY components of our new Thrust Vector using the Cosine and Sine of our ship's angle, but the ruby trigonometry functions expect Radians not Degrees, and our angle is currently stored in degrees. So first we apply degrees_to_radians function to our angle. Then perform our trig, multiply the ratios by our ships engine thrust number, and apply the results to the ship's velocity components.
Close encounters with our central star can cause the ship to gain incredible velocity very quickly, so for the moment we're applying a clamping function to the ships. If they would leave the screen, we zero out velocity in that direction so they remain on screen. In the future, we may just have a camera follow the player ship (and maybe a minimap) or we might wrap the screen edges and let the player try to regain control.
if s.x <= 8 or s.x >= 1272 s.x = s.x.clamp(16, 1264) s.vx = 0 end if s.y <= 8 or s.y >= 712 s.y = s.y.clamp(16, 704) s.vy = 0 end