Android-Action sensing technology, game programming 3D Coding

Source: Internet
Author: User

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:

  1. // Obtain sensormanager
  2. Sensormanager sensormgr = (sensormanager) getsystemservice (sensor_service );

3. registerSensormanager

Java code:

  1. // If true is returned, the registration is successful. If flase is returned, the opposite is true.
  2. Sensormgr. registerlistene (this, sensormanager. sensor_accelerometer, sensormanager. sensor_delay_game );

If you registerSensormanagerIf the call fails

Java code:

  1. Sensormgr. unregisterlistener (this, sensormanager. sensor_accelerometer );

4.SensorlistenerTwo methods must be implemented

Java code:

  1. Public void onaccuracychanged (INT arg0, int arg1 ){}
  2. Public void onsensorchanged (INT sensor, float [] values ){}

5. Define3DCoordinates

Java code:

  1. Class pos3d
  2. {
  3. Float X;
  4. Float y;
  5. Float Z;
  6. }
  7. Pos3d cur, last;

In MethodOnsensorchangedIn this case, data can be obtained.

Java code:

  1. Cur. x = values [sensormanager. data_x];
  2. Cur. Y = values [sensormanager. data_y];
  3. Cur. z = values [sensormanager. data_z];
  4. 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:

  1. If (speed> 1000)
  2. {
  3. // Here for processing
  4. }

In this way, the most basic implementation of motion sensing, but we can certainly feel the shortcomings of this method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.