Sprite collisions with a colour?

i was trying to make a pacman clone, but the walls were thinner than a tile so i couldnt use the built in walls feature, is there any wayt o make it collide with the blue colour?

we don’t have any support for that, but you could try coding it yourself! not trivial though.

instead, you could try using tilemaps with 8x8 tiles instead of 16x16 tiles to make your walls thinner. add the arcade-tile-util extension to get access to 8x8 tilemaps

2 Likes

But what if we want custom tile sizes?

@RedSprite the only tile sizes supported by MakeCode are ones where the side length is a power of 2. So 4x4, 8x8, 16x16, 32x32 are all supported (though we have only surfaced 8x8 and 16x16)

we have this restriction in place for performance reasons, when converting between x/y sprite coordinates to column/row tile coordinates you need to divide the x/y values by whatever the width of the tile is. division is slow on hardware, especially when floating point numbers are involved.

however, there is actually a trick to doing division when the divisor is a power of 2. because numbers are stored in binary on computers, you can divide and multiply numbers by powers of 2 using bitshifting. bitshifting is a super fast operation compared to division and multiplication!

for example, say we have the number 13. in binary, 13 is written as 00001101

if we shift all of the bits to the left, we get this:

00001101 << 1 = 00011010 = 26

if we shift all of the bits to the right, we get this:

00001101 >> 1 = 00000110 = 6

so shifting left is the same as multiplying by 2 and shifting right is the same as dividing by 2 (but the remainder gets cut off, which doesn’t matter for our purposes).

let’s look at another example: say we have a sprite at the coordinates x=50, y=37 and we want to figure out which tile column and row they are on in a tilemap with 8x8 tiles. 8 is equal to 2^3, so if we bitshift right by 3 we can divide by 8

// x = 50
00110010 >> 3 = 00000110 = 6

// y = 37
00100101 >> 3 = 00000100 = 4

so we know that this sprite is on the tile in column 6 and row 4

2 Likes

Noice

1 Like

Would it be possible to make tiles 20x20?

1 Like

@Sonicblaston that’s not a power of 2, so no not today

1 Like

I don’t see the problem with just coding it yourself! You can use the Color At block


to detect the color in front of Packman, depending on which direction it’s moving, and stop it if the color one pixel ahead of Packman is the blue color!
Personally, I would code a sort of grid system into this so that I could deal with that instead of all the sprite color detection stuff, but you do you!