Unresolved collision bug(please help me)

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
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Unresolved collision bug(please help me)

Post by Pospos »

Hello, i have a collision bug that's unresolved in my game.
I can create one block but when i create two, the game doesn't recognise both.
Edit ; OK so i think it may be useful to add some precisions.

this is the code supposed to handle collision
collision.lua

Code: Select all

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) --actual logic
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

 -- move this outside of the function so it's not overwritten every time
function nextID()
	_id = _id + 1
	return _id
end
function newCollisionBox(x, y, skin)   --used to add to a table
	local bloc = { 
		id = nextID(),
		x = x,
		y = y,
		sprite = love.graphics.newImage('assets/tiles/'..skin..'.png')  
	}

	blocs[bloc.id] = bloc
end
function collision() -- checks every player movement
	for i, v in pairs(blocs) do

	if CheckCollision(v.x, v.y, 32, 32, player.x+5, player.y+10, 1, 50) then
		left = 1
	else
	    left = 0
	end

	if CheckCollision(v.x, v.y, 32, 32, player.x+35, player.y+10, 1, 50) then
		right = 1
	else
	    right = 0
	end

	if CheckCollision(v.x, v.y, 32, 32,  player.x+5, player.y+10, 30, 1) then
		up = 1
	else
	    up = 0
	end

	if CheckCollision(v.x, v.y, 32, 32,  player.x+5, player.y+62, 30, 1) then
		down = 1
	else
	    down = 0
	end
	

	end
end
function drawhitbox()
	
    love.graphics.rectangle("fill", player.x+5, player.y+10, 1, 50) --create a hitbox rectangle around player
    love.graphics.rectangle("fill", player.x+35, player.y+10, 1, 50)
    love.graphics.rectangle("fill", player.x+5, player.y+10, 30, 1)
    love.graphics.rectangle("fill", player.x+5, player.y+62, 30, 1)
end
function drawblocs()
	-- body
	for i=#blocs, 1, -1 do
		 love.graphics.draw(blocs[i].sprite, blocs[i].x, blocs[i].y) -- supposed to draw a white rectangle 
	end
end
this is main.lua

Code: Select all

require("player")
require("collision")
require("maps")
require("debug")
function love.load()
    _id = 0
    love.window.setMode(500, 500) --sets window
    love.window.setTitle("Essai")
    love.graphics.setDefaultFilter("nearest")
    loop = 1 --a value used to animate stuff
    loopmaker = 1 --same
    playerload()
    blocs = {}
    newCollisionBox(400, 200,  1) -- my collisions experiments
    newCollisionBox(200, 200,  2) -- my collisions experiments

end
function love.update(dt)
    loop = loop + 0.1 --activate loop
    if loop > 3.9 then loop = 1 end
    playerupdate()
    loopmaker = math.floor(loop)
    collision()
end

function love.draw()
    drawhitbox()
    drawblocs()
    playerdraw()
    
    love.graphics.print(loop, 0, 0)-- values to debug
    love.graphics.print(up .. down .. left .. right, 0, 10)
    love.graphics.print("length of # blocs "..#blocs, 0, 20)
    love.graphics.print("number of blocs ".._id, 0, 30)
    love.graphics.print("loopmaker "..loopmaker, 0, 40)
    love.graphics.print("blocs[1].x "..blocs[1].x, 0, 50)
    love.graphics.print("blocs[2].x "..blocs[2].x, 0, 60)
    love.graphics.print("blocs[1].y "..blocs[1].y, 0, 70)
    love.graphics.print("blocs[2].y "..blocs[2].y, 0, 80)
    love.graphics.print("blocs[1].id "..blocs[1].id, 0, 90)
    love.graphics.print("blocs[2].id "..blocs[2].id, 0, 100)




end
there is also this line in player.lua

Code: Select all

    up = 0
    down = 0
    left = 0
    right = 0 -- collision keys
    speed = 5
    
    
and these lines

Code: Select all

    if right == 0 then
    if love.keyboard.isDown("right") then
        player.x = player.x + speed
        player.actualsprite = player.spriteright
    end
    end

    if left == 0 then
    if love.keyboard.isDown("left") then
        player.x = player.x - speed
        player.actualsprite = player.spriteleft
    end
    end

 

    if  down == 0 then
        player.y = player.y + 5
    else
        if love.keyboard.isDown("up")  then
        player.y = player.y - love.math.random(100, 150)
       

        end  

    end

those aren't very important but the bug might come from them
Attachments
game(1).love
(77.38 KiB) Downloaded 94 times
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Unresolved collision bug(please help me)

Post by master both »

The problem is that in collision.lua, you're updating up, down, right, left for every box, so it may update those variables with the last checked box in the for loop, which can be any box, having unexpected results. This would work better:

Code: Select all

function collision() -- checks every player movement

	left = 0
	right = 0
	up = 0
	down = 0
	
	for i, v in pairs(blocs) do
		
		if CheckCollision(v.x, v.y, 32, 32, player.x+5, player.y+10, 1, 50) then
			left = 1
		end

		if CheckCollision(v.x, v.y, 32, 32, player.x+35, player.y+10, 1, 50) then
			right = 1
		end

		if CheckCollision(v.x, v.y, 32, 32,  player.x+5, player.y+10, 30, 1) then
			up = 1
		end

		if CheckCollision(v.x, v.y, 32, 32,  player.x+5, player.y+62, 30, 1) then
			down = 1
		end
	end
end
So those variables are set to 1 only if it's coliding with anybox, but it doesn't set to 0 when checking another box.
I hope my explanation is clear :P my lack of english is making it hard to express myself.
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: Unresolved collision bug(please help me)

Post by Pospos »

thanks a lot for your cool help.
That's great beacause i'm having this problem for two hours from now.
thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 36 guests