Archived

This topic is now archived and is closed to further replies.

Nairou

Camera movement

Recommended Posts

Ok I''m working on a free-flying camera implementation. I''ve got camera rotations working fine using quaternions. No yaw/pitch/roll values anywhere, just pure quaternions and matrices. But now I''m trying to get camera movement to work. Basically I need to take the camera''s current world position, move X units in the direction its facing, and calculate the new x,y,z world coordinates. My best attempt so far is this:
D3DXVec3TransformNormal(&MovementVector, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), &CameraHeadingMatrix);
CameraPosition.x -= MovementVector.x * FrameTime;
CameraPosition.y -= MovementVector.y * FrameTime;
CameraPosition.z += MovementVector.z * FrameTime;
D3DXMatrixTranslation(&CameraPositionMatrix, CameraPosition.x, CameraPosition.y, CameraPosition.z);
Where CameraHeadingMatrix is the rotation matrix from the quaternion, and CameraPosition is the world position of the camera. However this only partly works. For certain directions or angles, the signs get screwed up and start moving in the opposite direction, and in some cases the movement isn''t even straight, it tends to move off to an angle. It just feels like a bad hack to me. So what is the proper way to do camera movement with quaternions? I''ve been working on this all day, doing numerous forum searches and tests, so any ideas and suggestions would be greatly appreciated!

Share this post


Link to post
Share on other sites
In 3D games, the camera is at point (0, 0, 0) staring straight down the z axis. The camera does not move. The actual purpose of the view matrix is not to move the camera to the position and orientation in world space you want it to be in, it is to actually move the world to the camera.

For this reason, you need to build the opposite matrix of what you are currently making. TO fix your problem simply negate all your values including rotation angles and translation points. If you do that, it should fix most, or all, of your problems.

Share this post


Link to post
Share on other sites
I suppose a stupid question, but:

Why not move the camera? What is the big disadvantage of moving the camera and what is the advantage of ''moving the world'' instead? I am a bit confused here...

Please keep in mind I am just a beginner

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There is no camera. Or rather if you want to think there is a camera, it is your monitor. Now if you want to move the camera, pick up your monitor and move it across the room. Does the view change? No. All you have to work with is the view matrix. When you change the matrix you are simply changing how objects in the world space are translated. It can give you the illusion of having a camera that is moving though a virtual space, but it is only an illusion.

Share this post


Link to post
Share on other sites
I am sorry, but I am still confused...

''walking/driving/flying/moving'' through a world is being accomplished by changing the world matrix, right? The view matrix will only be set at the beginning of the ''3d viewing'' to set its looking at point, the up vector, and its location(0,0,0). After that the view matrix does not change. Walking through a world is being accomplished by translating/rotating the world matrix...


?????

Share this post


Link to post
Share on other sites
boontje : World, View, Projection matrices don''t mean anything, in fact. We could as well call them A, B, C. All we have to know is that each vertex is multiplied by A, then B, then C.

We use A as a "world" matrix because this is the one we use when we want to take a model from it''s "object space" (centered at 0, looking down z-axis, up being y-axis, and with a certain scale) and put it where it needs to be, with correct scale and rotations. Hopefully, A does not depend on the eye location.

We use B as a "view" matrix : there exists a matrix, which we''ll call M, which takes the camera from it''s own object space and puts it where we want it to be. Problem : we need to modify vertex coordinates, not the camera. Since moving the camera forward, for instance, is the same as moving everything else backwards, then B will be the INVERSE of M. Hopefully, B does not depend on other object''s position.

We use C as a "projection" matrix because, once everything is setup correctly around the origin, we need to project vertices on a plane (or cube).

Nairou: how I dealt with this was to create a generic "location" object, which can me moved around, rotated, rescaled, etc... using matrix multiplications : it owns a world matrix you can use for your objects. Then, it becomes easy to turn one of these locations into a camera : get the inverse of it''s matrix, and voilà your view matrix.

ToohrVyk

Share this post


Link to post
Share on other sites