| 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, there is a single soccer ball on the screen. While you can alter the soccer ball's movement using the right thumbstick, you can clearly see that the soccer ball is following the path of a parabola across the screen, in a simple simulation of gravity's pull on a soccer ball that's been thrown upwards . The message at the top of the screen has been updated, as well.
2. Moving The Ball Along a Parabolic Arc.
Since the source code to this program is nearly identical to the program uses in the previous tutorial, we will only examine the code that's new, or different.
In order to know how to move the ball, we will need to track
the velocity of the ball. Velocity is defined as the speed and direction
in which the ball is moving. We will also define the initial velocity
using named constants, as well as the amount to decrease the Y speed by at each
update.
In a nutshell, we will set the starting speed for the ball, and at each update,
we will decrease the ball's speed in the Y direction (to simulate gravity's
downward pull), then move the ball. Across
numerous updates, the ball will follow a parabolic arc across the screen.
|
public class Game1
:
XNACS1Base
{ private XNACS1Circle m_TheSoccer; // a soccer ball private float m_XSpeed; private float m_YSpeed; // Constants for circle/ball definition private const float BALL_INIT_X = 5.0f; private const float BALL_INIT_Y = 2.0f; private const float BALL_INIT_RADIUS = 3.0f;
private const float INIT_SPEED_X = 0.53f; private const float INIT_SPEED_Y = 0.9f; private const float Y_SPEED_DECREASE_BY = 0.011f; |
private float m_YSpeed;
(Later on, we'll look at using a Vector2 struct to track both parts of the velocity in a single, "compound" variable)
private const float INIT_SPEED_Y = 0.9f;
Lastly, we will define the amount by which we will decrease the ball's
speed (but only in the Y direction!):
private
const
float
Y_SPEED_DECREASE_BY = 0.011f;
|
protected
override
void
InitializeWorld()
{ World.SetWorldCoordinate( new Vector2(0,0), 100.0f); // initialize the soccer ball InitializeSoccer(); // Initialize the X and Y speed m_XSpeed = INIT_SPEED_X; m_YSpeed = INIT_SPEED_Y; PlayACue( "Launch" );} |
m_YSpeed = INIT_SPEED_Y;
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); // Step 1: move the soccer MoveSoccerByThumbStick(); // Step 2: Update soccer y-speed, by fake gravitational pull UpdateSoccerYSpeed(); // Step 3: Compute new soccer position ComputeSoccerPosition(); // Step 4: Compute soccer size ComputeSoccerSize(); // Step 5: Print message for the user PrintMessageForUser(); } |
|
///
<summary>
/// Update soccer y-speed, by fake gravitational pull /// </summary> private void UpdateSoccerYSpeed() { m_YSpeed -= Y_SPEED_DECREASE_BY; } |
|
///
<summary>
/// Compute soccer ball's new position according to the X and Y speeds /// </summary> private void ComputeSoccerPosition() { m_TheSoccer.CenterX += m_XSpeed; m_TheSoccer.CenterY += m_YSpeed; } |
FURTHER EXERCISES::
Initializing the Velocity In A Separate Function
For this exercise, you should use the same project that was explained in
the above tutorial.
In the example code provided with this tutorial, the velocity of the ball
was initialized in the InitializeWorld method, while the ball itself was
initialized in the InitializeSoccer method. For this exercise, you
should take the code that initializes the ball's velocity, and put it into a
new, separate function. Make sure that you call that from
InitializeSoccer.