Page 1 of 3
Code for collisions
Posted: Fri Feb 06, 2009 12:54 pm
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.
Re: Code for collisions
Posted: Fri Feb 06, 2009 3:00 pm
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?
Re: Code for collisions
Posted: Fri Feb 06, 2009 3:48 pm
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 (3.41 KiB) Viewed 12594 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.
Re: Code for collisions
Posted: Fri Feb 06, 2009 3:55 pm
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.
Re: Code for collisions
Posted: Fri Feb 06, 2009 4:04 pm
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
Re: Code for collisions
Posted: Sat Feb 07, 2009 1:22 am
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.
Re: Code for collisions
Posted: Sat Feb 07, 2009 2:00 am
by osgeld
actually the old systems and arcades did it pixel perfect in hardware, since it was just a matter of comparing bits
Re: Code for collisions
Posted: Sat Feb 07, 2009 7:37 am
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
Re: Code for collisions
Posted: Sat Feb 07, 2009 10:08 am
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.
Re: Code for collisions
Posted: Sat Feb 07, 2009 1:08 pm
by farvardin
isn't the physic engine already handling collisions without the need to use boxes or masks?