Having problems with collisions
by Vishal Bhanderi · in Torque X 2D · 09/20/2008 (10:51 am) · 4 replies
Hey guys,
When I have multiple components on a object that performs an on collision event with another object only one of the components are firing off the onCollision delegate.
The rest don't detect the collision event. I'm thinking it's because one of the components detects a collision, runs it's delegate and moves the collided object out of collision. Is this the case? If so how are people getting around this issue?
Thanks in advanced.
Vishal
When I have multiple components on a object that performs an on collision event with another object only one of the components are firing off the onCollision delegate.
The rest don't detect the collision event. I'm thinking it's because one of the components detects a collision, runs it's delegate and moves the collided object out of collision. Is this the case? If so how are people getting around this issue?
Thanks in advanced.
Vishal
About the author
#2
This is pretty much what i've been doing. It's just that everytime a new component for a object is added. I need to go into the main component and add similar code to this.
I just thought they could be a cleaner way.
Vishal.
btw: I can't wait to dig into your book.
09/24/2008 (6:21 am)
Thanks,This is pretty much what i've been doing. It's just that everytime a new component for a object is added. I need to go into the main component and add similar code to this.
I just thought they could be a cleaner way.
Vishal.
btw: I can't wait to dig into your book.
#3
To further enhance this functionality, you can have a list in BaseRigidComponent that will contain a registry of components that will receive OnCollision calls. Then just register each component that you want to receive the call either in code or via the txscene. Now you can just iterate through the list of registered components that are listening for collisions.
I'm not in a position to give example code, so I apologize if you may be a little confused about my suggestion. If you are, let me know and I'll post sample code later in the day when I have the resources to. Hope this helps!
P.S. - I've found that by creating "base" classes for your major components can add great flexibility catered to your project and/or general gaming projects that you may create in the future.
- Charles
09/24/2008 (11:30 am)
Hey Vishal. I have an idea that might be able to help you eliminate redundant code in this situation. Build a BaseRigidComponent class that derives off of T3DRigidComponent (or 2D) and use this class for your rigid component additions in the txscene. In the Base class, write the code that John suggested. Now every object that contains your BaseRigidComponent will have this functionality without having to re-write the code.To further enhance this functionality, you can have a list in BaseRigidComponent that will contain a registry of components that will receive OnCollision calls. Then just register each component that you want to receive the call either in code or via the txscene. Now you can just iterate through the list of registered components that are listening for collisions.
I'm not in a position to give example code, so I apologize if you may be a little confused about my suggestion. If you are, let me know and I'll post sample code later in the day when I have the resources to. Hope this helps!
P.S. - I've found that by creating "base" classes for your major components can add great flexibility catered to your project and/or general gaming projects that you may create in the future.
- Charles
#4
One of the problems with this collision thing is that the order that components are added to the scene object makes a difference to which object process the collision.
09/27/2008 (11:11 am)
That could work in some situations, but because some scene objects have some components and some have another. I'm not sure how it would work then.One of the problems with this collision thing is that the order that components are added to the scene object makes a difference to which object process the collision.
Associate John Kanalakis
EnvyGames
public void OnCollision()
{
//do this component's collision stuff
//do the other component's collision stuff
MyOtherComponent comp = Owner.Components.FindComponent
comp.OnCollision( data, data, data);
}
This is not working code, but it demonstrates the point.
John K.