I wanted to ask how it could work that I can create an object which I can stack on another object and that the objects are always falling down. But not if they are on the ground. (I want to do this without love.physics/Box2D)
Yet its working that the objects are falling down and if they are colliding with the Ground then they are stopping falling/moving.
But if im trying to stack for example 2 objects on the ground then they are just going inside each other and nothing happens.
Heres the best result/code I archieved yet:
Code: Select all
function CheckCollision(obj1, obj2) --Function for collision checking
return obj1.x < obj2.x+obj2.w and
obj2.x < obj1.x+obj1.w and
obj1.y < obj2.y+obj2.h and
obj2.y < obj1.y+obj1.h
end
function love.load()
objects = {} --Define a home for the objects
g = {x = 50, y = 500, w = 50, h = 10} --Define the ground
end
function love.keyreleased(key)
if key=='space' then
table.insert(objects, {x = love.mouse.getX(), y = love.mouse.getY(), w = 5, h = 5}) --Create a new object
end
end
function love.update(dt)
for i, v in ipairs(objects) do --Here I also tried two for-querys and very much other things but the results wasn't really different then.
if CheckCollision(v, v) and not CheckCollision(v, g) then
v.y = v.y + 100*dt --Makes the objects fall down
end
end
end
function love.draw()
for i, v in ipairs(objects) do
love.graphics.rectangle("fill", v.x, v.y, v.w, v.h) --Draw the objects
end
love.graphics.rectangle("line", g.x, g.y, g.w, g.h) --Draw the ground
end