EUROPA Dev Blog #4: The Math

Europasprites1

Creating the “flies in a straight line” enemy was no problem, but as the enemies got more and more complicated, so did the programming challenges. Two enemies in particular, the Sweeper and the Sprinkler, attack you by spraying bullets – the Sweeper fires in an arc, and the Sprinkler shoots bullets in a spiral pattern. I’ve never had to program anything bullet hell-y before, so creating these enemies was an interesting experience.

I wrote the following for the Sweeper in my design document:
Sweepers (Up/Down): Swing their guns either up or down in a wide vertical arc, firing a stream of bullets. They retreat after firing.
This seems simple enough. The Sweeper comes in two flavors, the Sweeper that swings its gun up, and the Sweeper that swings its gun down. As its gun swings, it fires a bunch of bullets. Dodge em. Bingo bango.

However, this was more complicated than I initially anticipated. Simply firing “a bunch of bullets” doesn’t get the job done. I didn’t want it to just shoot a bunch of bullets horizontally; I wanted the bullets to match the angle of the gun as it made its arc. And how would the gun make its arc, anyways? Would I make a gun object and program it to rotate? Make separate Sweeper sprites with the gun at different angles?

As you can see, I ended up making a bunch of sprites for the Sweeper, creating a seven frame animation of its gun making an arc. My plan was to have the Sweeper move forward after spawning, stop, play this animation – spraying bullets as it did so, and then retreat back offscreen.

Sweeper

Seven frames of animation meant that the Sweeper would fire seven bullets at seven different angles. The gun starts and stops its arc at 45 degree angles. The bullets it shoots have a horizontal speed of 10, so by giving them a vertical speed of 10, they move at a 45 degree angle. The problem, then, was figuring out a way to make the Sweeper adjust the angle of its shots to match its gun.

Every time the fire script is called, the Sweeper advances its frame of animation and creates a bullet. I began with the Sweeper that moves its gun upward, so the first bullet it fires moves down at a 45 degree angle, meaning it has a vertical speed of 10, and the last bullet it fires moves upward at a 45 degree angle, meaning it has a vertical speed of -10. (In GameMaker, positive vspeed values move things down and negative vspeed values move things up.)

To efficiently create bullets that match the angle of its gun, I programmed the Sweeper to adjust the vertical speed of its bullets in increments of 10/3, or 3.33333333333. This way, it takes seven loops for the vertical speed of the bullet to go from 10 to -10, matching the seven angles of the gun.

mathcrap

I’m sure this all sounds incredibly boring, but I’m very happy that I was able to make the Sweeper efficiently program the speed of its own bullets instead of creating seven different bullet objects that move at different angles and making the Sweeper create them in order. I feel like I did something mildly smart, here. And isn’t that all that matters?!

SweeperSprinkler

The gif above shows the Sweepers in action, as well as the Sprinkler. The Sprinkler’s propellor is a separate object from its body – the two objects have the same movement speed so they stay together. Every time the propellor object fires, it rotates by 20 degrees and programs its bullets to also alter their direction by 20 degrees. This was actually much simpler than the Sweeper, as the propellor simply applies its current “image_angle” to the “direction” of the bullets it creates. Not much math involved. 😛

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s