Game Development Reference
In-Depth Information
Here we will do our safety checks to see if the inclinometer exists, and if the value
returned by it exists. As stated in the documentation, the operating system doesn't
have to return a value, and this should be checked for.
Based on the requested axis, we will retrieve the angle and then do a simple bounds
check to see if the angle is within the specified upper and lower bounds. If we are
within those bounds, we can indicate that this has been triggered and our normal
code will handle it from there.
The final touch comes from making use of this new trigger inside the Player class.
Just create a new InclinometerTrigger as you did the rest of the triggers and
set the data on it, as shown:
auto tiltDownTrigger =
_actionMoveDown->CreateTrigger<InclinometerTrigger>();
tiltDownTrigger->SetData(RotationAxis::Pitch,
5, 90);
auto tiltUpTrigger =
_actionMoveUp->CreateTrigger<InclinometerTrigger>();
tiltUpTrigger->SetData(RotationAxis::Pitch,
-90, -5);
Here we created the triggers and set some thresholds. It would be annoying if the
ship moved back and forth while the player tried to hold the device steady, so we
use a ten degree threshold between 5 and -5 degrees that indicates that nothing
should happen. This is done by specifying the bounds of the triggers from 5 to 90
degrees for movement down the screen, and -5 to -90 degrees for movement up
the screen. Remember that we're doing numeric comparisons, so -5 is higher than
-90 and should be arranged accordingly, otherwise the trigger will never be true.
Try it out now by deploying to a device that has sensors; if you have a tablet you
should be able to use that. Also try running this on a standard PC or laptop without
any sensors; you should not see any crashes and the game should behave as if the
sensor code never existed.
Search WWH ::




Custom Search