Code Snippet of the Week: CryEngine2 Third Person Camera

Recently I have been employeed at a new company where I work with the CryEngine2 quite frequently. CryTek has released a MOD sdk for their popular Crysis Wars game. In my personal time, I enjoy developing mods for this amazing game. Crysis Wars is a first person shooter game thus the camera by default is set in a first person perspective.


First person perspective

First person perspective

One of the very first mods I wrote for the CryEngine is a Third Person Camera. The CryEngine already supports a third person camera however a developer using Flow Graph is limited as to what they can do with it. For example, the third person camera is at a fixed distance away from the player. A developer can’t really control this at all. That is why I decided to write my own custom flow node for the CryMOD sdk that would give developers the ability to use a flexible third person camera system. Below, you can see a demo of my third person camera as well as the source code.

Third person perspective

And here is the source code.

////////////////////////////////////////////////////
// FlowThirdPersonCameraNode.cpp
//
// Purpose: This flow node gives the developer
//	    more flexibility of controlling
//	    the third person perspective 
//	    camera by allowing them to offset
//	    the camera's distance from the player.
//
// File History:
// - 6/05/09 : File created - Brandon Fogerty
////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Nodes/G2FlowBaseNode.h"
 
#include "Cry_Camera.h"
#include "Actor.h"
 
class CFlowThirdPersonCameraNode : public CFlowBaseNode
{
private:
 
	enum EInputPorts
	{
		EIP_SYNC = 0,
		EIP_DISTANCE,
		EIP_HEIGHT,
	};
 
public:
 
	CFlowThirdPersonCameraNode(SActivationInfo * pActInfo)
	{
	}
 
	virtual void GetConfiguration(SFlowNodeConfig &config)
	{
		static const SInputPortConfig inputs[] = 
		{
			InputPortConfig_Void("sync",_HELP("sync")),
			InputPortConfig<float>("distance",-7.0,_HELP("Distance from player")),
			InputPortConfig<float>("height",2.0,_HELP("Height relative to the player")),
			{0},
		};
 
		config.pInputPorts = inputs;
		config.pOutputPorts = NULL;
		config.sDescription = "Control the third person perspective camera.";
		config.SetCategory(EFLN_APPROVED);
	}
 
	virtual void ProcessEvent(EFlowEvent evt, SActivationInfo *pActInfo)
	{
 
		switch(evt)
		{
			case eFE_Activate:
				if(IsPortActive(pActInfo, EIP_SYNC))
				{
 
					pActInfo->pGraph->SetRegularlyUpdated(pActInfo->myID,true);
				}
			break;
 
			case eFE_Update:
				// Grab the game's camera view.
				CCamera cam = gEnv->pSystem->GetViewCamera();
				// Get the local player
				IActor * pActor = g_pGame->GetIGameFramework()->GetClientActor();
 
				// If we are in a vehicle,  use the default camera system.
				if(pActor->GetLinkedVehicle() == NULL)
				{
					// We need to toggle the regular third person camera
					// so that crysis renders the whole player model and
					// not just the limbs.
					if(pActor->IsThirdPerson() == false)
					{
						pActor->ToggleThirdPerson();
					}
 
					// Get our camera's position
					Vec3 vecCamPos = pActor->GetEntity()->GetPos();
 
					// Offset the camera on the z axis (Up/Down)
					vecCamPos.z = pActor->GetEntity()->GetPos().z + GetPortFloat(pActInfo, EIP_HEIGHT);
 
					float fDistance = GetPortFloat(pActInfo, EIP_DISTANCE);
 
					// This is a little math to which ensures that our camera is always facing the direction
					// that our player is looking.
					vecCamPos.y = pActor->GetEntity()->GetPos().y + (pActor->GetViewRotation().GetFwdY() * fDistance);
					vecCamPos.x = pActor->GetEntity()->GetPos().x + (pActor->GetViewRotation().GetFwdX() * fDistance);
 
					// Override the crysis camera with our newly calculated camera view and we are done!
					cam.SetPosition(vecCamPos);
					gEnv->pSystem->SetViewCamera(cam);
				}
 
			break;
		}
	}
 
	virtual void GetMemoryStatistics(ICrySizer *s)
	{
		s->Add(*this);
	}
 
	virtual ~CFlowThirdPersonCameraNode()
	{
	}
};
 
REGISTER_FLOW_NODE("Camera:SetThirdPersonView",CFlowThirdPersonCameraNode);

Well that is it for this week. You can also find this code snippet on the official CryMod website at
http://wiki.crymod.com/index.php/Third_Person_Camera

I hope that this makes sense and helps you get more out of the CryEngine! Happy modding!

Did you find this article useful?

View Results

Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.