Getting to grips with Co-ord system.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
pararealist
Posts: 6
Joined: Wed Feb 06, 2013 9:10 am
Location: UK

Getting to grips with Co-ord system.

Post by pararealist »

If you are like me, old, used to the other co-ord system, and new to Irrlicht, this may be of use or help.
I created this to help me with it, and also learn some of the irrlicht syntax before attempting anything grandios.
You can use any model you have with it.
If anyone would like to host the model i use, pm me and i will send it. It is a 3d gizmo.

Code: Select all

 
// Select demo
 
#define tutorial_1 // Testing object and camera move and rotation translations.
 
//-------------------------------------------------------------------------------------
#ifdef tutorial_1
 
#include "StdAfx.h"
 
//for windows users.
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
 
#include <irrlicht.h>
 
using namespace irr;
 
//-------------------------------------------------------------------------------------
class EventReceiver : public IEventReceiver
{
    public:
    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
    // Remember whether each key is down or up
    if (event.EventType == irr::EET_KEY_INPUT_EVENT)
        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
    return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    EventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
        KeyIsDown[i]= false;
    }
 
    private:
    // We use this array to store the current state of each key
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
 
//-------------------------------------------------------------------------------------
int main()
{
    //---------------------------------------------------------
    //set video type.
    //video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
    video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
 
    //---------------------------------------------------------
    // create event receiver.
    EventReceiver receiver;
 
    //---------------------------------------------------------
    //create device.
    IrrlichtDevice* device = createDevice(driverType, 
                        dimension2d<u32>(1024, 668), 16, false, false, false, &receiver);
 
    if (device == 0) return 1; // could not create selected driver.
 
    //create video driver.
    video::IVideoDriver* driver = device->getVideoDriver();
 
    //create scene manager.
    scene::ISceneManager* smgr = device->getSceneManager();
 
    //---------------------------------------------------------
    //create gizmo, rotation using keys. 
    scene::IAnimatedMeshSceneNode* camdummy =
        smgr->addAnimatedMeshSceneNode(smgr->getMesh("content/move.obj"));
 
    if (camdummy)
    {
        //camdummy>setScale(vector3df(0.1f,0.1f,0.1f));
        camdummy->setPosition(vector3df(0,0,0.1f));
        camdummy->setMaterialFlag(video::EMF_LIGHTING, false);
    }
 
    //---------------------------------------------------------
    //create GUI_Manager.
    gui::IGUIEnvironment* guienv = device->getGUIEnvironment();
    //get a gui font.
    gui::IGUIFont* font = guienv->getFont("content/fontcourier.bmp");
 
    //---------------------------------------------------------
    //create a camera.
    ////ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
    ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(0, 50.0f, 0.1f), vector3df(0,0,0));
    
    //set initial FOV.
    camera->setFOV(PI / 2.5f);
    
    //enable mouse cursor.
    device->getCursorControl()->setVisible(true);
    
    //---------------------------------------------------------
    int lastFPS = -1;
    //get last frame.
    u32 then = device->getTimer()->getTime();
 
    //movement speed in units per second.
    const f32 MOVEMENT_SPEED = 30.f;
    //position/rotation vectors.
    vector3df nodePosition;
    vector3df nodeRotation;
 
 
    //---------------------------------------------------------
    while(device->run())
    {
        //calc a frame delta time.
        const u32 now = device->getTimer()->getTime();
        const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
 
        //------------------------------------------------------
        //exit on esc.
        if(receiver.IsKeyDown(irr::KEY_ESCAPE))
            ::exit(1);
 
        //move cam dummy node.
        //------------------------------------------------------
        if(receiver.IsKeyDown(irr::KEY_KEY_W))//front/back
        nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_S))
        nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
 
        if(receiver.IsKeyDown(irr::KEY_KEY_Q)) //left/right
        nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_E))
        nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
 
        if(receiver.IsKeyDown(irr::KEY_KEY_C))//up/dn
        nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_V))
        nodePosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
 
        //------------------------------------------------------
        camdummy->setPosition(nodePosition);
        //------------------------------------------------------
        if(receiver.IsKeyDown(irr::KEY_KEY_A)) //pan
        nodeRotation.Z += (MOVEMENT_SPEED*2) * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_D))
        nodeRotation.Z -= (MOVEMENT_SPEED*2) * frameDeltaTime;
 
        if(receiver.IsKeyDown(irr::KEY_KEY_R)) //tilt
        nodeRotation.X += MOVEMENT_SPEED * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_T))
        nodeRotation.X -= MOVEMENT_SPEED * frameDeltaTime;
 
        if(receiver.IsKeyDown(irr::KEY_KEY_F)) //roll
        nodeRotation.Y += MOVEMENT_SPEED * frameDeltaTime;
        else if(receiver.IsKeyDown(irr::KEY_KEY_G))
        nodeRotation.Y -= MOVEMENT_SPEED * frameDeltaTime;
        //------------------------------------------------------
        camdummy->setRotation(nodeRotation);
        //------------------------------------------------------
 
        driver->beginScene(true, true, video::SColor(255,113,113,133));
 
        smgr->drawAll(); // draw the 3d scene
        device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
 
        //------------------------------------------------------
        //String
        stringw tmp(L"Camera Position and Rotation.");
        tmp += L"\nCam Move X: "; tmp += camera->getPosition().X;
        tmp += L"\nCam Move X: "; tmp += camera->getPosition().Y;
        tmp += L"\nCam Move X: "; tmp += camera->getPosition().Z;
        //
        tmp += L"\nCam Rot X: "; tmp += camera->getRotation().X;
        tmp += L"\nCam Rot X: "; tmp += camera->getRotation().Y;
        tmp += L"\nCam Rot X: "; tmp += camera->getRotation().Z;
        tmp += L"\n";
        //
        tmp += L"\nDummy Position and Rotation."; 
        tmp += L"\nDummy  Move X: "; tmp += camdummy->getPosition().X;
        tmp += L"\nDummy  Move Y: "; tmp += camdummy->getPosition().Y;
        tmp += L"\nDummy  Move Z: "; tmp += camdummy->getPosition().Z;
        //
        tmp += L"\nDummy  Rot X: "; tmp += camdummy->getRotation().X;
        tmp += L"\nDummy  Rot Y: "; tmp += camdummy->getRotation().Y;
        tmp += L"\nDummy  Rot Z: "; tmp += camdummy->getRotation().Z;
        tmp += L"\n";
        //
        tmp += L"\nKeys."; 
        tmp += L"\nW + S: Y Move,  Q + E: X Move, C + V: X Move ";
        tmp += L"\nA + D: Z Rot,  R + T: X Rot, F + G: Y Rot ";
 
        //display text.
        if(font)
        font->draw(tmp,rect<s32>(10,10,300,50),SColor(255,155,255,255));
 
        guienv->drawAll();
 
        //------------------------------------------------------
        driver->endScene();
        //------------------------------------------------------
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            stringw tmp(L"Movement Example - Irrlicht Engine [");
            tmp += driver->getName();
            tmp += L"] fps: ";
            tmp += fps;
 
            device->setWindowCaption(tmp.c_str());
            lastFPS = fps;
        }
    }
    //at end, delete the device.
    device->drop();
 
    return 0;
}
//Done. 
#endif //#ifdef define tutorial_1
 
pararealist now.
Post Reply