Jump to content

Flight Simulation Experiment


coolroar
 Share

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

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...