Page 1 of 1

Mario style platformer physics

Posted: Sat Feb 20, 2016 5:10 pm
by cfincher29
Hi! I'm new to LOVE and programming in general. I was able to make some basic character movements for a platformer. Jumping and X axis movement. Now I'm trying to make the character movement smoother. I've looked through YouTube and I've dug through the forums here but I still can't find an answer. I still have much to learn but I was hoping that some of you could help me figure out why I can't get my character to move smoothly. Once, I had friction working and smooth X axis movement, but my jump was disabled. Here's my code (It's sloppy I know).

Code: Select all

love.graphics.setDefaultFilter('nearest', 'nearest')


function love.load()
	face_right = true
	love.window.setMode(1280, 720, {resizable=true, vsync=false, minwidth=1280, minheight=720})
	bckgrnd = love.graphics.newImage('mario_background.png')
	
	player_image = {}
	player_image[1] = love.graphics.newImage('mariostand.png')
	player_image[2] = love.graphics.newImage('mariojump.png')
	player_image[3] = love.graphics.newImage('mariostandL.png')
	player_image[4] = love.graphics.newImage('mariojumpL.png')

	--PLAYER
		player = {}
		player.x = 20
		player.y = 480
		player.speed = 0.4
		player.yvel = 0
		player.xvel = 0
		player.float = 0.5
		player.float_max = 0.5
		player.friction = 1
		
	gravity = 800
	jump_height = 500
	winH, winW = love.graphics.getHeight(), love.graphics.getWidth()
	
	enemy = {}
	enemy.image = love.graphics.newImage('enemy.png')
	enemy.x = 1150
	enemy.y = 480
	enemy.speed = 0.2
	
end

function love.draw()
	love.graphics.draw(bckgrnd, 0, 0, 0, 10)
	if (player.yvel ~= 0 and face_right == true) then
		love.graphics.draw(player_image[2], player.x, player.y, 0, 5)
	elseif (player.yvel == 0 and face_right == true) then
		love.graphics.draw(player_image[1], player.x, player.y, 0, 5)
	elseif (player.yvel ~= 0 and face_right == false) then
		love.graphics.draw(player_image[4], player.x, player.y, 0, 5)
	elseif (player.yvel == 0 and face_right == false) then
		love.graphics.draw(player_image[3], player.x, player.y, 0, 5)
	end
	love.graphics.draw(enemy.image, enemy.x, enemy.y, 0 ,5)
end

function love.update(dt)
	
	--player movement
	if love.keyboard.isDown("right") then
		face_right = true			
		player.x = player.x + player.speed			
		if player.x >= 1200 then
			player.x = 1200
		end
	end
	
	if love.keyboard.isDown("left") then
		face_right = false
		player.x = player.x - player.speed
		if player.x <= 0 then
			player.x = 0
		end
	end	

	if love.keyboard.isDown("r") then
		player.speed = 0.8
	else
		player.speed = 0.4
	end

	if player.yvel ~= 0 then -- in air
		player.y = player.y - player.yvel * dt
		player.yvel = player.yvel - gravity * dt
	end
	
	if player.y > 480 then -- on ground
		player.yvel = 0
		player.y = 480
	end
end	


function love.keypressed(key, scancode, isrepeat) -- jump
  	if key == "space" then
    	if player.yvel == 0 then
     	   player.yvel = jump_height
    	end
  	end
end
Thanks in advance! I'm sorry if this has been covered somewhere before. I searched through a lot of threads before posting this.

Re: Mario style platformer physics

Posted: Sat Feb 20, 2016 6:05 pm
by Davidobot
It would help if you posted a .love file.
Also, right off the bat, I see you're not applying dt to your X movement, try that:

Code: Select all

 player.x = player.x + player.speed *dt

Re: Mario style platformer physics

Posted: Sat Feb 20, 2016 7:54 pm
by cfincher29
Davidobot wrote:It would help if you posted a .love file.
Also, right off the bat, I see you're not applying dt to your X movement, try that:

Code: Select all

 player.x = player.x + player.speed *dt
I could've sworn I uploaded the file at the bottom of the post. Sorry if this is spammy, I've never actually used forums other than browsing through them from search engines.

Re: Mario style platformer physics

Posted: Sat Feb 20, 2016 10:54 pm
by monolifed
Have you seen Mari0? It is open source (though the non-commercial-use-only term makes it a little less useful)

Re: Mario style platformer physics

Posted: Sun Feb 21, 2016 12:52 am
by pgimeno
Your speed is too low.

Re: Mario style platformer physics

Posted: Sun Feb 21, 2016 1:09 am
by cfincher29
ingsoc451 wrote:Have you seen Mari0? It is open source (though the non-commercial-use-only term makes it a little less useful)
I actually heard about that but I haven't ever tried it. I need too though. Mario and Portal are some of my favorite games.
pgimeno wrote:Your speed is too low
Thank you! I wouldn't have thought to change that.