I'm not very experienced c++ user, and have some Problems with irrlicht.
I made a very small scene(only a cube that can be moved by Keyboard).
The code is below.
And now I have some Questions:
1. My Eventhandler has a Problem with multiple keypress. I can press
"w" and the cube moves forward and with "a" it moves left, but with
"a" and "w" pressed together it don´t moves diagonal.
2. I want to do the camera follows the cube, but I dont have any Idea how to do this.
Thanks.
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
ISceneNode* cube = 0;
ISceneNode* camera = 0;
IrrlichtDevice* device = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown )
{
device->closeDevice();
return true;
}
vector3df v = cube->getPosition();
if (event.KeyInput.Key == KEY_KEY_W)
{
v.Z+=1;
cube->setPosition(v);
return true;
}
if (event.KeyInput.Key == KEY_KEY_S)
{
v.Z-=1;
cube->setPosition(v);
return true;
}
if (event.KeyInput.Key == KEY_KEY_A)
{
v.X-=1;
cube->setPosition(v);
return true;
}
if (event.KeyInput.Key == KEY_KEY_D)
{
v.X+=1;
cube->setPosition(v);
return true;
}
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
device = createDevice(DT_OPENGL, dimension2d<s32>(640, 480),
16, false, false, &receiver);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
cube = smgr->addTestSceneNode();
cube->setPosition(vector3df(0,0,30));
cube->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
camera = smgr->addCameraSceneNode(0, vector3df(cube->getPosition().X,cube->getPosition().Y+5,cube->getPosition().Z-20), vector3df(cube->getPosition()));
device->getCursorControl()->setVisible(false);
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine (%s)(fps:%d)",
driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
device->drop();
return 0;
}
need some help
Your returning prematurely and not allowing all of the if's to process.if (event.KeyInput.Key == KEY_KEY_A)
{
v.X-=1;
cube->setPosition(v);
return true;
}
if (event.KeyInput.Key == KEY_KEY_D)
{
v.X+=1;
cube->setPosition(v);
return true;
}
Code: Select all
if (event.KeyInput.Key == KEY_KEY_A)
{
v.X-=1;
cube->setPosition(v);
}
if (event.KeyInput.Key == KEY_KEY_D)
{
v.X+=1;
cube->setPosition(v);
}
Crud, how do I do this again?
The other thing you'd need to do is to set a state instead of moving it per event.
What's happening is that Irrlicht is catch the "press the button" event and moving the model. Then, each frame, nothing else happens because the event won't be re-triggered till you un-press and re-press the key.
Instead, you'd want to have something like:
Then you would need to update that position every frame. So in your gameloop.
What's happening is that Irrlicht is catch the "press the button" event and moving the model. Then, each frame, nothing else happens because the event won't be re-triggered till you un-press and re-press the key.
Instead, you'd want to have something like:
Code: Select all
if (event.KeyInput.Key == KEY_KEY_A)
{
if (event.KeyInput.PressedDown) {
velocity.X =1;
} else {
velocity.X = 0;
}
Code: Select all
pos = cube->getPosition();
pos.X = pos.X + velocity.X;
cube->setPosition(pos);
Crud, how do I do this again?
Tnx it worked fine.
Therefore was the 3rd Person camera easy to do. But now I made the cube rotating along Y-axis, if I press A or D. But to make a camera that follow the rotation of the cube, was not possible for me, I get only some strange rotations but nothing usefull.
Here is the Code that can only rotate the cube and move it forward and backward.
Maybe someone can help with it, to made a camera that stay always behind the cube.
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
//#include "samplescenenode.h" //only a plane, as a simple terrain
ISceneNode* cube = 0;
ICameraSceneNode* camera = 0;
IrrlichtDevice* device = 0;
vector3df v;
double R;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown )
{
device->closeDevice();
return true;
}
if (event.KeyInput.Key == KEY_KEY_W)
{
if (event.KeyInput.PressedDown)
{
v.Z = 0.01;
} else {
v.Z = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_S)
{
if (event.KeyInput.PressedDown)
{
v.Z =-0.01;
} else {
v.Z = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_A)
{
if (event.KeyInput.PressedDown)
{
R =-0.5;
} else {
R = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_D)
{
if (event.KeyInput.PressedDown)
{
R = 0.5;
} else {
R = 0;
}
}
return true;
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
device = createDevice(DT_OPENGL, dimension2d<s32>(640, 480),
16, false, false, &receiver);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// CSampleSceneNode plane(smgr->getRootSceneNode(), smgr, 666);
// plane.setMaterialTexture(0, driver->getTexture("t351sml.jpg"));
cube = smgr->addTestSceneNode(1.0f);
cube->setPosition(vector3df(0,0.5,0));
cube->setMaterialTexture(0, driver->getTexture("t351sml.jpg"));
camera = smgr->addCameraSceneNode(0, vector3df(cube->getPosition().X,cube->getPosition().Y+0.5f,cube->getPosition().Z-2.0f), vector3df(cube->getPosition()));
device->getCursorControl()->setVisible(false);
camera->setPosition(vector3df(0.0,1.0,-2.0));
int lastFPS = -1;
matrix4 cubemat;
cubemat.makeIdentity();
while(device->run())
{
vector3df cubepos = cube->getPosition();
vector3df cuberot=cube->getRotation();
cuberot.Y+=R;
cube->setRotation(cuberot);
cubemat.setRotationDegrees(cuberot);
vector3df cubetmppos;
cubemat.transformVect((const vector3df) vector3df(0.0,0.0,v.Z), cubetmppos);
cube->setPosition(cubepos+cubetmppos);
//camera->setTarget(cube->getPosition()); // Camera targets at cube
driver->beginScene(true, true, SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine (%s)(fps:%d)",
driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
device->drop();
return 0;
}
Therefore was the 3rd Person camera easy to do. But now I made the cube rotating along Y-axis, if I press A or D. But to make a camera that follow the rotation of the cube, was not possible for me, I get only some strange rotations but nothing usefull.
Here is the Code that can only rotate the cube and move it forward and backward.
Maybe someone can help with it, to made a camera that stay always behind the cube.
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
//#include "samplescenenode.h" //only a plane, as a simple terrain
ISceneNode* cube = 0;
ICameraSceneNode* camera = 0;
IrrlichtDevice* device = 0;
vector3df v;
double R;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown )
{
device->closeDevice();
return true;
}
if (event.KeyInput.Key == KEY_KEY_W)
{
if (event.KeyInput.PressedDown)
{
v.Z = 0.01;
} else {
v.Z = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_S)
{
if (event.KeyInput.PressedDown)
{
v.Z =-0.01;
} else {
v.Z = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_A)
{
if (event.KeyInput.PressedDown)
{
R =-0.5;
} else {
R = 0;
}
}
if (event.KeyInput.Key == KEY_KEY_D)
{
if (event.KeyInput.PressedDown)
{
R = 0.5;
} else {
R = 0;
}
}
return true;
}
return false;
}
};
int main()
{
MyEventReceiver receiver;
device = createDevice(DT_OPENGL, dimension2d<s32>(640, 480),
16, false, false, &receiver);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// CSampleSceneNode plane(smgr->getRootSceneNode(), smgr, 666);
// plane.setMaterialTexture(0, driver->getTexture("t351sml.jpg"));
cube = smgr->addTestSceneNode(1.0f);
cube->setPosition(vector3df(0,0.5,0));
cube->setMaterialTexture(0, driver->getTexture("t351sml.jpg"));
camera = smgr->addCameraSceneNode(0, vector3df(cube->getPosition().X,cube->getPosition().Y+0.5f,cube->getPosition().Z-2.0f), vector3df(cube->getPosition()));
device->getCursorControl()->setVisible(false);
camera->setPosition(vector3df(0.0,1.0,-2.0));
int lastFPS = -1;
matrix4 cubemat;
cubemat.makeIdentity();
while(device->run())
{
vector3df cubepos = cube->getPosition();
vector3df cuberot=cube->getRotation();
cuberot.Y+=R;
cube->setRotation(cuberot);
cubemat.setRotationDegrees(cuberot);
vector3df cubetmppos;
cubemat.transformVect((const vector3df) vector3df(0.0,0.0,v.Z), cubetmppos);
cube->setPosition(cubepos+cubetmppos);
//camera->setTarget(cube->getPosition()); // Camera targets at cube
driver->beginScene(true, true, SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Movement Example - Irrlicht Engine (%s)(fps:%d)",
driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
device->drop();
return 0;
}