Collision teleports rectangle to corner
Posted: Sat May 21, 2022 6:49 pm
Hello folks. I'm trying to make a physics engine but whenever a rectangle hits the corner of another rectangle, it teleports to the other end of the hit rectangle. I'm not sure if it's anything wrong with my code or what.
Code: Select all
function collide(object)
for i,v in ipairs(workspace) do
if CheckCollision(object.xpos,object.ypos,object.x,object.y,v.xpos,v.ypos,v.x,v.y) and v ~= object then
if object.olypos < v.ypos then
if object.xpos > v.xpos and (object.xpos + object.x) < (v.xpos + v.x) then
object.vely = 0
object.ypos = object.olypos
object.ypos = (v.ypos - object.y)
end
end
if object.olypos + object.y > v.ypos then
if object.xpos > v.xpos and (object.xpos + object.x) < (v.xpos + v.x) then
object.vely = 0
object.ypos = object.olypos
object.ypos = ((v.ypos + v.y))
end
end
if object.olxpos < v.xpos then
if object.ypos > v.ypos and (object.ypos + object.y) < (v.ypos + v.y) then
object.velx = 0
object.xpos = object.olxpos
object.xpos = (v.xpos - object.x)
end
end
if object.olxpos + object.x > v.xpos then
if object.ypos > v.ypos and (object.ypos + object.y) < (v.ypos + v.y) then
object.velx = 0
object.xpos = object.olxpos
object.xpos = ((v.xpos + v.x))
end
end
end
end
end