RTS style camera

A forum to store posts deemed exceptionally wise and useful
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

RTS style camera

Post by Yellow »

I had some trouble finding out how to get a RTS type camera to work in irrlicht (Birds perspective), so I thought this might be a usefull bit of code to share with others who are having the same trouble.

This cam will move above the scene when you get close to the screen borders. call this function from your main loop.

Oh and since this is my first post here after lurking the forums for a while, Hello all :)

Code: Select all

void CGame::updateCamera()
{		
	// make the camera look to a fixed point (45 degrees down)
	// in front of it, so it wont stay locked on the old target.
	vector3df cpos = pCamera->getPosition();
	cpos.Z += 2;
	cpos.Y -= 2;
	pCamera->setTarget(cpos);

	// get the cursor positions
	int x = pDevice->getCursorControl()->getPosition().X;
	int y = pDevice->getCursorControl()->getPosition().Y;

	// make the move distance the same for different frame rates
	int fps = pDriver->getFPS();
	float dist;

	if (fps > 1)
                                dist = 200.0f / fps;
	else
		dist = 1.0f;	
	
	if (x > (screenSize.X - 50))
	{	
		cpos = pCamera->getPosition();		
		cpos.X += dist;
		pCamera->setPosition(cpos);								
	}
	else if (x < 50)
	{	
		cpos = pCamera->getPosition();		
		cpos.X -= dist;
		pCamera->setPosition(cpos);							
	}

	if (y < 50)
	{
		cpos = pCamera->getPosition();		
		cpos.Z += dist;
		pCamera->setPosition(cpos);								
	}
	else if (y > (screenSize.Y - 50))
	{	
		cpos = pCamera->getPosition();		
		cpos.Z -= dist;
		pCamera->setPosition(cpos);							
	}
}
Demo (bin + source): http://home.planet.nl/~brons463/camdemo.rar
Last edited by Yellow on Sun Feb 05, 2006 1:30 am, edited 1 time in total.
Guest

Post by Guest »

That's great! I was planning to put together one too. But I've been sidelined trying to get other aspect of game up and going like options menu, fonts, sound, models, etc. Thanks for sharing. I'll definitely test it out for you!

What is "pCamera"?
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

Post by Yellow »

pCamera is a pointer to a static cam:

ICameraSceneNode *pCamera;

pCamera = pSceneMgr->addCameraSceneNode();

Glad someone could use this :)
Guest

Post by Guest »

Yellow wrote:pCamera is a pointer to a static cam:

ICameraSceneNode *pCamera;

pCamera = pSceneMgr->addCameraSceneNode();

Glad someone could use this :)
Thanks. I felt like pretty silly after reading your response. :roll:

Do you have demo? That way this can be posted in the wiki as a tutorial of sort.

Actually, I was wondering why a lot of the posts in this forum doesn't make it into the wiki. It would make it a lot easier to find and use if a lot of these info is written in the wiki as opposed to having to search through the forum to find the info you want. But that's not a bad thing either. I just wished the wiki had more tutorials then it currently contain.
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

I had to change the "X" in the following code snippet to "Width":

Code: Select all

	if (x > (screenSize.Width - 50)) 
	{    
		cpos = pCamera->getPosition();       
		cpos.X += dist; 
		pCamera->setPosition(cpos);                         
	} 
Same here but with "Y" and "Height":

Code: Select all

	else if (y > (screenSize.Height - 50)) 
	{    
		cpos = pCamera->getPosition();       
		cpos.Z -= dist; 
		pCamera->setPosition(cpos);                      
	}
After adding this code to the beginning of the function:

Code: Select all


	dimension2d<s32> screenSize = pDriver->getScreenSize();

