Coder Social home page Coder Social logo

boatattitude's Introduction

BoatAttitude

This Delphi Firemonkey 3D sample app uses phone sensors to position a sailboat. It is a FiremonkeySensorFusion usage sample ( see /omarreis/FiremonkeySensorFusion). Delphi's Gyroscope sample app tries to do that, but does not quite gets there.

("BoatAttitude" is a bad name since what changes is the camera orientation).

Compiled with D10.3.3 and tested on Android and iOS.

app screenshot

Tags: #Delphi #Firemonkey #Sensors #iOS #Android

FiremonkeySensorFusion

FiremonkeySensorFusion is a cross platform (Android and iOS) sensor fusion ( GPS + Accelerometer + Magnetometer ) The object receives sensor readings from the phone/tablet sensors and outputs phone attitude in the form of rectangular coordinates (aka Euler angles ).

These are also known as Yaw,Pitch,Roll or Azimuth,elevation,roll or Heading,altitude,roll. This profusion of names causes some confusion.

GPS reading is used to calculate the magnetic declination (using WMM model), so we can obtain the true heading from the magnetic heading.

In this example we use the sensor readings to orient a 3D sailboat scene. Also included in the scene: sea surface and cardinal points (N,S,E and W) Sensor readings are used to rotate a camera pointing to the boat.

Using rotation quaternion

Firemonkey 3D uses TControl3D.RotationAngle vector to change object rotation. These are known as Euler angles. Using rotation angles in this way has a couple problems. Setting Euler angles can lead to the so called "gymbal lock", with loss of degree of freedom. It is better to use quaternions to set object rotations.

Problem is Firemonkey FMX TControl3D has no public method to set the quaternion directly or manipulating the 3d matrix. This is worked around using a helper class (the solution was copied from https://github.com/tothpaul/Delphi/blob/master/Google%20Cardboard/4%20FullDemo/Main.pas )

 type  // helper class to help manipulating the matrix instead of changing RotationAngle
   TControl3DHelper = class helper for TControl3D
     procedure SetMatrix(const M: TMatrix3D);
   end;

 procedure TControl3DHelper.SetMatrix(const M: TMatrix3D);
 begin
   FLocalMatrix := M;
   RecalcAbsolute;
   RebuildRenderingList;
   Repaint;
 end;

This allows manipulating the object 3d rotation matrix directly.

Euler angle rotations are not commutative. Different order of rotations leads to different final position.
The quaternion generated in this sample uses the airplane take off sequence: first yaw while taxing, then pitch on the runway, then roll on the air.

phone attitude axis ( Euler angles )
     -Y     Z       altitude X 
      |    /        heading  Y 
      |   /         roll     Z 
  /=======\
  |   | / |
  |   |/  |
  |   *---|--------- X
  |       |
  |   O   |
  \-------/

This app can set rotations using both methods (checkbox "Use quaternion") If you use RotationAngle, you will note that if you move the phone around and then return to original position, the boat is no longer with the same attitude. Bottom line: use quaternion for setting object rotations.

dependencies

Check this tiktok video:

https://vm.tiktok.com/E2eQqP

AirlinerAttitude

AirlinerAttitude app (also in this repository) is a similar sample app, this time featuring a airliner plane model. Phone attitude sensors control:

  • phone true heading rotates the compass disk.
  • airplane yaw is set to roll/2, so it points generally away from the phone, but we can see the plane side as it rolls (like a real plane would do, I suppose).
  • otherwise, airplane pitch and roll same as phone's.

app screenshot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.