Free Flight (space flight) functions

A forum to store posts deemed exceptionally wise and useful

Postby arras » Mon Feb 19, 2007 10:29 am

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.
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Thanks!

Postby dudMaN » Tue Nov 06, 2007 10:15 pm

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
User avatar
dudMaN
 
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Postby arras » Thu Nov 08, 2007 12:23 am

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?
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Postby dudMaN » Thu Nov 08, 2007 1:26 am

Sorry, i got it working now :oops: .

-dudMan
User avatar
dudMaN
 
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Postby arras » Thu Nov 08, 2007 1:51 am

No problem :)
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Postby humbrol » Sat Nov 24, 2007 6:51 am

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

Postby humbrol » Sat Nov 24, 2007 8:05 am

is there a way to move meshes with this?
humbrol
 
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Postby arras » Mon Nov 26, 2007 11:19 am

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.
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Postby TheGameMaker » Mon Nov 26, 2007 7:05 pm

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"^^)
TheGameMaker
 
Posts: 275
Joined: Fri May 12, 2006 6:37 pm
Location: Germany

Postby arras » Tue Nov 27, 2007 5:09 pm

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!
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Postby igorfk » Thu Mar 20, 2008 5:33 am

Excellent help!
What about using two keys simultaneously, to turn the ship while moving? How it would be made?
igorfk
 
Posts: 15
Joined: Sat Mar 08, 2008 10:39 pm

Postby arras » Thu Mar 20, 2008 6:51 am

You simply use boolean keys. There is example code on page 2 of this post which shows that.
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

Postby MasterGod » Sat Mar 29, 2008 12:43 am

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%
User avatar
MasterGod
 
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel

Postby d3jake » Sat Mar 29, 2008 1:50 am

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
User avatar
d3jake
 
Posts: 197
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Postby arras » Sat Mar 29, 2008 8:49 am

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.
arras
 
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia

PreviousNext

Return to FAQs, Tutorials, Howtos, and external tool lists

Who is online

Users browsing this forum: No registered users and 1 guest

cron