Skip to content

Commit 52dc18e

Browse files
committed
Added joystick sample to getting started.
Requested as part of Issue #6830 on the main MonoGame repo and was asked to migrate over to this repo.
1 parent 0eb4845 commit 52dc18e

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

articles/getting_started/5_adding_basic_code.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,102 @@ The rest of the lines of code do the same thing but for the Down, Left and Right
125125

126126
If you run the game, you should be able to move the ball with the arrow keys.
127127

128+
Another option for user input is the Joystick class. Setting up input for the Joystick is very similar to setting up keyboard input. Find the **Update** method in the Game1.cs class file and add:
129+
```csharp
130+
if(Joystick.LastConnectedIndex == 0)
131+
{
132+
JoystickState jstate = Joystick.GetState(0);
133+
134+
float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
135+
136+
if (jstate.Axes[1] < 0)
137+
{
138+
ballPosition.Y -= updatedBallSpeed;
139+
}
140+
else if (jstate.Axes[1] > 0)
141+
{
142+
ballPosition.Y += updatedBallSpeed;
143+
}
144+
145+
if (jstate.Axes[0] < 0)
146+
{
147+
ballPosition.X -= updatedBallSpeed;
148+
}
149+
else if (jstate.Axes[0] > 0)
150+
{
151+
ballPosition.X += updatedBallSpeed;
152+
}
153+
}
154+
```
155+
156+
The following is a line-by-line analysis of the above code.
157+
```csharp
158+
if(Joystick.LastConnectedIndex == 0)
159+
```
160+
161+
This code assumes that we have a single controller plugged into our device. LastConnectedIndex is the index of the last connected controller. The default is -1, which would mean no controller is plugged in.
162+
If there is no controller, the code inside the if statement will be skipped over.
163+
164+
```csharp
165+
JoystickState jstate = Joystick.GetState(0);
166+
```
167+
168+
This code fetches the current joystick state ('Joystick.GetState(0)') and stores it into a variable called **jstate**.
169+
170+
```csharp
171+
if (jstate.Axes[1] < 0)
172+
```
173+
174+
This line checks whether the second Joystick axis is less than 0. The Joystick class stores multiple axis of direction for anything with an integer based range. For any number of 2D axis sticks, it stores it in an x,y format inside of an integer array.
175+
The axis of movement for 2D joysticks goes from -32768 to 32768 on most modern controllers. Aiming the Joystick upwards results in a negative value on the Y-axis ('Axes[1]').
176+
177+
The rest of the lines of the code do the same thing but for their relevant x and y directions.
178+
179+
If you run the game, you should be able to move the ball with the left Joystick on your controller if one is plugged in.
180+
181+
You will probably notice that the ball slightly moves on its own. This will likely be the result of your Joystick having a slight drift. You can fix that by adding a deadzone and changing the conditions to use this deadzone.
182+
183+
```csharp
184+
public class Game1 : Game
185+
{
186+
Texture2D ballTexture;
187+
Vector2 ballPosition;
188+
float ballSpeed;
189+
190+
int deadZone;
191+
```
192+
193+
Next, you need to initialize the deadzone. Find the **Initialize** method and add the following lines.
194+
195+
```csharp
196+
deadZone = 4096;
197+
```
198+
199+
Now, replace the conditions for the Joystick movement in **Update** to the following:
200+
201+
```csharp
202+
if (jstate.Axes[1] < -deadZone)
203+
{
204+
ballPosition.Y -= updatedBallSpeed;
205+
}
206+
else if (jstate.Axes[1] > deadZone)
207+
{
208+
ballPosition.Y += updatedBallSpeed;
209+
}
210+
211+
if (jstate.Axes[0] < -deadZone)
212+
{
213+
ballPosition.X -= updatedBallSpeed;
214+
}
215+
else if (jstate.Axes[0] > deadZone)
216+
{
217+
ballPosition.X += updatedBallSpeed;
218+
}
219+
```
220+
221+
If you run the game and move the Joystick around, you should notice that your Joystick has to move a decent distance before the ball starts moving. This is what a deadZone does, it allows for there to be a minimum distance before the input is reflected in the game.
222+
> Try experimenting with what happens when you change the value of the deadZone. Mess around and find an amount that fits your project.
223+
128224
You will probably notice that the ball is not confined to the window. You can fix that by setting bounds onto the ballPosition after it has already been moved to ensure it cannot go further than the width or height of the screen.
129225

130226
```csharp

0 commit comments

Comments
 (0)