Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm just getting into Three.js and the camera is causing some problems. Basically, I want to implement spectator-like movement controls, so that I can move around freely in the scene. The problem is that the camera seems to have it's own coordinate system, so rotating it rotates the camera, while the system itself is left untouched. This, in turn, causes problems when moving the camera.

To better illustrate the problem, please have a look at the scene. Use the arrow keys to look around and press w to move forward.

http://jsbin.com/anarid/1/

Ideally, I'd like to apply the rotation to the 'whole' camera instead, including its coordinate system. I know that Three.js provides controls, but I'd like to implement them myself for learning purposes. I also know that a lot of people do have this problem and that there are a couple posts dealing with this. However, I mostly fail to understand the solutions. If someone could shed some light on this, I'd really appreciate it. :)

Greetings

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You don't move a "whole coordinate system." The world coordinate system is meant to be completely independent of the camera (you might even have more than one camera, after all).

Take your movement vector, apply the camera's world-space rotation, and then add that the camera's world position. It's as simple as that.

That is, if you want to move forward (negative Z for sake of example), you'd have a movement vector <0,0,-1>. If your camera was turned 90 degrees right then after applying the rotation matrix to this vector you'd get <1,0,0>. Now add this vector to the camera's position to move along the positive X axis, which is the way the camera is facing and what you wanted.

share|improve this answer
    
Thanks for the answer. I was able to fix the movement, however, the rotation is still behaving rather unintuitive. With the forward movement fixed, how would I go about fixing the rotational behavior? Right now, you can rotate on the x axis just fine, but if you use y afterwards, it still doesn't work as expected. (Just as in the code above) –  user30340 Jun 18 '13 at 23:51
1  
Sounds like you're just calculating the matrix wrong. Could be one of a dozen things. If you implement your camera as a pair or triplet of vectors, make sure you rotate them all as appropriate, renormalize, and reorthagonalize where needed. That is, if you have forward and up vector, make sure you rotate both when looking up or down. –  Sean Middleditch Jun 18 '13 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.