help collision of TileMap en love2d

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
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

help collision of TileMap en love2d

Post by luislasonbra »

Hi I need help with collisions of TileMap

Code: Select all

require('Script/camara')

function love.load() 
    
    CELLSIZE=32
    
    createMap()
 
    text = "Estado: "
    Score = "0"
    FontSize = love.graphics.newFont(20)
    love.graphics.setFont(FontSize)
    
    image = love.graphics.newImage("personaje/elicopter.png")
    width = love.graphics.getWidth()
    height = love.graphics.getHeight()
    camera:setBounds(0, 0, width *500, height)
  
    player = {
      Cells={},
      x = width / 2,
      y = height / 2,
      width = image:getWidth(),
      height = image:getHeight(),
      speed = 150
    }

end

function createMap()
  map = {
      {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
      {0,0,0,1,0,0,0,0,0,0,},
      {0,0,0,1,0,0,0,0,0,0,},
      {0,0,0,0,0,0,0,0,1,0,},
      {0,0,0,0,0,0,0,1,0,0,},
      {0,0,0,0,0,0,0,0,0,0,},
      {0,0,0,0,0,0,0,0,0,0,},
      {0,0,0,0,0,0,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
  }
end

function love.draw()
    
    camera:set()
    
    love.graphics.print(text,900,100)
    
    love.graphics.setColor(0, 0, 255)
    for y=1,#map do
        for x=1,#map[y] do
        if map[y][x] == 1 then
            love.graphics.rectangle("fill", x*CELLSIZE - 32, y*CELLSIZE - 42, CELLSIZE, CELLSIZE)
        else
          if DEBUG then
              love.graphics.rectangle("line", x*CELLSIZE - 32, y*CELLSIZE - 42, CELLSIZE, CELLSIZE)
          end
        end
      end
    end
  
    love.graphics.setColor(255, 255, 255)
    love.graphics.print(Score, player.x - width + 400, 0)

    love.graphics.draw(image, player.x, player.y)
    
    camera:unset()
    
end

function love.update(dt)
    
    player.x = player.x + player.speed * dt 
    Score = player.x
  ------------------------------------------------------------------------------------------------------------
    
    if love.keyboard.isDown("return") then
        player.y = height / 2
        player.x = 10
        text = "Estado: reset"
    end
    
    if love.keyboard.isDown("r") then
        player.y = height / 2
        player.x = 10
        text = "Estado: reset"
    end
    
    if love.keyboard.isDown("x") then
        player.y = player.y - player.speed * dt 
    else
        player.y = player.y + player.speed * dt
    end
    
    camera:setPosition(player.x - width / 2, player.y - height / 2)

    if isOffMap(player.x, player.y) then
        text = "You loset"
        player.y = height / 2
        player.x = 10
    end
        
end

function math.clamp(x, min, max)
  return x < min and min or (x > max and max or x)
end

-- El usuario fuera del mapa?
function isOffMap(x, y)
    if x<CELLSIZE -32 or x+player.width> (1+#map[1])*CELLSIZE - 32
        or y<CELLSIZE -10 or y+player.height>(1+#map)*CELLSIZE - 75
  then
    return true
  else
    return false
  end
end

function CheckCollider(x, y)
    -- find which tile the point is in
    local tx, ty = math.floor(x*CELLSIZE / 32), math.floor(y*CELLSIZE / 42)
    -- check the tile
    if map[tx][ty] == 1 then
        return false
    else
    return true
    end
end

--======================================================================================================================



I have the following code but cant get what's this collide with tiles
kesac
Prole
Posts: 49
Joined: Tue Apr 03, 2012 12:29 am
Contact:

Re: help collision of TileMap en love2d

Post by kesac »

Can you post a .love file that includes your "camara" script and assets? Would be easier to debug this way...
Have you met the Child of Winter?
User avatar
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

Re: help collision of TileMap en love2d

Post by luislasonbra »

kesac wrote:Can you post a .love file that includes your "camara" script and assets? Would be easier to debug this way...

ok here I leave the. love

File love
http://www.mediafire.com/download.php?jom8elnv947bvkm
kesac
Prole
Posts: 49
Joined: Tue Apr 03, 2012 12:29 am
Contact:

Re: help collision of TileMap en love2d

Post by kesac »

You were only checking collisions with the ceiling and floor of the map. I added a few lines of code to your isOffMap() function to show you how to detect a collision with the center of your helicopter and any solid tile.

Basically what I did was:

Check which map cell the player's central point was in (I had to compensate for your map offset and other magic offsets in the attached file)

Code: Select all

player.tileX = math.floor((player.x + player.width/2) / CELLSIZE)
player.tileY = math.floor((player.y + player.height/2) / CELLSIZE)
Then find out if the cell the player was in was solid

Code: Select all

if map[player.tileY] and map[player.tileY][player.tileX] and map[player.tileY][player.tileX] == 1 then
    return true
end
Attachments
coopTer PC-PointCollision.love
(2.62 KiB) Downloaded 181 times
Have you met the Child of Winter?
User avatar
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

Re: help collision of TileMap en love2d

Post by luislasonbra »

kesac wrote:You were only checking collisions with the ceiling and floor of the map. I added a few lines of code to your isOffMap() function to show you how to detect a collision with the center of your helicopter and any solid tile.

Basically what I did was:

Check which map cell the player's central point was in (I had to compensate for your map offset and other magic offsets in the attached file)

Code: Select all

player.tileX = math.floor((player.x + player.width/2) / CELLSIZE)
player.tileY = math.floor((player.y + player.height/2) / CELLSIZE)
Then find out if the cell the player was in was solid

Code: Select all

if map[player.tileY] and map[player.tileY][player.tileX] and map[player.tileY][player.tileX] == 1 then
    return true
end

ok thanks for your help
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests