Can someone explain this Pulsate code to me

// Pulsate the size of the selected menu entry.
double time = gametime.TotalGameTime.TotalSeconds;
float pulsate = (float)Math.Sin(time * 6) + 1;
scale = new Scale(1 + pulsate * 0.05f,1 + pulsate * 0.05f);


I grabbed it from the XNA GameState sample and just curious.

1 Answer

Looks like it's using a sine wave to change the scale of an object. Sine ranges from -1 to +1, so adding one to the pulsate variable puts the range at 0 to 2. Then adding 1 to (pulstae * 0.05f) Puts the range at 1 to 1.10.

As the name implies, this would likely give the appearance of pulsating.

Multiplying the time by 6 compresses the sine wave on the x-axis. Making the pulsing happen faster. If you were to multiply it by a smaller number (or not multiply it by anything) you could slow the pulsing.

• Why do they multiply the time by 6 ? Apr 27 '12 at 17:07
• Multiplying the time by 6 will change the speed at which the pulsing happens. Basically it shrinks the sine wave on the x axis. Pulsing faster. Apr 27 '12 at 17:08
• How can I relate it to the frequency? Apr 27 '12 at 17:15
• @Setheron: No, sin(t)'s period is 2PI seconds, so sin(6*t) gives you a pulse every 2PI/6 seconds, which is slightly less often than once a second.
– Eric
Apr 27 '12 at 17:20
• @Setheron: A pulse every ~1.05 seconds is slightly less. :-)
– Eric
Apr 27 '12 at 18:34