I have the following scene:
And I have two 3D vectors, the camera position, and the focus position. The focus vector is always behind the monster. I know how to rotate the camera around the focus with the help of my mouse, but the monster is always facing the same direction, and I'd like to change that!
I'd like it to work almost as in World of Warcraft; while I'm standing still I can move the camera around freely, but as soon as I move, the monster should change the direction according to the camera.position >>> focus
vector. Also, I need to make it move in that direction, I guess I could have a 2D vector called speed
, which I should change accordingly (it would look like a rotating radius of a circle I guess).
How do I do this?
For reference, I'm working in Three.js, and this is the code with which I rotate the camera:
onDocumentMouseDown = function(event)
{
event.preventDefault();
game.isMouseDown = true;
game.onMouseDownTheta = game.theta;
game.onMouseDownPhi = game.phi;
game.onMouseDownPosition.x = event.clientX;
game.onMouseDownPosition.y = event.clientY;
}
onDocumentMouseMove = function(event)
{
event.preventDefault();
if (game.isMouseDown)
{
game.theta = -((event.clientX - game.onMouseDownPosition.x) * 0.5) + game.onMouseDownTheta;
game.phi = ((event.clientY - game.onMouseDownPosition.y) * 0.5) + game.onMouseDownPhi;
game.phi = Math.min(160, Math.max(20, game.phi));
game.cameraBuff.x = game.radious * Math.sin(game.theta * Math.PI / 360 ) * Math.cos( game.phi * Math.PI / 360 );
game.cameraBuff.y = game.radious * Math.sin(game.phi * Math.PI / 360 );
game.cameraBuff.z = game.radious * Math.cos(game.theta * Math.PI / 360 ) * Math.cos( game.phi * Math.PI / 360 );
}
}
And at render time I do this:
self.camera.position.x += self.cameraBuff.x;
self.camera.position.y += self.cameraBuff.y;
self.camera.position.z += self.cameraBuff.z;
self.camera.lookAt(self.focus);
self.renderer.render(self.scene, self.camera);
self.camera.position.x -= self.cameraBuff.x;
self.camera.position.y -= self.cameraBuff.y;
self.camera.position.z -= self.cameraBuff.z;
direction = normalize(position - focus)
should work. \$\endgroup\$