Free Flight (space flight) functions

A forum to store posts deemed exceptionally wise and useful
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks. I am looking forward to it :)

btw that pinnace project is very old, I did it sme 5 years ago when I was programing with Dark Basic language.
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Thanks!

Post by dudMaN »

Thanks alot! i was just making a flightsim and looking for functions like this :D

However, i'm using the cockpit cam to look just behind the plane, and when i do roll(model,2), i dont roll a little sidways(i use it while turning to add a more realistic effect..), is this something obvious :oops: or a bug?


Thx.

-dudMan
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Excuse me, but I don't really understand your problem. When you roll your plane, it should slip little side ways ...as in real? ...and camera doesn't follow that movement?
Can you please explain it bit more, or perhaps post some code?

Or do you mean you expect such behavior from code I posted?
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Post by dudMaN »

Sorry, i got it working now :oops: .

-dudMan
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

No problem :)
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

got the node moving now, is there a way to smooth out the keyboard controls?

Code: Select all

#include "irrlicht.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

double playerspeed = 0.0;

// 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(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* shuttle = smgr->addSphereSceneNode(); //replace with what ever you want
   if (shuttle)
      shuttle->setMaterialFlag(video::EMF_LIGHTING, false);
      
      scene::ISceneNode* earth = smgr->addSphereSceneNode();
     
   
   
   scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();
   
   while(device->run())
   {
                       
        driver->beginScene(true, true, SColor(255,100,101,140));
        smgr->drawAll();
       
        // direction control
        if(keys[irr::KEY_LEFT])
        {
            turn(shuttle, 0.01);
        }
        if(keys[irr::KEY_RIGHT])
        {
            turn(shuttle, -0.01);
        }
        if(keys[irr::KEY_UP])
        {
            pitch(shuttle, 0.01);
        }
        if(keys[irr::KEY_DOWN])
        {
            pitch(shuttle, -0.01);
        }
        if(keys[irr::KEY_COMMA])
        {
            roll(shuttle, 0.01);
        }
        if(keys[irr::KEY_PERIOD])
        {
            roll(shuttle, -0.01);
        }
       
        // movement control
        if(keys[irr::KEY_KEY_W])
        {
             playerspeed = playerspeed + 0.0001;
             if (playerspeed = 0.01) 
                   playerspeed =0.009;                
            move(shuttle, core::vector3df(0,0,0.01));
        }
        if(keys[irr::KEY_KEY_S])
        {
             playerspeed = playerspeed - 0.0001; 
             if (playerspeed = -0.01)
                    playerspeed = -.009;                
            move(shuttle, core::vector3df(0,0,playerspeed));
        }
        if(keys[irr::KEY_KEY_Q])
        {
             playerspeed = 0;
        }
        
        move(shuttle, core::vector3df(0,0,playerspeed));
       
        makeCockpit(camera, shuttle, core::vector3df(0,7,-30));
       
        driver->endScene();
    }
     
   device->drop();

   return 1;
}
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

is there a way to move meshes with this?
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

to smooth out the keyboard controls
What do you mean by that? To increase turning speed as you hold key?
is there a way to move meshes with this?
If you mean Irrlicht IMesh SMesh and other mesh classes than no, there is no way. But why would you do that? To display mesh you use node.
TheGameMaker
Posts: 275
Joined: Fri May 12, 2006 6:37 pm
Location: Germany

Post by TheGameMaker »

I didn't test it, but when I had a look on the code, I didn't notice anything to prevent a gimbal lock, is that correct?? Has anyone had a problem with that one in this code before? Any of the pros around knows how to avoid it?? (I mean realy not just "have a look for quaternions"^^)
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

There is no way to prevent gimbal lock since it is build in feature of eulers and matrices. So answer to your question is: HAVE A LOOK FOR QUATERNIONS :wink: But your dark mind is apparently closed to their benefits :) No salvation for you out there, you are doomed!
igorfk
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

Post by igorfk »

Excellent help!
What about using two keys simultaneously, to turn the ship while moving? How it would be made?
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

You simply use boolean keys. There is example code on page 2 of this post which shows that.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

arras! You ARE the MAN!
After studying a bit about matrices in relation to 3d graphics I've finally understood your code and converted my horrible math (damn sin/cos and trigo) to your way and everything is working excellent.
Smoothness and accurate-ness. Everything I need is there.
Thank you very much, your code indeed helped me a lot. :)

P.S
I've modified it just a bit to suit my movement animator which made it much more better now :D yay.. (though I didn't upload the changes, [not sure if 'yet'] - but it can all be accessed in my engine and I'll later edit this post with a link to the code)

Edit:
I forgot to say that you no longer need to "updateAbsolutePosition()" in your functions. I'm not sure what was with Irr v1.2/1.3 but I use v1.4 and it works the same without it.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
d3jake
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Post by d3jake »

Agreed! This code helped out my project, though I'm still trying to figure out what EXACTLY one of those functions does... I need to study my matrix math a bit more.
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Glad it is of some help to you both :) It is just matrix math. It does the same as sin/cos math.

MasterGod >> yes its good idea. I am also thinking for few weeks about that ...putting all in to scene node animator. I want to do the same with my cameras (code is not here).

updateAbsolutePosition() was necessary back in older versions of Irrlicht else you got strange results. I do not remember for which version this was coded originally but it was well before 1.0 :)

But do not forget gimbal lock. If you want to avoid it, you should look at quaternions.
Post Reply