[SOLVED]If you derive a class, how can you add it to the sce

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
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

[SOLVED]If you derive a class, how can you add it to the sce

Post by Anthony »

Well title sais it (almost) all.

Am trying to write my own camera class (derived from ICameraSceneNode, duh) and I am wondering how to add that to the manager.

Not sure yet but it seems that the next snippet won't work.

Code: Select all

cMyCam* camera = (cMyCam*)smgr->addCameraSceneNode( 0, vector3df( 0, 0, 10 ), vector3df( 0, 0, 0 ), -1, true );
Edit: Well am sure now, the implementation won't be executed.

Code: Select all

void cMyCam::setPosition( const vector3df& newpos )
{ 
 s32 i = 1; 
 i++;
}
---- SOLVED ----
This would be the correct way.
Get the root node of the scenemanager and pass that as parent.

Code: Select all

cMyCamera *camera = new cMyCam( smgr->getRootSceneNode(), smgr, -1 );
Last edited by Anthony on Sun Jan 09, 2011 11:01 pm, edited 1 time in total.
No :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Code: Select all

void cMyCam::setPosition( const vector3df& newpos ) 
{ 
 s32 i = 1; 
 i++; 
}
Whoa, what the hell is this supposed to do ? :D
Neither it uses newpos reference, nor it returns anything.
And the local variable 'i' is not being used. :?
Working on game: Marrbles (Currently stopped).
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Post by Anthony »

Edit: Well am sure now, the implementation won't be executed.
Using breakpoints you will find out if it is executed. i++; would ignore compilers optimization. Just a quick test, if I may.

And yes this is my final code, why use newpos if you have an eye.

thanks for the time anyway.
No :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

I find your signature pretty ironic ^^

You have derived your class from the ICameraSceneNode class, this class most probably has a constructor, in that constructor you'll probably need to pass the scene manager

so you just create a new instance of you derived camera class, pass the scene manager and the scene manager will take care of the rest
check the custom scene node tutorial
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

Instead of implementing a custom CameraSceneNode, you can simply write an Animator.

Derieve your Animator Class from ISceneNodeAnimator and do whatever you want with the camera in OnEvent() and animateNode().

Then add the animator to a camera like this:

Code: Select all

ICameraSceneNode* camera = smgr->addCameraSceneNode();
CMySceneNodeAnimator* animator = new CMySceneNodeAnimator();
camera->addAnimator(animator);
animator->drop();
Never take advice from someone who likes to give advice, so take my advice and don't take it.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You don't need a scene manager function for adding new scene node classes. Just check the constructors of the existing scene nodes. If you pass the root node (i.e. scene manager) and it will be correctly added.
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Post by Anthony »

thanks, will try that tutorial and look at the animator.

In the mean while I found out

Code: Select all

cMyCamera = new cMyCamera( (cMyCamera&)*smgr->addCameraSceneNode( 0, vector3df( 0, 0, 10 ), vector3df( 0, 0, 0 ), -1, false ) );
does work too.
No :shock: I am no noob, just checking if your not one too :roll:

Thanks to Niko and all others that made Irrlicht possible.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Anthony wrote:thanks, will try that tutorial and look at the animator.

In the mean while I found out

Code: Select all

cMyCamera = new cMyCamera( (cMyCamera&)*smgr->addCameraSceneNode( 0, vector3df( 0, 0, 10 ), vector3df( 0, 0, 0 ), -1, false ) );
does work too.
That's not the right way to do that...
When you call addCameraSceneNode() you're actually creating a CCameraSceneNode, not your own class
With the prefix you've added you're trying to type-cast the CCameraSceneNode to your own class, which can cause undefined (and unwanted) behaviour
Anthony
Posts: 121
Joined: Sun Jan 09, 2011 12:03 pm

Post by Anthony »

yes propably some memoryleaking at least. Also it would slow down the engine if it's not occuring within the set up.

This would be the correct way, logical ;) once you know it.

Code: Select all

cMyCamera *camera = new cMyCam( smgr->getRootSceneNode(), smgr, -1 );
Not yet tested though.

Thanks
Post Reply