The Incredibly Useful Sine Waves PART 2 – More Applications – (Trigonometry) (Game Dev Primer)

The Incredibly Useful Sine Waves PART 2 – More Applications – (Trigonometry) (Game Dev Primer)

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

If you did read it, or don’t care, please continue.

Last time it was all about the basic sine function and how to make game objects move in the shape of a sine wave. This time let’s take it a step further. I’m going to show you a few more practical applications of sine waves, that isn’t limited to just moving objects.

Oscillations

I think I used the word oscillate more than 6 times in the last post, but it’s the essence of sine waves. This post isn’t going to be too long, since I think you’ll catch on quick if you haven’t already. Basically what I’m telling you, is that if you want anything to smoothly move back and forth (oscillate) between two states, values, locations, etc, USE THE SINE FUNCTION.

Rotation Example:

This will cause an object to rotate on it’s axis back and forth. And it still uses all the same properties from the sin function discussed in the previous post.

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

Line by line:

1. _period is set to 2. An arbitrary choice, the larger it is, the slower your object will rotate back and forth. And the smaller it is, the faster it will rotate.

2. _angle is how far to rotate back and forth. 60 is how many degrees it will rotate. Starting from 0, move to 60, then all the way to -60, and repeat.

3. _phase is the sin of a division of time. So the larger _period is, the slower the input increases for the sin function.

4. Lastly, since _phase uses sin, it will only oscillate between -1 and 1, so let’s crank that up to 60 and -60 by multiplying by the angle.

5. The last line sets the Z rotation of the object since it will be rotating on the X and Y plane because in this context, our object rotates in a 2D world.

Just 5 lines of code and you get something that looks like this.

SineRotate1

 

NOW LET’S DO AN EXPERIMENT:

Audio Example:

Just like the previous example but applied differently.

Practically the exact same lines of code. But can you guess what this will sound like.

 

I found this interesting because I never thought about what would happen with negative pitches. Well just listen to the output of the code.

Since pitch makes the sound higher and lower by having it play faster and slower, if you go negative IT PLAYS BACKWARDS! WHAT!?

Anyway this was a very cool experiment. The overall output was extremely creepy and/or funny. If you increase the _pitchVariation he’ll start to sound like a chipmunk, and also backwards.

The voice is saying “Awaiting Instructions” if you couldn’t make it out.

 

Here’s one more, but adjusting the pan instead of the pitch. So be wearing headphones to get the best effect out of this one.

The voice jumps from ear to ear.

 

Anyway what I’m getting at with all this is if you’re looking for the oscillation effect in anything. Use the sin function. Saves you lots of time and produces much nicer looking code, in my opinion.

Have fun with this, if you’ve got any questions feel free to ask.

 

I feel like there should be ONE MORE PART to the whole sin waves discussion. Perhaps adding cosines and getting circles involved. We’ll see soon.

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.

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