Physics and Triggers oh my

Started by
3 comments, last by BeerNutts 12 years, 2 months ago
I have an event driven engine. Each subsystem will fire off events that other systems and objects can register for. example. scene will fire off of an "Update" event, and skinned meshes are registered to listen for those events. OnUpdate they update the animation, etc...

now my question is this. I have N amount of triggers. And i have different cinematics that play depending on which trigger is hit. What is the best way for me to "trigger" the cinema.

Thoughts are

1) user enters trigger_0, trigger_0 calls OnTouch. Inside OnTouch i cycle thru a list of all methods that have preregistered for this trigger. This is different then teh registering i currently have.

2) trigger_0 sends an event container an triggerID. All systems and objects that register for the generic triggerOnTouch event will listen. WHen they "hear" the event, they will check to see if the triggerID is the one they care about, and if so, will do the corresponding action. if not, they will just continue on thier business as normal.

option 2 seems like a bad choice, as if i have 1000 triggers, and i only care about trigger999, i will be checking against each one.

option 1 seems counter productive to me, as it almost defeats the purpose of the event drive system.

Thanks for any input
Code makes the man
Advertisement
You can make the data structure that holds who registered for a message be a tree: each node has a list of objects listening for that message and a hash map of children, with each level becoming more specific. For example, when someone registers with the root node, they get all messages. The root node has two children in its hash map, triggerOnTouch and buttonActivated (or whatever other messages). When an object registers with the triggerOnTouch node, it only gets all triggerOnTouch events. The triggerOnTouch node's hash map contains the nodes for a specific triggerID, so when an object registers with it, it would get only triggerOnTouch events sent by something with that specific id.

Its kind of like a BHV tree, but for messages. I've used this before and it works really well both from a usability and performance standpoint.
can you give a little more detail into that explanation. i kind of follow it... but i am kinda lost at the same point. that sounds like a option 2, where the trigger would send out messages with onTouch(ID=x) to the world. but only those that care about ID=x woudl actually do anything on that event
Code makes the man


class Message
{
unsigned int messageType;
unsigned int senderID;
}
class MessageNode
{
virtual void register(MessageReceiver* receiver, Message* messageToReceive)
{
if (messageToReceive->messageType == 0)
{
registeredReceivers.push_back(receiver);
}
else
{
std::map<unsigned int*, MessageNode*>::iterator iter;
iter = childNodes.find(messageToReceive->messageType);

if(iter == childNodes.end())
{
childNodes.insert(pair<unsigned int, MessageNode*>(messageToReceive->messageType, new MessageNodeType()));
iter = childNodes.find(messageToReceive->messageType);
}

(*iter).second.register(receiver, messageToReceive);
}
}
virtual void sendMessage(Message* message)
{
for each receiver in registeredReceivers
{
receiver.sendMessage(message)
}

MessageNode* node = childNodes[message->messageType].second;

if (node != NULL)
{
(*iter).second->sendMessage(message);
}
}

std::list<MessageReceiver*> registeredReceivers;
std::map<unsigned int*, MessageNode*> childNodes;
};
class MessageNodeType : public MessageNode
{
virtual void register(MessageReceiver* receiver, Message* messageToReceive)
{
if (messageToReceive->senderID == 0)
{
registeredReceivers.push_back(receiver);
}
else
{
std::map<unsigned int*, MessageNode*>::iterator iter;
iter = childNodes.find(messageToReceive->senderID);

if(iter == childNodes.end())
{
childNodes.insert(pair<unsigned int, MessageNode*>(messageToReceive->senderID, new MessageNodeID()));
iter = childNodes.find(messageToReceive->senderID);
}

(*iter).second.register(receiver, messageToReceive);
}
}

virtual void sendMessage(Message* message)
{
for each receiver in registeredReceivers
{
receiver.sendMessage(message)
}

std::map<unsigned int*, MessageNode>::iterator iter;
iter = childNodes.find(message->senderID);

if (iter != childNodes.end())
{
(*iter).second->sendMessage(message);
}
}
}



Message:
We use a hashed string for a lot of stuff, which is basically just a fancy unsigned int. messageType would be HashString("triggerOnTouch").hash
senderID is just whatever unique id you use to identify stuff.

MessageNode:
Base class for other message managing structures. Holds a map of children keyed by the type of message.
register - first, check the message type. If it is 0, the caller is registering for all messages, so add caller to the list of registered receivers. Otherwise, call register on appropriate child node, creating it if necessary.
sendMessage - send message to all objects in registeredReceivers list (objects that want all messages) and pass along to appropriate child node (the one which is in the map with a key matching messageType).

MessageNodeType:
Registered receivers all want a message of a certain type.
register - same as above, except the key is senderID instead of message type
sendMessage - same as above, except the key is senderID instead of message type

MessageNodeID: (not shown)
Registered receivers all want a message of a certain type from a certain sender. Same as other two classes, except doesn't have children (is a leaf node)

I just threw that together, you can make it much more elegant and simple by using comparison functions and templates rather than derived classes, and of course using something other than stl smile.png
That also allows you to use anything you write a comparison function for, you could have all messages from a certain sender (not possible above, only specific message from specific sender) or sending to all objects within x distance of location.
The obvious suggestion would be to make more detailed events. So, instead of just a onTrigger event, you have onTrigger_Area1, so you only get trigger in area1 (say, some level, world, or area of a the world).

Or, make the sub event filters more efficient, so it works more like what turch suggested. Except, your second level (under the top level onTrigger) is some sub-event that filtered similarly to the root event.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement