Help. Need to fix the jumping

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. Need to fix the jumping

Post by ShadowPenguins » Fri Dec 15, 2017 5:57 am

i need help to make the jumping better and need help with the collisions heres my code.

Code: Select all

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

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

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("space") then
			player.y = player.jump 
	end
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(0,255,0)
	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 = 50,
	y = 400,
	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)
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)
end
Last edited by bartbes on Fri Dec 15, 2017 8:09 pm, edited 1 time in total.
Reason: Add code tags
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: Google [Bot], pgimeno, unixfreak and 19 guests