Help. Can't fix the collisions

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
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Help. Can't fix the collisions

Post 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
.
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Help. Can't fix the collisions

Post 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.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help. Can't fix the collisions

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
jojomickymack
Prole
Posts: 45
Joined: Tue Dec 26, 2017 4:52 pm

Re: Help. Can't fix the collisions

Post 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 2844 times
User avatar
ShadowPenguins
Prole
Posts: 14
Joined: Thu Sep 21, 2017 5:50 am

Re: Help. Can't fix the collisions

Post by ShadowPenguins »

Hey thank you guys so much. I'll deffinetly use all these tips
Current Projects, Insomnia and The Wolf's Spiral
Insomnia Progress and files if you want to help can be found here https://github.com/TheShadowPenguin/Insomnia

~Silver~
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 41 guests