Game Development Reference
In-Depth Information
private float MapMinusOneToOne(short value)
{
float output = ((float)value / short.MaxValue);
// Be careful of rounding error
output = Math.Min(output, 1.0f);
output = Math.Max(output, -1.0f);
if (Math.Abs(output) < _deadZone)
{
output = 0;
}
return output;
}
}
}
SDL represents analog controls using axes that can be polled with SDL_
JoystickGetAxis . The control sticks are made from two axes. A controller
might have a number of different axes, so in the constructor, two indices are pas-
sed in to identify which axis we want this control stick to represent. These identi-
fying numbers will change for each type of controller. The numbers that represent
the different controls on the gamepad aren't guaranteed to be the same for every
type of gamepad. One gamepad's left control stick might have the index one, but
another type of gamepad might index the left control stick with the index five. For
this reason, it's often a good idea to allow the player to remap his controls.
The Update method is called once per frame, and it updates the X and Y values
with values from -1 to 1, depending on the position of the stick. There's also a
little buffer for the dead zone that ignores small movements of the control stick.
The Xbox controller has two control sticks so the controller class can now be
updated to represent this.
public ControlStick LeftControlStick { get; private set; }
public ControlStick RightControlStick { get; private set; }
public XboxController(int player)
{
_joystick = Sdl.SDL_JoystickOpen(player);
 
Search WWH ::




Custom Search