Game Development Community

Behaviors that use Collisions

by Deozaan · in Torque Game Builder · 11/19/2007 (5:59 pm) · 4 replies

I know there are behaviors that use collision to modify behavior, but I'm not clear on how exactly they work.

Do behaviors have their own collision information or do they go off the behavior owner object?

For instance, if I want a behavior to do something when the behavior's owner collides with something, do I need to build that into the owner's onCollision or will having a Behavior::onCollision be called when the owner collides?

#1
11/19/2007 (7:41 pm)
Behaviors maintain their own collision callback. You can do everything you need from a behavior without having to write any specific code on the actual object. Pretty cool, eh?

The Deals Damage behavior is a good example.

The one thing to keep in mind is to be sure and turn on your object's collision callback. You can accomplish this through code when adding the behavior as shown below. You may also want to set the send/receive callbacks based on the actual needs of the behavior. Setting the checks can also be accomplished manually by setting the checkboxes in the editor. Using code will automatically set the appropriate checkboxes in the editor, so it does not require any extra work on the part of the user.

function MyNewBehavior::onBehaviorAdd(%this)
{
   %this.owner.collisionCallback = true;
   %this.owner.collisionActiveSend = true;
   %this.owner.collisionActiveReceive = true;
}

Then, just add the onCollision callback and you are good to go.

function MyNewBehavior::onCollision(%this, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   // Add your special code here.
   echo("Entered MyNewBehavior::onCollision.");
}
#2
11/19/2007 (9:01 pm)
So the callback for collision on the object will also activate all of it's behavior's onCollision functions? Great! Thanks for the explanation!
#3
11/19/2007 (10:29 pm)
Ah, I was actually wondering about this myself. That's actually quite helpful. Thanks for the explanation.
#4
11/20/2007 (5:14 pm)
Yes. As long as the collision checkbox for the object is set, the behavior's onCollision will get called. Please let me know if you have any problems. Hopefully what I wrote made sense.