-
Hi, I've just started with DefaultECS (great framework by the way!) and ECS in general. I've read a lot about how to handle collisions in different ways but I'm still unsure how to approach it and keep ECS benefits of reduced cache misses and less code branch mispredictions. I wonder if anyone can help me here, I also think that this might be a valuable future reference for other devs in similar situations. Important Premisses
The ProblemSay we've collision detection worked out (in my case I've implemented a Swept AABB Collision Detection - reference video). Now our ConsiderationsAs far as I understand, DefaultECS and other ECS frameworks works with the concept of only one component of each type per entity (or world). So we can't just register a set of Approaches# 1Add a This way we can easily have collisions properly sorted in a queue. But is this good practice? Is this efficient? If we create components with queues/arrays/stacks of variable size won't we lose all the memory management benefits of storing components adjacent in memory? # 2Create "collision entities", each with a This way we do not need to manage queues/arrays, our But how can we resolve collisions in order? Maybe we can have our # 3Resolve collisions in the same system where we detect'em, effectively creating a big This way all we need to do is to sort collisions and resolve them. But isn't this a bad pattern? Collision can happen between solid bodies, dynamic bodies, a player can collide with a wall but also with a coin, each will have different rules for collision resolution, we no longer can have specialized collision systems. We could divide the code in order not to end with a massive, monstrous I'm leaning towards approach number # 2 since it looks more readable, clearer and easier to understand and seems not to violate basic principles of ECS but I'm unsure if I'm losing something that way or even if I will effectively be able to resolve these collisions in order without having to fight the DefaultECS framework. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey, thanks for you kind words :) I will try to comment as best as I can your different approaches: # 1Adding storage as component seems indeed a little too memory hungry. You could always use a pool of queues but I feel it complexifies too much for too little gain. I do have an idea I want to explore to allow storing multiple components of the same type on an entity, maybe it could help with this scenario in the futur but it is not there yet ^^" # 2I have seen this approach in a lot of other framework, using entities as messages. In DefaultEcs I would actually advise against doing this. Changing an entity composition is not free, using the full inner storage of entities and components just to store a single component that live only one frame seems a little too much when you can easily just store the messages to be processed directly later on. # 3This look like a bad idea:
At least this is where I am at now, I am all ears for other ideas on this problem :) |
Beta Was this translation helpful? Give feedback.
Hey, thanks for you kind words :) I will try to comment as best as I can your different approaches:
# 1
Adding storage as component seems indeed a little too memory hungry. You could always use a pool of queues but I feel it complexifies too much for too little gain. I do have an idea I want to explore to allow storing multiple components of the same type on an entity, maybe it could help with this scenario in the futur but it is not there yet ^^"
# 2
I have seen this approach in a lot of other framework, using entities as messages. In DefaultEcs I would actually advise against doing this. Changing an entity composition is not free, using the full inner storage of entities and components …