Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
capnrefsmmat committed Jan 28, 2012
1 parent 3b0e5f3 commit f188696
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
43 changes: 28 additions & 15 deletions relativity/js/relLibs.js
Expand Up @@ -2,6 +2,12 @@


"use strict"; "use strict";


// glMatrix defaults to 32-bit arrays, but we'd like 64-bit arrays
// as we sometimes get nasty rounding errors otherwise.
if (typeof Float64Array !== "undefined") {
glMatrixArrayType = Float64Array;
}

// Convenient constants. // Convenient constants.
var c; // Default value set in scene.defaults var c; // Default value set in scene.defaults
var nullQuat4 = quat4.create([0,0,0,0]); var nullQuat4 = quat4.create([0,0,0,0]);
Expand Down Expand Up @@ -70,24 +76,32 @@ function genEnergy(P,c,m) {
} }


/** /**
* Takes a velocity and a speed of light and returns a boost matrix * Takes a velocity and a speed of light and returns a boost matrix
* A bit less efficient than it should be, create some temporary variables lazy git. :/ *
* * A bit less efficient than it should be, create some temporary variables lazy
* Until that time, here's an explanation. * git. :/
* An acceleration (or boost) in relativity is almost exactly equivalent to a rotation. *
* You can think of a particle/object as moving at the speed of light at all times in the t direction, * Until that time, here's an explanation. An acceleration (or boost) in
* The acceleration 'rotates' that path so that it is moving in the x direction a little bit as well as the y. * relativity is almost exactly equivalent to a rotation. You can think of a
* This matrix implements the appropriate cosh(artanh(B)) and similar values, using linear algebra for speed (although not clarity) * particle/object as moving at the speed of light at all times in the t
* One way to get an idea for how hyperbolic space works is to play with a triangle. Declare the wrong side to be the hypotenuse, and change * direction. The acceleration 'rotates' that path so that it is moving in the x
* The angles. You'll notice that both the opposite and adjacent tend to infinity, so too do x and t intervals for a given proper time (hypotenuse) * direction a little bit as well as the y. This matrix implements the
* appropriate cosh(artanh(B)) and similar values, using linear algebra for
* speed (although not clarity).
*
* One way to get an idea for how hyperbolic space works is to play with a
* triangle. Declare the wrong side to be the hypotenuse, and change the angles.
* You'll notice that both the opposite and adjacent tend to infinity, so too do
* x and t intervals for a given proper time (hypotenuse).
*
* I plan to make a demo of this some time. * I plan to make a demo of this some time.
* *
* NB: Does not yet handle boost in z direction. If you give it a z component it will not work correctly. * NB: Does not yet handle boost in z direction. If you give it a z component it
* will not work correctly.
*/ */
function cBoostMat(boostV, c) { function cBoostMat(boostV, c) {
var gamma = boostV[3] / c; var gamma = boostV[3] / c;
if (gamma - 1 < 0.0000001) if (gamma - 1 < 0.0000001) {
{
return (mat4.create([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])); return (mat4.create([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]));
} }
var bx = boostV[0] / boostV[3]; var bx = boostV[0] / boostV[3];
Expand All @@ -103,7 +117,6 @@ function cBoostMat(boostV, c) {


/** /**
* Take a 3-velocity and return a boost matrix from cBoostMat. * Take a 3-velocity and return a boost matrix from cBoostMat.
*
*/ */
function boostFrom3Vel(vx, vy, vz) { function boostFrom3Vel(vx, vy, vz) {
var gamma = vToGamma([vx, vy, vz]); var gamma = vToGamma([vx, vy, vz]);
Expand Down
46 changes: 17 additions & 29 deletions relativity/js/scene.js
Expand Up @@ -6,31 +6,19 @@
* demo and set of interacting objects. * demo and set of interacting objects.
*/ */
function Scene() { function Scene() {
// glMatrix defaults to 32-bit arrays, but we'd like 64-bit arrays
// as we sometimes get nasty rounding errors otherwise.
if (typeof Float64Array !== "undefined") {
glMatrixArrayType = Float64Array;
}

this.initialTime = new Date().getTime(); this.initialTime = new Date().getTime();


if (typeof FlashCanvas != "undefined") { this.lightConeCanvas = document.createElement('canvas');
if (typeof FlashCanvas !== "undefined") {
FlashCanvas.initElement($('#canvas-2d')[0]); FlashCanvas.initElement($('#canvas-2d')[0]);
FlashCanvas.initElement($('#canvas-minkowski')[0]); FlashCanvas.initElement($('#canvas-minkowski')[0]);
FlashCanvas.initElement($('#canvas-3d')[0]); FlashCanvas.initElement($('#canvas-3d')[0]);
FlashCanvas.initElement(this.lightConeCanvas);
} }


this.g = $('#canvas-2d')[0].getContext("2d"); this.g = $('#canvas-2d')[0].getContext("2d");
this.h = $('#canvas-minkowski')[0].getContext("2d"); this.h = $('#canvas-minkowski')[0].getContext("2d");
this.TDC = $('#canvas-3d')[0].getContext("2d"); this.TDC = $('#canvas-3d')[0].getContext("2d");

this.loaded = false;

this.lightConeCanvas = document.createElement('canvas');
if (typeof FlashCanvas != "undefined") {
FlashCanvas.initElement(this.lightConeCanvas);
}

this.lCCtx = this.lightConeCanvas.getContext('2d'); this.lCCtx = this.lightConeCanvas.getContext('2d');


// fillText was only introduced with Firefox 3.5, and some older browsers do // fillText was only introduced with Firefox 3.5, and some older browsers do
Expand All @@ -49,6 +37,10 @@ function Scene() {
this.timeZoom = 0.25; this.timeZoom = 0.25;
this.t = 0; this.t = 0;
this.keyDown = false; this.keyDown = false;
this.loaded = false;
this.drawing = false;
this.debug = false;
this.audio = new AudioManager();


// The default set of object-specific settings. These can be overridden by // The default set of object-specific settings. These can be overridden by
// a specific demo, and by individual objects in that demo. // a specific demo, and by individual objects in that demo.
Expand All @@ -74,11 +66,7 @@ function Scene() {
"neverShowFramePos": false, "neverShowFramePos": false,
"alwaysShowVisualPos": false, "alwaysShowVisualPos": false,
"neverShowVisualPos": false, "neverShowVisualPos": false,
"interactions": true "interactions": true};
};

this.drawing = false;
this.debug = false;


// Records whether the keys for various actions are currently pressed // Records whether the keys for various actions are currently pressed
// interface.js binds various key events to these actions and toggles their // interface.js binds various key events to these actions and toggles their
Expand All @@ -98,14 +86,9 @@ function Scene() {
"zoomOut": false, "zoomOut": false,
"timeZoomIn": false, "timeZoomIn": false,
"timeZoomOut": false}; "timeZoomOut": false};
this.audio = new AudioManager();
} }


Scene.prototype = { Scene.prototype = {
toggle3D: function() {
this.curOptions.show3D = !this.curOptions.show3D;
},

/** /**
* Load the specified demo at the given step. (The step indexes into * Load the specified demo at the given step. (The step indexes into
* the demo's steps array.) * the demo's steps array.)
Expand Down Expand Up @@ -133,7 +116,7 @@ Scene.prototype = {


// Clone our default object-specific options into curOptions, rather than // Clone our default object-specific options into curOptions, rather than
// getting a reference // getting a reference
this.curOptions = jQuery.extend({}, this.defaults); this.curOptions = $.extend({}, this.defaults);


// If the demo specifies option overrides, apply them // If the demo specifies option overrides, apply them
if (typeof demo.steps[step].options === "object") { if (typeof demo.steps[step].options === "object") {
Expand Down Expand Up @@ -255,9 +238,10 @@ Scene.prototype = {


this.clear(); this.clear();


// Draw the light cone; if we're using flashCanvas, don't use offscreen canvas. // Draw the light cone; if we're using flashCanvas, don't use offscreen
// canvas.
if (typeof FlashCanvas != "undefined") { if (typeof FlashCanvas != "undefined") {
drawLightCone(this,this.h); drawLightCone(this, this.h);
} else { } else {
this.h.drawImage(this.lightConeCanvas, 0, 0); this.h.drawImage(this.lightConeCanvas, 0, 0);
} }
Expand Down Expand Up @@ -467,6 +451,10 @@ Scene.prototype = {
this.replay(); this.replay();
} }
}, },

toggle3D: function() {
this.curOptions.show3D = !this.curOptions.show3D;
},


/** /**
* Reload the current demo from scratch and restart the animation. * Reload the current demo from scratch and restart the animation.
Expand Down Expand Up @@ -535,7 +523,7 @@ Scene.prototype = {


/** /**
* Helper function to draw the scene. Necessary because of the setInterval() * Helper function to draw the scene. Necessary because of the setInterval()
* this problem. * `this` problem.
*/ */
function drawScene(scene) { function drawScene(scene) {
return function (event) { return function (event) {
Expand Down

0 comments on commit f188696

Please sign in to comment.