Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created February 6, 2013 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhulha/4720712 to your computer and use it in GitHub Desktop.
Save rhulha/4720712 to your computer and use it in GitHub Desktop.
NodeInverse
// CLASS
define(["glMatrix", "Spatial"], function(glmat, Spatial){
// a Node is a Spatial without Mesh data
function Node(child)
{
Spatial.call(this, false);
this.name ="";
this.type ="";
this.enabled = true;
this.mvMatrix = mat4.create();
// children inheret the parent matrix for its rotation and position
this.children = [];
if( child)
this.children.push(child);
this.temp2 = mat4.create();
}
var p = Node.prototype = Object.create(Spatial.prototype);
p.add = function(obj) {
this.children.push(obj);
return obj;
}
// overwrite to animate this object
p.animate = function(elapsed)
{
}
p.draw = function(program, parentMVMatrix)
{
mat4.set( parentMVMatrix, this.mvMatrix); // copy the mvMatrix, so we don't change the original
// funky stuff going on below, I don't know why it is needed, but otherwise ship rotation is wrong
// found this code by trial and error
mat4.set( this.matrix, this.temp2);
mat4.inverse(this.temp2);
mat4.copyPosition( this.matrix, this.temp2);
mat4.multiply( this.mvMatrix, this.temp2);
for (var i = 0; i < this.children.length; i++)
{
this.children[i].draw( program, this.mvMatrix);
}
}
return Node;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment