I need some help with collision

3 threads actually seems quite doable. Anyway, I need some help with collision.

How?

Quite blunt, but really, how? How do I check for collision between the player, possibly enemies, and the (tile based) map?

Another thing is that one of the tiles, designated as ‘Cap,’ needs to have both wall, and ground logic, depending on if the player hits it at the side, or falls on top.

I can’t stall the collision any longer, seriously, besides the map, this is was always my biggest fear :/

Thanks! Btw, I’ve added what I have currently to my GitHub repo: GitHub - ArdFlamingo/hi-walter-wheres-hank: 😳😳😳😳😳

It’s probably going to change by the same you see this though. I think I finally cracked a good system for all the map pointers and what not when I was in the shower, so I’m going to try and add that in right now.

I’m not going to hang around because it’s late, but since I’m here:

It may or may not make much sense.
(Probably less if you don’t know what Entity & means. I can’t remember if I told you about references or not.)


I’m marginally worried about this:

I can think of good reasons to have arrays of pointers, but I’m wondering why you’ve got arrays of pointers that are seemingly something to do with map dimensions.

1 Like

Yeah, you did!

That’s some stuff from earlier before. I decided to just make the map dimensions themselves an array then use a single variable to just select the current map dimensions. Of course, still a pointer for the actual map data.

There are two inbuilt commands in the Arduboy2 library:

https://mlxxxp.github.io/documents/Arduino/libraries/Arduboy2/Doxygen/html/classArduboy2Base.html#a2bda6693a08b299e275a97dd30254560

They detect collisions between two rectangles or a rectangle and point.

You can search in this forum for examples of people using them or refer to this page.

2 Likes

Yeah, I use those in my other games. The hitboxes I need are a lot more complex than a simple rectangle, so I have to do it all manually.

In the magazine (linked in above article), I show how to do a pixel level collision.

2 Likes

If they’re boxes then by definition they’re rectangles.

What shapes are you trying to test exactly?

As far as I saw your map was made up entirely of rectangular tiles, hence I showed you some code that deals precisely with a grid of rectangles.

Are you planning to incorporate slopes or something?

Depending on the shape you don’t necessarily need per-pixel collision, e.g. circles are pretty easy to do. It really all depends on what your goal is.

1 Like

I was assuming that the entire map would be one hitbox, that’s what Rect does, right? With Rect you can’t have like . . . idk, organic shapes? Not really organic, but like more complex than a single rectangle because it’s like multiple rectangles protruding from the ground-which is a rectangle too-if you know what I mean.

If the entire map was a single box nothing would ever move because everything on the map would be colliding with the map.

Rect represents a rectangle.
But you get to choose what rectangle it represents.
That rectangle doesn’t have to be the entire map.

You’re halfway there, but you can’t quite see the wood through the trees yet.

Your entire map is already made of rectangles: your tiles are effectively rectangular.
All you need is the right calculations to create a Rect.

Consider this for a moment:

Rect getTileBounds(uint8_t tileX, uint8_t tileY)
{
	Rect tileBounds { tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight  };
	return tileBounds;
}

Given the tile coordinates of a tile (i.e. its row and column index in the array of map data, not its position in the virtual world, or the position at which it is drawn to the screen) you can calculate a rectangle that represents the ‘physical’ space a tile occupies in the virtual world.

Then you need to create a Rect for the player, for which you’ll need the player’s world coordinates (as opposed to the player’s screen coordinates).

That’s only half the battle though. Once you know that a player is colliding with a tile, you need to decide how to resolve that situation.

3 Likes