Components... collision?
by Craig Fortune · in iTorque 2D · 08/16/2009 (11:27 am) · 6 replies
I'm having an odd problem in regards to the onCollision callback for components, I just wanted to know if anyone else is experiencing something similar...
I have objects which are linked up to components, they are set to collide (send + receive) and do onCollision callbacks. If I set one of the objects to have a namespace of something like "missileClass" I will get a script callback for the collision as expected to missileClass::onCollision(.....) (The collisions are visibly happening just fine too by the way)
The oddity is that my component isn't getting an onCollision callback called. I don't really know why, as the onUpdate in the collision IS being called, so the component is other respects is working a-ok.
Thoughts? I didn't think there were any more extra magical steps required.
I have objects which are linked up to components, they are set to collide (send + receive) and do onCollision callbacks. If I set one of the objects to have a namespace of something like "missileClass" I will get a script callback for the collision as expected to missileClass::onCollision(.....) (The collisions are visibly happening just fine too by the way)
The oddity is that my component isn't getting an onCollision callback called. I don't really know why, as the onUpdate in the collision IS being called, so the component is other respects is working a-ok.
Thoughts? I didn't think there were any more extra magical steps required.
#2
You're quite right. I had looked into the code a bit further after posting this and updated the code to support onCollision callbacks (haven't tested fully yet). Can someone please tell me why on earth this wasn't considered normal functionality to desire?
08/17/2009 (4:42 pm)
Mat,You're quite right. I had looked into the code a bit further after posting this and updated the code to support onCollision callbacks (haven't tested fully yet). Can someone please tell me why on earth this wasn't considered normal functionality to desire?
#3
08/17/2009 (9:13 pm)
Brief (let me say that again, brief) testing done and all appears good. Weird thing is it feels like someone originally started implementing this and then stopped... it requires next to no work at all to get working. I'll post the changes up tomorrow (its late now here in the UK) as I'm sure there will be other people who will want similar functionality.
#4
and the corresponding declaration in the header...
t2dPhysics::collisionStatus contains all the stuff we need to determine what to do with the collision. It is pretty much all the information you normally have available in a script onCollision callback al packaged up.
Now go to t2dSceneObject and find where the onUpdate and onAddToScene callbacks are defined. Add this below them (above the #endif)
And also go to t2dSceneObject.h and find the onCollision declaration and get rid of the {}, it should look like this:
So now we have your t2dSceneObject wanting to throw a callback, but simcomponents need to support this (as they ultimately get called obviously) so lets handle that.
In simComponent.h find the onUpdate and onAddToScene and add... you guessed it:
and in simComponent.cpp add this alongside the onUpdate and onAddToScene (above the #endif again):
You'll notice that we are doing precisely zilch here. If you have good coding-fu you'll know that all we are doing here is providing a blank method at the base level of our simComponents hierarchy so we don't HAVE to implement onCollision in all of our inherited component classes.
Oh and one last thing, you'll want to add:
to your simComponent.h file too
08/18/2009 (8:21 pm)
Right, first off lets actually implement our component's onCollision callback in its cpp file. Here I have a component called "MissileComponent"void MissileComponent::onCollision(t2dPhysics::cCollisionStatus* collisionStatus)
{
// do stuff
}and the corresponding declaration in the header...
void onCollision(t2dPhysics::cCollisionStatus* collisionStatus);
t2dPhysics::collisionStatus contains all the stuff we need to determine what to do with the collision. It is pretty much all the information you normally have available in a script onCollision callback al packaged up.
Now go to t2dSceneObject and find where the onUpdate and onAddToScene callbacks are defined. Add this below them (above the #endif)
void t2dSceneObject::onCollision(t2dPhysics::cCollisionStatus* collisionStatus)
{
//call onUpdate for each component
SimComponent *pComponent = NULL;
for( U32 i = 0; i < this->getComponentCount(); i++ ) {
pComponent = this->getComponent( i );
if( pComponent == NULL ) {
break;
}
pComponent->onCollision(collisionStatus);
}
}And also go to t2dSceneObject.h and find the onCollision declaration and get rid of the {}, it should look like this:
virtual void onCollision(t2dPhysics::cCollisionStatus* collisionStatus);
So now we have your t2dSceneObject wanting to throw a callback, but simcomponents need to support this (as they ultimately get called obviously) so lets handle that.
In simComponent.h find the onUpdate and onAddToScene and add... you guessed it:
virtual void onCollision(t2dPhysics::cCollisionStatus* collisionStatus);
and in simComponent.cpp add this alongside the onUpdate and onAddToScene (above the #endif again):
void SimComponent::onCollision(t2dPhysics::cCollisionStatus* collisionStatus)
{
}You'll notice that we are doing precisely zilch here. If you have good coding-fu you'll know that all we are doing here is providing a blank method at the base level of our simComponents hierarchy so we don't HAVE to implement onCollision in all of our inherited component classes.
Oh and one last thing, you'll want to add:
#include "t2dPhysics.h"
to your simComponent.h file too
#5
Because SimComponent must not rely stuff further down the hierarchy ie t2d
I guess you could implement it deeper down the hierarchy at a point where the class is aware of t2d at all.
08/18/2009 (8:34 pm)
You likely just pointed to a potential reason it was not done.Because SimComponent must not rely stuff further down the hierarchy ie t2d
I guess you could implement it deeper down the hierarchy at a point where the class is aware of t2d at all.
#6
So may I correct you on something? "must not" -> "should not" :)
08/18/2009 (9:34 pm)
Marc, yes that is something I thought about initially, however I think the benefits outweigh the negatives for what I personally am doing (maybe it isn't for you, who knows? Well, probably you, lol.). It is safe to do this in a code sense, just not good practice in a engineering sense.So may I correct you on something? "must not" -> "should not" :)
Torque 3D Owner Mat Valadez
Default Studio Name