In one of my classes, we are making a game where you drive around a city gathering collectables. I am currently using the (entity.CollisionInformation.Events.DetectingInitialCollision += HandleCollision;) to put event handlers in the collectables, but I would like to switch out to the CollisionCreated event handler. It doesn't work the same way, I tried just switching out to CollisionCreated, but it doesn't like the HandleCollision method. Doesn't fit the signature. I can't figure out what signature the function needs from the metadata either.
public override void HandleCollision(EntityCollidable sender, Collidable other, CollidablePairHandler pair)
That's the signature used for the DetectingInitialCollision, but what signature do I use for CollisionCreated?
Collision Created Event Handler
Re: Collision Created Event Handler
By CollisionCreated, I assume you mean ContactCreated:
where T is the type of the collidable that is providing the event.
Note that there are two differences to be aware of. First, DetectingInitialCollision is not a deferred event, while the ContactCreated event is. Immediate events like DetectingInitialCollision have direct access to some properties of the collision pair and contact. Deferred events lack that access, but make up for it by being much safer. Deferred events execute sequentially at the end of an update, rather than in the middle the threaded execution.
Second, the deferred version of DetectingInitialCollision is InitialCollisionDetected. ContactCreated is the deferred version of CreatingContact. While the initial collision event only fires on the transition from 0 contacts to 1 or more contacts, the contact created event fires for each and every contact created during a collision.
The ContactCreated/CreatingContact events are not often the best choice outside of playing sound effects on impact or fiddling with contact data. If you want an event which fires as long as the objects are touching instead of a one-shot event like InitialCollisionDetected, I would recommend using PairTouched:
By the way, VS has an autocomplete feature for delegates. Just hit tab when prompted after typing the += and it will create a function of the appropriate signature.
Code: Select all
void ContactCreatedEventHandler<T>(T sender, Collidable other, CollidablePairHandler pair, ContactData contact)
Note that there are two differences to be aware of. First, DetectingInitialCollision is not a deferred event, while the ContactCreated event is. Immediate events like DetectingInitialCollision have direct access to some properties of the collision pair and contact. Deferred events lack that access, but make up for it by being much safer. Deferred events execute sequentially at the end of an update, rather than in the middle the threaded execution.
Second, the deferred version of DetectingInitialCollision is InitialCollisionDetected. ContactCreated is the deferred version of CreatingContact. While the initial collision event only fires on the transition from 0 contacts to 1 or more contacts, the contact created event fires for each and every contact created during a collision.
The ContactCreated/CreatingContact events are not often the best choice outside of playing sound effects on impact or fiddling with contact data. If you want an event which fires as long as the objects are touching instead of a one-shot event like InitialCollisionDetected, I would recommend using PairTouched:
Code: Select all
void PairTouchedEventHandler<T>(T sender, Collidable other, CollidablePairHandler pair)