--Input (sprite2): another sprite to check for collision detection |
if (yoursprite1:collision(yoursprite2) = true) then
-- a collision has happened, do something
end
ruxpin said:
Put this code in your init.lua file so all Sprites can use it.
--Input (sprite2): another sprite to check for collision detection
--Return: return true if the rectangles overlap otherwise return false
function Sprite:collision(sprite2)
-- self bottom < other sprite top
if self:getY() + self:getHeight() < sprite2:getY() then
return false
end
-- self top > other sprite bottom
if self:getY() > sprite2:getY() + sprite2:getHeight() then
return false
end
-- self left > other sprite right
if self:getX() > sprite2:getX() + sprite2:getWidth() then
return false
end
-- self right < other sprite left
if self:getX() + self:getWidth() < sprite2:getX() then
return false
end
return true
end
--Input (sprite2): another sprite to check for collision detection |
--Input (sprite2): another sprite to check for collision detection |
--[[
Input (sprite2): another sprite to check for collision detection
Return: return true if the rectangles overlap otherwise return false
]]
function Sprite:collidesWith(sprite2)
local x,y,w,h = self:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
-- self bottom < other sprite top
if y + h < y2 then
return false
end
-- self top > other sprite bottom
if y > y2 + h2 then
return false
end
-- self left > other sprite right
if x > x2 + w2 then
return false
end
-- self right < other sprite left
if x + w < x2 then
return false
end
print('self bounds:',x,y,w,h,' sprite2 bounds:',x,y,w,h)
return true
end
victor_wujitouch said:Thanks for the code. I found (as alexzheng mentioned) that you have to be take the anchor point on the Bitmap into consideration. Then I found the post below that uses Sprite:getBounds(). Using this function returns the correct bounds relative to the stage, solving the anchor point problem.
This function should be added back to the class reference documentation.
--[[
Input (sprite2): another sprite to check for collision detection
Return: return true if the rectangles overlap otherwise return false
--]]
function Sprite:collidesWith(sprite2)
local x,y,w,h = self:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
-- self bottom < other sprite top
if y + h < y2 then
return false
end
-- self top > other sprite bottom
if y > y2 + h2 then
return false
end
-- self left > other sprite right
if x > x2 + w2 then
return false
end
-- self right < other sprite left
if x + w < x2 then
return false
end
print('self bounds:',x,y,w,h,' sprite2 bounds:',x,y,w,h)
return true
end
function Sprite:collidesWith(sprite2)
local x,y,w,h = self:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
end
Loves: sunnyguy
local cHandle = { x = 0, y = 0 }
-- set Anchor Point
function setCollisionAnchorPoint(x, y)
cHandle.x = x
cHandle.y = y
end
-- get Anchor Point
function getCollisionAnchorPoint()
return cHandle.x, cHandle.y
end
function boxToBox(spriteAX, spriteAY, widthA, heightA, spriteBX, spriteBY, widthB, heightB)
-- Check for vertical GAP
local Ay1, By1 = spriteAY - (heightA * cHandle.y ), spriteBY - (heightB * cHandle.y )
if Ay1 + heightA < By1 then
return false
end
if Ay1 > By1 + heightB then
return false
end
-- Check for horizontal GAP
local Ax1, Bx1 = spriteAX - (widthA * cHandle.x ), spriteBX - (widthB * cHandle.x )
if Ax1 > Bx1 + widthB then
return false
end
if Ax1 + widthA < Bx1 then
return false
end
-- Boxes Collide...
return true
end
Cyberience said:Is there a way to do this for odd shapes? Like a Star. or TRiangle?
It looks like you're new here. If you want to get involved, click one of these buttons!
ar2rsawseen
2646
OZApps
1300
techdojo
1235