Hi so I wanted to use the mouse in pico 8 and I know the poke and stat thhing butI just cant figure out how to make a good collision detection Because there is no way to check if sprite istuching a nother sprite that I know of ? So could someone helpme me ? Beacuse I cant find any tutorials on this
It's going to depend on what you're checking for collisions with. Do you want to see if the mouse is over a particular map tile? Then you would do something like this:
x = stat(32) y = stat(33) if mget(x/8, y/8) == tile_number then -- do stuff here end
Or you could check for flags, just change the if statement:
if fget(mget(x/8, y/8), flag_number) then -- do stuff here end
If you're checking for collisions with enemies or whatever then you'll need to keep an array of their positions and loop through them (but you'll need to do that anyway to draw them on screen.) Assuming that each enemy is stored as a table that has an x- and a y- coordinate:
for i=1,#enemies do local e = enemies[i] if x > e.x and x - e.x < 8 and y > e.y and y - e.y < 8 then -- do stuff here end end
That if statement just checks that the mouse pointer is somewhere inside the 8x8 area of the enemy sprite you're checking. If your sprites are bigger or smaller then you might want to adjust those numbers to suit your situation. If your sprites are a bunch of different sizes then you might store a width and height along with the enemy's x- and y-coordinate so you can calculate it per enemy. You'd just have to change it like so:
if x > e.x and x - e.x < e.width and y > e.y and y - e.y < e.height then -- do stuff here end
Hope that helps.
I'll note that the above only checks the mouse pointer position itself. It just occurred to me that you might want to know if a sprite controlled by the mouse is overlapping another sprite. This isn't much more difficult. Let's assume that the sprites are called player and enemy:
if player.x < enemy.x + enemy.width and player.x + player.width > enemy.x and player.y < enemy.y + enemy.height and player.y + player.height > enemy.y then -- do stuff here end
Where player.x and player.y are just the x- and y-coordinates of the mouse.
[Please log in to post a comment]