Download Kobold2D v2.x Documentation

Transcript
You should not enable Device Motion and regular Gyroscope or Accelerometer input at the same time. This can result in
incorrect motion values. You get access to Gyroscope and Accelerometer values through Device Motion.
Retrieving Device Motion values
The Device Motion values are wrapped in a KKDeviceMotion (class reference) object.
KKInput* input = [KKInput sharedInput];
KKDeviceMotion* motion = input.deviceMotion;
// sensor-fused acceleration values
KKAcceleration* userAcceleration = motion.acceleration;
KKAcceleration* gravityAcceleration = motion.gravity;
CGPoint userAcc, gravityAcc;
userAcc = CGPointMake(userAcceleration.rawX, userAcceleration.rawY);
gravityAcc = CGPointMake(gravityAcceleration.rawX, gravityAcceleration.rawY);
KKRotationRate* rotationRate = motion.rotationRate;
CGPoint rot = CGPointMake(rotationRate.x, rotationRate.y);
// sensor-fused device orientation
CGPoint attitude = CGPointMake(motion.pitch, motion.roll);
#if KK_PLATFORM_IOS
CMAttitude* cmAttitude = (CMAttitude*)motion.attitude;
CMRotationMatrix rotMatrix = cmAttitude.rotationMatrix;
CMQuaternion quaternion = cmAttitude.quaternion;
#endif
With Device Motion you get access to two types of acceleration values: userAcceleration and gravityAcceleration. If you would add both value
sets together you would get the total acceleration of the device. With userAcceleration you get only the user component of the acceleration,
minus the constant gravitational acceleration. Likewise gravityAcceleration gives you only the gravitational component of the acceleration,
minus the user acceleration.
For both userAcceleration and gravityAcceleration you will want to access the "raw" values, since these acceleration values
provide a much smoother input than non-DeviceMotion acceleration values thanks to sensor-fusion with the Gyroscope values.
You also get access to the Gyroscope KKRotationRate object through Device Motion. The values are the same as described in KKInput
Gyroscope.
KKDeviceMotion exposes the commonly used pitch, roll and yaw attributes (also known as Euler angles). These values provide you with the
current orientation of the device. Alternatively, if you need and are comfortable with rotation Matrices and Quaternions, you can get access to the
CMAttitude object and thus the rotationMatrix and quaternion properties. This however would be iOS-specific code and needs to be wrapped in
#if KK_PLATFORM_IOS .. #endif in case your app should also have a Mac OS version.
You can keep a weak reference to the KKDeviceMotion object. It is valid during the entire lifetime of your app and its values
continue to be updated.
KKInput Keyboard
KKInput supports testing the state of the Keyboard keys. The keyboard is accessed with virtual key codes in a locale-independent manner.
About Locale Independent Keyboard Input
Any Key Down or Up
Testing for a particular Key press
Key presses with modifiers
Keyboard Input References
About Locale Independent Keyboard Input
It is a common mistake among beginning game developers to assume that testing for the key character "/" will work correctly on all keyboards.