Log In  

Hello!
I've started learning how to do some programming with Pico-8 and am following a youtube tutorial (https://www.youtube.com/watch?v=T9z6RPvyypE)

I've been typing the same code more or less and have encountered a problem with wall collision.

It seems that my player character will move through certain tiles I've flagged as walls and there are also invisible walls on tiles that are not flagged at all.

code for collision:

function is_tile(tile_type,x,y)
tile=mget(x,y)
has_flag=fget(tile,tile_type)
return has_flag
end

function can_move(x,y)
return not is_tile(wall,x,y)
end

and the code for player movement/wall detection:

function move_player()
newx=plr.x
newy=plr.y

if (btnp(⬅️)) newx-=1
if (btnp(➡️)) newx+=1
if (btnp(⬆️)) newy-=1
if (btnp(⬇️)) newy+=1

if (can_move(newx,newy)) then
    plr.x=mid(0,newx,127)
    plr.y=mid(0,newy,63)
else
    sfx(0)
end

end

not sure this happens, the youtubers character is able to move and hit the proper tiles and I haven't changed much in my code compared to his.

P#105924 2022-01-29 21:03

1

It sounds like your drawing code is more likely the issue. The code you posted checks the map in memory based on the raw position of the player, which only allows the stuff on screen to match if you drew both the player and the map with aligned values.

P#105926 2022-01-29 21:40

ah ha! I noticed the youtuber multiplied the PLR.X & Y by 8, I made mine different numbers so I could change the starting position. My character can walk around now, however now I'm trying to think how I can move the character without the *8 added and not collide invisible walls...

P#105928 2022-01-29 22:12

The reason the *8 is there is because the spr() function takes a position in pixels, but the code in that tutorial keeps track of the player position in tiles. If you want the starting position to be different, that would be in the make_player() function, or in the _init() function after calling make_player() if you need to check other things too.

If you want the character to move between tiles rather than always snapping to the next tile, you can either change the movement code to use values less than 1 (in which case the *8 would still need to be there), or you can change the collision detection to translate from pixels to tiles (by dividing newx and newy by 8). However, if you do that, then the character will be able to partially enter walls due to only checking the top-left of the character. To freely move while checking against the map, you would need to check all 4 corners of the player's sprite to see if any part of the player's sprite in colliding with a wall.

P#105940 2022-01-30 04:16

[Please log in to post a comment]