ChaseCamera Problem

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
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

ChaseCamera Problem

Post by Hairan »

Hey!

I got alittle problem with the camera for my game.

I have been using a lot of code from the chase camera at: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=26944

The problem is that the camera is not going to the correct position at all, at first it spins around alittle and then it just positions itself completly different and facing completly wrong, for some reason I can't even see the skybox...

I am thankful for any help!

The code:

Code: Select all

cam = ChaseCamera(smgr, 10.0f, 10.0f, 2.0f, 2.0f);
On each update:

Code: Select all

cam.Update(frameDeltaTime, node->getPosition(), node->getRotation(), speed);
My class:

Code: Select all

#include <irrlicht.h>
#include <iostream>

using namespace irr;

class ChaseCamera
{
	
	// Private variables
private:
	scene::ICameraSceneNode *camera; // The camera
	float distance; // Distance behind the object
	float height; // Height above the object
	float aimHeight; // Aim height over the object
	float camSpeed; // Speed of the camera
	float camZoom; // If user zooms the camera

	// Public functions
public:
	// Default Constructor
	ChaseCamera(){}

	// Constructor
	ChaseCamera(scene::ISceneManager *smgr, float Distance, float Height, float AimHeight, float CamSpeed)
	{
		// Store all variables
		distance = Distance;
		height = Height;
		aimHeight = AimHeight;
		camSpeed = CamSpeed;
		camZoom = 1;

		// Setup the camera
		camera = smgr->addCameraSceneNode();
	}

	// Run this each update
	// TargetPosition, targetRotation, and targetVelocity
	void Update(const float frameDeltaTime, core::vector3df targetPos, core::vector3df targetRot, float targetVel)
	{
		// Find desired camera position
		core::vector3df cameraPos;

		float length = 60 * camZoom * distance;
		height += 15 * height * camZoom;

		core::matrix4 m;
		m.setRotationDegrees(targetRot);
		core::vector3df dirBack(-1, 0, 0);
		m.rotateVect(dirBack);

		// Add the distance to the target
		cameraPos = targetPos;
		cameraPos += dirBack * length;
		cameraPos.Y += height;

		// Get current camera position
		core::vector3df cameraNow = camera->getPosition();
		float cameraSpeed = 0.01 * camSpeed;

		float disX = (core::abs_(cameraPos.X - cameraNow.X));
		float disY = (core::abs_(cameraPos.Y - cameraNow.Y));
		float disZ = (core::abs_(cameraPos.Z - cameraNow.Z));

		// Move distance this tick
		float deltaX = cameraSpeed * disX;
		float deltaY = cameraSpeed * disY;
		float deltaZ = cameraSpeed * disZ;

		// Apply move distance to cameraPos
		if(cameraPos.X > cameraNow.X)
			cameraNow.X += deltaX * frameDeltaTime * 200;
		if(cameraPos.X < cameraNow.X)
			cameraNow.X -= deltaX * frameDeltaTime * 200;

		if(cameraPos.Y > cameraNow.Y)
			cameraNow.Y += deltaY * frameDeltaTime * 200;
		if(cameraPos.Y < cameraNow.Y)
			cameraNow.Y -= deltaY * frameDeltaTime * 200;
		
		if(cameraPos.Z > cameraNow.Z)
			cameraNow.Z += deltaZ * frameDeltaTime * 200;
		if(cameraPos.Z < cameraNow.Z)
			cameraNow.Z -= deltaZ * frameDeltaTime * 200;

		// Set the position
		camera->setPosition(cameraNow);
		
		// Set camera target
		core::vector3df cameraAim = targetPos;
		cameraAim.Y += height * aimHeight;
		camera->setTarget(cameraAim);

		// Move camera due to last frames rotation (Needed to keep same behaviour with moving targets)
		core::vector3df pos = camera->getPosition();
		core::vector3df rot = targetRot;
		m.setRotationDegrees(rot);
		core::vector3df dir2(1, 0, 0);
		m.rotateVect(dir2);
		pos += dir2 * targetVel * frameDeltaTime * 200;
		camera->setPosition(pos);

		// OUTPUT DEBUG DATA
		std::cout << "X: " << camera->getPosition().X;
		std::cout << " Y: " << camera->getPosition().Y;
		std::cout << " Z: " << camera->getPosition().Z << std::endl;
	}

	// Use this to zoom in
	void IncreaseZoom(const float frameDeltaTime)
	{
		camZoom += 2.0 * frameDeltaTime;
		// Clamp the values
		core::clamp(camZoom, 1.0f, 2.0f);
	}

	// Use this to zoom out
	void DecreaseZoom(const float frameDeltaTime)
	{
		camZoom -= 2.0 * frameDeltaTime;
		// Clamp the values
		core::clamp(camZoom, 1.0f, 2.0f);
	}

	// Private functions
private:
	
};
Post Reply