Page 2 of 5

Posted: Fri Apr 15, 2005 5:10 pm
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);

Posted: Thu Apr 21, 2005 7:47 am
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...

Posted: Thu Apr 21, 2005 7:53 am
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;
}

Posted: Thu Apr 21, 2005 6:25 pm
by Natol7777
when I try to use this code I get a linker error

Code: Select all

  [Linker error] undefined reference to `_imp__createDevice' 

Posted: Fri Apr 22, 2005 11:16 am
by arras
May be your have to set up your compiler (linker). I use DevC++ to compile code above.

Posted: Sun Jul 31, 2005 1:36 pm
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

Posted: Fri Aug 19, 2005 2:37 am
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.

Posted: Fri Aug 19, 2005 2:42 am
by Browndog
I get the exact same problem the camera moves in the wrong direction. cant work out why.

Posted: Fri Aug 19, 2005 3:35 am
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.

Posted: Fri Aug 19, 2005 4:01 am
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.

Posted: Thu Sep 29, 2005 2:30 am
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?

Posted: Tue Mar 21, 2006 7:20 am
by Browndog
I've total seperated the offset from the target vector and it makes the camera 100% smooth.

Posted: Sun Mar 26, 2006 2:58 am
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

Posted: Sat Feb 17, 2007 7:10 pm
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 :)

Posted: Mon Feb 19, 2007 6:00 am
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. :)