Having problems with an AABB Collision Function

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
JacKobOriginal
Prole
Posts: 10
Joined: Thu Mar 22, 2018 12:05 am
Location: Colombia
Contact:

Having problems with an AABB Collision Function

Post by JacKobOriginal » Mon Jun 17, 2019 9:30 pm

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
I don't even know what to put in my signature please help me.

pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Having problems with an AABB Collision Function

Post by pedrosgali » Tue Jun 18, 2019 5:43 am

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.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end

User avatar
AdrianN
Citizen
Posts: 66
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Re: Having problems with an AABB Collision Function

Post by AdrianN » Tue Jun 18, 2019 7:01 pm

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

User avatar
Karai17
Party member
Posts: 879
Joined: Sun Sep 02, 2012 10:46 pm

Re: Having problems with an AABB Collision Function

Post by Karai17 » Sat Jun 22, 2019 5:38 pm

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
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests