I think it depends on what you want to achieve with the collision system. The current approach gives more importance to "easily get collisions per object" rather than "keep a log of all collisions".
Here is your approach and an alternative:
Your approach:
Every collision is pushed into object.relationShipList
of the colliding object.
- Easy and fast way to fetch all collisions per object: Only read
object.relationShipList
.
- Very hard way to get a log of all collisions: Getting a log of all collisions would require calling
object.relationShipList
on every object and saving each collision in a new array.
- Will lead to data duplication: If there are multiple objects colliding, each object will generate its own collision.
Alternative that I would chose:
Every collision is pushed into a global array. When object a
collides with object b
, you register a window.collisionSystem.push([a, b])
.
- A little bit more complex way to get collisions per object: You would need to query
window.collisionSystem
to get collisions. This can be achieved with a function like
function getCollisionsFor(ob) {
return window.collisionSystem.filter(e => {
if (e[0] === ob || e[1] === ob) return true;
})
}
And then call it like getCollisionsFor([objectIWant])
to get a new array of collisions for the objectIWant
object.
- Easy way to keep track of all collisions without duplicate data: This is -of course- the
window.collisionsSystem
array.
- Easy way to prune data: Just
window.collisionSystem = [];
to clear up the object.
Recommended reading about this particular question in Game Engines, see: https://books.google.com/books?id=EwlpDwAAQBAJ&pg=PT726&lpg=PT726&dq=havok+collision+tracking&source=bl&ots=Eav72q2T95&sig=ACfU3U15UvAqvf4FmjYKOAsm1CPMlyaFQA&hl=en&sa=X&ved=2ahUKEwi559zSgJDmAhXHnuAKHQv0ATAQ6AEwCXoECAwQAQ#v=onepage&q=havok%20collision%20tracking&f=false