Code for collisions

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Peter Hickman
Prole
Posts: 15
Joined: Fri Jan 30, 2009 8:19 am

Code for collisions

Post by Peter Hickman »

So far the experimenting that I have been doing has involved points and boxes and it is pretty easy to do the 'is this point inside this box'. But the code I am now looking at will involve object collisions and I'm not 100% as to how I should proceed. Given two images, one in a 32*32 box and another in a 64*64 box for example, my plan is as follows. Given that the position of an object / image is it's centre:

1) See if the boxes are more than (32 + 64) / 2 pixels apart on the x or y axis. If true then they have not collided, otherwise
2) See if the boxes overlap. If false they have not collided, otherwise
3) Pixel level collision!

Now point 1 is a cheap test to remove most of the candidates. I am even thinking that point 2 might not actually be very useful but I am completely stumped as to how to do pixel level collision detection in love.

Any pointers please.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Code for collisions

Post by osuf oboys »

Not sure what you're asking. Pixel level? Why would you do that? You know the corners and the sides that makes them up. Are you doing something like in http://love2d.org/forum/viewtopic.php?f=4&t=435?
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
Peter Hickman
Prole
Posts: 15
Joined: Fri Jan 30, 2009 8:19 am

Re: Code for collisions

Post by Peter Hickman »

The problem with relying on boxes for collision detection is that there are times when the character will die when the player can clearly see that the object did not touch them. Which is very very annoying, as you can see from the image the boxes collide but the objects did not collide.
Untitled.png
Untitled.png (3.41 KiB) Viewed 12132 times
This sort of thing can completely ruin a game. If we were doing this in C we would be using masks along with the image and matching those. Not sure how to do this in love though.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Code for collisions

Post by osuf oboys »

Of course you would give the balls circular collision shapes. For the body - well, depends on how exact you want it to be. You can add a number of convex polygons and a circle for head - or simply a fairly tight single rectangle.

Regarding a mask, you could do that, but would have to make it in a format you can read in. Not sure if there's a simple image loader in lua and working in LÖVE where you get access to the bits. If you would place the mask it would be easy though.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Code for collisions

Post by osgeld »

osuf oboys wrote: Not sure if there's a simple image loader in lua and working in LÖVE where you get access to the bits. If you would place the mask it would be easy though.
i have one that i made for psp luaplayer, it uses binary pbm, ill convert it and post it when i get home tonight (its 10 am here so dont go holding your breath) then using a mask should be pretty easy if you wanted to do so
deltaphc
Prole
Posts: 2
Joined: Sat Feb 07, 2009 1:06 am

Re: Code for collisions

Post by deltaphc »

For box collisions, it can work if you fine-tune the hitbox for each sprite. The hitbox doesn't have to match the size of the entire sprite; it can be much smaller.

This is how it was done in classic games, especially when CPU power was limited. The hitbox would cover a small-ish portion of the sprite (so that it looks decent when colliding), and it worked fine for the most part. Of course we're no longer limited by CPU power, but that doesn't mean everything should use per-pixel collisions just because it can.

Either way is fine; I'm just throwing out a case for bounding boxes. ^^
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Code for collisions

Post by osgeld »

actually the old systems and arcades did it pixel perfect in hardware, since it was just a matter of comparing bits
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Code for collisions

Post by osgeld »

osgeld wrote:
osuf oboys wrote: Not sure if there's a simple image loader in lua and working in LÖVE where you get access to the bits. If you would place the mask it would be easy though.
i have one that i made for psp luaplayer, it uses binary pbm, ill convert it and post it when i get home tonight (its 10 am here so dont go holding your breath) then using a mask should be pretty easy if you wanted to do so
well i got home, and got over ambitious, decided to make a sprite mask lib, that is really open and generic to fit most peoples needs, including animation

got about a third of the way tru it, finally woke up and noticed theres no way in hell i could get it done tonight, so i started porting my pbm script

which i have functional, mostly, but atm i have to manually strip out the pbm header info in a hex editor .... so its not 100% yet

so expect a pbm loader by tomorrow, which reads binary (black n white ) masks into a lua table for use, expect the full masking lib hopefully by the end of this weekend, but i dont promise it (but since its something ive done before in lua it wont be toooooo long)

and since it was originally designed for a psp (333mhz with 90% of that sucked down) it will include box collisions, then as sprites get closer it will increase in resolution via a matrix type logic, that way your not checking a per pixel map on 1000000000000 sprites per loop
Attachments
Screenshot.jpg
Screenshot.jpg (23.05 KiB) Viewed 12062 times
Screenshot2.jpg
Screenshot2.jpg (62.04 KiB) Viewed 12062 times
User avatar
Peter Hickman
Prole
Posts: 15
Joined: Fri Jan 30, 2009 8:19 am

Re: Code for collisions

Post by Peter Hickman »

Thanks for the effort that you are putting in to this. I look forward to my spaceships not exploding from simply being in the proximity of a rock.
User avatar
farvardin
Party member
Posts: 167
Joined: Sat Jun 28, 2008 6:46 pm

Re: Code for collisions

Post by farvardin »

isn't the physic engine already handling collisions without the need to use boxes or masks?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 58 guests