Chase Camera

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
User avatar
Longstreet
Halfling
Posts: 94
Joined: Tue Jan 20, 2004 10:41 am

Chase Camera

Post by Longstreet »

I've been playing around in the tutorials and wrote this chase camera class for the SpaceApp.

It has some jerky behaviour sometimes that I think is related to the direct setOrientation I use. However, I don't know how else to keep the camera node lined up with the target node in the "up" direction. Maybe there is some math trick to do it smoothly...? (ie to use rotateSpeed)

Here is a compile of the demo/tutorial with this (controls in the readme.txt):
http://www.andromedaspace.com/followCamera.rar

edit: sorry, I don't have it setup very well to get the media files. I'm new at this stuff :oops:.

Code: Select all

/*
	FollowingCamera.h
	Takes a camera and a scene node and has the camera follow the node

	@author K. Carter Dodd (aka Longstreet)
*/

#include "ExampleFrameListener.h"
#include "OgreMath.h"

class FollowingCamera : public ExampleFrameListener
{
public:
	FollowingCamera(RenderWindow* win, Camera* cam, SceneNode* followednode, Vector3 offset, Real chaseSpeed, Real rotateSpeed) : ExampleFrameListener(win, cam)
	{
		// attatch camera to the chasing node
		mCameraNode = followednode->getParentSceneNode()->createChildSceneNode();
		mCameraNode->attachObject(cam);

		// turn it so its not upside down... ?
		cam->rotate(Vector3::UNIT_Z, Angle(180));

		// node to chase
		mFollowedNode = followednode;

		// offset for camera to chase (ie not right on top of target)
		mCameraOffset = offset;

		// speed multipliers
		moveMultiplier = chaseSpeed;
		rotateMultiplier = rotateSpeed;

	}

	bool frameStarted(const FrameEvent& evt)
	{

		// get the vector toward the ideal position
		Vector3 moveVector = (mFollowedNode->getOrientation()*(mCameraOffset*mFollowedNode->getScale()) + mFollowedNode->getPosition()) - mCameraNode->getPosition();

		// move a bit of that distance
		mCameraNode->translate(moveVector * evt.timeSinceLastFrame * moveMultiplier);

		// update camera orienation around target
		mCameraNode->setOrientation(mFollowedNode->getOrientation());

		// turn camera toward target center
		mCameraNode->setDirection(mFollowedNode->getPosition() - mCameraNode->getPosition());
			
		return true; 
	}

protected:
	SceneNode* mCameraNode;
	SceneNode* mFollowedNode;
	Vector3 mCameraOffset;
	Real moveMultiplier, rotateMultiplier;

};
snowblind
Gnoblar
Posts: 14
Joined: Tue Jan 25, 2005 5:51 am

Post by snowblind »

I got an error window saying

"This application has failed to start because stlport_vc6_stldebug46.dll was no found..."

You might want to include that in the rar for those of us that don't have STLPort installed.
richardwhiuk
Gnoblar
Posts: 7
Joined: Sat Mar 05, 2005 10:20 pm

Post by richardwhiuk »

If your using Ogre then you should have STLPort already installed - see the wiki for more info.
richardwhiuk
Hope
User avatar
Longstreet
Halfling
Posts: 94
Joined: Tue Jan 20, 2004 10:41 am

Post by Longstreet »

Ok, I put a new rar that has the media files, although I broke the texture on the ship I don't know why.
User avatar
Longstreet
Halfling
Posts: 94
Joined: Tue Jan 20, 2004 10:41 am

Post by Longstreet »

Through basically trial and error I have come to this bit of code. I feel there must be a better way to do this though and keep the camera stable. The only change is the extra flip around UNIT_X, and this keeps the camera from going bonkers when the camera and target z axis are lined up (i.e. when it doesn't know which way is best to rotate the camera).

I've updated the .exe with this too, and some other things.

Code: Select all

		// get the vector toward the ideal position
		Vector3 moveVector = (mFollowedNode->getOrientation()*(mCameraOffset*mFollowedNode->getScale()) + mFollowedNode->getPosition()) - mCameraNode->getPosition();

		// move a bit of that distance
		mCameraNode->translate(moveVector * evt.timeSinceLastFrame * moveMultiplier);

		// update camera orienation around target
		mCameraNode->setOrientation(mFollowedNode->getOrientation());
		
		// flip the camera around to -z
		mCameraNode->rotate(Vector3::UNIT_X, Angle(180));

		// turn camera toward target center 
		mCameraNode->setDirection(mFollowedNode->getPosition() - mCameraNode->getPosition());
Have your machines contact my machines.