Advertisement

Tip: easyest camera ever and no prob. to implement

Started by June 28, 2002 05:20 AM
1 comment, last by DarkKiller 22 years, 8 months ago
First #include <gl\glu.h> and (depens on your way of working): #pragma comment(lib, "glu32.lib") then copy and paste the folloing structure somewhere at the top of your main.cpp (or the file where your renderscene() is in, too)

typedef struct
{
	POINT	Pos;
	float	AngleX;
	float	AngleY;
	float	Radius;
	float	DeltaX;
	float	DeltaY;
	float	PlayerX;
	float	PlayerY;
	float	PlayerZ;
	float	LookAtX;
	float	LookAtY;
	float	LookAtZ;
	float	SlideX;
	float	SlideZ;
}
Camera;
Camera CameraSettings;
  
after that goto your renderscene() function and copy and paste the following:

	glLoadIdentity();

// here goes the camera calculation. Not very simple but effective and short!
	GetCursorPos(&CameraSettings.Pos);
	CameraSettings.DeltaX  = ((float)(ScreenX/2) - (float)CameraSettings.Pos.x);
	CameraSettings.DeltaY = ((float)(ScreenY/2) - (float)CameraSettings.Pos.y);
	CameraSettings.AngleX += (CameraSettings.DeltaX/300);
	CameraSettings.AngleY += (CameraSettings.DeltaY/300);
	if (CameraSettings.AngleY > 1.569f) CameraSettings.AngleY = 1.569f;
	if (CameraSettings.AngleY < -1.569f) CameraSettings.AngleY = -1.569f;
	CameraSettings.LookAtY = (float)(sin(CameraSettings.AngleY) * 6);
        CameraSettings.Radius  = (float)(cos(CameraSettings.AngleY) * 6);
        CameraSettings.LookAtX = (float)(sin(CameraSettings.AngleX) * CameraSettings.Radius);
        CameraSettings.LookAtZ = (float)(cos(CameraSettings.AngleX) * CameraSettings.Radius);
	CameraSettings.SlideX = (float)(sin(CameraSettings.AngleX) * 6);
	CameraSettings.SlideZ = (float)(cos(CameraSettings.AngleX) * 6);
	SetCursorPos(ScreenWidth/2, ScreenHeight/2);
	gluLookAt(CameraSettings.PlayerX, CameraSettings.PlayerY, CameraSettings.PlayerZ, CameraSettings.PlayerX+CameraSettings.LookAtX, CameraSettings.PlayerY+CameraSettings.LookAtY, CameraSettings.PlayerZ+CameraSettings.LookAtZ, 0, 1,	0);
// End camera calculation
  
Thats all you have to do. No classes, no vector mathmatics! (@vector-coders: ahh, please don't kick me :D its much easier to understand this instead of the complicated tutorials about this topic and its much shorter and fast, too) dark [edited by - DarkKiller on June 28, 2002 6:25:06 AM]
DarkMcNugget next time... ;)
for movement do the following: (I use nehe''s "bool keys[256]")
copy and paste the folowing code into your mainloop

if (keys[''W'']){	CameraSettings.PlayerX += CameraSettings.LookAtX * ValProc;	CameraSettings.PlayerY += CameraSettings.LookAtY * ValProc;	CameraSettings.PlayerZ += CameraSettings.LookAtZ * ValProc;}if (keys[''S'']){	CameraSettings.PlayerX -= CameraSettings.LookAtX * ValProc;	CameraSettings.PlayerY -= CameraSettings.LookAtY * ValProc;	CameraSettings.PlayerZ -= CameraSettings.LookAtZ * ValProc;}if (keys[''A'']){	CameraSettings.PlayerX += CameraSettings.SlideZ * ValProc;	CameraSettings.PlayerZ -= CameraSettings.SlideX * ValProc;}if (keys[''D'']){	CameraSettings.PlayerX -= CameraSettings.SlideZ * ValProc;	CameraSettings.PlayerZ += CameraSettings.SlideX * ValProc;} 
DarkMcNugget next time... ;)
Advertisement
or you could just use a matrix and a vector object. assuming you'd already made the objects (which isn't a hard thing to do provided you understand how they work)

then it'd be a matter of:

set the camera:

glLoadMatrixf((float*)&martix.transpose());
glTranslatef(-position.x,-position.y,-position.z);


and to rotate,

if (keys[VK_UP])
matrix.RealitiveRotate(deltaTime,0,0);

etc..

to move,

if (keys[VK_W])
position+=Vector3(0,1,0)*matrix*deltaTime;



this is exactly what the code for a space game I'm making looks like at this rough testing stage.

[edited by - RipTorn on June 28, 2002 6:52:13 AM]

This topic is closed to new replies.

Advertisement