Stacking Objects on other Objects

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
LuckyChicken
Prole
Posts: 11
Joined: Sun Oct 01, 2017 11:16 am

Stacking Objects on other Objects

Post by LuckyChicken »

Hello Love2D forum

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
User avatar
steVeRoll
Party member
Posts: 140
Joined: Sun Feb 14, 2016 1:13 pm

Re: Stacking Objects on other Objects

Post by steVeRoll »

Welcome to the forums!
It seems that the code for updating the objects is a single for-loop that goes through all of the objects and updates them.
To check collision with other objects, we need another for-loop inside of the already existing for-loop, to check every object with every object.
It can be done like this:

Code: Select all

function love.update(dt)
  for i, v in ipairs(objects) do
    local canMove = true
    if CheckCollision(v,g) then
      canMove = false -- object has touched ground
    else
      for j,k in ipairs(objects) do -- here we check if the object is touching another object
        if i ~= j and CheckCollision(v, k) then
          canMove = false -- object has touched another object
        end
      end
    end
    if canMove then -- if the object hasn't touched anything
      v.y = v.y + 100*dt 
    end
  end
end
(That is probably not the most efficient approach. This can probably be optimized.)
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Stacking Objects on other Objects

Post by ivan »

The way that Box2D deals with stacking and jitter is through “sub-stepping“ and “positional correction“.
It does a fairly decent job when it comes to stacking.
It's typically stable, as long as you don't have a very massive object on top of light ones.
Please keep in mind that Box2D is a sophisticated piece of software that has been in development for over a decade.
You're not going to come up with anything better by messing around in Lua.
On the other hand, if you're going for non-realistic physics, then yea go ahead.
User avatar
LuckyChicken
Prole
Posts: 11
Joined: Sun Oct 01, 2017 11:16 am

Re: Stacking Objects on other Objects

Post by LuckyChicken »

Thank you very much!
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests