Sign in to follow this  
coolroar

Flight Simulation Experiment

Recommended Posts

I have a minimal flight simulator here:

https://www.babylonjs-playground.com/#UL7W2M

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

flightsim1.png.22277080e4f5fcd97b21c4c43a468748.png

 

The code that makes the flight simulation possible:

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

where addYawPitchRollFunction(...) is defined as:

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.

I welcome your comments, corrections, suggestions, & accolades! 😅

Enjoy! - - :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.