slide effect

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
thewifitree
Prole
Posts: 32
Joined: Sat Dec 21, 2013 5:16 pm

slide effect

Post by thewifitree »

when it comes to making a game more realistic, I suck. I created a jump and move effect with the player, the only thing is when the player stops moving and you take your hand of the key to move him it is a very abrupt stop. I want to create an effect for the player to be able to slide a couple pixels after the player takes their hand off of the move key, but don't know where to start with this.
Thank you for any help!

Code: Select all

world = {}
world.ground = 590

player = {}
player.x = 300
player.y = world.ground - 60
player.h = 60
player.speed = 200
player.vy = 0
player.vx = 0
player.canJump = true
player.moveleft = true
player.moveright = true

gravity = 1800
jumpv = -900

function love.load()
	mattlemen1 = love.graphics.newImage("graphics/mattlemen1.jpg")
end

function love.draw()
	love.graphics.setBackgroundColor(0, 128, 128)
	
	--draw player
	love.graphics.setColor(255, 255, 255, 255)
	--love.graphics.draw(mattlemen1, player.x, player.y)
	love.graphics.rectangle("fill", player.x, player.y, player.h, player.h)
	
	--ground
	love.graphics.setColor(0, 128, 0, 100)
	love.graphics.rectangle("fill", 0, world.ground, 800, 800)
	
	--drawlevel1()
	
end

function love.update(dt)
	--controls
	if player.moveleft == true then
	if love.keyboard.isDown("left") then
		player.x = player.x - player.speed*dt
	end
	end
	
	if player.moveright == true then
	if love.keyboard.isDown("right") then
		player.x = player.x + player.speed*dt
	end
	end
	
	--gravity
	player.y = player.y + (player.vy * dt)
	player.vy = player.vy + ( gravity * dt)
	

	if player.y >= world.ground - player.h then
	   player.y = world.ground - player.h; player.vy = 0;
	   player.canJump = true
    end	
end

function love.keypressed(key, unicode)
	if key == "space" and player.canJump then
		player.vy = jumpv
		player.canJump = false
	end
end
User avatar
evgiz
Citizen
Posts: 83
Joined: Mon Aug 29, 2016 11:05 pm
Contact:

Re: slide effect

Post by evgiz »

Hey!

I can see that you're using player.vy for gravity, which creates smooth movement on the y-axis. You can do the same for horizontal movement, by storing a player.vx and adding that to the x position each frame instead. If the player releases the key you can lerp the vx to zero.

Pseudo code:

Code: Select all

if love.keyboard.isDown("left") then
	player.vx = -player.speed
elseif love.keyboard.isDown("right") then
	player.vx = player.speed
else
	--Lerp functions are easy to implement, there are tons of libraries for this too
	player.vx = lerp(player.vx, 0, delta)
end

player.x = player.x + player.vx * delta
Computer science student and part time game dev! Currently working on Depths of Limbo! :cool:

Check out the game website DepthsOfLimbo.com! :ultrahappy:
And my personal website with all my projects evgiz.net! :megagrin:
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: slide effect

Post by peterrust »

thewifitree,

If you want to go really advanced/realistic, you can use acceleration (as evgiz points out, you're doing this already with gravity on the y axis), combined with friction. The friction will essentially put a cap on the velocity while the horizontal acceleration is still on, and it will drop the velocity to zero over time if the horizontal acceleration drops to zero (the player stops running). If you do some searches for friction on the forum, I think some people were recently talking about this & maybe even provided some code.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests