Tile collision problem

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
qrux
Prole
Posts: 28
Joined: Thu Nov 03, 2011 1:11 am

Tile collision problem

Post by qrux »

Hello,
for some reason my collision detection won't work very well. I followed the steps in this thread: http://love2d.org/forums/viewtopic.php?f=4&t=2096
It does stop my player, but not when bumping into tiles but instead at random (probably not-so-random) places. Would anyone be kind enough to take a look at my code? My main.lua:

Code: Select all

require "LICK"

function love.load()

  love.graphics.setMode(640,480,false,true,30);
  love.graphics.setCaption("LOL");
  love.graphics.setFont(14)
  text = "Alien";
  
  player_img = love.graphics.newImage("img/NEUTRAL_ALIEN.png");

  bgimg = love.graphics.newImage("img/ye.png");
  
  player = {}
  player.x = 50
  player.y = 361
  player.speed = 200
  
  
  -- MAP --
     
   love.graphics.setFont(12)
   
   -- map-variabler
   map_w = 640
   map_h = 480
   tile_w = 32
   tile_h = 32

   map={ 
    
   {1, 1, 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, 0, 0, 1},
   {1, 0, 0, 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, 0, 0, 1},
   {1, 0, 0, 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, 0, 0, 1},
   {1, 0, 0, 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, 0, 0, 1},
   {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1},
   {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   {1, 0, 0, 0, 4, 4, 4, 4, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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}
    
}
  map_display_w = #map[1]
  map_display_h = #map

   --tiles
   tile = {}
   for i=0,4 do
      tile[i] = love.graphics.newImage( "img/tiles/tile"..i..".png" )
   end
end


function map2world(mx,my)
	return mx*tile_w, my*tile_h
end
function world2map(x,y)
	return math.floor(x/tile_w), math.floor(y/tile_h)
end


function draw_map(mx, my)

   for y=1, map_display_h do
      for x=1, map_display_w do
         love.graphics.draw(tile[ map[y] [x] ], map2world((x-1), (y-1)))
      end
   end
end


-- SLUT PÅ MAP

function love.update(dt)

  fps = love.timer.getFPS()

local tileX, tileY = world2map( player.x, player.y )
local leftTileX, leftTileY = tileX - 1, tileY
local leftTileNo = map[leftTileY][leftTileX]

local rightTileX, rightTileY = tileX - 1, tileY
local rightTileNo = map[rightTileY][rightTileX]


  if love.keyboard.isDown("left") and leftTileNo == 0 then
    player.x = player.x - player.speed*dt
    
    player_img = love.graphics.newImage("img/ALIENWHEELSHIT.png");
    
  elseif love.keyboard.isDown("right") then
    player.x = player.x + player.speed*dt
    
    player_img = love.graphics.newImage("img/ALIENWHEELSHIT2.png");
  
  else
    player_img = love.graphics.newImage("img/NEUTRAL_ALIEN.png");
  end

end

function love.draw()

  love.graphics.print("FPS: "..fps, 40, 40)

  draw_map()

  love.graphics.draw(player_img,player.x,player.y,0,1,1,0,0)

  love.graphics.print(text, player.x + 15, player.y - 30);
end
Thanks!
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Tile collision problem

Post by miko »

qrux wrote:Hello,
for some reason my collision detection won't work very well. I followed the steps in this thread: http://love2d.org/forums/viewtopic.php?f=4&t=2096
It does stop my player, but not when bumping into tiles but instead at random (probably not-so-random) places. Would anyone be kind enough to take a look at my code?
It is better to upload entire working *.love package, because I have no idea what is inside LICK.lua (which may be important).
qrux wrote:

Code: Select all


local tileX, tileY = world2map( player.x, player.y )
local leftTileX, leftTileY = tileX - 1, tileY
local leftTileNo = map[leftTileY][leftTileX]

local rightTileX, rightTileY = tileX - 1, tileY
local rightTileNo = map[rightTileY][rightTileX]
Are you sure about that? I think you meant:

Code: Select all

local rightTileX, rightTileY = tileX + 1, tileY
Still, any "collision" I see is here:

Code: Select all

  if love.keyboard.isDown("left") and leftTileNo == 0 then
    player.x = player.x - player.speed*dt
  elseif love.keyboard.isDown("right") then
    player.x = player.x + player.speed*dt
I would print out the value of leftTileNo to the console to debug it if still does not work.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest