help, Collider

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

help, Collider

Post by luislasonbra »

Hello, I'm trying to make a scale effect, for a platform game, but I have problems when trying to climb the stairs.

This is the code that I have so far.

Code: Select all

function love.load()
    player = {}
    player.x = 64
    player.y = 400
    player.w = 32
    player.h = 32
    player.speed = 400
    
    map = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    }
end

function love.update(dt)
    local oldX, oldY = player.x, player.y
    
    --player.y = player.y + 550 * dt
    
    if love.keyboard.isDown("up") then
        player.y = player.y - player.speed * dt
    elseif love.keyboard.isDown("down") then
        player.y = player.y + player.speed * dt
    end
    if love.keyboard.isDown("left") then
        player.x = player.x - player.speed * dt
    elseif love.keyboard.isDown("right") then
        player.x = player.x + player.speed * dt
    end
    
    if IsColliderTileMap(player.x, player.y) then
        player.x = oldX
        player.y = oldY
    end
    
    IsColliderTileMapLine(dt)
end

function love.draw()
    for y = 1, #map do
       for x = 1, #map[y] do
          if map[y][x] == 1 then
              love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
          end
          if map[y][x] == 2 then
             love.graphics.polygon('line',x*32,(y+1)*32,(x+1)*32,(y+1)*32,(x+1)*32,(y*32)) 
          end
       end 
    end

    love.graphics.rectangle("line", player.x, player.y, player.w, player.h)
end

function IsColliderTileMap(x, y)
    local x1 = math.floor(x / player.w)
    local y1 = math.floor(y / player.h)
    if map[y1][x1] == 1 or map[y1][x1+1] == 1 or map[y1+1][x1] == 1 or map[y1+1][x1+1] == 1 then
        return true
    end
    return false
end

function IsColliderTileMapLine(dt)
    for y = 1, #map do
       for x = 1, #map[y] do
           if Iscollider(player,  x*32,y*32, 32, 32) then
               if map[y][x] == 2 then
                  player.x = player.x - player.speed * dt
                  player.y = player.y - player.speed * dt 
               end
           end
       end
    end
   --love.graphics.polygon('line',x*32,(y+1)*32,(x+1)*32,(y+1)*32,(x+1)*32,(y*32))
end

function Iscollider(obj1, x, y, w, h)
    if obj1.x+obj1.w > x and obj1.x < x+w and obj1.y+obj1.h > y and obj1.y < y+h then
        return true
    end
    return false
end
the problem I have is with the blocks of the stairs, which are irregular, and do not know how to detect these types of rectangles.

I hope you can help me
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests