Playing around with classes / prototypes. Basically I'm trying to get a 3d cube to face the direction the mouse is in. I think it's almost there, but right now it's not rendering the cube, which it was doing fine before I added this code:
cube.look(xTarget, yTarget);
which is giving this error:
Uncaught TypeError: Cannot read property 'look' of undefined`
What am I doing wrong here? Why is it unable to access the var cube
? (which is what I think the problem is).
Here's the relevant js:
Cube.prototype.updateBody = function(speed){
this.box.rotation.y += (this.tBoxRotY - this.box.rotation.y) / speed;
this.box.rotation.x += (this.tBoxRotX - this.box.rotation.x) / speed;
this.box.position.x += (this.tBoxPosX-this.box.position.x) / speed;
this.box.position.y += (this.tBoxPosY-this.box.position.y) / speed;
this.box.position.z += (this.tBoxPosZ-this.box.position.z) / speed;
}
Cube.prototype.look = function(xTarget, yTarget){
this.tBoxRotY = rule3(xTarget, -200, 200, -Math.PI/4, Math.PI/4);
this.tBoxRotX = rule3(yTarget, -200,200, -Math.PI/4, Math.PI/4);
this.tBoxPosX = rule3(xTarget, -200, 200, 70,-70);
this.tBoxPosY = rule3(yTarget, -140, 260, 20, 100);
this.tBoxPosZ = 0;
}
function loop() {
render();
var xTarget = (mousePos.x-windowHalfX);
var yTarget= (mousePos.y-windowHalfY);
console.log('Mouse X position: ' + xTarget +', Y Target = '+yTarget );
cube.look(xTarget, yTarget);
requestAnimationFrame(loop);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire