Log In  

I don't know how to run a line of code when one sprite collides with one another, could someone please help me out with this. (I am VERY new to pico-8 so keep it simple) Thanks :)

P#121887 2022-12-06 02:46

You check for overlap in the _update function and do something.
See https://mboffin.itch.io/pico8-overlap

P#121889 2022-12-06 02:54

Without getting too complex, you don't actually check to see if a sprite you drew on the screen hit another sprite you drew on the screen. What you're actually checking is, "If I make a box around the spot where I drew this sprite, and I make another box around the spot where I drew this other sprite, would those two boxes overlap?"

And to answer that question, you need to know information about both boxes. Specifically, you need to know where the opposite corners of those boxes would be. (Usually you use the top-left and bottom right.) Knowing where those corners are, there's some math you can do with those numbers to see if those boxes overlap.

You can also use the x/y coordinates of one corner of the box (usually the top-left) and the width/height of the box. (Because you can just add the width to the x coordinate and the height to the y coordinate to get the new x/y of the opposite corner.)

Usually when you draw a sprite, you know the x/y of where you're drawing the sprite, and you know how wide/tall it is (usually 8x8). Using that info, you can check for overlap.

Here's a visualization I made that shows how the overlap-checking works. It might not be super clear, but hopefully it helps. You can move the boxes in the visualization around with up/down/left/right and E/D/S/F, and pressing X will show the box boundaries.


(I'm sure you'll get some other helpful answers in this thread as well.)

P#121891 2022-12-06 02:55 ( Edited 2022-12-06 02:58)

Thanks for the help, but I still cant quite understand how and when you sense when both boxes overlap.

P#121892 2022-12-06 03:00

Hopefully the first half of this video helps make it more visually clear:


Basically, if you do that kind of overlap-checking, then you can do something if it does find an overlap. Like this:

function overlap(box1,box1)
  --do a bunch of math stuff and give back
  --a true/false whether the two boxes overlap

function _update()
  if overlap(player,enemy) then
    --do something here if these two
    --things overlap. Make a sound?
    --Subtract a number from the player's
    --health? Print, "You got hit!"
    --Whatever you want.
P#121893 2022-12-06 03:12

If it's the classic 8x8 sprites you want, @FloatyBoi, to check a collision on you can use this:

function collide(x1,y1,x2,y2)
if (abs(x1-x2)<8 and abs(y1-y2)<8) return true

Or change the 8 to a smaller number so near misses are not counted.

Here is a sample. Use the arrow keys to navigate and the 🅾️ key to swap between which target to control, the player or the enemy. Click on Code▽ below to see source-code.

Cart #misawonupe-0 | 2022-12-06 | Code ▽ | Embed ▽ | No License

P#121894 2022-12-06 03:34 ( Edited 2022-12-06 18:04)

Thanks it worked!

P#121945 2022-12-06 20:07

Also is there a way to make it so that the blue sprite disappears when you collide with it (while controlling red cube)

P#121946 2022-12-06 20:14

Hey! I see you want to make the blue one disappear from the screen when colliding, so here's an update on @dw817's cart to make it do just that.

Cart #sihonebume-0 | 2022-12-07 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

This one simply hides the player and disables collision and movement. Deleting the player is more complicated, but if you would like something like that I can try my best.

P#121957 2022-12-07 01:13

thanks for the help!! deleting the blue square was more of what i was looking for but no pressure :)

P#122012 2022-12-08 03:20

[Please log in to post a comment]