Skip to main content
gamedev.net
🔒 Locked

OpenGl Camera Setup

Started by t_man700 Apr 5, 2011 at 12:02 PM 1 replies 1.7k views
Original Post
t_man700
t_man700
Hey guys, new here and first post. Awesome site

Anyways, I have a quick question about how to setup a camera that moves forward at all times and when you hit left or right on the keyboard for example, the camera will start turning left or right so that you could go in a full 360 if you so choose (like driving a car for example). It can (to be simple) stay on the same plane on the Y-axis at all times (doesn't have to move up or down).

I'm using GLUT, and I am setting the camera at each frame using gluLookAt(); If anyone can give me some tips on how to do this I would greatly appreciate it.

Thanks.
haegarr
haegarr
Hi, be welcome.

I suggest you to drop the usage of GLUT. Instead, define a transformation matrix. Increment the rotational part of the matrix using its up vector as rotation axis and an incrementation angle (positive for turning to the one side, negative for the other side), perhaps scaled by the latest time delta. Pick the new forward vector, scale it by the speed and time delta, and add that to the positional part of the matrix. Use the inverse of that matrix as next VIEW matrix portion.
Solano
Solano

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xf, yf, zf, xf + lx, yt, zf + lz, 0, 1, 0);

if keyboard_check(vk_left){
dir -= 1
lx = sin(degtorad(dir))
lz = -cos(degtorad(dir));
}
if keyboard_check(vk_right){
dir += 1
lx = sin(degtorad(dir))
lz = -cos(degtorad(dir));
}
//primitive drawing functions here

xf yf zf represents where you are looking for
yf is the upvector so if youre used to x y z now it will be x z y get it ?
yt is the height of the point you are looking at.
hope that helps

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.