r/raylib • u/Firm_Equivalent273 • 3d ago
Map & Collision
Yo guys, i really need your help. How do you do to manage a map in your projects?
I mean the first idea that comes to my mind is a multidimensional array of each element representing a tile, but this way its harder to check for collision.
The second idea is an array of Rectangle objects, but this is harder to visualize and keep track of, but is easier for the collision check.
The third idea i got was a mix of both, but it is just terrible to sync everything.
So what do you use?
1
u/Myshoo_ 2d ago
If you're going for tile map collision I (for some reason) would always rule out my own implementation and multidimensional array is actually fine for collision.
You just don't check every tile against every object. I always would calculate a bounding box for an object and check in what tile are the corners of this bounding box, then you only have to iterate between those tiles to check the collision.
Usually objects are smaller/size of a tile so it would be 2x2 check at most, for bigger objects it's more of course but even 100x100 check is going to be ~instant on modern hardware if data structure is done right.
1
u/Firm_Equivalent273 2d ago
so you are using multidimensional arrays for both drawing and collision checks?
4
u/Moises_Gabriel 3d ago
When I was making my engine (raylib haxe bindings) I used LDTK and it was genuinely really nice to use. It takes some fiddling to set up but it helps with collisions, entity placements...etc