MoveCamera with Key_Key_W with Very Little Code,Possible?

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
ElevatorOperator

MoveCamera with Key_Key_W with Very Little Code,Possible?

Post by ElevatorOperator »

Okay, I only pasted part of my code here because this is the part I believe to be giving me errors. The NODE moves correctly. However, when I press W, I was wanting my Camera to change positions accordingly also. However, I tried using Camera->Update() and the compile, DevC++ gave me an error. So I just left the Update thing out. When I run the code below, it works just fine, when I hit S, Node moves back, A, left, D, right. W.. well when W is pressed, the window crashes. Anyone know the SIMPLEST way to move an FPS modified camera? I just want my Camera attatched to Player. For it to be EXACTLY behind player, always Targeting on Player, and a little bit higher in the Y Axis, than Player.

All help appreciated.

Code: Select all


#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>

using namespace irr;
int lcamx;
int lcamy;
int lcamz;

#pragma comment(lib, "Irrlicht.lib")



scene::ISceneNode* player = 0;
IrrlichtDevice* device = 0;
scene::ICameraSceneNode* camera = 0;


class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
	
		if (event.EventType == irr::EET_KEY_INPUT_EVENT&&
			event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
                                      
			case KEY_KEY_W:
                {
				     core::vector3df w = player->getPosition();
				     core::vector3df cam = camera->getPosition();
				     cam.Z -= 1;
				     w.Z -= 1;
				     camera->setPosition(cam);
			         player->setPosition(w);
					 return true;
				}
			case KEY_KEY_S:
				{
                     core::vector3df s = player->getPosition();
				     s.Z += 1;
				     player->setPosition(s);
				     return true;
				}
			case KEY_KEY_A:
                {
					 core::vector3df a = player->getPosition();
					 a.X += 1;
					 player->setPosition(a);
					 return true;
				}	
			case KEY_KEY_D:
                {   
                     core::vector3df D = player->getPosition();
                     D.X -= 1;
                     player->setPosition(D);
                     return true;
			    }
            
			case KEY_KEY_F:
                {
					 core::vector3df f = player->getPosition();
					 f.Y += 1;
					 player->setPosition(f);
					 return true;
				}
            case KEY_KEY_V:
                {
					 core::vector3df v = player->getPosition();
					 v.Y -= 1;
					 player->setPosition(v);
					 return true;
				}    	
            }
		}

		return false;
	}
};

int main()
{
	MyEventReceiver receiver;

	device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600),
		16, false, false, false, &receiver);

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();




	player = smgr->addTestSceneNode(1,0,-1,

    
    
        
    
    core::vector3df(0.0f,0.0f,0.0f));
    
    
    
    
    
    
	player->setPosition(core::vector3df(0,1,0));
    player->setMaterialTexture(0, driver->getTexture("media/dummy.bmp"));

	//device->getCursorControl()->setVisible(false);





scene::ICameraSceneNode* camera = 
		smgr->addCameraSceneNodeFPS(0,100.0f,1200.0f);
core::vector3df px = player->getPosition();

lcamx = px.X;
lcamy = px.Y;
lcamz = px.Z;
	camera->setPosition(core::vector3df(lcamx,lcamz + 3,lcamy + 2));
	camera->setTarget(player->getPosition());
	camera->setFarValue(10000.0);
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Post by puh »

Read this first: http://irrlicht.sourceforge.net/phpBB2/ ... php?t=8162
And then choose appropriate forum (but definetly not "FAQs, Howtos, and tool lists")
afecelis
Admin
Posts: 3075
Joined: Sun Feb 22, 2004 10:44 pm
Location: Colombia
Contact:

Post by afecelis »

no questions here.

moving to beginners help.
William Finlayson
Posts: 61
Joined: Mon Oct 25, 2004 12:11 am

Post by William Finlayson »

Change

Code: Select all

scene::ICameraSceneNode* camera =
      smgr->addCameraSceneNodeFPS(0,100.0f,1200.0f);
to

Code: Select all

camera =  smgr->addCameraSceneNodeFPS(0,100.0f,1200.0f); 
You were creating the camera with local scope rather than refering to your previously defined pointer, so the camera variable that the event receiver was using was undefined.
Post Reply