What is the best way to handle collisions?
by MotoSV · in Torque X 2D · 02/10/2010 (3:20 pm) · 4 replies
Hi Everyone!
My name is Michael and I've just recently (last week in fact) started using TX2D to hopefully write a game that I can release on Xbox Live Indie Games. I've been working my way through the tutorials, forums, examples, etc, etc, and I'm finding the design of engine a bit...different...than what I'm used to. It's the whole "component" design approach. But I'm getting there.
Anyway, my question! It's to do with collisions. I've created a component, let's say COMP_A, that has a static OnCollision method and a static property that returns the OnCollision method. In the Builder app I can click on an object, go to the Collision component and the property reference for COMP_A shows up in the OnCollision combobox as expected.
But, if I create another component (COMP_B), which also has a static method and property, and I go into the same object/component as above the COMP_B collision callback shows as well as COMP_A's callback even though the COMP_B component is not assigned to the object.
Is there no relation of collision callbacks to components?
My initial thoughts were to have one OnCollision method and that in turn could call a HandleCollision on the ownObject and theirObject parameters. That way each object involved in the collision could perform their own logic.
What is the best approach for handling collisions between objects?
Thanks
Michael
EDIT: The whole HandleCollision method idea doesn't make sense because it's components not objects...I have to try and remember that. :)
My name is Michael and I've just recently (last week in fact) started using TX2D to hopefully write a game that I can release on Xbox Live Indie Games. I've been working my way through the tutorials, forums, examples, etc, etc, and I'm finding the design of engine a bit...different...than what I'm used to. It's the whole "component" design approach. But I'm getting there.
Anyway, my question! It's to do with collisions. I've created a component, let's say COMP_A, that has a static OnCollision method and a static property that returns the OnCollision method. In the Builder app I can click on an object, go to the Collision component and the property reference for COMP_A shows up in the OnCollision combobox as expected.
But, if I create another component (COMP_B), which also has a static method and property, and I go into the same object/component as above the COMP_B collision callback shows as well as COMP_A's callback even though the COMP_B component is not assigned to the object.
Is there no relation of collision callbacks to components?
My initial thoughts were to have one OnCollision method and that in turn could call a HandleCollision on the ownObject and theirObject parameters. That way each object involved in the collision could perform their own logic.
What is the best approach for handling collisions between objects?
Thanks
Michael
EDIT: The whole HandleCollision method idea doesn't make sense because it's components not objects...I have to try and remember that. :)
About the author
#2
www.torquepowered.com/community/resources/view/19137
02/10/2010 (7:00 pm)
You can see the way I did it by downloading the resource I posted. It has a number of generic game control modules to help you get started.www.torquepowered.com/community/resources/view/19137
#3
When working with XNA & the Farseer Physics engine each type of object, Spaceship, Bullet, etc, would be informed when they are involved in a collision. Then each type of object could respond to that collision in their own way.
Let's say I had a Health PowerUp component and a InflictsDamage component and both were placed on a object. When the object collides with another object I want the Health PowerUp to repond by increasing a health value on the object and the InflictsDamage component to add damage to the other object. Here I've got an object that has 2 components which each respond in different ways to the collision.
Are you saying I would need to create a delegate that would look specifically for these two components?
Michael
02/11/2010 (2:03 am)
@Scott When working with XNA & the Farseer Physics engine each type of object, Spaceship, Bullet, etc, would be informed when they are involved in a collision. Then each type of object could respond to that collision in their own way.
Let's say I had a Health PowerUp component and a InflictsDamage component and both were placed on a object. When the object collides with another object I want the Health PowerUp to repond by increasing a health value on the object and the InflictsDamage component to add damage to the other object. Here I've got an object that has 2 components which each respond in different ways to the collision.
Are you saying I would need to create a delegate that would look specifically for these two components?
Michael
#4
There would also be a single method called T2DOnCollisionDelegate OnCollision(...) that can be assigned to any CollisionComponent. When there is a collision the method would locate the components on the ourObject and theirObject parameters that implement the ICollisionHandler interface and call the HandleCollision method.
This way it is possible for a object to perform a number of different collision responses based on the components attached to it and therefore no delegates to handle different collision scenarios.
Michael
02/11/2010 (2:11 am)
I was thinking of a design where each component that wants to respond to a collision would have a ICollisionHandler interface with a method called HandleCollision. There would also be a single method called T2DOnCollisionDelegate OnCollision(...) that can be assigned to any CollisionComponent. When there is a collision the method would locate the components on the ourObject and theirObject parameters that implement the ICollisionHandler interface and call the HandleCollision method.
This way it is possible for a object to perform a number of different collision responses based on the components attached to it and therefore no delegates to handle different collision scenarios.
Michael
Torque Owner Scott Cameron
one way you could do this is to setup a Collision handler component for each way you want to handle the collisions and add them to the objects and then create a collision delegate that would call these handlers on the objects through the ownObject and theirObject.
IE:
MyCollisionHandler myHandler = theirObject.findcomponents<MyCollisionHandler>();
myHandler.DoCollision();
is there something specific that your trying to accomplish?