follow camera
follow camera
I still dont understand how to attach a normal camera to a scenenode. I thought setParent was for that, but it doesnt work. How would you attach a camera to a node ( so when u move the node the camera would move with it)?
Re: follow camera
that's exactly how setParent would work. all you have to do is create an empty node which is a child of either the scenenode or camera node and update your camera's target to it's location.someguy99 wrote:I still dont understand how to attach a normal camera to a scenenode. I thought setParent was for that, but it doesnt work. How would you attach a camera to a node ( so when u move the node the camera would move with it)?
My company: http://www.kloena.com
My blog: http://www.zhieng.com
My co-working space: http://www.deskspace.info
My blog: http://www.zhieng.com
My co-working space: http://www.deskspace.info
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
in my own proyect i am using this code :
its simple but it works
Code: Select all
ICameraSceneNode *cam= irrlicht_device.smgr->addCameraSceneNode();
cam->setPosition(vector3df(0,9,-14));
cam->setParent(ninja.node);
its simple but it works
Finally some free time.
doesnt seem to work for me.
I just added that to example 4 but the camera doesnt move with the sphere.
I dont think theres a need to post the code , right? Its the same exakple 4 with normal camera instead of FPS...
It does? Maybe you add something else?Lil Margin wrote:in my own proyect i am using this code :Code: Select all
ICameraSceneNode *cam= irrlicht_device.smgr->addCameraSceneNode(); cam->setPosition(vector3df(0,9,-14)); cam->setParent(ninja.node);
its simple but it works
Code: Select all
Camera->setPosition(vector3df(0,9,-14));
Camera->setParent(node);
I just added that to example 4 but the camera doesnt move with the sphere.
I dont think theres a need to post the code , right? Its the same exakple 4 with normal camera instead of FPS...
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
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(video::EDT_OPENGL,
core::dimension2d<s32>(640, 480), 16, false, false, false, &receiver);
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
/*
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(core::vector3df(0,0,30));
node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
}
// add a camera and attach it to the node
ICameraSceneNode *camera= smgr->addCameraSceneNode();
camera->setPosition(vector3df(0,9,-14));
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(irr::KEY_KEY_W))
nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(irr::KEY_KEY_S))
nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
if(receiver.IsKeyDown(irr::KEY_KEY_A))
nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
else if(receiver.IsKeyDown(irr::KEY_KEY_D))
nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
node->setPosition(nodePosition);
driver->beginScene(true, true, video::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)
{
core::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;
}
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
So....
maybe something like this?
before
?
NEW:
Can someone tell me what is parenting for and why cant you just use ?
maybe something like this?
Code: Select all
camera->setTarget(node->getPosition());
Code: Select all
driver->beginScene(true, true, SColor(255,113,113,133));
NEW:
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->addCameraSceneNode();
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;
}
Code: Select all
camera->setTarget(node->getPosition());
Last edited by someguy99 on Tue Oct 20, 2009 11:08 am, edited 1 time in total.
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
Dude, i mean why is it used, what does it actually do. I know those terms.Lil Margin wrote:parenting is used to make your camera a child of node
The latest one works...as for your code...you say it doesnt work but i cant see the problem...
EDIT: Whatever, now it works... weird, so
Code: Select all
camera->setParent(node);
Code: Select all
camera->setTarget(node->getPosition());
Last edited by someguy99 on Tue Oct 20, 2009 11:06 am, edited 2 times in total.
i`ts doesnt bug,
perhaps you made a mistake, in my little experiment any camera follows for any object/
i dont seen you code, but this worked:
when init level:
in render loop:
perhaps you made a mistake, in my little experiment any camera follows for any object/
i dont seen you code, but this worked:
when init level:
Code: Select all
//i try this
scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
//and this
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0,100.0f,0.4f);
// ... some code
// from my_object_loader_func(...):
// object.node = smgr->addAnimatedMeshSceneNode(object.mesh);
// ... some code
player.node->addChild(camera);
Code: Select all
core::vector3df PlayerPos = this->player.node->getPosition();
PlayerPos.Y += 20;
this->player.node->setPosition(PlayerPos);
from Russia with love )))