Code: Select all
local function findIntersect(l1p1x,l1p1y, l1p2x,l1p2y, l2p1x,l2p1y, l2p2x,l2p2y, seg1, seg2)
local a1,b1,a2,b2 = l1p2y-l1p1y, l1p1x-l1p2x, l2p2y-l2p1y, l2p1x-l2p2x
local c1,c2 = a1*l1p1x+b1*l1p1y, a2*l2p1x+b2*l2p1y
local det,x,y = a1*b2 - a2*b1
if det==0 then
return false
end
x,y = (b2*c1-b1*c2)/det, (a1*c2-a2*c1)/det
if seg1 or seg2 then
local min,max = math.min, math.max
if seg1 and not (min(l1p1x,l1p2x) <= x and x <= max(l1p1x,l1p2x) and min(l1p1y,l1p2y) <= y and y <= max(l1p1y,l1p2y)) or
seg2 and not (min(l2p1x,l2p2x) <= x and x <= max(l2p1x,l2p2x) and min(l2p1y,l2p2y) <= y and y <= max(l2p1y,l2p2y)) then
return false, "The lines don't intersect."
end
end
return x,y
end --Taken off of https://love2d.org/wiki/Additional_math Probably not meant for collision, but oh well, it works
local function collisionCheck(v1, v2, v1name, v2name, dt)
local should = true
if v1.shouldcollide then --Checks a bunch of lines and stuff.
down = findIntersect(v1.x + v1.width, v1.y, v1.x, v1.y+v1.height, v2.x + v2.width, v2.y, v2.x, v2.y, true, true)
down2 = findIntersect(v1.x, v1.y, v1.x + v1.width, v1.y+v1.height, v2.x, v2.y, v2.x + v2.width, v2.y, true, true)
right = findIntersect(v1.x + v1.width, v1.y, v1.x, v1.y + v1.height - 5, v2.x, v2.y, v2.x, v2.y + v2.height, true, true)
right2 = findIntersect(v1.x, v1.y, v1.x + v1.width, v1.y + v1.height - 5, v2.x, v2.y, v2.x, v2.y + v2.height, true, true)
left = findIntersect(v1.x + v1.width, v1.y, v1.x, v1.y + v1.height - 5, v2.x + v2.width, v2.y, v2.x + v2.width , v2.y + v2.height, true, true)
left2 = findIntersect(v1.x, v1.y, v1.x + v1.width, v1.y + v1.height - 5, v2.x + v2.width, v2.y, v2.x + v2.width, v2.y + v2.height, true, true)
up = findIntersect(v1.x + v1.width, v1.y, v1.x, v1.y, v2.x + v2.width, v2.y + v2.height, v2.x, v2.y + v2.height, true, true)
up2 = findIntersect(v1.x, v1.y, v1.x + v1.width, v1.y, v2.x, v2.y + v2.height, v2.x + v2.width, v2.y + v2.height, true, true)
end
if down or down2 or up or up2 then --Dissection into horizontal and vertical
vertical = true
end
if right or right2 or left or left2 then
horizontal = true
end
if down or down2 then
if v1.x + v1.width >= v2.x and v1.x + v1.width <= v2.x + 2 and v1.inAir and name == "Bighead" or v1.x - 2 >= v2.x + v2.width and v1.x <= v2.x + v2.width and not v1.inAir and name == "Bighead" then
if math.abs(v1.ySpeed-gravity*dt) < math.abs(v1.xSpeed) then --A small part where dt is used -_-
horizontal = true
vertical = false
else
vertical = true
horizontal = false
end
end
end
if v1.x + 20 > v2.x and v1.x < v2.x + v2.width - 10 and v1.y + v1.height >= v2.y and v1.y <= v2.y + v2.height and v1name == "Bighead" and not horizontal then
vertical = true
end
if v1name == "Bighead" and v2name == "block" and down or down2 or right or rigth2 then --Ice and fallthrough stuf.
if v2.fallthrough then
if v1.ySpeed > 500*dt then
vertical, horizontal = false, false
local x, y = math.min(v2.x/(32*scale)), math.min(v2.y/(32*scale))
if not editormode then
map[x][y] = 0
end
v2.kill = true
end
end
if v2.ice then
v1.friction = 1
else
v1.friction = 5
end
end
if horizontal then
if v1.xSpeed > 0 then --Moving right
v1.x = v2.x - v1.width - 2 --Move back
if v1.rightcollide then
v1:rightcollide(v2, v2name)
end
elseif v1.xSpeed < 0 then
v1.x = v2.x + v2.width + 2 --Where do you think you're going?
if v1.leftcollide then
v1:leftcollide(v2, v2name)
end
end
v1.xSpeed = 0 --HOLD UP
end
if vertical then
should = false
if v1.ySpeed == 0 then
v1.y = v1.oldy
elseif v1.ySpeed < 0 then
if v1.upcollide then
v1:upcollide(v2, v2name)
end
v1.y = v2.y + v2.height + 2
elseif v1.ySpeed >=0 then
v1.y = v2.y - v1.height
if v1.downcollide then
v1:downcollide(v2, v2name)
end
end
v1.ySpeed = 0 --HOLD UP
end
--GET RID OF ALL THE EVIDENCE.
down, down2, right, right2, up, up2, left, left2, horizontal, vertical = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
return should
end
local function docollision(dt) --Handles loops for physics. Lot less complicated than it seems.
for j, w in pairs(objects) do --
if j ~= "block" then --Blocks don't need to collide with blocks. Cuz they dont move.
for i, v in pairs(w) do -- Select first objects table
for name, lol in pairs(objects) do --Go through all objects names again
for __, value in pairs(lol) do -- Goes through second objetcs table.
if (j~=name) then -- They dont need to collide with themselves.
local properx = gamescroll
if v.x >= properx and v.x < properx + love.graphics.getWidth() and value.x >= properx and value.x < properx + love.graphics.getWidth() then
if collisionCheck(v, value, j, name, dt) then --LETS DO SOME COLLISION.
v.inAir = true
else
v.inAir = false
end
end
end
end
end
end
end
end
end
function loadphysics(dt) --Deltatime Put in game, will load all the physics
docollision(dt) --Yay collision system that does physics for me
end
Thanks for telling me, heres a .love. (BTW the gui is mouse based)