The Incredibly Useful Sine Waves PART 1 – Using The Sin Function – (Trigonometry) (Game Dev Primer)

The Incredibly Useful Sine Waves PART 1 – Using The Sin Function – (Trigonometry) (Game Dev Primer)

Don’t worry, this isn’t going to be a whole Trig class. I will however, go over one very useful trigonometric function that is easily overlooked by beginner, and even veteran game developers. The Sine Wave. Hopefully this will fuel your own curiosity to learn more about trigonometry and how applicable it is to the world of game development.

If you know what the Sine function is, fantastic, if not, I’m sure you’ve heard the term. You may have encountered it in high school geometry or trigonometry, and it’s applied in just about every field that involves some kind of math.

Sine waves are a graph of the Sine function, and given a basic form, as the input value changes, the output will oscillate between 1 and -1. Here’s what that looks like:

sine animation
Note: You may notice that the sine wave perfectly maps to a circle of radius 1. That form of circle is called a Unit Circle. Understanding it is helpful but not necessary. As I said this is not a trig class. Moving on.

As you can see as the red line moves across the x-axis, the graph oscillates between 1 and -1. Right now the wheels may be turning in your head. If not, you may be thinking, “So what, why is a function that jumps back and forth between -1 and 1 so special?”. Well, I’d like you to think back on an NES game called Castlevania and try to remember the flying Medusa heads… or look at this gif:

Medusa Heads

Anything look familiar?
This function is also used a lot in shooter game to create bullet movement and enemy movement patterns. I don’t have anymore gifs, but trust me, now that you know, you’ll see it a lot, especially in 2D games.

 

Try to implement the function into your game!

How can we make a game object move like the medusa head does? It’s actually very easy. Without being specific to a programming language it would look something like this.

 

This will work, somewhat. What you will see is that your object(medusa) is at the very bottom of the screen, jumping in and out of the frame. That’s because its Y position is oscillating between -1 and 1, exactly like a normal sine wave would.

low-tiny-waves

I know what you’re thinking. “But Justin, you told me it would make my game object move like the medusa heads in castlevania!” Well I didn’t quite tell you everything yet.

The real formula!

That’s right, there’s more. The real sine wave formula is a little more complex, and looks something like this:

A * ( sin ( B * ( x – C ) ) ) + D

What are all those letters doing there?

A = Amplitude (Tallness) of the wave.
B = How many waves there are for each cycle.
C = How far to shift the wave’s X position.
D = How far to shift the wave’s Y position.

Let’s put this to work. We know we don’t want the wave at the bottom of the screen for starters. So let’s through in that D value to our original medusa code.

D = screenHeight/2, so it should move up half the screen and look like this:

sinwave2

That’s a little better. But we want the medusa head to fly higher and lower than that. For that we need to change the amplitude of the wave. So let’s throw in the A value to our code and set it so something like 20.

sinwave3

This produces our nice tall, red wave. This is ok, but wow are those waves close together. How can we stretch them out. Well we’ll need to change B, the number of waves per cycle, and make it much less. Let’s try 1/4 waves per cycle. This way, a whole wave takes 4 cycles. Again to the code.

sinwave4

And there we have it. Our green wave is pretty close to what we’re looking for. You’ll have to play with the values of A,B,C, and D to get it just the way you like.

Now you know how to move object in 2D space as sine waves. Go ahead and play around with sine functions because there are many more uses for them, which I will cover in the next post.

IN PART 2:
More on sine waves and some practical applications and experiments. Check it out Here.

 

The following two tabs change content below.
Game Designer, Programmer, Lover of all things retro. I've been a programmer for about 8 years. Got into game development part time 6 years ago and haven't been able to put it down.

Latest posts by Justin Fletcher (see all)

Liked it? Take a second to support Justin Fletcher on Patreon!

About Author

Justin Fletcher

Game Designer, Programmer, Lover of all things retro. I've been a programmer for about 8 years. Got into game development part time 6 years ago and haven't been able to put it down.

2 Comments

The Incredibly Useful Sine Waves PART 2 – (Trigonometry) (Game Dev Primer) | The Weekly Coder

July 28, 2015 at 12:13 am

[…] If you didn’t read part 1, and want to get background on sine waves for game development. Click Here (PART 1). […]

    Henrietta

    May 10, 2017 at 9:39 pm

    That’s a subtle way of thknniig about it.

Leave a Reply

Show Some Love

Support The Weekly Coder on Patreon!

WeeklyCoderTV

Recent Popular Info

About:
This channel is all about coding. We will mostly post tutorials on making games with Unity 5 and above. Other posts will include tutorials on specific game mechanics and whatever else we feel like talking about.

Don't be shy.. If you have a question, leave a comment. If you you'd like to see a specific tutorial feel free to request it. If we think its worthwhile, then we may just do it.

Please don't forget to click the like button and remember to subscribe. Feel free to support our channel and the awesome content we create.

Total subscribers:
767

Total uploads:
71

Total upload views:
89.1 thousand

Follow me on Twitter

Subscribe

RSS Feed

Blog Authors