Component Architecture, Collisions and Messaging

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Kataan
Posts: 10
Joined: Tue Mar 22, 2011 10:57 am

Component Architecture, Collisions and Messaging

Post by Kataan »

Hello,

This is more of a general game implementation question than a purely physics related question, but it is physics based so I thought I'd still post it here, sorry if its outside the realm of this forum.

First some background context:
I'm currently trying to implement a component based architecture for my game. I have a container class to store components that make up that object, and component classes to implement various aspects of that object. ie. a "player" container could container the following components: rendering, playerinput, physics, etc.

My physics component contains an Entity and handles it being added/removed to the Space. This component relays information such as position and rotation to other components who listen for changes in such information. The components is also aware of the container it belongs to.

Now, my current dilemma. I wish to create a physics component to act as a weapon, so that I can tell when the weapon's Entity has hit an enemy's physics component's Entity and then send a message from the weapon to the Enemy container class saying "take this much damage, blah blah blah".

Entity A can tell that it collided with Entity B.
Entity A can tell PhysicsComponent A (which it belongs to) that it has collided with Entity B.
So PhysicsComponent A can know it hit Entity B

and obviously vice-versa,

Entity B can tell that it collided with Entity A.
Entity B can tell PhysicsComponent B (which it belongs to) that it has collided with Entity A.
So PhysicsComponent B can know it hit Entity A

But how can PhysicsComponent A know that it collided with PhysicsComponent B? So that I can (for example) send a message from PhysicsComponent A to PhysicsComponentB's container class.

I hope this makes sense, if its needs further clarification please just ask. I appreciate your time for reading this and any possible pieces of advice you can offer.

Kind Regards,

Kataan

PS. I guess I could just cycle through each container and check if its physics component's Entity matches the one I collided with, but that seems like a horrible way to do it and I'd rather not resort to that.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Component Architecture, Collisions and Messaging

Post by Norbo »

On the physics engine side of things, using collision events and tags seem like they would do the trick.

The Entity.CollisionInformation.Events property has a bunch of events available. The Entity also has a Tag, which is just an object which stores arbitrary user data (like, say, a reference to a PhysicsComponent). Note that the entity's collidable (the thing returned by the CollisionInformation property) itself has another, separate tag which it inherits from BroadPhaseEntry. You can use whichever one is more convenient.
Kataan
Posts: 10
Joined: Tue Mar 22, 2011 10:57 am

Re: Component Architecture, Collisions and Messaging

Post by Kataan »

As always you come to the rescue. Thank you for your prompt response, that solution should be perfect.

I'd already hooked up the events, the doco for that is clear and very helpful btw, doing a great job!

The ability to use Tag as an object should be the missing piece of the puzzle, if I can set that to reference the PhysicsComponent everything should be good.

Thank you,

Kataan :D
Post Reply