Log In  

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...

P#15988 2015-10-29 05:55 ( Edited 2017-11-25 10:36)

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
P#15996 2015-10-29 11:11 ( Edited 2015-10-29 15:11)

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.

P#46679 2017-11-25 03:09 ( Edited 2017-11-25 08:09)

I'm guessing it's just making sure that you're not checking if an object is colliding with itself.

P#46680 2017-11-25 03:37 ( Edited 2017-11-25 08:38)

Correct

P#46683 2017-11-25 04:38 ( Edited 2017-11-25 09:38)

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.

P#46684 2017-11-25 04:53 ( Edited 2017-11-25 09:53)

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.

P#46685 2017-11-25 05:36 ( Edited 2017-11-25 10:36)

[Please log in to post a comment]