opengl camera class not working

Started by
-1 comments, last by speciesUnknown 17 years ago
HI, Im re-writing the camera class for my game engine since the one im currently using is from somebody else and i cant GPL it with the rest of the engine. The camera is misbehaving! It seems to work fine, but as i move it forward and backwards, the orientation vector is being de-formed,causing teh camera to take a parabolic path. If I normalize it, I get strange behaviour. The camera class will call glulookat with the three vectors position, (position + orientation) and the up vector which as yet im not modifying. the orientation vector is rotated in teh x and y using euler matrices. Gimbal lock is not a problem since im using incremental rotation, i think thats what its called anyway. Can anybody see what i've done wrong? My vector and matrix classes and the function gv_x_gm() which multiplies a vector and a matrix have passed every test ive thrown at them, but if anybody thinks thats the problem i will include those as well. camera_control.h:

/*
 *  camera_control.h
 *  game_engine
 *
 *  Created by gavin on 10/04/2007.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

class camera_control;
#ifndef class_camera_control_h
#define class_camera_control_h 1
#include "SDL_GL_include.h"
#include "g_maths.h"


class camera_control
{
private:
	void normalize();
	float xr,yr;
	g_vector orientation;
	g_vector up;
public:
	g_vector position;
	g_vector target;

	void set_position(float x, float y, float z);
	void set_target(float x, float y, float z);
	void set_up(float x, float y, float z);
	void rotate(float x, float y);
	void move_forward(float d);
		
	camera_control();
	~camera_control();
	void look_at_scene();
};
#endif



camera_control.cpp:

/*
 *  camera_control.cpp
 *  game_engine
 *
 *  Created by gavin on 10/04/2007.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */
#include "camera_control.h"

camera_control::camera_control()
{
	xr = 0;
	yr = 0;

	position.set_vector(0,10,0);
	target.set_vector(0,0,1);
	up.set_vector(0,1,0);
	normalize();
};


void camera_control::normalize()
{
    orientation = position - target;
    orientation.normalize();
};


camera_control::~camera_control()
{

};

void camera_control::look_at_scene()
{
	normalize();
	g_matrix xrot(4,4);
	g_matrix yrot(4,4);
	yrot.y_rot(xr*pirad);
	xrot.x_rot(yr*pirad);
	g_matrix rotation(4,4);
	rotation = matrix_multiply(xrot,yrot);
	orientation = gv_x_gm(orientation,rotation);

	float px,py,pz,tx,ty,tz,ux,uy,uz;
	position.get_vector(px,py,pz);
	orientation.get_vector(tx,ty,tz);
	up.get_vector(ux,uy,uz);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(px,py,pz,px+tx,py+ty,pz+tz,ux,uy,uz);
};

void camera_control::rotate(float x, float y)
{
	xr -= x;
	yr -= y;
};


void camera_control::move_forward(float d)
{
	position = position + orientation*d;
};




Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

This topic is closed to new replies.

Advertisement