FMX.Gyroscope Sample

From Appmethod Code Examples
Jump to: navigation, search

This sample demonstrates how to use the Gyroscope in order to rotate a 3D cube when your device is tilted.

Location

You can find the Gyroscope sample project at:

Description

The Gyroscope sample demonstrates how to use the Gyroscope in order to rotate a 3D cube when your device is tilted. This sample suports both iOS or Android devices. The sample uses the TOrientationSensor component and the TCustomOrientationSensor class to act like a gyroscope that rotates a 3D cube while you tilt your device.

Note: The TCustomOrientationSensor class supports Android, iOS and Windows platforms. However, each platform provides different orientation sensors and each type of sensor supports a different set of properties (see platform support for a completed list of platform, sensors and suported properties).

How to Use the Sample

  1. Navigate to one of the locations given above and open:
    • Object Pascal: Gyroscope.dproj.
    • C++: Gyroscope.cbproj.
  2. Select the target platform, iOS and Android supported.
  3. Press F9 or choose Run > Run.
  4. Interact with the sample:
    • There is a 3D cube displayed in the screen.
    • Tilt your device and see how the 3D cube rotates.

Files

File in Object Pascal File in C++ Contains

Gyroscope.dproj

Gyroscope.cbproj

The project itself.

uMain.fmx

uMain.fmx

The main form where the components are located.

uMain.pas

GyroscopePCH.h, Gyroscope.cpp

Used to define and implement the sample.

Information.txt

Information.txt

Further information about the sample.

Classes

TGyroscopeForm is the main form that represents the main window of the sample. It contains the following components:

Implementation

  • The sample uses TRectangle3D to implement a 3D shape under the form of a 3D rectangle that can be placed on a 3D FireMonkey form.
  • The sample uses TSensorManager to identify the sensor of your device.
  • The sample uses TOrientationSensor to obtain information about the device inclination (tilt).
  • The sample uses TTimer to repeat specific actions after a given time interval. TTimer specifies the time interval in wich TOrientationSensor offers the tilt information used to rotate the TRectangle3D.
  • The sample uses TLight to imitate the rays of light and their propagation on the 3D object.
  • The sample uses TLightMaterialSource to link a TLightMaterial to 3D objects. The link is made through MaterialSource property of the 3D object.

When you run the application, you see a 3D cube on the screen. If you tilt your device, the sample uses the TiltX, TiltY and TiltZ properties of TCustomOrientationSensor to obtain the angle that has to be used to rotate the 3D cube using the RotationAngle.X, RotationAngle.Y and RotationAngle.Z properties of TRectangle3D.

Uses

See Also

Samples