Skip to content

Commit

Permalink
added some properties
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schuetz committed Mar 28, 2014
1 parent 89399df commit 2905691
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 66 deletions.
87 changes: 36 additions & 51 deletions src/scenegraph/SceneNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,35 +270,25 @@ SceneNode.prototype.yaw = SceneNode.prototype.rotateY;
SceneNode.prototype.pitch = SceneNode.prototype.rotateX;
SceneNode.prototype.roll = SceneNode.prototype.rotateZ;

///**
// * rotation around the up vector
// */
//SceneNode.prototype.yaw = function(angle){
// var pos = this.localPosition;
// this.translate(-pos.x, -pos.y, -pos.z);
// this.rotate(angle, this.getUpVector());
// this.translate(pos.x, pos.y, pos.z);
//};
//
///**
// * rotation around the side vector
// */
//SceneNode.prototype.pitch = function(angle){
// var pos = this.localPosition;
// this.translate(-pos.x, -pos.y, -pos.z);
// this.rotate(angle, this.getSideVector());
// this.translate(pos.x, pos.y, pos.z);
//};
//
///**
// * rotation around the view direction
// */
//SceneNode.prototype.roll = function(angle){
// var pos = this.localPosition;
// this.translate(-pos.x, -pos.y, -pos.z);
// this.rotate(angle, this.getLocalDirection());
// this.translate(pos.x, pos.y, pos.z);
//}
Object.defineProperty(SceneNode.prototype, "yaw",{
set: function(value){
var yaw = this.getYaw();
this.rotateY(value-yaw);
},
get: function(){
return this.getYaw();
}
});

Object.defineProperty(SceneNode.prototype, "pitch",{
set: function(value){
var pitch = this.getPitch();
this.rotateX(value-pitch);
},
get: function(){
return this.getPitch();
}
});

/**
* rotation around arbitrary vector
Expand Down Expand Up @@ -355,29 +345,24 @@ SceneNode.prototype.getPitch = function(){
return pitch;
};

/**
* Set point of view
* position: x,y,z
* orientation: yaw, pitch, roll
*/
SceneNode.prototype.setPOV = function(x, y, z, yaw, pitch){
var t = M4x4.clone(M4x4.I);
this.transform = t;
// this.roll(roll);
this.pitch(pitch);
this.yaw(yaw);
this.translate(x,y,z);
};

SceneNode.prototype.getPOV = function(){
var pov = {
pos: this.localPosition,
yaw: this.getYaw(),
pitch: this.getPitch()
}
Object.defineProperty(SceneNode.prototype, "pointOfView", {
set: function(value){
this.resetTransformation();
this.pitch += value.pitch;
this.yaw += value.yaw;
this.localPosition = value.pos;
console.log(this.yaw);
},
get: function(){
var pov = {
pos: this.localPosition,
yaw: this.yaw,
pitch: this.pitch
}

return pov;
}
return pov;
}
});



Expand Down
32 changes: 17 additions & 15 deletions test/test_scene_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ test("euler rotations", function(){

// repeatedly call yaw(1) and check if getYaw() returns the correct value
for(var i = 0; i < 10; i++){
cam.yaw(1);
ok(V3.equalScalar(cam.getYaw(), (i+1) % (2*Math.PI), eps), "yaw: " + cam.getYaw());
// cam.yaw(1);
cam.yaw += 1;
ok(V3.equalScalar(cam.yaw, (i+1) % (2*Math.PI), eps), "yaw: " + cam.getYaw());
ok(V3.equalScalar(cam.getPitch(), 0, eps), "pitch: " + cam.getPitch());
}

// test pitch
cam.resetTransformation();
cam.pitch(1);
cam.pitch += 1;
ok(V3.equalScalar(cam.getPitch(), 1, eps), "pitch: " + cam.getPitch());

// doing a pitch with value 1 two times will result in a pitch of PI -2
Expand All @@ -102,23 +103,24 @@ test("euler rotations", function(){
// /|
// / |
//
cam.pitch(1);
cam.pitch += 1;
ok(V3.equalScalar(cam.getPitch(), Math.PI - 2, eps), "pitch: " + cam.getPitch());
ok(V3.equalScalar(cam.getYaw(), Math.PI, eps), "pitch: " + cam.getPitch());
ok(V3.equalScalar(cam.yaw, Math.PI, eps), "pitch: " + cam.getPitch());
});

test("setPOV", function(){
var cam = new SceneNode("cam");
cam.setPOV(1,2,3, Math.PI/2, 0.2);
var pov = cam.getPOV();
ok(V3.equalScalar(pov.yaw, Math.PI/2, eps), "");

cam.pointOfView = {pos: [1,2,3], pitch: 0.2, yaw: 1.5};
var pov = cam.pointOfView;
ok(V3.equalScalar(pov.yaw, 1.5, eps), "yaw: " + pov.yaw);
ok(V3.equalScalar(pov.pitch, 0.2, eps), "pitch: " + cam.getPitch());
ok(V3.equal(pov.pos, [1,2,3], eps), "");

cam.setPOV(0,0,0, 0.3, -0.1);
pov = cam.getPOV();
ok(V3.equalScalar(pov.yaw, 0.3, eps), "");
ok(V3.equalScalar(pov.pitch, -0.1, eps), "pitch: " + cam.getPitch());
cam.pointOfView = {pos: [0,0,0], pitch: -0.7, yaw: 6};
var pov = cam.pointOfView;
ok(V3.equalScalar(pov.yaw, 6, eps), "");
ok(V3.equalScalar(pov.pitch, -0.7, eps), "pitch: " + cam.getPitch());
ok(V3.equal(pov.pos, [0,0,0], eps), "");

});
Expand All @@ -136,10 +138,10 @@ test("position property", function(){
ok(V3.equal(palm.globalPosition, [10, 100, 30], eps));
ok(V3.equal(coconut.globalPosition, [11, 95, 31], eps));

coconut.globalPosition = [10, 11, 12];
coconut.globalPosition = [10, 95, 30];
ok(V3.equal(palm.globalPosition, [10, 100, 30], eps));
ok(V3.equal(coconut.globalPosition, [10, 11, 12], eps));
ok(V3.equal(coconut.localPosition, [0, -89, -18], eps));
ok(V3.equal(coconut.globalPosition, [10, 95, 30], eps));
ok(V3.equal(coconut.localPosition, [0, -5, 0], eps));

});

Expand Down

0 comments on commit 2905691

Please sign in to comment.