Page 1 of 1

Having problems with an AABB Collision Function

Posted: Mon Jun 17, 2019 9:30 pm
by JacKobOriginal
I'm trying to work with an AABB collision function for an RPG game, and I'm basically trying to make it so the player doesn't go through the block and just treats it as a solid object, the player is always at the center of the screen and the objects that exist within the game move when I press a key, so far I have this and when I tested it, the player just stops when it touches the block, I tried to change the true/false value in blockcollision to false whenever it goes the opposite direction, but it just doesn't seem to work.

Here's the code:

Code: Select all

function love.update(dt)
    if CheckCollision(player.x, player.y, player.w, player.h, blockx, blocky, blockw, blockh) then
        blockcollision = true
    end
    if love.keyboard.isDown("a") then
        if blockcollision == false then
            textx = textx + player.spd * dt
            blockx = blockx + player.spd * dt
        end
    elseif love.keyboard.isDown("d") then
        if blockcollision == false then
            textx = textx - player.spd * dt
            blockx = blockx - player.spd * dt
        end
    end
    if love.keyboard.isDown("w") then
        if blockcollision == false then
            texty = texty + player.spd * dt
            blocky = blocky + player.spd * dt
        end
    elseif love.keyboard.isDown("s") then
        if blockcollision == false then
            texty = texty - player.spd * dt
            blocky = blocky - player.spd * dt
        end
    end
end

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

Re: Having problems with an AABB Collision Function

Posted: Tue Jun 18, 2019 5:43 am
by pedrosgali
This isn't the best way to do collisions as you'll run into problems when you have multiple things to check. Sadly I don't have time to e plain it right now, I'm sure someone else will. To fix your immediate problem however, you need to set blockcollision to false at the start of update. Essentially set it to false, then check if it's TRUE. If there is no collision then it will always be false.

Re: Having problems with an AABB Collision Function

Posted: Tue Jun 18, 2019 7:01 pm
by AdrianN
Hello, I'm not sure if this will solve your problem.
You save your last xy axis and if it collides you assign it to your player.x and player.y.
In theory it should work.

Code: Select all

player={x=0,y=0,w=25,h=25,oldx=0,oldy=0}
boxes={}
table.insert(boxes,{x=40,y=0,w=50,h=50})

col=false


function love.update(dt)
	--first you move your player
	player_move()
	--you check the collisions
	collisions()
end


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

function collisions()
	for _,box in ipairs(boxes) do
		col=CheckCollision(player.x,player.y,player.w,player.h,box.x,box.y,box.w,box.h) 

		if col then

			player.x=player.oldx
			player.y=player.oldy
		end
	end
end

function player_move()
	if love.keyboard.isDown("a") then
		player.x=player.x-1
	elseif love.keyboard.isDown("d") then
		player.x=player.x+1
	elseif love.keyboard.isDown("w") then
		player.y=player.y-1
	elseif love.keyboard.isDown("s") then
		player.y=player.y+1
	end
	
	if not col then
		--save old position
		player.oldx=player.x
		player.oldy=player.y
	end
	
end

function love.draw()
	love.graphics.rectangle("fill",player.x,player.y,player.w,player.h)

	for i,box in ipairs(boxes) do
		love.graphics.rectangle("line",box.x,box.y,box.w,box.h)
	end

	love.graphics.print(tostring(col),500,500)
end
Edit : Also, you can see this post, Karolek's reply is more detailed.

https://love2d.org/forums/viewtopic.php ... 92#p225592

Re: Having problems with an AABB Collision Function

Posted: Sat Jun 22, 2019 5:38 pm
by Karai17

Code: Select all

function love.update(dt)
    if not CheckCollision(player.x, player.y, player.w, player.h, blockx, blocky, blockw, blockh) then
        if love.keyboard.isDown("a", "left") then
            textx = textx + player.spd * dt
            blockx = blockx + player.spd * dt
        end

        if love.keyboard.isDown("d", "right") then
            textx = textx - player.spd * dt
            blockx = blockx - player.spd * dt
        end

        if love.keyboard.isDown("w", "up") then
            texty = texty + player.spd * dt
            blocky = blocky + player.spd * dt
        end

        if love.keyboard.isDown("s", "down") then
            texty = texty - player.spd * dt
            blocky = blocky - player.spd * dt
        end
    end
end

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