Hi, I'm trying to place pickups in my game using the map function, but I cannot figure out how to enable the player object to pick them up...
There is no native pixel-per-pixel sprite collision. You must use distances to check collisions, or bounding boxes.
Have you looked into how Jelpi.p8 does that? Does that suit you?
Basically you have two actors, one is your player and the other is the object, somewhat like:
They must have at least: X and Y positions, W (width) and H (height), providing the sprites do not rotate.
function make_simplified_actor(s,x,y,w,h) local a = {} a.sprite = s a.x=x a.y=y a.w=w a.h=h return a end |
then you check for collisions by testing the distance between them. This can be done in many ways. Jelpi does it like that (where a1 is actor 1 and a2 is actor2):
function collide(a1, a2) if (a1==a2) then return end local dx = a1.x - a2.x local dy = a1.y - a2.y if (abs(dx) < a1.w+a2.w) then if (abs(dy) < a1.h+a2.h) then -- COLLISION HAPPENED collide_event(a1, a2) end end end |
I know the above post is over 2 years old, but can anyone explain the...
If (a1==a2) then return end |
...line please?
Why are we checking if actor 1 equals actor 2 - I don’t understand that bit.
Many thanks.
I'm guessing it's just making sure that you're not checking if an object is colliding with itself.
Thanks for the reply.
I’m not in front of my PC at the moment so I can’t test this, but I can’t make the above logic work in my head in the following example:
You have two sprites - A1 is a 5x5 sprite located at 10,10, and A2 is also a 5x5 sprite but located at 17,17 - so they are not overlapping.
DX works out 10-17 for its value, so -7, and DY does the same so also -7, yes?
It asks if the abs of DX (so +7) is less than the sum of both sprite widths (which equal 10) to which the result is true so it moves on to a similar question for DY and the heights which will also be true.
...but with both these resulting in true, the supplied logic suggests that a collision has happened - which it hasn’t.
I can’t actually try this for a while so if anyone can shed any light I’d be very grateful.
The problem you're running into is that jelpi.p8 doesn't use pixel units for positioning and sizing. If you look at draw_actor(), you'll see that actors are drawn at pl.x*8-4, pl.y*8-8, not at pl.x, pl.y.
[Please log in to post a comment]