Why do we need to achieve the effect of motion sensing? This is mainly used again.AndroidIn the game, with this effect, it will make our players have a very good visual sense. Let's see how it is implemented.AndroidAction sensing.
1. DiscoveryAndroidProvidedSensorlistenerInterface
2. You must have an action sensor manager to use.Sensormanager
Java code:
- // Obtain sensormanager
- Sensormanager sensormgr = (sensormanager) getsystemservice (sensor_service );
3. registerSensormanager
Java code:
- // If true is returned, the registration is successful. If flase is returned, the opposite is true.
- Sensormgr. registerlistene (this, sensormanager. sensor_accelerometer, sensormanager. sensor_delay_game );
If you registerSensormanagerIf the call fails
Java code:
- Sensormgr. unregisterlistener (this, sensormanager. sensor_accelerometer );
4.SensorlistenerTwo methods must be implemented
Java code:
- Public void onaccuracychanged (INT arg0, int arg1 ){}
- Public void onsensorchanged (INT sensor, float [] values ){}
5. Define3DCoordinates
Java code:
- Class pos3d
- {
- Float X;
- Float y;
- Float Z;
- }
- Pos3d cur, last;
In MethodOnsensorchangedIn this case, data can be obtained.
Java code:
- Cur. x = values [sensormanager. data_x];
- Cur. Y = values [sensormanager. data_y];
- Cur. z = values [sensormanager. data_z];
- Float speed = math. Abs (cur. x + cur. Y + cur. Z)-(last. x + last. Y + last. z)/Interval
The following is how to judge thisSpeedWhen it is greater than a defined value
Java code:
- If (speed> 1000)
- {
- // Here for processing
- }
In this way, the most basic implementation of motion sensing, but we can certainly feel the shortcomings of this method