# PixAnvil Orientation Control Demo

Er, JavaScript or WebGL doesn't seem to be running, so basically all you're going to see is the bare code...

 // Orientation change var rot=new vec3(0,0,0); if (lib.key['a']) rot.y-=1.0; if (lib.key['d']) rot.y+=1.0; if (lib.key['w']) rot.x+=1.0; if (lib.key['s']) rot.x-=1.0; if (lib.key['q']) rot.z+=1.0; if (lib.key['e']) rot.z-=1.0; // Euler angles by default: var radsPerSec=1.0; var ang=radsPerSec*lib.dt; o.box.rotation.pe(rot.t(ang)); trace("Euler angles: "+o.box.rotation); // Press 't' for quaTernion mode if (lib.key_toggle['t']) { // quaternions if (!o.box.rotationQuaternion) o.box.rotationQuaternion=new BABYLON.Quaternion(); var q=new BABYLON.Quaternion(); BABYLON.Quaternion.RotationAxisToRef(rot,ang,q); o.box.rotationQuaternion.multiplyInPlace(q); o.box.rotationQuaternion.normalize(); trace("Quaternion mode:"); trace("q: "+q); trace("obj: "+o.box.rotationQuaternion); } else o.box.rotationQuaternion=null; // Press 'm' for Matrix mode if (lib.key_toggle['m']) { // Push the Z axis around by the X and Y rotations m.Z.pe(m.X.t(ang*rot.y)); m.Z.me(m.Y.t(ang*rot.x)); // Push the X and Y around by the Z rotation m.X.pe(m.Y.t(ang*rot.z)); m.Y.me(m.X.t(ang*rot.z)); // Orthonormalize m.X=normalize(cross(m.Y,m.Z)); m.Y=normalize(cross(m.Z,m.X)); m.Z=normalize(m.Z); // Output as euler angles (for BABYLON) o.box.rotation = BABYLON.Vector3.RotationFromAxis(m.X, m.Y, m.Z); trace("Matrix mode:"); trace(m.X); trace(m.Y); trace(m.Z); } // Rocket physics: o.P.pe(o.V.t(lib.dt)); if (o.P.z<0.0) { // rockets bounce, right? o.P.z=0.0; o.V.z=0.0; } var Fg=-9.8*o.mass; // gravity force (in +z direction) // Engine physics: var Fe=0.0; // newtons of engine force var drymass=1000.0; if (lib.key_toggle['z'] && o.mass>drymass) { trace("ROCKET ENGINE RUNNING"); var burn_rate=100.0; // kg/sec burn rate var dm=burn_rate*lib.dt; // kg propellant burned this step // Don't burn more propellant than exists... if (o.mass-dmMath.abs(o.V.z)) dVdrag=-o.V.z; // Scalar Z-only physics: o.V.z+=dVdrag+Fg/o.mass*lib.dt; // Vector thrust (only works with matrix mode) o.V.pe(m.Z.t(Fe/o.mass*lib.dt)); // Make the camera follow the rocket around camera.setTarget(o.P); trace("atmosphere="+density); trace("mass="+o.mass); trace("time="+lib.time); trace("P="+o.P); trace("V="+o.V); // Simulation Parameters: // Make an object (looks more like a brick for now) function objSim() { var o=this; o.box = BABYLON.Mesh.CreateBox("box", 0.6, scene); o.box.scaling.x=4; // wing-like (X to tip of wing) o.box.scaling.z=9; var m = new BABYLON.StandardMaterial("texture", scene); o.box.material = m; o.mass=10000.0; // mass in kg o.V=new vec3(0,0,0.0); // stationary o.P=new vec3(0,0,0.0); // on the launchpad o.box.position=o.P; // hook display to sim lib.showAxes(0.4,o.box); } var o=new objSim(); lib.createGround(-4); // Set up object orientation matrix, // initially just to match the world orientation var m={ X:new vec3(1.0,0.0,0.0), Y:new vec3(0.0,1.0,0.0), Z:new vec3(0.0,0.0,1.0) }; // User Interface (UI) code: