ice wrote:great!
I assume that your Point3D is a vector3df, right?
Yes you can use vector3df. I was using a simple class with 3 float x, y, z.
ice wrote:
also, how can I get the "lookat" and "up" vectors?
thanks.
The code I posted was from an old OpenGL Camera class I made so yes it gives you the new UP and Look at vectors.
Would you like the source for the whole class? You'd need to change it to use irrlicht instead of OpenGL but the math remains the same.
BTW the camera was used in a space shooter I made as a demo, so it works like you want. I made a simple 3D asteroids game were you shoot the rocks and try not to run into them or the sun. There was simulated gravity and movement was done via a vector system with physics of my own design.
Point3D.h
Code: Select all
class Point3D
{
public:
Point3D(void);
~Point3D(void);
double x;
double y;
double z;
void SetPoint(double lx, double ly, double lz);
CString toString(void);
void operator= (const Point3D& aPoint3D);
Point3D(const Point3D& aPoint3D);
};
Point3D.cpp
Code: Select all
#include ".\point3d.h"
Point3D::Point3D(void)
: x(0)
, y(0)
, z(0)
{
}
Point3D::~Point3D(void)
{
}
void Point3D::SetPoint(double lx, double ly, double lz)
{
x = lx;
y = ly;
z = lz;
}
CString Point3D::toString(void)
{
CString str;
str = "(";
char buffer[25];
_gcvt( x, 12, buffer );
str = str + buffer;
str = str + ", ";
_gcvt( y, 12, buffer );
str = str + buffer;
str = str + ", ";
_gcvt( z, 12, buffer );
str = str + buffer;
str = str + ")";
return str;
}
void Point3D::operator= (const Point3D& aPoint3D)
{
x = aPoint3D.x;
y = aPoint3D.y;
z = aPoint3D.z;
}
Point3D::Point3D(const Point3D& aPoint3D)
{
x = aPoint3D.x;
y = aPoint3D.y;
z = aPoint3D.z;
}
Camera.h
Code: Select all
#include "point3d.h"
// a camer class to make doing the spherical trig not quite as bad as sharp forks in my eyes
class Camera
{
public:
Camera(void);
Camera(float eyex, float eyey, float eyez);
~Camera(void);
// translate along x y and z axis
void translate(float du, float dv, float dn);
// change the pitch of the cam
void pitch(float angle);
// roll the camera
void roll(float angle);
// yaw the cam left and right
void yaw(float angle);
// do the lookat call
void applyView(void);
// current camera location, the lookat vector, up vector and roll displacment
Point3D loc, lookat, up, rollaxis;
};
Camera.cpp
Code: Select all
#include ".\camera.h"
#include <math.h>
#include <gl\glut.h>
#ifndef M_PI
#define M_PI (4*atan(1.0))
#endif
//the default constructor
Camera::Camera(void)
{
rollaxis.x=1;
rollaxis.y=0;
rollaxis.z=0;
up.x=0;
up.y=1;
up.z=0;
lookat.x=0;
lookat.y=0;
lookat.z=-1;
}
//the copy constructor
Camera::Camera(float eyex,float eyey,float eyez)
{
loc.x=eyex; loc.y=eyey; loc.z=eyez;
rollaxis.x=1;rollaxis.y=0;rollaxis.z=0;up.x=0;up.y=1;up.z=0;lookat.x=0;lookat.y=0;lookat.z=-1;
}
Camera::~Camera(void)
{
}
// translate along x y and z axis
void Camera::translate(float du, float dv, float dn)
{
loc.x+=(du*rollaxis.x+dv*up.x+dn*lookat.x);
loc.y+=(du*rollaxis.y+dv*up.y+dn*lookat.y);
loc.z+=(du*rollaxis.z+dv*up.z+dn*lookat.z);
}
// change the pitch of the cam
void Camera::pitch(float angle)
{
float tempx=up.x;
float tempy=up.y;
float tempz=up.z;
float cosine=cos(angle*M_PI/180.0);
float sine=sin(angle*M_PI/180.0);
up.x=tempx*cosine-lookat.x*sine;
up.y=tempy*cosine-lookat.y*sine;
up.z=tempz*cosine-lookat.z*sine;
lookat.x=tempx*sine+lookat.x*cosine;
lookat.y=tempy*sine+lookat.y*cosine;
lookat.z=tempz*sine+lookat.z*cosine;
}
// roll the camera
void Camera::roll(float angle)
{
float tempx=rollaxis.x;
float tempy=rollaxis.y;
float tempz=rollaxis.z;
float cosine=cos(angle*M_PI/180.0);
float sine=sin(angle*M_PI/180.0);
rollaxis.x=tempx*cosine-up.x*sine;
rollaxis.y=tempy*cosine-up.y*sine;
rollaxis.z=tempz*cosine-up.z*sine;
up.x=tempx*sine+up.x*cosine;
up.y=tempy*sine+up.y*cosine;
up.z=tempz*sine+up.z*cosine;
}
// yaw the cam left and right
void Camera::yaw(float angle)
{
float tempx=rollaxis.x;
float tempy=rollaxis.y;
float tempz=rollaxis.z;
float cosine=cos(angle*M_PI/180.0);
float sine=sin(angle*M_PI/180.0);
rollaxis.x=tempx*cosine+lookat.x*sine;
rollaxis.y=tempy*cosine+lookat.y*sine;
rollaxis.z=tempz*cosine+lookat.z*sine;
lookat.x=-tempx*sine+lookat.x*cosine;
lookat.y=-tempy*sine+lookat.y*cosine;
lookat.z=-tempz*sine+lookat.z*cosine;
}
// do the lookat call
void Camera::applyView(void)
{
//eye, lookat point, up vector
gluLookAt(loc.x,loc.y,loc.z,loc.x+lookat.x,loc.y+lookat.y,loc.z+lookat.z,up.x,up.y,up.z);
}