Usage of setCollisionCallback and other coll. questions

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.
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Usage of setCollisionCallback and other coll. questions

Post by extralite »

I'm trying to figure out how to access the collision response related information provided by its class functions. Basically I don't know where to attach functions like setCollisionCallback. I use createCollisionResponseAnimator the way it is used in example 7 of the engine package. Then add the animator. So far so good, still using the example code. Now I want to add a callback but I get an error because anim is of class ISceneNodeAnimator and not of ISceneNodeAnimatorCollisionResponse.

Code: Select all

        scene::ISceneNodeAnimator* anim;
 
        if (selector)
        {
         anim = smgr->createCollisionResponseAnimator(
                        selector, obj, core::vector3df(25,25,25),
                        core::vector3df(0,-10,0), core::vector3df(0,0,0));
                selector->drop(); // As soon as we're done with the selector, drop it.
                obj->addAnimator(anim);
                anim->setCollisionCallback(printf("bump\n"));
        } 
 
Error message from the compiler:

Code: Select all

main.cpp:201:13: error: ‘class irr::scene::ISceneNodeAnimator’ has no member named ‘setCollisionCallback’
 
Can someone help? Thanks!
Last edited by extralite on Sat Aug 20, 2011 4:58 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct way of using setCollisionCallback?

Post by CuteAlien »

createCollisionResponseAnimator does not just return ISceneNodeAnimator* but ISceneNodeAnimatorCollisionResponse* - and that has the function. And please note that you have to pass a class derived from the ICollisionCallback interface to setCollisionCallback.
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
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

Sorry, I'm still stumped. How do I store both of those returned types at the same time?

As for the callback itself, would this work?

Code: Select all

 
ICollisionCallback * bump()
{
 printf("bump\n");
}
anim->setCollisionCallback(bump());
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct way of using setCollisionCallback?

Post by CuteAlien »

I think you should read up some more about classes and inheritance in c++. ISceneNodeAnimatorCollisionResponse is derived from ISceneNodeAnimator - which means when using ISceneNodeAnimatorCollisionResponse* you have a pointer to both already (derived classes are bascially appended to the base-classes so in memory the reserved block is just a little larger, but still starts with the address of the base-class).
Which means you can pass ISceneNodeAnimatorCollisionResponse* variables to any function expecting ISceneNodeAnimator*.

And as I already wrote in my last post - the callback is a class(!) derived from ICollisionCallback. Not a function returning a pointer to ICollisionCallback *. I won't code that for you - you have to know at least enough c++ to create a class which is derived from another class to be using a c++ engine.
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
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

Thanks, that solved one problem at least. I guess I'll have to read up on C++ classes as I haven't really used C in around ten years. I do mostly web-based stuff like Javascript and PHP nowadays. My current code:

Outside main():

Code: Select all

void bump()
        {
         printf("bump\n");  
        }
 
 
Previous bit:

Code: Select all

scene::ISceneNodeAnimatorCollisionResponse* anim;
        class bump : public scene::ICollisionCallback
        { 
         public:
          bump(); 
        };
 
        if (selector)
        {
         anim = smgr->createCollisionResponseAnimator(
                        selector, ball, core::vector3df(25,25,25),
                        balldirection, core::vector3df(0,0,0));
                selector->drop(); // As soon as we're done with the selector, drop it.
                drop->addAnimator(anim);
                anim->setCollisionCallback(???);
        }
Not sure what to put in the brackets though.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Correct way of using setCollisionCallback?

Post by serengeor »

I strongly suggest to read everything in this page: http://www.learncpp.com/, it should make some things more clear for you :)
Working on game: Marrbles (Currently stopped).
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct way of using setCollisionCallback?

Post by CuteAlien »

Now you have a class called bump with an (still undefined) function bump and additionally an function bump which has nothing in common with the class or it's member so far :-)

And what you pass around are always objects (or pointers to objects). An object is the instance of a class. A class is basically a rubber stamp while the instanced objects are like the pictures you get then pushing that stamp on some paper. Except that instead of pushing stamps on paper you push classes into memory (and to take this metaphor too far - a derived class is like gluing 2 rubber stamps together before using them).

But really - safe yourself the frustration and learn a little more c++ first before trying to use it.
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
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

I learned all this class stuff back in 1998 but wasn't using it much afterwards. So I forgot a lot of it, sorry.

Anyway, this is my current code. It compiles fine. But it doesn't do anything.

Code: Select all

 
        class deflect : public scene::ICollisionCallback
        { 
         public:
          virtual bool onCollision(const irr::scene::ISceneNodeAnimatorCollisionResponse& animator)
          {
            printf("bump\n");
          }
        };
 
 

Code: Select all

        if (selector)
        {
         deflect *bump;
         ballanim = smgr->createCollisionResponseAnimator(
                        selector, ball, core::vector3df(25,25,25),
                        balldirection, core::vector3df(0,0,0));
                selector->drop(); // As soon as we're done with the selector, drop it.
                ball->addAnimator(ballanim);
                ballanim->setCollisionCallback(bump);
        } 
 
 
