Page 1 of 1

Platformer Collision Problem [Solved]

Posted: Sun Jul 16, 2023 6:08 pm
by malcolm3308
Nevermind I figured it out.

I am trying to make the player collide with a block. This works for colliding left and right however, if you land on top of the block the player is pushed off the block as if you were walking into the block from the side.

How do I fix this.

Code: Select all

function love.load()
	platform = {}
	platform.width = love.graphics.getWidth()
	platform.height = love.graphics.getHeight()
	
	platform.x = 0
	platform.y = platform.height * 2/3
	
	player = {}
	
	player.x = platform.width * 1/2
	player.y = (platform.height * 2/3) - 15
	
	player.img = love.graphics.newImage("cube.png")
	player.xspeed = 150
	player.ground = player.y
	player.velocity = 0
	player.jumpHeight = -600
	player.gravity = -1200
	player.width = 15

	tilemap = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 1
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 2
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 3
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 4
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 5
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 6
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 7
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 8
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 9
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},-- 10
		{0, 1, 0, 0, 0, 0, 0, 0, 0, 0},-- 11
    }

	blocks = {} 	 	
test = 0


end

function love.keypressed(key)
	if key == "space" then
			if player.velocity == 0 then
				player.gravity = -1200
				player.velocity = player.jumpHeight
			end
	end	
end

function love.update()

local dt = love.timer.getDelta()

	if love.keyboard.isDown("a") then
		player.x = player.x - player.xspeed * dt
	end

	if love.keyboard.isDown("d") then
		player.x = player.x + player.xspeed * dt
	end

	if love.keyboard.isDown('s') then
		
 	end
	
	if player.velocity ~= 0 then
		player.y = player.y + player.velocity * dt
		player.velocity = player.velocity - player.gravity * dt
	end
	
	if player.y > player.ground then
		player.velocity = 0
		player.y = player.ground
	end

	if collision(player.x, player.y, player.width, 25 * 2, 25*15,25) == true then
		local player_current_x = player.x
		local player_current_y = player.y

		if verticallyAligned(player_current_x, player_current_y, player.width, 25 * 2, 25 * 15, 25) == true then
			if player_current_x + player.width/2 < 50 + 25/2 then
				local move = player.x + player.width - 50
				player.x = player.x - move
			else
				local move = 50 + 25 - player.x
				player.x = player.x + move
			end
		elseif horizontallyAligned(player_current_x, player_current_y, player.width, 25 * 2, 25 * 15, 25) == true then
			player.velocity = 0
			if player_current_y + player.width/2 < 25*15 + 25/2 then
				local move = player.y + player.width - 25*15
				player.y = player.y - move
			else
				local move = 25*15 + 25 - player.y
				player.y = player.y + move
			end
		end
	end

		if verticallyAligned(player.x, player.y, player.width, 25 * 2, 25*15,25) == true then
			test = 2
		elseif horizontallyAligned(player.x, player.y, player.width, 25 * 2, 25*15,25) == true then
			test = 3
		else test = 0
		end
end
function love.draw()

	love.graphics.setColor(1,1,1)
	love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
	love.graphics.draw(player.img, player.x, player.y)
	
	for i,row in ipairs(tilemap) do
		for j,tile in ipairs(row) do
			if tile == 1 then
				love.graphics.rectangle("fill", j * 25, i * 25 + 100, 25, 25)
			end
		end
	end
	love.graphics.setColor(1,0,0)
	love.graphics.rectangle("fill",25 * 2,25*15,25,25)
	love.graphics.setColor(1,1,1)
    love.graphics.print(table.concat(
	{'player y:'..player.y,
	"platform y:".. platform.y,
	"player velo:"..player.velocity,
	"player gravity:"..player.gravity,
	"test: "..test,}, '\n'))

end

function collision(ax,ay,awidth,bx,by,bwidth)
	local a_left = ax
	local a_right = ax + awidth
	local a_top = ay
	local a_bottom = ay + awidth
	local b_left = bx
	local b_right = bx + bwidth
	local b_top = by
	local b_bottom = by + bwidth

	return a_right > b_left 
	and a_left < b_right
	and a_top < b_bottom
	and a_bottom > b_top 
end

function verticallyAligned(ax,ay,awidth,bx,by,bwidth)
	return ay < by + bwidth and ay + awidth > by
end

function horizontallyAligned(ax,ay,awidth,bx,by,bwidth)
	return ax < bx + bwidth and ax + awidth > bx
end
Note: the player is a cube with a width and height of 15 and the block is a cube with a width and height of 25

Re: Platformer Collision Problem

Posted: Sun Jul 16, 2023 9:15 pm
by pgimeno
Collisions are hard to resolve properly. I suggest you switch to a collision resolution library like bump.lua.

Re: Platformer Collision Problem

Posted: Sun Jul 16, 2023 9:30 pm
by malcolm3308
pgimeno wrote: ↑Sun Jul 16, 2023 9:15 pm Collisions are hard to resolve properly. I suggest you switch to a collision resolution library like bump.lua.
😭😭😭😭😭😭😭😭