my collision is bugging out and i cant't figure it out
Posted: Thu Sep 26, 2013 4:24 pm
i have worked on a collision detection system for a week now and cannot make it work properly. i have tried to search for help but none of the cases have worked for me.
it works fine with two objects but when it gets more than tree to handle it bugs out. I am quite new to lua and very new to love so try to keep it simple (not necessary can google it but it would help alot )
here is the code:
[function physics:Collided(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
-- obj2 side
local height1 = obj1.texture:getHeight( )
local width1 = obj1.texture:getWidth( )
local height2 = obj2.texture:getHeight( )
local width2 = obj2.texture:getWidth( )
if obj1.pos.x > obj2.pos.x and obj1.pos.x < obj2.pos.x+width2 or obj1.pos.x+width1 > obj2.pos.x and obj1.pos.x+width1 < obj2.pos.x+width2 then
if obj1.pos.y > obj2.pos.y and obj1.pos.y < obj2.pos.y+height2 or obj1.pos.y+height1 > obj2.pos.y and obj1.pos.y+height1 < obj2.pos.y+height2 then
local left = obj2.pos.x+width2 < obj1.pos.x+1
local right = obj2.pos.x > obj1.pos.x+width1-1
local up = obj2.pos.y+height2 < obj1.pos.y+1
local down = obj2.pos.y > obj1.pos.y+height1-1
--if obj2.name == "lol1" then
print(left,right,up,down)
--end
if up then
return true, "up"
elseif down then
return true, "down"
elseif right then
return true, "right"
elseif left then
return true, "left"
end
end
end
return false
end][/code]
also tips is appreciated
it works fine with two objects but when it gets more than tree to handle it bugs out. I am quite new to lua and very new to love so try to keep it simple (not necessary can google it but it would help alot )
here is the code:
[function physics:Collided(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
-- obj2 side
local height1 = obj1.texture:getHeight( )
local width1 = obj1.texture:getWidth( )
local height2 = obj2.texture:getHeight( )
local width2 = obj2.texture:getWidth( )
if obj1.pos.x > obj2.pos.x and obj1.pos.x < obj2.pos.x+width2 or obj1.pos.x+width1 > obj2.pos.x and obj1.pos.x+width1 < obj2.pos.x+width2 then
if obj1.pos.y > obj2.pos.y and obj1.pos.y < obj2.pos.y+height2 or obj1.pos.y+height1 > obj2.pos.y and obj1.pos.y+height1 < obj2.pos.y+height2 then
local left = obj2.pos.x+width2 < obj1.pos.x+1
local right = obj2.pos.x > obj1.pos.x+width1-1
local up = obj2.pos.y+height2 < obj1.pos.y+1
local down = obj2.pos.y > obj1.pos.y+height1-1
--if obj2.name == "lol1" then
print(left,right,up,down)
--end
if up then
return true, "up"
elseif down then
return true, "down"
elseif right then
return true, "right"
elseif left then
return true, "left"
end
end
end
return false
end][/code]
also tips is appreciated