Access scene nodes from out of Main.cpp

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.
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

main.cpp:

Code: Select all

 
// head of the file before this
 
int main()
{
    IrrlichtDevice* device = createDevice( video::EDT_OPENGL, dimension2d<u32>(640, 480), 16, false, false, false, 0);
    if (!device) return 1;
 
 
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
 
 
    ICameraSceneNode *camera = smgr->addCameraSceneNode();
    MyEventReceiver receiver;
    receiver.setCamera(camera);
 
    camera->setPosition(vector3df(20, 50, -50));
    camera->setTarget(vector3df(25, 35, -40));
 
    device->setEventReceiver(&receiver);
 
// The rest doesn't matter
 
receiver.cpp:

Code: Select all

 
//head of the file before this
 
void MyEventReceiver::setCamera(ICameraSceneNode *passedCamera)
{
    intcamera = passedCamera;
}
 
 
bool MyEventReceiver::OnEvent (const SEvent& event)
{
    float oldMousePos[2];
    float newMousePos[] = {event.MouseInput.X, event.MouseInput.Y};
    float mouseMove []= {newMousePos[0] - oldMousePos[0], newMousePos[1] - oldMousePos[1]};
    if (event.EventType == irr::EET_KEY_INPUT_EVENT)
              KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
 
    // if (event.EventType == EET_MOUSE_INPUT_EVENT)
    // {
 
            intcamera->setPosition(vector3df(-1000, -1000, -1000));
 
    // }
    return false;
}
 
// the rest doesn't matter
 
My problem was that I had declared MyEventReceiver *receiver, instead of MyEventReceiver receiver. Then I didn't initialize receiver, it was an uninitialized pointer which didn't give a compiler error but caused a crash when I called the receiver -> setCamera(camera).

Thanks for the help though. :) Now I'm getting no effect on using intcamera->setPosition... This is 90% perpetual debugging after all, heh? :P

Off I go to perpetual debugging then... ;)
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Access scene nodes from out of Main.cpp

Post by CuteAlien »

Looking mostly fine so far. intcamera->setPosition should have an effect, maybe it's happening so fast that you don't see the difference (now called on every event, without comments called on every mouse-event,aka every time the mouse is moved)? Or you don't have objects in the scene to see the difference?

oldMousePos will probably not work as expected as it's scope is the OnEvent function - meaning it will get created on every call of the function, so you probably want to put that in the class itself so it stays valid as long as the class-object (and then set it to newMousePos after you have figure out mouseMove). Also initialize every variable (for example oldMousePos) unless you have a really good reason not to do so (for a start just do it always), that will make your life a lot easier in the long run as then you then you can always know what value variables should have (right now it starts with a random value of whatever is in the memory at that place at the time of creation).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Access scene nodes from out of Main.cpp

Post by Mel »

The good old "scrap" value :)

Irrlicht also has some built in classes that help a lot when handling the vectors and stuff. take a look at the docs, and read about the vector2df class, that way you won't need to declare float xxx[] vars. It also makes your code safer and more portable.

Take a look for instance:

Code: Select all

 
core::vector2d oldMousePos, newMousePos;//take these as class attributes.
...
//this is inside the onEvent method.
oldMousePos = newMousePos;
newMousePos = core::vector2d(event.MouseInput.X,event.MouseInput.Y);
core::vector2df mouseMove = newMousePos-oldMousePos;
...
 
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

It turns out the Irrlicht scene I was loading had an active camera already (set up through IrrEdit), so in spite of adding a camera with exactly the same position and target, It wasn't the active one, it was a clone. So, I was changing the position of the clone (inactive) camera.

Fixed it with

Code: Select all

ICameraSceneNode *camera = smgr->getActiveCamera();
Yes, you're right, the oldMousePos should be a class member. Thanks for the tip, I'll correct that.

Thanks for the help!! :D It's running the way I want now.
booe
Posts: 76
Joined: Thu Jul 29, 2010 2:12 pm

Re: Access scene nodes from out of Main.cpp

Post by booe »

What about LEARNING C++ instead of HAUNTING MY DEAR SHEEP-CUTIE?! She's got a lot of important thing to do! You wolf bastard! Boooo!
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

?... Are you high?
Post Reply