Rotate Camera

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
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Rotate Camera

Post by crackingod »

Hi!
I want to rotate my camera 90 degrees left or right, but I can't find how to do that, setRotation doesn't seem to work.
Any help?
Cheers
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Try using the setTarget - Should work

BTW: Which camera type are you using?
Call me Wice, Miami Wice!
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

setTarget doesn't seem to work either.
I'm using addCameraSceneNode()
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

You sure you got the right pointer?
That camera should have no problems what so ever both rotating and targeting.
Call me Wice, Miami Wice!
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

mmm
Well I think I'm doing it right:

Code: Select all

ICameraSceneNode	*camera;
then

Code: Select all

camera	 =	smgr->addCameraSceneNode();
then

Code: Select all

gameCore.camera->setPosition(vector3df(0,500,-250));
gameCore.camera->setTarget(vector3df(0,0,0));
(In fact even the setTarget here doesn't work. setPosition does though

And finnaly in my EventReceiver:

Code: Select all

		case EGET_BUTTON_CLICKED:
			if(id == 101)
			{
			camera->setRotation(vector3df(0,90,0));
			}
AussieMike
Posts: 10
Joined: Fri May 05, 2006 10:38 am
Location: Australia

Post by AussieMike »

Code: Select all

camera    =   smgr->addCameraSceneNode();
I believe you can only rotate that camera using the setTarget() function.
How would you know if setTarget(vector3df(0,0,0)) is working or not?
I thought that was the default heading of a camera???,
try setTarget(vector3df(100,500,-250));

I was told setRotation() is only for smgr->addCameraSceneNodeFPS()

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=13080
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Actually the default target of the camera is (0, 0, 100) - according to the docs anyways:

Code: Select all

addCameraSceneNode (ISceneNode *parent=0, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &lookat=core::vector3df(0, 0, 100), s32 id=-1)=0
Call me Wice, Miami Wice!
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

Yup I tried with different values of setTarget() and it doesnt change anything.
Quite weired!
Any idea? Cos I'm quite stuck with my game if I can't rotate my camera (citybuilder, rts like camera which works perfectly well except for zooming and for this)
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

Please I really need help on this one... :?
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Are you absolutly 100 % sure that it isnt working?? Cuz sometimes it just seems like it, since "the code works in mysterius way", and sometimes actually does work! :)
Try debug and check that the values are indeed not changing. Dont just trust your eye on this one!

Anyways, maybe if you could show us your code it would help a bit understanding what you posibly could be doing wrong.
Try pasting you code here: http://pastebin.co.uk/
Then link us with code.
Call me Wice, Miami Wice!
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

I can't manage to see their anti spam image so I'll just post it here:

Main:

Code: Select all

#include "CGameCore.h"


int main()
{
	CGameCore gameCore;
	if (!gameCore.GameInit()) 
	{
		return 0;
	}

	IAnimatedMesh* mesh;
	ISceneNode* node;
	gameCore.guienv = gameCore.device->getGUIEnvironment();
	mesh = gameCore.smgr->getMesh("test.3ds");
	node = gameCore.smgr->addAnimatedMeshSceneNode(mesh);

	gameCore.device->setEventReceiver(&gameCore);
	node->setScale(vector3df(2.0f,0.8f,2.0f));
	node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
	node->setMaterialFlag(video::EMF_LIGHTING, false);

	IGUIImage *map;
	IGUITabControl* tabctrl = gameCore.guienv->addTabControl(core::rect<int>(0,320,640,480),0, true, true);
	IGUITab* optTab = tabctrl->addTab(L"Info");
	IGUITab* aboutTab = tabctrl->addTab(L"Construction");
	map = gameCore.guienv->addImage(gameCore.driver->getTexture("minimap.tga"),position2d<s32>(500,5),true,optTab);
	IGUIButton* button1 = gameCore.guienv->addButton(rect<s32>(30,30,60,60),optTab,101,L"Rotate");


	gameCore.camera->setPosition(vector3df(0,500,-250));
	gameCore.camera->setTarget(vector3df(100,500,-250));

	while (gameCore.GameLoop())
	{	
		gameCore.CameraUpdate();
		gameCore.Render();
	}

	return 0;
}
CGameCore.h

