A different onCollision for each object
by Spider · in Torque Game Builder · 12/25/2005 (11:36 pm) · 7 replies
I have a bunch of objects with unique responses to collisions. Is there any way to specify different oncollision functions per object? Or do I pretty much have to have a massive switch in the general t2dSceneObject?
#2
Thanks!
12/26/2005 (8:57 am)
Oh yeah, that's smart. Didn't know about that call function, but now that you mention it I guess I coulda figured that out with 'eval'.Thanks!
#3
05/28/2006 (3:27 am)
I think a better approach would be to assign different classes to your objects, e.g.%rolling = new t2dStaticSprite() { class = rollingClass; };
%stone = new t2dStaticSprite() { class = stoneClass; }and create separate callback handlers for every classrollingClass::onCollision( ... ) { ... }
stoneClass::onCollision( ... ) { ... }This way you can create different collision logic for different objects and even group collision behavoiur more easily with less code.
#4
05/28/2006 (3:35 am)
Yes, but at the time the original post was written, that functionality didn't exist in TGB. :)
#5
my collision function is
function playerBody::onCollision(%this,%obj,%col)
{
if(//the thing you ran into is the baddie)
schedule(3000, 0, "hurtPlayer");
}
hurtPlayer of course is another function ive written, the problem is that i cant figure out what to make the condition of the if statement... right now the player gets hurt everytime he hits a buildig or falls or walks into a pick up... HELP lol
02/03/2010 (10:13 pm)
Im using torque 1.5.2 and im trying to write up a collision function that will damage the player if its colliding with a badguy(faking melee) but not if its running into a wallmy collision function is
function playerBody::onCollision(%this,%obj,%col)
{
if(//the thing you ran into is the baddie)
schedule(3000, 0, "hurtPlayer");
}
hurtPlayer of course is another function ive written, the problem is that i cant figure out what to make the condition of the if statement... right now the player gets hurt everytime he hits a buildig or falls or walks into a pick up... HELP lol
#6
Also, consider using the formal callback signature:
02/04/2010 (10:52 am)
Did you enable the collision callback for you object?Also, consider using the formal callback signature:
function t2dSceneObject::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
//put collision code here.
}
#7
function playerBody::onCollision(%this,%obj,%col)
{
%classy = %col.getClassName();
if(%classy $= "AIPlayer")
schedule(3000,0,"hurtPlayer")
}
02/04/2010 (10:57 am)
I ended up fixing it with function playerBody::onCollision(%this,%obj,%col)
{
%classy = %col.getClassName();
if(%classy $= "AIPlayer")
schedule(3000,0,"hurtPlayer")
}
Torque 3D Owner Luke D
Default Studio Name
%object.onCollisionCallback = "myobjectCollisionCallback"; function t2dSceneObject::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { if (%srcObj.onCollisionCallback !$= "") { call(%srcObj.onCollisionCallback, %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts); } } function myobjectCollisionCallback(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { // Do collision stuff for this type of object }