[solved] tilemap collision problems
Posted: Tue Dec 07, 2021 5:18 pm
helo, so im trying to make this tetris clone, and everything is working except that the actual gameplay doesnt, the collision detection (atleast i think thats it) is basically making u lose instantly
im linking the .love file, i recommend playing it w/console because i have made some debug,
the collision detection code is tetris.lua at lines 278-295 and the code that locks the pieces in place is in the same file at lines 133-163
thanc in advanc
EDIT: to give a deeper explanation how the code works:
each piece is represented by four squares, each with an x and a y coordinate { {x1,y1}, {x2,y2}, {x3,y3}, {x4,x4} }
however, they are not stored in the piece variable directly - all possible pieces (each rotation is a separate piece) are stored in a table and the piece variable controlled by the player only has 3 components: x and y position and an id, determining which one of the (19) possible pieces & rotations the player controls. there is only one function for checking collisions - you supply it with the position (x and y) and the id of the piece you want to check collisions for, and it should check if any of the squares is either out of bounds, or overlapping with the board (the board is a tilemap with either 0s (no square) or a value from 1 to 3 (the square graphic, all should act the same)), and returns true if it does. the locking-in-place is fairly simple - the game checks if the piece can be moved down (checks with the coordinates x, y + 1) and if it can't, it adds all 4 of its squares into the tilemap (this part works). the problem is, the overlap checking code returns true even if on the very top of the board, when it's all empty (found out through debugging). can anyone help? (btw thak you for your time if you read all this and decide to help you're *awesome*)
im linking the .love file, i recommend playing it w/console because i have made some debug,
the collision detection code is tetris.lua at lines 278-295 and the code that locks the pieces in place is in the same file at lines 133-163
thanc in advanc
EDIT: to give a deeper explanation how the code works:
each piece is represented by four squares, each with an x and a y coordinate { {x1,y1}, {x2,y2}, {x3,y3}, {x4,x4} }
however, they are not stored in the piece variable directly - all possible pieces (each rotation is a separate piece) are stored in a table and the piece variable controlled by the player only has 3 components: x and y position and an id, determining which one of the (19) possible pieces & rotations the player controls. there is only one function for checking collisions - you supply it with the position (x and y) and the id of the piece you want to check collisions for, and it should check if any of the squares is either out of bounds, or overlapping with the board (the board is a tilemap with either 0s (no square) or a value from 1 to 3 (the square graphic, all should act the same)), and returns true if it does. the locking-in-place is fairly simple - the game checks if the piece can be moved down (checks with the coordinates x, y + 1) and if it can't, it adds all 4 of its squares into the tilemap (this part works). the problem is, the overlap checking code returns true even if on the very top of the board, when it's all empty (found out through debugging). can anyone help? (btw thak you for your time if you read all this and decide to help you're *awesome*)