I would just use collisionOccurred (which I'm also doing at the moment) but it often misses the occurrance and only does what it's supposed to do in 1 out of 5 cases. So I'm trying to get this callback to work because it should catch the collision always and process it right away which is better than to poll for collisionOccurred in the doScene loop.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Correct way of using setCollisionCallback?

Post by CuteAlien »

Ok now you have a class looking as it should. And you have a variable bump of type deflect* which is uninitializes - basically meaning that it can point to any place in the universe. You are passing that to the function which will now use this unitialized memory and acting like there would be a deflect object at that place (very unlikely as it just points to some random place). I'm surprised this does not crash. But go on - you're getting closer to get this working, but really not the way to code a whole game :-)
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
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

EDIT: Code edited! Problem summary edited!

Okay so I guess I need to pass the ballanim variable to a member of the class first?

Code: Select all

        class deflect : public scene::ICollisionCallback
        { 
         public:
          scene::ISceneNodeAnimatorCollisionResponse* animator;
          virtual bool onCollision(const irr::scene::ISceneNodeAnimatorCollisionResponse& animator)
          {
            printf("bump\n");
          };
        };
 
 

Code: Select all

        scene::ISceneNodeAnimatorCollisionResponse* ballanim;
        if (selector)
        {
         ballanim = smgr->createCollisionResponseAnimator(
                        selector, ball, core::vector3df(25,25,25),
                        balldirection, core::vector3df(0,0,0));
         deflect *bump;
         bump->animator = ballanim;
                selector->drop(); // As soon as we're done with the selector, drop it.
                ball->addAnimator(ballanim);
                ballanim->setCollisionCallback(bump);
        } 
 
 
 
This gives me a memory access error. :(

BTW I think the reason why it didn't crash before is because although the animator variable didn't point anywhere I also didn't use it in the function at all. Why didn't it execute the printf though?
Last edited by extralite on Sat Aug 20, 2011 8:30 am, edited 1 time in total.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Correct way of using setCollisionCallback?

Post by serengeor »

extralite wrote:Okay so I guess I need to put the ballanim variable inside the class?
deflect *bump;

This gives me a memory access error. I guess because I'm passing the same class to a member of the class it was derived from?

BTW I think the reason why it didn't crash before is because although the animator variable didn't point anywhere I also didn't use it in the function at all. Why didn't it execute the printf though?
A pointer is a variable that holds the address of another variable.
http://www.learncpp.com/cpp-tutorial/67 ... -pointers/

As CuteAlien tried to say, your "bump" pointer is pointing to some random memory so it crashes when you try to access it.
You need to create the object of type deflect and assign it's address in memory to "bump" pointer to be able to use it.
Last edited by serengeor on Sat Aug 20, 2011 8:35 am, edited 1 time in total.
Working on game: Marrbles (Currently stopped).
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

serengeor wrote:A pointer is a variable that holds the address of another variable.
http://www.learncpp.com/cpp-tutorial/67 ... -pointers/
I know what a pointer is. Doesn't mean I can use them correctly, lol. In C I mean. Implementation of pointers is much more lenient in Javascript.

How about my new attempt? See edit.
As CuteAlien tried to say, your "bump" pointer is pointing to some random memory so it crashes when you try to access it.
It didn't crash when he said it. It's only crashing now that I try to actually pass something for the animator parameter.
Last edited by extralite on Sat Aug 20, 2011 8:45 am, edited 1 time in total.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Correct way of using setCollisionCallback?

Post by serengeor »

Still the same, see my edit.
As CuteAlien tried to say, your "bump" pointer is pointing to some random memory so it crashes when you try to access it.
You need to create the object of type deflect and assign it's address in memory to "bump" pointer to be able to use it.
Working on game: Marrbles (Currently stopped).
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Correct way of using setCollisionCallback?

Post by serengeor »

extralite wrote:Implementation of pointers is much more lenient in Javascript.
Java script has pointers? I thought there were only references :roll:

(sorry for double post :\)
Working on game: Marrbles (Currently stopped).
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

Okay, I added a "= 0" for initialization.

Code: Select all

        scene::ISceneNodeAnimatorCollisionResponse* ballanim;
        if (selector)
        {
         ballanim = smgr->createCollisionResponseAnimator(
                        selector, ball, core::vector3df(25,25,25),
                        balldirection, core::vector3df(0,0,0));
         deflect *bump = 0;
         bump->animator = ballanim;
                selector->drop(); // As soon as we're done with the selector, drop it.
                ball->addAnimator(ballanim);
                ballanim->setCollisionCallback(bump);
        } 
 
 
This line gave me the memory access error before:

Code: Select all

         bump->animator = ballanim;
 
Now it's giving me a compilation error:

Code: Select all

main.cpp:206:20: error: no match for ‘operator=’ in ‘bump->deflect::animator = ballanim’
 
Which is odd, since both ballanim and animator in the class are of the same type, pointer to ISceneNodeAnimatorCollisionResponse.

As for references in Javascript, yes they're not really the same but they share that characteristic that they will change values when passed to a function as opposed to passing the values themselves. So I associated them with pointers in C. Because they're basically more forgiving pointers.
Post Reply