(C++) Simple 3rd person camera object but with no controls

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
jreuschel1
Posts: 25
Joined: Sun Nov 12, 2006 7:51 pm
Contact:

(C++) Simple 3rd person camera object but with no controls

Post by jreuschel1 »

Here is a simple third person camera class that positions a camera based on code input only but with a litle work you can use it to follow a SceneNode very easily.


Here is the code

Code: Select all

//I3rdPersonCam.h                          3rd Person camera for Irrlicht 3D 1.1   
// version 0.1

#include <irrlicht.h>
using namespace irr;
using namespace irr::core;
using namespace irr::scene;
// 1.  create I3rdPersonCam object <Cam1>;
// 2.  attach a CameraSceneNode with Cam1.attachCamera()
// 3.  Set a target with Cam1.setTarget()
// 4.  call Cam1.modOrientation(pan, tilt, zoom) or Cam1.setOrientation(pan, tilt, zoom) to position camera
// 5.  call Cam1.updatePosition() to apply new Position data
// 6.  repeat 4 and 5 as needed



class I3rdPersonCam{

public:
	float camPan;
	float camTilt;
	float camZoom;
	float maxZoom;
	float camSpeed;
	ICameraSceneNode* cam;
	ISceneNode* camTarget;
	vector3df offset;
	
	I3rdPersonCam(){
		camPan=0;
		camTilt=45;
		camZoom=14;
		maxZoom=250;
		camSpeed=1;
		}

	void setTarget(ISceneNode *newTarget){
		camTarget=newTarget;
		}

	void setOffset(vector3df newOffset){
		offset=newOffset;
		}

	void attachCamera(ICameraSceneNode *camtoattach){
		cam=camtoattach;
		}

	void modOrientation(float pan1,float tilt1,float zoom1){
		camPan=camPan+pan1;
		camTilt=camTilt+tilt1;
		camZoom=camZoom+zoom1;
		if(camPan>360)camPan=camPan-360;
		if(camPan<-360)camPan=camPan+360;
		if(camTilt>89)camTilt=89;
		if(camTilt<-89)camTilt=-89;
		if(camZoom>maxZoom)camZoom=maxZoom;
		if(camZoom<0)camZoom=0;
		}

	void setOrientation(float pan1,float tilt1,float zoom1){
		camPan=pan1;
		camTilt=tilt1;
		camZoom=zoom1;
		}


void updatePosition()
{
vector3df CPosVector;
vector3df NewCamLocation;
vector3df Target1;
//CamPan==0 places camera behind Model--- CamPan range 0-360
//camTilt inputs should be between -89 and +89
CPosVector.X=cos((camPan+180)*PI/180)*sin((camTilt+90)*PI/180);
CPosVector.Y=cos((camTilt+90)*PI/180);
CPosVector.Z=sin((camPan+180)*PI/180)*sin((camTilt+90)*PI/180);

matrix4 m2;
m2.setRotationDegrees(camTarget->getRotation());
m2.transformVect(CPosVector);

Target1=camTarget->getPosition()+offset;
NewCamLocation.X=Target1.X+CPosVector.X*camZoom;
NewCamLocation.Y=Target1.Y+CPosVector.Y*camZoom;
NewCamLocation.Z=Target1.Z+CPosVector.Z*camZoom;

cam->setPosition(NewCamLocation);
cam->setUpVector(vector3df(0,1,0));
cam->setTarget(vector3df(Target1));
cam->updateAbsolutePosition();
}

};
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

so its an camera wich circles around a node?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It looks like it follows another node, and you can move the camera around that node in a spherical orbit. Similar to the camera used in games like Super Mario 64.

Travis
Post Reply