Code: Select all

#ifndef __CGameCore_h__
#define __CGameCore_h__

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

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")


struct Screen
{
	int X;
	int Y;
};

class CGameCore : public IEventReceiver
{

public:
	IrrlichtDevice		*device;
	IVideoDriver		*driver;
	ISceneManager		*smgr;
	ICameraSceneNode	                *camera;
	IGUIEnvironment		*guienv;
	Screen screen;
	
	CGameCore();
	virtual ~CGameCore(void);
	bool GameInit();
	void Destroy();
	void Render();
	bool GameLoop();
	void CameraUpdate();
	
	
	bool fullscreen;
	int bitdepth;

	virtual bool OnEvent(SEvent event);
private:
	int cursx;
	int cursy;
	vector3df camerapos;

};

#endif
GameCore.cpp

Code: Select all

#include "CGameCore.h"

CGameCore::CGameCore()
{
	fullscreen = true;
	bitdepth = 32;
}

CGameCore::~CGameCore()
{
	Destroy();
}

bool CGameCore::GameInit()
{
	screen.X=640;
	screen.Y=480;
	device	 =	createDevice(EDT_OPENGL, dimension2d<s32>(screen.X, screen.Y),bitdepth,fullscreen,true,true,0);
		if (device == 0) return false;
	driver	 =  device->getVideoDriver();
	smgr	 =	device->getSceneManager();
	camera	 =	smgr->addCameraSceneNode();


	guienv	 =	device->getGUIEnvironment();

	quit = false;
	return true;
}

void CGameCore::Destroy()
{
	if	(device)
	{
		device->closeDevice();
	}
}


void CGameCore::Render()
{
	if	(!device->isWindowActive())	return;
	  driver->beginScene(true, true, 0);

	  smgr->drawAll();

      guienv->drawAll();
      driver->endScene();
}

bool CGameCore::GameLoop()
{
	if (!device->run())
	{
		return false;
	}
	return true;
}


bool CGameCore::OnEvent(SEvent event)
{
	if(event.EventType==EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
		guienv = device->getGUIEnvironment();
		
		switch(event.GUIEvent.EventType)
		{
		case EGET_BUTTON_CLICKED:
			if(id == 101)
			{
			camera->setRotation(vector3df(0,90,0));
				return false;
			}
			break;
		}
	}

	return false;

}

void CGameCore::CameraUpdate()
{
		
	camerapos =  camera->getPosition();

	camerapos.Z += 2;
	camerapos.Y -= 2;

	camera->setTarget(camerapos);

	cursx = device->getCursorControl()->getPosition().X;
	cursy = device->getCursorControl()->getPosition().Y;
	
	float distance = 5.0;

	if (cursx > (screen.X - 40))
	{	
		camerapos = camera->getPosition();				
		camerapos.X += distance;
		camera->setPosition(camerapos);								
	}
	else if (cursx < 40)
	{	
		camerapos = camera->getPosition();				
		camerapos.X -= distance;
		camera->setPosition(camerapos);							
	}
	if (cursy < 40)
	{
		camerapos = camera->getPosition();				
		camerapos.Z += distance;
		camera->setPosition(camerapos);								
	}
	else if (cursy > (screen.Y - 40))
	{	
		camerapos = camera->getPosition();				
		camerapos.Z -= distance;
		camera->setPosition(camerapos);							
	}
	
	
}
Any help?
Thanks a lot
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

mhm.. Do you think this might have someting to do with your problem:

Code: Select all

void CGameCore::CameraUpdate()
{
camerapos =  camera->getPosition();

camerapos.Z += 2;
camerapos.Y -= 2;

camera->setTarget(camerapos);

...

}
yes? ;)
Call me Wice, Miami Wice!
crackingod
Posts: 15
Joined: Fri Aug 04, 2006 11:36 am

Post by crackingod »

I do feel quite stupid here :D
But I need that bit of code for my mouse moved camera, otherwise it just keeps looking at the target.
I'll have to see how to work around that!
Anyway thanks a lot...what an idiot I am :(
Post Reply