Help with Collision Detection

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
PaleWhiteHorse
Prole
Posts: 7
Joined: Tue May 15, 2012 4:08 am

Help with Collision Detection

Post by PaleWhiteHorse »

I am just starting with LOVE with basic python and visual basic experience. I have worked out a lot in the past few days but now i am stuck on a collision detection problem. In the game you are a duck who dodges falling objects which are objects loaded in a table. For some reason when i run a For loop through the table to check for collision it doesn't work. I have tried bounded box collision, a collision function someone posted here and will try Hardon Collider tomorrow but would like to know why nothing is currently working. Thanks!

Code: Select all

collision = "Null"
fallingtrue = 0
fallspeed = 100
falling = {}
function love.load()
	player_x = 0
	player_y = 360
	fuel = 0
	counter = 0
	player = love.graphics.newImage("babyduck.png")
	background = love.graphics.newImage("rain_background.png")
end

function love.update(dt)
	counter = counter +(math.ceil(dt) )
	if fallingtrue > 1 then
		for i = 0,13 do
			enemy = {}
			enemy.width = 20
			enemy.height = 20
			enemy.x = i * (enemy.width + 100) + (math.random(-100, 100))
			enemy.y = 0
			table.insert(falling, enemy)
		end
		fallingtrue = 0
	end
	if counter == 500 then
		fallingtrue = 5
	end
	if fuel > 0 then
		fuel = fuel - .5
	end
	if player_x > 1200 then
		player_x = -20
	end
	if player_x < -40 then
		player_x = 1200
	end
	if player_y < 10 then
		player_y = 10
	end
	
	if love.keyboard.isDown("right") then
		player_x = player_x + 2
	end
	if love.keyboard.isDown("left") then
		player_x = player_x - 2
	end
	
	if love.keyboard.isDown("escape") then
		love.event.push("quit")
	end
	if love.keyboard.isDown("up") and fuel < 600 then
		player_y = player_y - 2.3
		fuel = fuel + 2
	end
	if player_y < 360 then
		player_y = player_y + 1
	end
	for i,v in ipairs(falling) do
		if player_x > (v.x + v.height) or player_x + 40 < v.x then
			collision = "Miss"
		else
			collision = "HIT"
		end
		v.y = v.y + (dt * fallspeed)
		if v.y > 450 then
			table.remove(falling, i)
			fallingtrue = fallingtrue + .1
			if fallspeed < 700 then
				fallspeed = fallspeed + 5
			end
		end
	end
end

function love.draw()
	love.graphics.draw(background, 0, 0)
	love.graphics.draw(player, player_x, player_y)
	love.graphics.setColor(255,255,255,255)
	love.graphics.print(fuel, 10, 10)
	love.graphics.print(counter, 10, 40)
	love.graphics.print(collision, 10, 70)
	love.graphics.setColor(255,0,255,255)
	for i,v in ipairs(falling) do
		love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
	end
end
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Help with Collision Detection

Post by coffee »

Your code isn't the best atm but it will let that for later. Several problems in your collision code.
1 - You reset your hit indicator after each individual check so in the end only the last result check is retained/printed after the multiple contact check. So if existed contacts before you are erasing it.
2 - Your code was only checking X positions. This way even that enemies were only above you would mark as true collision.

A very quick fix to put you in track. I disabled images and put player as rectangle for now. You can put it back later and put player_width and player_height as player:getWidth() and player:getHeight()

Code: Select all

function love.load()
	collision = "Null"
	fallingtrue = 0
	fallspeed = 100
	falling = {}
   player_x = 0
   player_y = 360
   player_width = 40
   player_height = 40
   fuel = 0
   counter = 0
--   player = love.graphics.newImage("babyduck.png")
--   background = love.graphics.newImage("rain_background.png")
end

function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)

  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end

function love.update(dt)
   counter = counter +(math.ceil(dt) )
   if fallingtrue > 1 then
      for i = 0,13 do
         enemy = {}
         enemy.width = 20
         enemy.height = 20
         enemy.x = i * (enemy.width + 100) + (math.random(-100, 100))
         enemy.y = 0
         table.insert(falling, enemy)
      end
      fallingtrue = 0
   end
   if counter == 500 then
      fallingtrue = 5
   end
   if fuel > 0 then
      fuel = fuel - .5
   end
   if player_x > 1200 then
      player_x = -20
   end
   if player_x < -40 then
      player_x = 1200
   end
   if player_y < 10 then
      player_y = 10
   end
   
   if love.keyboard.isDown("right") then
      player_x = player_x + 2
   end
   if love.keyboard.isDown("left") then
      player_x = player_x - 2
   end
   
   if love.keyboard.isDown("escape") then
      love.event.push("quit")
   end
   if love.keyboard.isDown("up") and fuel < 600 then
      player_y = player_y - 2.3
      fuel = fuel + 2
   end
   if player_y < 360 then
      player_y = player_y + 1
   end
   
   collision = "No Hit"
   for i,v in ipairs(falling) do
      if  CheckCollision(player_x,player_y,player_width,player_height, v.x,v.y,v.width,v.height) then
         collision = "Hit!"
      end
      v.y = v.y + (dt * fallspeed)
      if v.y > 450 then
         table.remove(falling, i)
         fallingtrue = fallingtrue + .1
         if fallspeed < 700 then
            fallspeed = fallspeed + 5
         end
      end
   end
end

function love.draw()
   -- love.graphics.draw(background, 0, 0)
   love.graphics.setColor(255,0,0,255)
   -- love.graphics.draw(player, player_x, player_y)
   love.graphics.rectangle("fill", player_x, player_y, player_width, player_height)
   love.graphics.setColor(255,255,255,255)
   love.graphics.print(fuel, 10, 10)
   love.graphics.print(counter, 10, 40)
   love.graphics.print(collision,10, 70)
   love.graphics.setColor(255,0,255,255)
   for i,v in ipairs(falling) do
      love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
   end
end
PaleWhiteHorse
Prole
Posts: 7
Joined: Tue May 15, 2012 4:08 am

Re: Help with Collision Detection

Post by PaleWhiteHorse »

Thank you so much for the quick response it works perfectly now! I looked at the website in your desc but it only had one post :cry: you should definitely update it more!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests