It looks like you're new here. If you want to get involved, click one of these buttons!
function Sprite:collision(sprite2) local myX = self:getX() local myY = self:getY() local myWidth = self:getWidth() local myHeight = self:getHeight() local otherX = sprite2:getX() local otherY = sprite2:getY() local otherWidth = sprite2:getWidth() local otherHeight = sprite2:getHeight() if myY + myHeight < otherY or myY > otherY + otherHeight then return false end if myX > otherX + otherWidth or myX + myWidth < otherX then return false end return true end |
Comments
Likes: misterhup
There's another function that tells you whether a point is inside a box or not, so you can check the 4 points of a rotated box and check if any of them is inside the other box.
So it won't work in those cases
Are you needing to detect collisions with rotated rectangles?
Likes: mertocan, misterhup
But about Sprite implementation, I remember there were hacks, like transforming coordinates yourself, or using your own sprite local bounds and transforming coordinates to global.
Theoretically it could work, but not 100% sure
This thread could help you:
http://giderosmobile.com/forum/discussion/1733/spritegetboundstargetsprite/p1
Likes: misterhup
When you run the example project you can click and drag one of the boxes around. If it collides with the other, both turn red.
NOTE: The example is fairly basic and does not perform any collision resolution but with a bit of work it could easily do so. The "response" table in the main loop contains pretty much everything you need to resolve the collisions.
Anyway, it should be enough to get you up and running
EDIT: See further down the thread for the latest version of the example
Likes: misterhup, totebo, rolfpancake
Likes: antix
Likes: misterhup, pie, MoKaLux