lundi 18 septembre 2017

When an element in a container needs to affect the container its in

I'm working on a simulation for raindrops, and I have a Simulation class which has a container (vector) of Raindrop objects, these raindrops take care of their own movement when cycling through the container calling each Drops Update() function.

These raindrops inherit from a Collision Observer and a separate collision detection class checks to see if any raindrops overlap.

My wondering is how best to make changes to the raindrop collection. I could use the observer pattern to notify the individual drops of a collision but then I cant make the changes to the collection unless the drop contains a pointer to the simulation class with the collection that contains it, which seems very wrong. The approach I have gone for is to keep a pointer to the simulation in the collision detection class and have every raindrop keep a unique ID. Then I can call methods to alter the vector from the collision detection as long as I pass the id to the simulation, this, however, means having to search through and compare IDS for every Drop in the vector which seems like a slow method to make the changes.

Do you have any advice on how on how to change the contents of a container from an element contained within the container?

Thanks Adam

Aucun commentaire:

Enregistrer un commentaire