Page 3 of 5

Posted: Mon Feb 19, 2007 10:29 am
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.

Thanks!

Posted: Tue Nov 06, 2007 10:15 pm
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

Posted: Thu Nov 08, 2007 12:23 am
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?

Posted: Thu Nov 08, 2007 1:26 am
by dudMaN
Sorry, i got it working now :oops: .

-dudMan

Posted: Thu Nov 08, 2007 1:51 am
by arras
No problem :)

Posted: Sat Nov 24, 2007 6:51 am
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;
}

Posted: Sat Nov 24, 2007 8:05 am
by humbrol
is there a way to move meshes with this?

Posted: Mon Nov 26, 2007 11:19 am
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.

Posted: Mon Nov 26, 2007 7:05 pm
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"^^)

Posted: Tue Nov 27, 2007 5:09 pm
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!

Posted: Thu Mar 20, 2008 5:33 am
by igorfk
Excellent help!
What about using two keys simultaneously, to turn the ship while moving? How it would be made?

Posted: Thu Mar 20, 2008 6:51 am
by arras
You simply use boolean keys. There is example code on page 2 of this post which shows that.

Posted: Sat Mar 29, 2008 12:43 am
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.

Posted: Sat Mar 29, 2008 1:50 am
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.

Posted: Sat Mar 29, 2008 8:49 am
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.