You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
When doing collision testing, it is common to do a collision check for every collider against every other collider, ignoring checking self against self and ignoring the order of the colliders.
I would normally write code like the following to accomplish that:
for id_1 in0..num_ids - 1{for id_2 in id_1 + 1..num_ids {// check collider with id_1 against collider with id_2}}
The above loop is nice since it avoids any unnecessary iterations and combinations of id_1 and id_2. id_1 vs id_2 is the same as id_2 vs id_1 and this loop nicely ignores those.
With shipyard, I'm struggling to do this consisely.
Idea 1
Keep track of which id combos we have checked and continue if we detect it's already done
Idea 1 seems unnecessarily slow with hashing and allocations, Idea 2 seems a bit convoluted but is perhaps better since it can easily be parallellized with par_iter if I want to.
I feel like one could do better. I would like to do the above more idiomatically or in a more performant way with less allocations if possible. How would you do this kind of iteration in shipyard? Also, next level: what if I want to do mutable borrows of the colliders? xP
The text was updated successfully, but these errors were encountered:
Hi!
I just want to preface this message by saying that an ECS is not the best data-structure for collision. For prototyping it's fine but you might want to move to a dedicated data-structure in the future.
I agree idea 1 would be pretty slow but idea 2 will be good once simplified a bit.
Since it's not easy to ignore the last value of an iterator I use a range instead of enumerate.
For the inner iteration you could use colliders.iter().skip(i + 1) but I haven't optimized it so using as_slice() will be faster for now.
And that's it for the immutable version. For the mutable version it's a bit more complicated.
We can't both iterate over the ids and access components at the same time. So we'll do it in two steps like idea 2.
I start by collecting the ids, ids method is the same as .with_id().map(|(id, _)| id). apply_mut takes 2 different EntityId and a closure to apply to the 2 components.
Hi!
When doing collision testing, it is common to do a collision check for every collider against every other collider, ignoring checking self against self and ignoring the order of the colliders.
I would normally write code like the following to accomplish that:
The above loop is nice since it avoids any unnecessary iterations and combinations of id_1 and id_2.
id_1 vs id_2
is the same asid_2 vs id_1
and this loop nicely ignores those.With shipyard, I'm struggling to do this consisely.
Idea 1
Keep track of which id combos we have checked and
continue
if we detect it's already doneIdea 2
Create a list of all ids with a collider, build up pairs of ids and components according to the loop above, then finally iterate over the pairs.
Idea 1 seems unnecessarily slow with hashing and allocations, Idea 2 seems a bit convoluted but is perhaps better since it can easily be parallellized with par_iter if I want to.
I feel like one could do better. I would like to do the above more idiomatically or in a more performant way with less allocations if possible. How would you do this kind of iteration in shipyard? Also, next level: what if I want to do mutable borrows of the colliders? xP
The text was updated successfully, but these errors were encountered: