Free Flight (space flight) functions

A forum to store posts deemed exceptionally wise and useful
garwen
Posts: 3
Joined: Thu Mar 31, 2005 6:24 pm
Location: Poland

Post by garwen »

For me the camera didn't worked well, it was always looking in the direction of the node. I have added offset in the camera target and it's ok now.

camera->setTarget(node->getPosition() + offset + frv);
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

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:

Code: Select all

deleted by arras on 17.02.2007
functions at firsth page were updated and should work correctly now
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...
Last edited by arras on Sat Feb 17, 2007 7:35 pm, edited 2 times in total.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

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

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.
Natol7777

Post by Natol7777 »

when I try to use this code I get a linker error

Code: Select all

  [Linker error] undefined reference to `_imp__createDevice' 
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

May be your have to set up your compiler (linker). I use DevC++ to compile code above.
Beam

Post by Beam »

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
Guest

Post by Guest »

I have use this camera but I have a problem with move() the ship node moves the correct way but the camera moves in the opposite direction. I cant seem to work out why? As an experiment I used your code exactyl as is and I get the same result the camera is moving the wrong way.
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

I get the exact same problem the camera moves in the wrong direction. cant work out why.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

is the camera moving backwards, or is it just not moving, think you need to update the camera with the same transformations you're applying to the node.
Image
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

I've managed to fix that problem I changed this

Code: Select all

offset += node->getPosition();
to

Code: Select all

offset -= node->getPosition();
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.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

I'm having problems with these two lines

m.setRotationDegrees(node->getRotation());


I've made my app rather custom but the error's keep pointing to these lines and can't figuare out why I'm using irrlicht 12 anyone have a clue?
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

I've total seperated the offset from the target vector and it makes the camera 100% smooth.
Mark_Solo
Posts: 10
Joined: Sun Sep 25, 2005 6:30 pm

Post by Mark_Solo »

teh code avove maybe did not complie due the lack of the pragma sentence

add it a #pragma comment (lib,"irrlicht.lib"); and theres no drama :D
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

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 :)
luckymutt
Posts: 453
Joined: Sun Mar 06, 2005 11:56 pm
Location: C-Ville

Post by luckymutt »

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. :)
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
Post Reply