I'm is there a way of checking tile and Sprite collisions

So I was wondering is there a way of checking if a Sprite is on a map tile? I recently started using maps and I was hoping there was a way of checking for sprites on tiles?

If you are using the code that @Pharap helped you with, then you map is simply an array of tiles?

Assuming the tiles are 8 x 8, can’t you simply divide the sprites X and Y values by 8 to determine the indices of the array?

For example, if the sprite is at X=57, Y=45 then …

arrayX = (57 / 8)  .. equals 6 as an integer divided by an integer equals an integer.
arrayY = (45 / 8). .. equals 5.

… then check what is at array (6,5).

What’s? I don’t follow.

Which bit don’t you understand ?

Are you using an array?

@Pharap has given you this:

uint8_t getMapTile(uint8_t x, uint8_t y) {

  return pgm_read_word(&mapArrray[x + (MapWidth * y)]);

}

So, you can do this:

uint8_t x = (spriteXPos / TileWidth);
uint8_t y = (spriteYPos / TileHeight);

uint8_t tile = getMapTile(x, y);

// Do appropriate thing ..

switch (tile) {

  case TileTypes::Nothing: ...
  case TileTypes::Wall ...

}

I hope you have a proper enum for the tiles themselves (I have assumed it is called TileTypes).

1 Like

What! How do I do enum and what is an enum!

And what’s the “…”?

What is get map tile?EXPLAIN EVERY THING!

Enums are a list of values:

enum class TileTypes : uint8_t {
  Nothing,
  Wall,
  ...
}

Nothing is equal to 0, Wall to 1 and so forth. In my example you will need to cast the tile value to a TileType like that shown below.

uint8_t x = (spriteXPos / TileWidth);
uint8_t y = (spriteYPos / TileHeight);

uint8_t tile = getMapTile(x, y);

switch (**(TileType)**tile) {

  case TileTypes::Nothing: ...
  case TileTypes::Wall ...

}

Sorry should have picked that up before.

The … is where you add your code to do whatever it is that you want the program to do.

And getMapTile() is the function that @Pharap gave you …

Rude little … you could say ‘Please explain everything.’

2 Likes

Ouch! Also thanks. Also

There is an error saying “getMapTile not declared”

Ok … firstly, the ** either side of the (TileType) were bold formatting and you should remove them, so the line looks like:

switch ((TileType)tile) {

Not sure why the forum made my bold formatting look like that as it looked right on the screen.

Secondly, did you copy all of @Pharaps code into your project? He had that function in his code (that’s where I copied it from).

Finally, lines 32 to 39 will need to be inside your loop or other function to work. It looks like it is currently with the other declarations outside of your setup() and loop().

1 Like

Well one fixed one to goimage

Now it is lines 44 to 52.

You also cannot leave the … ( you need to put code there).
You need to put a : after Ladder.

It might pay to post the code … not these ‘screenshots’.

I’m using the iPad and can’t use computer because it’s too slow and I know it’s hard but I think this might be the last error
image

Read my comments above - its the third time I have posted it.

Also lines 32 and 33 should be calculated every time you move the player - not once like above.

Also line 43 uses the var ‘tiles’. Line 44 uses ‘tile’.

What does the #endif relate to?

1 Like

Sorry wrong pic and ifndef was just my maps.h
image

Wrong photo again!

`

You cannot declare a function inside another. Move the getMapTile() outside of the loop();