I need layer based collision for my game, the problem is I don't really have an idea how to implement it. I'm using C++ and a graphics library called SFML.

The only idea I got would be an array of unsigned integers that represent the collision layers the object has. However, I don't feel like this is an efficient way to implement this, because it's probably inefficient to loop through this array for every object, and for each layer it has loop through all the objects again to see if they are on the same layer.

I'm sure there is a better way but I couldn't find much on this online except for one article I couldn't understand at all.

  • \$\begingroup\$ The only better way I can think of is to use bit fields to represent this, instead of an array of integers. \$\endgroup\$ – Vaillancourt Dec 3 '20 at 18:23
  • \$\begingroup\$ Do you need objects to be able to span multiple layers? If not, then you could keep the objects in each layer in separate arrays and then separately iterate over each layer’s objects. \$\endgroup\$ – Ryan1729 Dec 5 '20 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.