I try to make a Maya camera child of a moving node (moved by user input)
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
// event reciever
class MyEventReceiver : public IEventReceiver
{
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main()
{
// create device
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice(EDT_OPENGL,
dimension2d<s32>(1024, 768), 16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// add another object to prove it moves
IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
if (!mesh)
return 1;
IAnimatedMeshSceneNode* sydney = smgr->addAnimatedMeshSceneNode( mesh );
if (sydney)
{
sydney->setMaterialFlag(EMF_LIGHTING, false);
sydney->setMD2Animation(EMAT_STAND);
sydney->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
}
/*
Create the node which will be moved with the WSAD keys. We create a
sphere node, which is a built-in geometry primitive. We place the node
at (0,0,30) and assign a texture to it to let it look a little bit more
interesting. Because we have no dynamic lights in this scene we disable
lighting for each model (otherwise the models would be black).
*/
scene::ISceneNode * node = smgr->addSphereSceneNode();
if (node)
{
node->setPosition(vector3df(0,0,0));
node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
node->setMaterialFlag(EMF_LIGHTING, false);
}
// add a camera and attach it to the node
ICameraSceneNode *camera= smgr->addCameraSceneNodeMaya();
camera->setPosition(vector3df(0,9,-14));
// move with the node
camera->setParent(node);
// hide mouse
device->getCursorControl()->setVisible(false);
int lastFPS = -1;
// In order to do framerate independent movement, we have to know
// how long it was since the last frame
u32 then = device->getTimer()->getTime();
// This is the movemen speed in units per second.
const f32 MOVEMENT_SPEED = 5.f;
while(device->run())
{
// Work out a frame delta time.
const u32 now = device->getTimer()->getTime();
const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
then = now;
/* Check if keys W, S, A or D are being held down, and move the
sphere node around respectively. */
core::vector3df nodePosition = node->getPosition();
if(receiver.IsKeyDown(KEY_KEY_W))
nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_S))
nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
if(receiver.IsKeyDown(KEY_KEY_A))
nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(KEY_KEY_D))
nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
node->setPosition(nodePosition);
// always look at the node
camera->setTarget(node->getPosition());
driver->beginScene(true, true, SColor(255,113,113,133));
smgr->drawAll(); // draw the 3d scene
device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
stringw tmp(L"Movement Example - Irrlicht Engine [");
tmp += driver->getName();
tmp += L"] fps: ";
tmp += fps;
device->setWindowCaption(tmp.c_str());
lastFPS = fps;
}
}
// In the end, delete the Irrlicht device.
device->drop();
return 0;
}
Btw, works fine for normal camera. Doesnt work for FPS (doesnt make sence to use it here anyway)