# Camera Movement Math problem

This topic is 2015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to get my first moving projection (camera) and I'm having some troubles with moving left and right it seems to not be as easy as +90 degrees, Okay I'll jump into the details of what this means. The camera_angle contains the angles from -180 through to 180, moving the mouse a pixel to the left will cause the angle to decrease. the camera_position is the position which the camera is located. Simple really, probably didn't even need to explain.

Does anyone have a reason why adding 90 will not work? or as least correctly. Thank you for any help possible

if(GetAsyncKeyState(0x57)){
// W
camera_position.x += sin(camera_angle.x * PI  / 180) * 2 - 1;
camera_position.z += cos(camera_angle.x * PI  / 180) * 2 - 1;
}
if(GetAsyncKeyState(0x41)){
// A
camera_angle.x -= 90;
camera_position.x += sin(camera_angle.x * PI  / 180) * 2 - 1;
camera_position.z += cos(camera_angle.x * PI  / 180) * 2 - 1;
camera_angle.x += 90;
}
if(GetAsyncKeyState(0x53)){
// S
camera_position.x -= sin(camera_angle.x * PI  / 180) * 2 - 1;
camera_position.z -= cos(camera_angle.x * PI  / 180) * 2 - 1;
}

if(GetAsyncKeyState(0x44)){
// D
camera_angle.x += 90;
camera_position.x += sin(camera_angle.x * PI  / 180) * 2 - 1;
camera_position.z += cos(camera_angle.x * PI  / 180) * 2 - 1;
camera_angle.x -= 90;
}


##### Share on other sites

Normally you just read the forward and right-facing vectors from the camera matrix rather than use the angle.

And why are you multiplying the sin, cos results by 2 and subtracting 1?

##### Share on other sites

I have no real knowledge of how a matrix works, since my school didn't want to go over it. (I am learning it witch Khan Academy)
the * 2 - 1 comes from when I used it to look around, by moving a point around the camera position based on the angle.

##### Share on other sites

Well first I'd get rid of the *2-1 thing, it's wrong.

You can just read the upwards, face and right vectors directly from the rows of the camera matrix. Have a look in the debugger at the values and you will see.

##### Share on other sites

I've removed them. for some reason * 2 - 1 made the W key move in the direction it should of now it moves as if the angle was 25 degree's off.

I use the camera_lookat and camer_position to create the view matrix though, how can I use it's values?

Edited by Joshhua5

##### Share on other sites

Have you looked at the values in the camera matrix in the debugger? One of the rows will be the right facing vector, another will be the forward facing vector and another will be the upwards facing vector. Which is which depends upon which way your objects are oriented in their local reference frame, if they face along the Z axis with Y up, the first row will be the right vector, the second will be the up vector and the third will be the forward vector. If you use column major matrices instead of row major, you will probably need to look at the columns instead. You may have to replace right with left if you have a left handed coordinate system as well.

The last row will be the position of the camera.

But reading values directly from the camera matrix will enable you to get rid of the trig you are doing and everything will be easy peasy.

##### Share on other sites

It's
-1  0  0
0  1  0
0  0 -1

so If I has a normalized vector of where' I'm looking it would be
x 0 0
0 y 0
0 0 z

?

##### Share on other sites

No, it would be (for looking along Z axis, Y axis is up):

look direction := 3rd row = (0, 0, -1)

up direction := 2nd row = (0, 1, 0)

right vector := 1st row = (-1, 0, 0)

although you may have to swap the rows around and/or negate them depending on how your objects are oriented in their local space.

EDIT: It looks like you are looking down the camera's -Z axis and up in your world is +Y. I'm guessing maybe the object is at the origin and your camera is at (0, 0, 100) or something?

##### Share on other sites

So the look direction is the normalized vector for the angle?
What's the right vector?

The object is at 0 0 0 (Well it's imported so it kind of depends on the models local space, if it all started at 100 then it would be in a different position.

the camera starts at 0, 0, -10

##### Share on other sites

The look direction is which way you move to get nearer to the object (i.e. the normalised vector from the camera to the target: normalize(targetpos - campos)). The right vector is the direction you move to move the camera right and left relative to the object you are looking at. You can read both from the rows of the camera matrix, you just need to change the position of the camera, write down the matrix that is generated by the lookat function, and work it out from there (try positioning the camera at (0, 0, pos), (0. 0 -pos), (0, pos, 0), (0, -pos, 0), etc.). Well bedtime for me now ;) Good luck.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013228
×