Camera Question

Started by
21 comments, last by Jason2Jason 21 years, 11 months ago
Hi, i know this has been asked many time, and I have done a search, but thought I''d understand the answers more if they related to my situation. I''ve made a camera class that works fine, even the rotating it fine how it should. But how do you make the camera move (or the scene if u want) in the direction your facing. I warn you I''m not very good with this vector stuff yet! Another thing: I saw a eg that centres the mouse pos, then get the new pos, to make a vector to go in the direction (gametutorials.com), but I''m using DX input, so thats not quite the same since it (in my program) add and subtracts to a variable. Just thought I''d say. If you need any of the code just say, but its all basic stuff at the moment. Thx, -J
Advertisement
if you know how many degrees you are rotated then
glTranslatef( r*cos(angle), 0.0f, r*sin(angle) );
should get you where you want to be.
angle is your rotation, r is how far you want to move.
0 degrees means the camera is facing along the x-axis.
so if you have rotated 45 degrees and want to move 10 units
you will
glTranslatef( 7.0711f, 0.0f, 7.0711f );
That didn't work at all! anyother ideas?

thx,

-J

EDIT: Heres my code for the camera (theres more, but this actully does the work):


        	glRotatef(Roll,		0.0,0.0,1.0);	glRotatef(Pitch,	1.0,0.0,0.0);	glRotatef(Heading,      0.0,1.0,0.0);	glTranslatef(Posit[0]*(float)sin(Heading), Posit[1], Posit[2]*(float)cos(Heading));        



[edited by - jason2jason on May 21, 2002 2:57:01 PM]
yeah figure out your view vector (view coords - position coords)

view vector = (x,y,z)

you should normalize the view vector (do a google search on normalizing vectors). it's basically dividing all the terms by the lenght of the vector which is sqrt(x^2 + y^2 + z^2)

give yourself a speed at which you'd like the camera to move = s

velocity - (x * s, y * s, z * s)

add velocity to position vector

new position = (p.x + x * s, p.y + y * s, p.z + z * s)

that'll do a constant speed per frame which is bad. to be better about it you want framerate independant velocity for which you would use:

p.x = x * s * timeSinceLastLoop + p.x;

then the same for y and z coords

framerate indepentant velocity assums you have implemented some sort of timing mechanism to figure out how long it's been since the last game loop ran. you can use that number for all sorts of useful things like FPS calculations. so if you haven't implemented that you can do the frame dependant movement to get a leg up and at least be able to see your camera moving.

-me

[edited by - Palidine on May 21, 2002 3:01:38 PM]
Right.... I just got lost in all the math there!! Is there an english version of what you said. Sorry, but we''ve only just covered the basics of cos sin and tan at skool, we haven''t done all this advanced stuff yet! I''m only 15! Is there a tutorial that is clear that explains vectors AND how to uses them in practise, like with a camera?

thx,

-J
http://www.gametutorials.com/Tutorials/OpenGL/OpenGL_Pg1.htm

Go through all (yes, ALL) of the camera tutorials veeery slowly, and you''ll be damned glad you did. =)
Not only will it give you a fully functional camera class to work with (extremely easy to use and modify), but you''ll get a great understanding of vectors and their uses.

I''m 14, while I don''t understand every line of it (not used to using trig functions yet) it''s helped me a ton.
_______________________________________Pixelante Game Studios - Fowl Language
heh. sorry.

ok. so first i assume you''re using gluLookAt. which takes view, up and position coordinates, yes? or are you using a different way to position the camera?

if you are using gluLookAt, then you are storing the view up and position coods in some camera struct presumably.

a vector (in 3D space) is just 3 numbers (x,y,z) sometimes you''ll see (x,y,z,w) but ignore w for now. a vector can be just a point or it can be an arrow pointing from one point to another. if it''s an arrow then the x,y,z coords are the x,y,z coords of the tip of the arrow if the base of the arrow is at 0,0,0 (the origin). it''s a way of defining a direction in 3D space

so basically you can get an arrow type of vector form your view coords and your position coords by just subtracting the position from the view coords:

viewVector = (v.x - p.x, v.y - p.y, v.z - p.z)

see how you still get 3 numbers out of that? an (x,y,z) triplet? that basically tells you which DIRECTION you are facing.

then basically you want to give your camera a nudge in that direction. the easiest way to do that is say How many units to i want to move in that direction. that''s your speed. lets say you want the speed to be 5 units

so, you know your position (p.x, p.y, p.z). and you know your view vector (v.x - p.x, v.y - p.y, v.z - p.z). and you know your speed b/c we just defined it = 5.

so 2 steps:

1) make a velocity vector. which will also be an x,y,z number. it says move me x units on the x axis, y units on the z axis and z units on the z axis. to get that:

a. normalize your view vector. basically make the length of the bitch = 1. take each of the x,y,z coordinated and divided them by the number sqrt(x^2 + y^2 + z^2) where x,y,z in that equation are equal to each of the calculations (v.x - p.x, v.y - p.y, v.z - p.z)

b. multiply your "normalized" viewVector by your speed. to do that you multiply each term of the vector by the speed. the result will be your velocity vector. (velocity is a physics word for speed that means speed in a certain direction)


2) add your velocity vector to your current position vector. to add 2 vectors you add each of the terms seperately
Vector 1 = (1,2,3)
Vector 2 = (4,5,6)

Vector 1 + Vector 2 = (1 + 4, 2 + 5, 3 + 6)

the answer of your position plus the velocity vector is your new camera position translated speed units in the direction that you were facing.


an example:
view coords: (4,4,4)
position coords (5,5,5)
speed = 6

calculate the viewVector:
viewVector = (4-5, 4-5, 4-5) = (-1, -1, -1)

normalize the viewVector:
length of viewVector = sqrt(1^2 + 1^2 + 1^2) = sqrt(3)
viewVector = (-2/sqrt(3), -2/sqrt(3), -2/sqrt(3))

multiply viewVector times the speed:
viewVector = (6 * -2/sqrt(3), 6 * -2/sqrt(3), 6 * -2/sqrt(3))

add your position to the velocity:
newPosition = (5 + 6 * -2/sqrt(3), 5 + 6 * -2/sqrt(3), 5 + 6 * -2/sqrt(3))

make your camera position = newPosition.

you are done.

if you''re using gluLookAt you''ll have to move your view coords the same way. to do that, when you get to the add your position to the velocity make another calculation to add the velocity to your view coords to get the new view coods.

-me
Correct me if I''m wrong, but isn''t a vector defined as 2 sets of £D coord, one is a point, the other indicates a direction. I''m not using glulookat() function. I''m currently making a vector class which I''ve just started because if all this. Would u recomend any other functions to put in it, or just the stuff u said just now?

thx,

-J
well, not entirely sure of technical definitions, but if you''re using a 4 member vector the w parameter indicates if it''s a point or a direction. w = 0 for one 1 for the other (i always forget which).

.normalize() is a good function (dividing by the vector length).

i''d look up "3d math tutorial" on google for some basic 3D math stuff. and as LockePick posted above take ALL those camera tuts. it''ll give you a much better practical understanding of what''s going on and how to implement it.

also search around in articles & resources section of this site for more math tuts you can take.

-me
thx. those camera tutorial are wot got me into this mess! I would of used them if the input system wasn''t different. I''ll go look for some £D math stuff, and come back with more problems l8r!

-J

This topic is closed to new replies.

Advertisement