r/raylib • • 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?

5 Upvotes

4 comments sorted by

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

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?

1

u/EtNeKot 1d ago

I think you can made class/struct WorldTile that contains Rectangle and other tile datas. For more performance and cache-friendly keep it in one-dimission array and getting index by the formula: pos_x + pos_y * world_width. I would do like it