Moving camera and target scene node

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
invas10n
Posts: 10
Joined: Wed May 28, 2008 4:40 pm

Moving camera and target scene node

Post by invas10n »

Firstly, I apologise if something similar to this has already been posted. The forums are so large, I cannot find anything.

Right, so, this is my problem. I am creating my own custom camera for a FPS game. I do not want to use the FPSCamera because I want to have more control over the camera and do some tricks with it later. So far, the camera look is working perfectly. The problem is moving the camera.

The code I am posting below has been pruned of unnecessary code. Firstly, my init() method:

Code: Select all

//INITIALIZES THE LEVEL
void gameManager::init()
{
	cursor->setVisible(true);
	device->setEventReceiver(eventReceiver);
	smgr->loadScene("resources/meshes/test.irr");
	
	camera = smgr->addCameraSceneNode();
	target = smgr->addCubeSceneNode(0.1f,camera); //for debug purposes
	
	NoVerticalMovement = true;

	target->setPosition(vector3df(0,0,1));
	camera->setPosition(vector3df(0,0,0));
	smgr->setActiveCamera(camera);
	camera->bindTargetAndRotation(false);
	camera->render();

	isMovingForwards = isMovingBackwards = isMovingLeft = isMovingRight = false;
	moveSpeed = 0.2f;
}
That creates my camera scene node and a target scene node which is a child to my camera. The movement code I have taken from the FPSCamera class. To be honest, there is some code in here that I do not fully understand, and thats most probably my main problem. Anyway, here is the movement code. Please note that I have removed the strafe code because I am not concerned with that at the moment:

Code: Select all

//UPDATE PLAYER MOVEMENT
void gameManager::updateMovement()
{
		vector3df camPos = camera->getPosition();
		vector3df direction = (camera->getTarget() - camera->getAbsolutePosition());
		vector3df relativeRotation = direction.getHorizontalAngle();

		direction.setLength(1.0f);
		//direction.set(0,0, max_(1.0f, camPos.getLength()));

		vector3df movedir = direction;

		matrix4 mat;
		mat.setRotationDegrees(vector3df(relativeRotation.X, relativeRotation.Y, 0));
		mat.transformVect(direction);

		if (NoVerticalMovement)
		{
			mat.setRotationDegrees(vector3df(0, relativeRotation.Y, 0));
			mat.transformVect(movedir);
		}
		else
		{
			movedir = direction;
		}
		
		movedir.normalize();
		
		if (isMovingForwards) camPos += movedir * moveSpeed;  		
		if (isMovingBackwards) camPos -= movedir * moveSpeed;

		updateCamera(camPos);
}
At the end of this method, the updateCamera() method is called with camPos being sent to it. This method is ment to update the camera position and target:

Code: Select all

//UPDATE PLAYER CAMERA WHEN MOVING
void gameManager::updateCamera(vector3df camPos)
{	
		camera->setPosition(camPos);
		camera->setTarget(target->getPosition());
		camera->updateAbsolutePosition();	

		target->updateAbsolutePosition();
}
The Problem:
When I move the camera with the mouse keys, the target, in most cases, goes shooting off into the distance. With some messing around, I can get it to behave chaotically, however, I can not keep the target within the 1.0f sphere around the camera. I hope I have explained the problem in enough detail.

Thanks.
Post Reply