Page 1 of 1

Help. Can't fix the collisions

Posted: Sun Dec 17, 2017 4:46 pm
by ShadowPenguins
So i have been working on a platformer for school senior project but im new and don't know how i would fix the collisions or add a camera to follow the player so i can add more platforms.

Thank you in advanced for replying and helping :awesome:

Code: Select all

local bump = require "bump"
world = bump.newWorld()

player = {
	x = 0,
	y = 0,
	width = 32,
	height = 64,
	jump = -350,
	gravity = 500,
	runSpeed = 400,
	xVelocity = 0,
	yVelocity = 0,
	terminalVelocity = 420
}

function player.setPosition(x, y)
	player.x, player.y = x, y
end

function player.update(dt)
	player.move(dt)
	player.applyGravity(dt)
	player.collied(dt)
end

function player.move(dt) 
if love.keyboard.isDown("d") then
	player.xVelocity = player.runSpeed
elseif love.keyboard.isDown("a") then
	player.xVelocity = -player.runSpeed
else
	player.xVelocity = 0
	end
	
	if love.keyboard.isDown("w") then
	
		if player.yVelocity == 0 then
			player.yVelocity = player.jump
			end
			 
	end
end

if player.yVelocity ~= 0 then
		player.y = player.y + player.yVelocity 
		player.yVelocity = player.yVelocity - player.gravity 
	end

function player.applyGravity(dt)
	if player.yVelocity < player.terminalVelocity then
		player.yVelocity = player.yVelocity + player.gravity * dt
	else
		player.yVelocity = player.terminalVelocity
	end
end

function player.collied(dt)
	local futureX = player.x + player.xVelocity * dt
	local futureY = player.y + player.yVelocity * dt
	local nextX, nextY, cols, len = world:move(player, futureX, futureY )
	
	for i = 1, len do
	local col = cols[i]
	if col.normal.y == -1 or col.normal.y == 1 then
		player.yVelocity = 0 
		end
	end
	
	player.x = nextX
	player.y = nextY
end

function player.draw()
	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
end

local block = {
	x = 120,
	y = 500,
	width = 300,
	height = 32
}

local block2 = {
	x = 450,
	y = 400,
	width = 100,
	height = 32
}

local block3 = {
	x= 50,
	y = 400,
	width = 100,
	height = 32
}

local block4 = {
	x = 200,
	y = 100,
	width = 100,
	height = 32
}

local block5 = {
	x = 400,
	y = 100,
	width = 100,
	height = 32
}

function love.load()
	player.setPosition(love.graphics.getWidth()/2, 0)
	player.img = love.graphics.newImage('purple.png')
	world:add(player, player.x, player.y, player.width, player.height)
	world:add(block, block.x, block.y, block.width, block.height)
	world:add(block2, block2.x, block2.y, block2.width, block2.height)
	world:add(block3, block3.x, block3.y, block3.width, block3.height)
	world:add(block4, block4.x, block4.y, block4.width, block4.height)
	world:add(block5, block5.x, block5.y, block5.width, block5.height)
end

function love.update(dt)
	player.update(dt)
end

function love.draw()
	love.graphics.draw(player.img, player.x, player.y, 0, 1, 1, 0, 32)
	love.graphics.setColor(0, 255, 0)
	love.graphics.rectangle("line", block.x, block.y, block.width, block.height)
	love.graphics.setColor(0, 0, 255)
	love.graphics.rectangle("line", block2.x, block2.y, block2.width, block2.height)
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("line",block3.x, block3.y, block3.width, block3.height)
	love.graphics.setColor(255,255,0)
	love.graphics.circle("line", block4.x, block4.y, block4.width, block4.height)
	love.graphics.setColor(255,255,255)
	love.graphics.rectangle("line", block5.x, block5.y, block5.width, block5.height)
end
.

Re: Help. Can't fix the collisions

Posted: Tue Dec 19, 2017 10:27 am
by Sasha264
Hello!
It is nice that you attached code!
Your previous question looks the same https://love2d.org/forums/viewtopic.php?f=4&t=84678
I tried to run it to detect your problem.
But I failed in that because first line of your code is require "bump" and I don't have any "bump"s :crazy:
Maybe you will attach it into the question too, so someone will help you.

Re: Help. Can't fix the collisions

Posted: Tue Dec 19, 2017 3:05 pm
by zorg
Better yet, learn how to make a .love file, instructions on the wiki, and upload that as an attachment instead; we don't need the whole bump library's contents posted onto the forums. :P

Re: Help. Can't fix the collisions

Posted: Sun Dec 31, 2017 6:07 am
by jojomickymack
I don't see anything wrong with the collisions, the jumping and platforming are satisfactory. Actually I see what you mean on the left side of each of these platforms. I made an offset of 40 and subtracted it from each block's x coordinate and it's working as it should now.

You don't need a camera, just translate and scale relative to the player's position.

Add this to your function love.draw()

Code: Select all

  scale = 0.8
  dx = player.x - (love.graphics.getWidth() / 2) / scale
  dy = player.y - (love.graphics.getHeight() / 2) / scale

  love.graphics.scale(scale)
  love.graphics.translate(-dx, -dy)
it's working great now, ready for more platforms and shapes etc further to the right - nice work!
jumping.gif
jumping.gif (31.26 KiB) Viewed 2863 times

Re: Help. Can't fix the collisions

Posted: Wed Jan 10, 2018 2:52 am
by ShadowPenguins
Hey thank you guys so much. I'll deffinetly use all these tips