XNA Game-Themed CS1 Examples (XGC1) | |
Release 2.0 (XNA V3.1) |
References:
Goals:
1. Obtain the example code
When the game starts, you'll see a screen that
looks similar to this:
As you can see, this program draws a 'wave' of small soccer balls across the screen, plus a larger basket. You can move the basketball using the right thumbstick, and you change the amplitude (height) of the wave using the left thumbstick's Y part, and the frequency (how many waves on the screen at once) using the left thumbstick's X part.
We will get the wave using the mathematical sine function, which can be graphed like so:
Notice how the soccer balls in the screenshot follow the same pattern as the blue wave line in the above picture. Essentially, if you plug lots of values (between 0 and 6.28318... ( 2 * π, really) into the sine function, and then graph the y values that you get out, you'll get the above picture. If you've taken trigonometry, then this is all review. If you haven't yet taken trigonometry, then the key things to remember are that the 'amplitude' is the height of the wave, and that the 'frequency' is the number of times the wave is drawn in a given distance, more-or-less. A fuller explanation of the sine function is beyond the scope of this tutorial.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
// initialize the frequency and amplitude of the sine curve m_Frequency = 2.0f; // initially fit 2 cycles into 0 to 100
m_Amplitude = 10.0f; ;
//
initially oscillate from -10 to 10 |
protected
override
void
UpdateWorld() { if (GamePad.ButtonBackClicked()) this.Exit(); #region Plotting the Sine curve with while loop allow LeftThumbStick to change frequency and amplitude
RemoveAllFromDrawSet(); // remove all previous soccer balls ...
// left thumb stick change the amplitude and frequency m_Amplitude += SCALE_FACTOR * GamePad.ThumbSticks.Left.Y; m_Frequency += SCALE_FACTOR * GamePad.ThumbSticks.Left.X; float xPos = 0.0f, yPos;
// here is the while loop while (xPos < World.WorldMax.X) { float radian = (xPos / World.WorldMax.X) * (2.0f * ((float)Math.PI)) * m_Frequency; yPos = BALL_INIT_Y + (m_Amplitude * ((float)Math.Sin(radian))); CreateABallAt(xPos, yPos, "SoccerBall"); xPos = xPos + 1; } #endregion m_BasketBall.AddToDrawSet(); // make sure we draw the basket ball! m_BasketBall.CenterX += GamePad.ThumbSticks.Right.X; m_BasketBall.CenterY += GamePad.ThumbSticks.Right.Y; EchoToBottomStatus("Complete sine period:" + m_Frequency + " with amplitude:" + m_Amplitude); } |
Variable Name | Value |
xPos | 0.0f |
yPos | < uninitialized > |
float radian = (xPos / World.WorldMax.X) * (2.0f * ((float)Math.PI)) * m_Frequency;
In a nutshell, 0 /World.WorldMax.X is zero, and zero times anything is still
zero, so radian will end up being zero.
Once we've done that, we've got:
Variable Name | Value |
xPos | 0.0f |
yPos | < uninitialized > |
radian | 0.0f |
yPos = BALL_INIT_Y + (m_Amplitude * ((float)Math.Sin(radian)));
This gives us:
Variable Name | Value |
xPos | 0.0f |
yPos | 30.0f |
radian | 0.12566 |
CreateABallAt(xPos, yPos, "SoccerBall");
xPos = xPos + 1;
So at the end of the first iteration of the loop, we've got:
Variable Name | Value |
xPos | 1.0f |
yPos | 30.0f |
radian | 0.12566 |
float radian = (xPos / World.WorldMax.X) * (2.0f * ((float)Math.PI)) * m_Frequency;
Once we've done that, we've got:
Variable Name | Value |
xPos | 1.0f |
yPos | 0.12533f |
radian | 0.12566 |
yPos = BALL_INIT_Y + (m_Amplitude * ((float)Math.Sin(radian)));
This gives us:
Variable Name | Value |
xPos | 0.0f |
yPos | 31.2533f |
radian | 0.12566 |
CreateABallAt(xPos, yPos, "SoccerBall");
xPos = xPos + 1;
So at the end of the first iteration of the loop, we've got:
Variable Name | Value |
xPos | 2.0f |
yPos | 31.2533f |
radian | 0.12566 |
FURTHER EXERCISES::
// here is the while loop while (xPos > World.WorldMax.X) { float radian = (xPos / World.WorldMax.X) * (2.0f * ((float)Math.PI)) * m_Frequency; yPos = BALL_INIT_Y + (m_Amplitude * ((float)Math.Sin(radian))); CreateABallAt(xPos, yPos, "SoccerBall"); xPos = xPos + 1; } |
Kelvin Sung Computing and Software Systems University of Washington, Bothell ksung@u.washington.edu |
Michael Panitz Business And Information Technology Cascadia Community College mpanitz@cascadia.eduu |
This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531. | |
2/8/2010 |