First Person Camera
Hi,
I've got the first person camera working in the "walking around" sense. What I want to do now is create a camera that can rotate side to side and up/down; then move in the direction it is facing.
I'm guessing this isn't much harder (or even easier) but I'm just looking for some tips before I do it the wrong way first. Any help is appreciated; thanks!
It is a fairly simple process:
1 - Get your mouse coordinates around a reference point (IE: The center of the screen).
2 - Measure the distance the mouse moved from that point.
3 - Rotate the camera accordingly by the axis the mouse moved (IE: If you move the mouse up, rotate around the X-axis, if the mouse moves sideways, use the Y-axis).
4 - If you are using DirectX, your camera has a position vector and a target vector, in order to move your camera in the direction you are looking, you need to get a vector in the form direction = (target - position, transform it by your rotation matrix, normalize it, and set the position to be (position + (direction * speed)), and the target to be (target + (direction * speed)).
Note that your camera will move according to how many FPS you get, which is not a good thing for a serious game or project (you will have many jumps if you do so), but this is pretty much a basic method to get a FPS camera going.
Hope this helps!
Asterisk Man.
1 - Get your mouse coordinates around a reference point (IE: The center of the screen).
2 - Measure the distance the mouse moved from that point.
3 - Rotate the camera accordingly by the axis the mouse moved (IE: If you move the mouse up, rotate around the X-axis, if the mouse moves sideways, use the Y-axis).
4 - If you are using DirectX, your camera has a position vector and a target vector, in order to move your camera in the direction you are looking, you need to get a vector in the form direction = (target - position, transform it by your rotation matrix, normalize it, and set the position to be (position + (direction * speed)), and the target to be (target + (direction * speed)).
Note that your camera will move according to how many FPS you get, which is not a good thing for a serious game or project (you will have many jumps if you do so), but this is pretty much a basic method to get a FPS camera going.
Hope this helps!
Asterisk Man.
What camera system are you using, do you represent the camera simply with a position, target and up vector passing them to gluLookAt or D3DXMatrixPerspectiveFOV. Or do you have a UVN camera system?
How are you doing your camera now? The kind of 6dof Descent-style motion you're describing can be quite a bit more difficult than an FPS camera if you're new to 3d math. You pretty much have to abandon Euler angles, at least as far as the 'construct the matrix from scratch every frame' approach goes.
Once you get a few concepts down, though, it's really pretty easy. Basically, you'll need to be able to:
1. Extract direction vectors from a matrix
2. Create an arbitrary axis-angle matrix
3. Multiply two matrices
4. Orthogonalize a matrix
Unless I've forgotten something, that should give you the tools to create the kind of motion you're looking for.
You can actually do the same thing with only vectors and trig. There's a good document online about this approach, but for some reason my google search isn't turning it up at the moment...
Once you get a few concepts down, though, it's really pretty easy. Basically, you'll need to be able to:
1. Extract direction vectors from a matrix
2. Create an arbitrary axis-angle matrix
3. Multiply two matrices
4. Orthogonalize a matrix
Unless I've forgotten something, that should give you the tools to create the kind of motion you're looking for.
You can actually do the same thing with only vectors and trig. There's a good document online about this approach, but for some reason my google search isn't turning it up at the moment...
Some good tips; but a bit more advanced than what I'm looking for I think. All I'm doing is rendering a scene; then I need the ability to move about it and look at it from every angle. This isn't neccessarily going to be on the fly changes; the path is going to be built into the program.
All I really need is a way to add the ability to rotate the view upwards to the orientation function; which stands now as
I have a new variable 'theta' which tracks the amount of rotation up or down.... is there a quick way to add this in? And then maybe simply change the height of the camera? I can't seem to find a good example of this sort of thing; but I'll keep looking. Thanks again!
All I really need is a way to add the ability to rotate the view upwards to the orientation function; which stands now as
void orientMe(float ang) { lx=sin(ang); lz=-cos(ang); glLoadIdentity(); gluLookAt(x,y,z,x+lx,y+ly,z+lz,0.0,1.0,0.0); glutPostRedisplay();}void moveMeFlat(int direction) { if(checkX(direction)) x=x+direction*(lx); if(checkZ(direction)) z=z+direction*(lz); cx=x;cz=z; glLoadIdentity(); gluLookAt(x,y,z,x+lx,y+ly,z+lz,0.0,1.0,0.0);}
I have a new variable 'theta' which tracks the amount of rotation up or down.... is there a quick way to add this in? And then maybe simply change the height of the camera? I can't seem to find a good example of this sort of thing; but I'll keep looking. Thanks again!
I really would look into implementing your camera using a UVN system, it's not that hard to implement, I don't have time to post some code now gotta dash to watch liverpool thrash AC Milan (hopefully :) !) But will try and post some code l8er.
It will simplify the movement of the camera, and get rid of gimbal lock. It may take a bit longer to implement but you can put it in a class and use it time and time again, you won't regret it I promise!
It will simplify the movement of the camera, and get rid of gimbal lock. It may take a bit longer to implement but you can put it in a class and use it time and time again, you won't regret it I promise!
Ok, here's some example code for an FPS-style cam:
float sy = sin(yaw);
float cy = cos(yaw);
float sp = sin(pitch);
float cp = cos(pitch);
// For movement up and down and in the xz plane
Vector3 forward(sy, 0, -cy);
Vector3 side(cy, 0, sy);
Vector3 up(0, 1, 0);
// For gluLookAt()
Vector3 eye = camera.position;
Vector3 view(sy * cp, sp, -cy * cp);
Vector3 target = eye + view;
gluLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);
I didn't include input, updating the camera position, etc., but all the stuff you need is there. Note that the forward vector as given above will move you in the xz plane. If you want to be able to 'fly around' like in Q3 spectator mode, simply replace the forward vector with the view vector.
To update your camera's position, simply add the appropriate direction vector, scaled by speed and delta time, to your camera's position.
The above is untested, so I can't guarantee it'll work. But I think it's close to what you're looking for.
float sy = sin(yaw);
float cy = cos(yaw);
float sp = sin(pitch);
float cp = cos(pitch);
// For movement up and down and in the xz plane
Vector3 forward(sy, 0, -cy);
Vector3 side(cy, 0, sy);
Vector3 up(0, 1, 0);
// For gluLookAt()
Vector3 eye = camera.position;
Vector3 view(sy * cp, sp, -cy * cp);
Vector3 target = eye + view;
gluLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);
I didn't include input, updating the camera position, etc., but all the stuff you need is there. Note that the forward vector as given above will move you in the xz plane. If you want to be able to 'fly around' like in Q3 spectator mode, simply replace the forward vector with the view vector.
To update your camera's position, simply add the appropriate direction vector, scaled by speed and delta time, to your camera's position.
The above is untested, so I can't guarantee it'll work. But I think it's close to what you're looking for.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement