Bouncing balls - changing parameters from callback function?

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
wzerek
Posts: 7
Joined: Mon Jun 03, 2013 4:32 pm

Bouncing balls - changing parameters from callback function?

Post by wzerek »

Hello, I'm quite new to Irrlicht and OOP in general. I'm writing a game, where, among other features, I shoot balls and I want them to bounce off the buildings' walls with transformed vector. I don't want to implement standalone physics engine because my time is quite limited and it would be a little overkill I guess.
For balls I've created a class CBall, which has pointer to ISceneNode object - sphere, and couple of other fields (vectors for movement, previous position etc). For walls I'm using scaled cube scene nodes.
So far I managed to attach callback function to my collision response animator and I'm getting messages, when the ball hits wall. But now I'd like to modify balls's vector and here's my question: is it possible, to do it from within callback function? In my callback class I have pointer to ISceneNodeAnimatorCollisionResponse object (my animator attached to ball) and I found getTargetNode and getCollisionNode methods.The thing is, they return pointer to ISceneNode object, which in my case is kinda parallel to fields I want to modify.

I hope I stated my problem clearly, thanks in advance for any help.
Cheers, wzerek
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Bouncing balls - changing parameters from callback funct

Post by CuteAlien »

Hm, not really sure what you mean with "parallel to fields". You mean it's not the pointer you want? In that case you can maybe lookup the object you really want by the pointer you have. Or use an own ISceneNodeAnimatorCollisionResponse which has a pointer to the original collisionresponse object and to your own class object. Or maybe I understood you wrong...
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
wzerek
Posts: 7
Joined: Mon Jun 03, 2013 4:32 pm

Re: Bouncing balls - changing parameters from callback funct

Post by wzerek »

Yes, it's not the pointer I want. Class CRedBall looks like this:

Code: Select all

CRedBall::CRedBall(scene::ISceneManager* smgr, video::IVideoDriver* driver, core::vector3df pos) {
    m_ball = smgr->addSphereSceneNode(CConfig::BALL_SIZE);
    m_ball->setPosition(pos);
    m_dest = m_ball->getPosition();
    m_old_dest = m_ball->getPosition();
    m_direction = m_ball->getPosition();
    m_nearPrey = NULL;
    m_velocity = CConfig::INITIAL_SPEED;
    m_energy = CConfig::MAX_ENERGY;
    m_ball->setMaterialTexture(0, driver->getTexture("red_ball.jpg"));
    m_ball->setMaterialFlag(video::EMF_LIGHTING, CConfig::LIGHT);
}
In simple words: I can probably get to m_ball, but I want to change m_direction. Is it possible? Or am I completely mistaken?

In addition I'm posting my callback class and animator attachment method:

CDeflect.h

Code: Select all

class CDeflect : public scene::ICollisionCallback {
    public:
        scene::ISceneNodeAnimatorCollisionResponse* m_animator;
        virtual bool onCollision(const scene::ISceneNodeAnimatorCollisionResponse& animator);
};
CDeflect.cpp

Code: Select all

bool CDeflect::onCollision(const scene::ISceneNodeAnimatorCollisionResponse& animator) {
    std::cout << "collision" << std::endl;
 
    return 0;
}
CreateCollisionResponseAnimator

Code: Select all

void CRedBall::CreateCollisionResponseAnimator(scene::ITriangleSelector* selector, scene::ISceneManager* smgr) {
    scene::ISceneNodeAnimatorCollisionResponse* anim = smgr->createCollisionResponseAnimator(
        selector,                       // triangle selector
        this->m_ball,                 // node
        core::vector3df(CConfig::BALL_SIZE,CConfig::BALL_SIZE,CConfig::BALL_SIZE),     // ellipsoid radius
        core::vector3df(0,0,0),         // gravity Per Second
        core::vector3df(0,0,0)         // ellipsoid Translation
        );
 
    CDeflect* callback = new CDeflect;
    callback->m_animator = anim;
    anim->setCollisionCallback(callback);
 
    this->m_ball->addAnimator(anim);
    anim->drop();
}
Cheers and thanks for hints,
wzerek
wzerek
Posts: 7
Joined: Mon Jun 03, 2013 4:32 pm

Re: Bouncing balls - changing parameters from callback funct

Post by wzerek »

Ok, got it, I just added pointer to CRedBall class inside CDeflect class.
Post Reply