Free Flight (space flight) functions
Yes it is bug in cockpit camera code. Also problem with node->getRelativeTransformation() whitch doesnt work. You have to use setRotationDegrees(node->getRotation()) as was already sugested by Zeuss.
Here are corrected functions:There is still problem I cant manage to resolve with camera jumping a bit while turning/pitching/rolling ship. Acording to Paul it is in order of event procesing in Irrlicht. I have all those functions build in classes and I never have noticed it.
[edit 17.02.2007] finaly I have found solution: updateAbsolutePosition() should be called, but don't ask me why...
Here are corrected functions:
Code: Select all
deleted by arras on 17.02.2007
functions at firsth page were updated and should work correctly now
[edit 17.02.2007] finaly I have found solution: updateAbsolutePosition() should be called, but don't ask me why...
Last edited by arras on Sat Feb 17, 2007 7:35 pm, edited 2 times in total.
EXAMPLE CODE
Here is small example program which shows use of all free flight functions. You don't need any media to run it:
[edited 20.03.2008 this code should work with Irrlicht 1.4]
use:
"W" and "S" -move
direction keys -turn and pitch
"," and "." -roll
Here is small example program which shows use of all free flight functions. You don't need any media to run it:
[edited 20.03.2008 this code should work with Irrlicht 1.4]
use:
"W" and "S" -move
direction keys -turn and pitch
"," and "." -roll
Code: Select all
#include "irrlicht.h"
using namespace irr;
// free flight functions
void makeCockpit(irr::scene::ICameraSceneNode *camera,
irr::scene::ISceneNode *node,
irr::core::vector3df offset)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
m.transformVect(frv);
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
m.transformVect(offset);
offset += node->getPosition();
camera->setPosition(offset);
camera->setUpVector(upv);
offset += frv;
camera->setTarget(offset);
camera->updateAbsolutePosition();
}
void move(irr::scene::ISceneNode *node, irr::core::vector3df vel)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vel);
node->setPosition(node->getPosition() + vel);
node->updateAbsolutePosition();
}
void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::matrix4 n;
n.setRotationDegrees(rot);
m *= n;
node->setRotation( m.getRotationDegrees() );
node->updateAbsolutePosition();
}
void turn(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, rot, 0.0f) );
}
void pitch(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(rot, 0.0f, 0.0f) );
}
void roll(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, 0.0f, rot) );
}
// event reciever
bool keys[KEY_KEY_CODES_COUNT];
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent &event)
{
if(event.EventType == EET_KEY_INPUT_EVENT)
{
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
return true;
}
return false;
}
};
int main()
{
for(s32 i=0; i<KEY_KEY_CODES_COUNT; i++) keys[i] = false;
MyEventReceiver receiver;
IrrlichtDevice *device =
createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 32, false, false, false, &receiver);
device->setResizeAble(true);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
scene::ISceneNode* node1 = smgr->addCubeSceneNode(); //replace with what ever you want
if (node1)
node1->setMaterialFlag(video::EMF_LIGHTING, false);
scene::ISceneNode* node2 = smgr->addCubeSceneNode(); //just for reference
if (node2)
node2->setMaterialFlag(video::EMF_LIGHTING, false);
node2->setPosition(core::vector3df(0,0,100));
scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
// direction control
if(keys[irr::KEY_LEFT])
{
turn(node1, -0.1);
}
if(keys[irr::KEY_RIGHT])
{
turn(node1, 0.1);
}
if(keys[irr::KEY_UP])
{
pitch(node1, 0.1);
}
if(keys[irr::KEY_DOWN])
{
pitch(node1, -0.1);
}
if(keys[irr::KEY_COMMA])
{
roll(node1, 0.1);
}
if(keys[irr::KEY_PERIOD])
{
roll(node1, -0.1);
}
// movement control
if(keys[irr::KEY_KEY_W])
{
move(node1, core::vector3df(0,0,0.1));
}
if(keys[irr::KEY_KEY_S])
{
move(node1, core::vector3df(0,0,-0.1));
}
makeCockpit(camera, node1, core::vector3df(0,7,-30));
driver->endScene();
}
device->drop();
return 1;
}
Last edited by arras on Sun Jun 14, 2009 6:21 pm, edited 8 times in total.
when I try to use this code I get a linker error
Code: Select all
[Linker error] undefined reference to `_imp__createDevice'
Do I have to you that makeCockpit? I dont really understand all of the code but I just need the rotate() function for my aircraftgame where the cam is looking at the plane from the side.
It dosnt work... Rotation can not be controlled at all. Is the makeCockpit required or are rotation-function supposed to work on any node, becouse it doesnt.
Thanks
Beam
It dosnt work... Rotation can not be controlled at all. Is the makeCockpit required or are rotation-function supposed to work on any node, becouse it doesnt.
Thanks
Beam
I've managed to fix that problem I changed this
to
and it seems to work
but the camera seems to jump a bit at the start of the movment ie when the button is press the camera moves a little bit worngly but it straightens it self up when it is realeased.
I have tryed many things to fix this but cant seem to get it to work.
Code: Select all
offset += node->getPosition();
Code: Select all
offset -= node->getPosition();
but the camera seems to jump a bit at the start of the movment ie when the button is press the camera moves a little bit worngly but it straightens it self up when it is realeased.
I have tryed many things to fix this but cant seem to get it to work.
Hi all,
I decided to update code in this post since I have finaly found reason for camera behaving bit strangely. For some reason unknown to me and related to howe Irrlicht render everything, afther camera or node change position/rotation updateAbsolutePosition() should be called. This solwe problem so everything runs smooth now.
Also code using gerRelativeTransformation() doesnt work howe I taught it is so I changet everything to use setRotationDegrees() to set up transformation matrix.
Finaly I replaced old TestSceneNode with CubeSceneNode of more recent wersions of Irrlicht in example code.
Code at the begining of this post is the corrected one.
Hope you find this code useful
I decided to update code in this post since I have finaly found reason for camera behaving bit strangely. For some reason unknown to me and related to howe Irrlicht render everything, afther camera or node change position/rotation updateAbsolutePosition() should be called. This solwe problem so everything runs smooth now.
Also code using gerRelativeTransformation() doesnt work howe I taught it is so I changet everything to use setRotationDegrees() to set up transformation matrix.
Finaly I replaced old TestSceneNode with CubeSceneNode of more recent wersions of Irrlicht in example code.
Code at the begining of this post is the corrected one.
Hope you find this code useful
Very cool.
Thanks for sharing.
btw: I saw your site. The Pinnacle looks like a good start.
If I can ever find the time, I been planning on making a ship like that. I'll share once I get it together.
Thanks for sharing.
btw: I saw your site. The Pinnacle looks like a good start.
If I can ever find the time, I been planning on making a ship like that. I'll share once I get it together.
Join us in the Irrlicht chatroom:
[url]irc://irc.freenode.net/irrlicht[/url]
I love deadlines. I like the whooshing sound they make as they fly by. -D.Adams
[url]irc://irc.freenode.net/irrlicht[/url]
I love deadlines. I like the whooshing sound they make as they fly by. -D.Adams