I also changed the Update() function to include three parameters (this is to tailor it to use irrWizard's game framework):

Code: Select all


void 
BirdsEyeCAM::Update(ICameraSceneNode * pCamera, IrrlichtDevice * pDevice, IVideoDriver * pDriver)
{
	dimension2d<s32> screenSize = pDriver->getScreenSize();
...
}

As you can see from the last code post, I threw the code into a class and called it "BirdsEyeCAM."

.h:

Code: Select all


#pragma once

#include <irrlicht.h>

using namespace irr; 
using namespace core; 
using namespace scene; 
using namespace video; 
using namespace io; 
using namespace gui; 

class BirdsEyeCAM
{
public:
	BirdsEyeCAM(void);
	~BirdsEyeCAM(void);
	void Update(ICameraSceneNode *,IrrlichtDevice *,IVideoDriver*);
};
.cpp:

Code: Select all


#include "BirdsEyeCAM.h"

BirdsEyeCAM::BirdsEyeCAM(void)
{
}

BirdsEyeCAM::~BirdsEyeCAM(void)
{
}

void 
BirdsEyeCAM::Update(ICameraSceneNode * pCamera, IrrlichtDevice * pDevice, IVideoDriver * pDriver)
{
	dimension2d<s32> screenSize = pDriver->getScreenSize();

	// make the camera look to a fixed point (45 degrees down) 
	// in front of it, so it wont stay locked on the old target. 
	vector3df cpos = pCamera->getPosition(); 
	cpos.Z += 2; 
	cpos.Y -= 2; 
	pCamera->setTarget(cpos); 

	// get the cursor positions 
	int x = pDevice->getCursorControl()->getPosition().X; 
	int y = pDevice->getCursorControl()->getPosition().Y; 

	// make the move distance the same for different frame rates 
	int fps = pDriver->getFPS(); 
	float dist; 

	if (fps > 1) 
		dist = 200.0f / fps; 
	else 
		dist = 1.0f;    

	if (x > (screenSize.Width - 50)) 
	{    
		cpos = pCamera->getPosition();       
		cpos.X += dist; 
		pCamera->setPosition(cpos);                         
	} 
	else if (x < 50) 
	{    
		cpos = pCamera->getPosition();       
		cpos.X -= dist; 
		pCamera->setPosition(cpos);                      
	} 

	if (y < 50) 
	{ 
		cpos = pCamera->getPosition();       
		cpos.Z += dist; 
		pCamera->setPosition(cpos);                         
	} 
	else if (y > (screenSize.Height - 50)) 
	{    
		cpos = pCamera->getPosition();       
		cpos.Z -= dist; 
		pCamera->setPosition(cpos);                      
	}
}

It works! But I need to adjust the height since the original numbers places it very close to the floor at least for the level model that I used. :D
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

Yellow -- How do you adjust the height??? I've tried to play around with the following values (cpos.Z and cpos.Y) but got no where fast:

Code: Select all


	// make the camera look to a fixed point (45 degrees down) 
	// in front of it, so it wont stay locked on the old target. 
	vector3df cpos = pCamera->getPosition(); 
	cpos.Z += 2; 
	cpos.Y -= 2; 

Help!
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

Post by Yellow »

jclins wrote:Yellow -- How do you adjust the height??? I've tried to play around with the following values (cpos.Z and cpos.Y) but got no where fast:

Code: Select all


	// make the camera look to a fixed point (45 degrees down) 
	// in front of it, so it wont stay locked on the old target. 
	vector3df cpos = pCamera->getPosition(); 
	cpos.Z += 2; 
	cpos.Y -= 2; 

Help!
That part of the code just there to make the camera look down in a fixed angle. If you omit it, the camera would keep looking at its previous target.
I havent been using irrlicht for long, so there might be a better methode to do this..

Anyway, if you want to change the height of that cam, just set it correctly after initializing it using pCamera.setPosition()

If you meant like moving op and down dynamicly, I just use the pCamera.setFOV() function linked to the mouse wheel to zoom in.

As for the width / height versus x/y. I just used my own struct to store that data when initializing so i dont have to call getSceenSize() every frame.
guest wrote: Do you have demo? That way this can be posted in the wiki as a tutorial of sort.

Actually, I was wondering why a lot of the posts in this forum doesn't make it into the wiki. It would make it a lot easier to find and use if a lot of these info is written in the wiki as opposed to having to search through the forum to find the info you want. But that's not a bad thing either. I just wished the wiki had more tutorials then it currently contain.
I supose I could fling together a demo if anyone could use that, but I thought this code snippet would be enough.

And I'm not really a wiki person :lol:

Anyway, if wanted, I'll make a cute demo ;)
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

Yellow -- excellent! I'll try your suggestions out as soon as I can figure out how to get rid of the nasty "runtime error R6025 Pure Virtual Function Call" error. A question for you: how does one go about linking the pCamera.setFOV() function to the mouse wheel? Thanks. I would re-interate what guest suggest about putting up an example demo. Well, at least it would help me out. :wink:
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

Post by Yellow »

something like this would do the trick...

Code: Select all

bool CGame::OnEvent(SEvent event)
{
	if (event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_MOUSE_WHEEL)
	{
		pCamera->setFOV(pCamera->getFOV() + (event.MouseInput.Wheel/20));
	}
}
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

Well, thanks to area51, I got rid of the pure virtual function call error. :P

Yellow -- cool deal! I can't wait to try it out. Thanks!
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

Post by Yellow »

Okay, as requested, here is a demo of the above flung together. I'm new to irrlicht, so dont expect to much.
You can download it HERE.
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

Yellow -- How do you move the camera? I can zoom with the mouse wheel but I can't move the camera around. :?
Yellow
Posts: 9
Joined: Sun Jan 29, 2006 11:53 am

Post by Yellow »

You're suposed to be able to move the camera when your mouse cursor is near the edge of the screen. Doesnt that work for you?
jclins
Posts: 86
Joined: Thu Jan 01, 2004 10:29 pm
Location: Texas, USA

Post by jclins »

Yellow -- doh! You're right, it does work with the mouse. I tried using the keyboard for some reason to move the camera (out of habit, I guess). That's so neat. I also like how you used an .ini file to set the video options. :D I plan to use an xml config file. So, is this the beginning of the next Age of Empire/Starcraft game? :wink:
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Wow, this camera is really great! This is 99% exactly like loads of RTS games I've seen, nice job!
Post Reply