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

Category : 2D , 3D , Game Dev Primer , Sine Waves , Trigonometry

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:**

1 2 3 4 5 |
float _period = 2.0f; float _angle = 60; float _phase = sin( time / _period); float _newAngle = _phase * _angle; object.rotation = vector3(0,0, _newAngle); |

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.

## NOW LET’S DO AN EXPERIMENT:

**Audio Example:**

Just like the previous example but applied differently.

1 2 3 4 5 |
float _period = 2.0f; float _pitchVariation = 1; float _phase = sin( time / _period); float _newPitch = _phase * _pitchVariation; audioSource.pitch = _newPitch; |

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.

#### Latest posts by Justin Fletcher (see all)

- UITableView scroll to new row only when at the end of table. - December 7, 2015
- Unity Shader for a Scanlines Effect - August 16, 2015
- Unity Shader Alpha Blended With Color - August 16, 2015