# Flight Simulation Experiment

## Recommended Posts

I have a minimal flight simulator here:

and at my website jounce.space/flightsimhome that can be full-screened (f11) and has skybox as shown here:

The code that makes the flight simulation possible:

``````var camera = new BABYLON.UniversalCamera("cam",  BABYLON.Vector3.Zero(), scene);

``````function addYawPitchRollFunction(ob, gravity) {
ob.rotationQuaternion  = new BABYLON.Quaternion();
ob.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(ob.rotation.y, ob.rotation.x, ob.rotation.z);
ob.myGrav = gravity;

ob.yawPitchRoll = function(yaw, pitch, roll) {
var axis = new BABYLON.Vector3(0, 0, -1);
var partRotQuat  = new BABYLON.Quaternion();

BABYLON.Quaternion.RotationAxisToRef(axis, roll, partRotQuat);
this.rotationQuaternion.multiplyInPlace(partRotQuat);

BABYLON.Quaternion.RotationAxisToRef(axis.set(-1, 0, 0), pitch, partRotQuat);
this.rotationQuaternion.multiplyInPlace(partRotQuat);

BABYLON.Quaternion.RotationAxisToRef(axis.set(0, 1, 0), yaw, partRotQuat);
this.rotationQuaternion.multiplyInPlace(partRotQuat);

this.rotationQuaternion.toEulerAnglesToRef(this.rotation);
}

ob.autoBank = function(yaw, pitch, speed) {
var axis = new BABYLON.Vector3(0, 0, -1);
var partRotQuat  = new BABYLON.Quaternion();

var roll = Math.atan2(-yaw*222*speed, this.myGrav);
this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, roll);

BABYLON.Quaternion.RotationAxisToRef(axis.set(-1, 0, 0), pitch, partRotQuat);
this.rotationQuaternion.multiplyInPlace(partRotQuat);

BABYLON.Quaternion.RotationAxisToRef(axis.set(0, 1, 0), yaw, partRotQuat);
this.rotationQuaternion.multiplyInPlace(partRotQuat);

this.rotationQuaternion.toEulerAnglesToRef(this.rotation);
}

ob.clearYawPitchRoll = function() {
this.rotationQuaternion.set(0, 0, 0, 1);
this.rotation.set(0,0,0);
}
}
``````

Now you can add increments of yaw, pitch, & roll to camera's orientation:

``camera.yawPitchRoll(yawInc, pitchInc, rollInc);``

Or for automatically coordinated turns:

``camera.autoBank(yawInc, pitchInc, speed);``

Look at https://www.babylonjs-playground.com/#UL7W2M as a complete example.

Enjoy! - -

really nice

##### Share on other sites

Thanks.

Well, some of us are not overwhelmed by the EXUBERANT colors 👍!

Excellent!

Ohh very cool 👍