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.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Correct way of using setCollisionCallback?

Post by serengeor »

You can't access an object that has not been created.

You could do

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 = new deflect;
         bump->animator = ballanim;
                selector->drop(); // As soon as we're done with the selector, drop it.
                ball->addAnimator(ballanim);
                ballanim->setCollisionCallback(bump);
        }
but this wouldn't be such a good idea as you wouldn't be able to delete it later on and memory leaks would occur.
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 »

So a better way would be to allocate memory and free it again? Or just make the scope global? I mean this bump class will be needed for the whole run time.
Last edited by extralite on Sat Aug 20, 2011 10:03 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 »

Well if you don't intend to make this a big application than global scope might be a solution :)
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 »

Where exactly you put it - well it's your design, there are not many rules for that. Give it exactly as much scope as it needs. Usually you need about 1 global object - the application class object. And that contains objects of other classes which contain objects of other classes etc.

I usually put collision response somewhere in a physics class, or maybe in the game-class (which contains game-logic in my case - the stuff that runs when the player really plays and doesn't just hang around in menus).
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'm going to think about scope later. But I checked it with serengeor's additional line and this still gives me the same compilation error as before as well as a new one:

Code: Select all

linking ../../bin/Linux/Collision (g++)
main.cpp: In constructor ‘deflect::deflect()’:
main.cpp:45:2: error: uninitialized reference member ‘deflect::animator’
main.cpp: In function ‘int main()’:
main.cpp:205:30: note: synthesized method ‘deflect::deflect()’ first required here 
main.cpp:206:27: error: no match for ‘operator=’ in ‘bump->deflect::animator = ballanim’
 
Line 45 is here:

Code: Select all

        class deflect : public scene::ICollisionCallback
        { 
         public:
          scene::ISceneNodeAnimatorCollisionResponse& animator; //line 45
          virtual bool onCollision(const irr::scene::ISceneNodeAnimatorCollisionResponse& animator)
          {
            printf("bump\n");
          };
        };
 
 
And line 206 is here:

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 = new deflect;
         bump->animator = ballanim;  //line 206
                selector->drop(); // As soon as we're done with the selector, drop it.
                ball->addAnimator(ballanim);
                ballanim->setCollisionCallback(bump);
        }      
 
I appreciate the help btw.

Edit: Okay, found the mistake, replaced & with * in line 45, everythings fine now. Thanks.
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Correct way of using setCollisionCallback?

Post by extralite »

Okay, I put my actual code there instead of just the printf and it works fine except for setting the new gravity:

Code: Select all

        class deflect : public scene::ICollisionCallback
        { 
         public:
          scene::ISceneNodeAnimatorCollisionResponse* animator;
          virtual bool onCollision(const irr::scene::ISceneNodeAnimatorCollisionResponse& animator)
                {
                 core::vector3df balldirection = animator.getGravity();
                 printf("bump. Old Direction: %f,%f,%f\n", balldirection.X, balldirection.Y, balldirection.Z);
                 core::triangle3df hit = animator.getCollisionTriangle();
                 core::vector3df a0 = hit.getNormal().normalize();
                 printf("bump. Normal: %f,%f,%f\n", a0.X, a0.Y, a0.Z);
                 balldirection = balldirection - 2 * balldirection.dotProduct(a0) * a0;
                 animator.setGravity(balldirection); //line 56
                 printf("bump. New Direction: %f,%f,%f\n", balldirection.X, balldirection.Y, balldirection.Z);
                };
          
        };
 
 
I get a compilation error:

Code: Select all

main.cpp: In member function ‘virtual bool deflect::onCollision(const irr::scene::ISceneNodeAnimatorCollisionResponse&)’:
main.cpp:56:51: error: passing ‘const irr::scene::ISceneNodeAnimatorCollisionResponse’ as ‘this’ argument of ‘virtual void irr::scene::ISceneNodeAnimatorCollisionResponse::setGravity(const irr::core::vector3df&)’ discards qualifiers
 
Sorry for the double post and for the new question.
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 »

Still all errors because you don't read your c++ book first.
As soon as you use a variable-name more than once - for example once a paramter that got passed and then again the same name in your class you already are on your way to doing something seriously wrong.

You receive an animator - from Irrlicht. It's const - meaning you can't modify it. I'm not sure if that was a good design decision - sometimes Irrlicht uses too many consts and I have the feeling this is such a case - but I didn't write that part of code so I have to check first myself if there was maybe any reason why the programmer of this did make that const.
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 »

That helped, it works now.Thanks for your patience so far.
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Usage of setCollisionCallback and other coll. questions

Post by extralite »

Okay, now I'm trying to make CollisionResponse work with multiple meshes. I have a number of 3D models which should reflect my ball mesh. I tried to add multiple CollisionResponseAnimators to this ball mesh but the moment I have two of them they don't work correctly anymore, triggering collisions again and again.

I guess another option would be to have one CollisionResponseAnimator based on one TriangleSelector that holds triangle data for multiple meshes. But I'm not sure if and how you can combine multiple TriangleSelectors into one. Any quick answers or other ideas how to solve this task?
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Usage of setCollisionCallback and other coll. questions

Post by serengeor »

You could try batching those meshes into one mesh, but you shouldn't use irrlichts collisions for scenes with multiple objects (big slow down!).
You'd better be of with some physics engine and a simple wrapper for it.
Working on game: Marrbles (Currently stopped).
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Usage of setCollisionCallback and other coll. questions

Post by extralite »

Would the slow down be due to the increased number of polygons or the fact that they belong to individual models? And do I have to create a new TriangleSelector each time I rotate or move a mesh?

I understand that there might be limitations to the Irrlicht collision engine but depending on how well it can handle collision of rotating meshes I think it might still be viable to use it.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Usage of setCollisionCallback and other coll. questions

Post by serengeor »

extralite wrote:Would the slow down be due to the increased number of polygons or the fact that they belong to individual models? And do I have to create a new TriangleSelector each time I rotate or move a mesh?
Both things will lead to slow down, I don't really know which one would be worse as I never used irrlicht collision response animators. I started off with bullets physic engine and IrrBullet wrapper, later on wrote my own wrapper which is a lot smaller (6 files total :) ) but does the job just fine :)

If you don't want to use big physics engines like bullet for some strange reason, you could try: http://irrlicht.sourceforge.net/forum/v ... +collision

The download link in the first post is broken, but there is a new one in 4th page I think.
Working on game: Marrbles (Currently stopped).
extralite
Posts: 15
Joined: Thu Aug 18, 2011 12:53 pm

Re: Usage of setCollisionCallback and other coll. questions

Post by extralite »

Apparently createMetaTriangleSelector can do what I asked for originally. I will try it with this first, thanks.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Usage of setCollisionCallback and other coll. questions

Post by CuteAlien »

Just was thinking a little more about the fact that the ISceneNodeAnimatorCollisionResponse your receive is const. Still not sure if it is a good idea to make that const in Irrlicht. But there is some workaround - you can use getTargetNode to access the node on which it acts and from there on you can get a list of that nodes animators with getAnimators () and going through that list you can access a non-const version of it. Somewhat ugly admittably ... but should work. Another - even more ugly but probably working solution (even a little faster) would be to cast the const away. I really wish I had an idea why this is const...
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: Usage of setCollisionCallback and other coll. questions

Post by extralite »

Currently I'm getting the non const version by accessing the public variable itself (renamed anim) instead of the function one (still named animator). Works fine.
Post Reply