Jumping(Platform)[SOLVED!:)]

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
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Jumping(Platform)[SOLVED!:)]

Post by baconhawka7x »

So, I am using a timer to help me with player jumping...

Code: Select all

function love.keypressed(key)
		if key == "w" and
		grounded == true then
			jump_timer = jump_timer + dt
		end
end

Code: Select all

function love.update(dt)
if jump_timer > 0 then
		oliver.yvel = -5
	end
	if jump_timer > 0.2 then
		oliver.yvel = 5
	end
end
For some reason the player just keeps going up, so if someone could help me out I'd really appreciate it.

From,
MunkeeBacon

PS, Sorry for posting a lot.
PPS, I put "love.keypressed()" Inside of love.update().
Last edited by baconhawka7x on Mon Feb 13, 2012 4:31 pm, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Jumping(Platform)

Post by tentus »

baconhawka7x wrote: PPS, I put "love.keypressed()" Inside of love.update().
You shouldn't do that.

Without seeing more of your code, I can't help you much, but I would advise that you to use only an if statement where needed. Since your character is presumably given a y velocity when the jump is started, you only need one if statement to reverse it.

Code: Select all

function love.update(dt)
	if jump_timer > 0.2 and oliver.yvel < 0 then
		oliver.yvel = 5
	end
end
A better solution would probably be to have your character jump in a parabola. Set a y vel when he jumps, which decreases over time. It'll make the jump bell shaped instead of triangle-shaped. Here's an example of that:

Code: Select all

function love.load()
	ground = 500
	jumpspeed = 250
	gravity = 125
	p = {
		x = 400,
		y = 300,
		r = 15,
		v = jumpspeed
	}
end
function love.update(dt)
	if p.v ~= 0 then
		p.v = p.v + (dt * gravity)
		p.y = p.y + (p.v * dt)
	end
	if p.y > ground - p.r then
		p.y = ground - p.r
		p.v = 0
	end
end
function love.draw()
	love.graphics.circle("line", p.x, p.y, p.r)
	love.graphics.line(0, ground, love.graphics.getWidth(), ground)
end
function love.keypressed(key)
	if key == "up" and p.v == 0 then
		p.v = -jumpspeed
	end
end
Kurosuke needs beta testers
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

Re: Jumping(Platform)

Post by utunnels »

If I'm making the game, I'll make at least 5 related properties.
oliver.xpos, oliver.ypos, oliver.xvel, oliver.yvel, oliver.ybase

xpos and ypos are your current coordinates, xvel and yvel are your velocities. ybase is tricky, it is your base altitude. If you have some platforms, and your character is above one of them, then the base altitude is the height of the platform instead of 0. Although you need to update this base value in love.update.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Jumping(Platform)

Post by tentus »

Utunnels, are you thinking he's making a game like Sonic 3D blast? Because I just assumed he was making a sidescrolling platformer.

If he is going for an isometric platformer, then I would use not use a base variable but rather a z position variable, since that's what it really is. Why give an axis it's own unique units and whatnot?
Kurosuke needs beta testers
utunnels
Citizen
Posts: 75
Joined: Fri Jan 06, 2012 5:20 pm

Re: Jumping(Platform)

Post by utunnels »

If he is going for an isometric platformer, then I would use not use a base variable but rather a z position variable...
Yeah, but I doubt it. Or it is too early too assume now.

Back to topic:

For example, when the character jumps forward to a platform which is higher than the ground. When his x-axis overlaps the platform, his base altitude changes to the height of the platform.

So in love.update, you check like this:

Code: Select all

.....

-- check base
local max_height = 0
for _,p in pairs(platforms) do
    if p.ypos<oliver.ypos and oliver.xpos>p.xpos and oliver.xpos<p.xpos+p.width and p.ypos>max_height then
        max_height = p.ypos
    end
end

oliver.ybase = max_height

if oliver.ypos>oliver.base  then  --gravity 
    oliver.yvel = oliver.yvel + g
end

oliver.ypos = oliver.ypos + oliver.yvel
oliver.xpos = oliver.xpos + oliver.xvel

if oliver.ypos<=oliver.base then -- land
    oliver.ypos = oliver.base
    oliver.yvel = 0
end
I know you can make oliver.ybase local ybase in this example. But I prefer to make it a property just in case I use it elsewhere, for example, another function.

--------

Edit*

And the character probably needs a height property, if you want to do the bump into ceiling logic. But that'll be another story.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Jumping(Platform)

Post by baconhawka7x »

tentus wrote:
baconhawka7x wrote: PPS, I put "love.keypressed()" Inside of love.update().
You shouldn't do that.

Without seeing more of your code, I can't help you much, but I would advise that you to use only an if statement where needed. Since your character is presumably given a y velocity when the jump is started, you only need one if statement to reverse it.

Code: Select all

function love.update(dt)
	if jump_timer > 0.2 and oliver.yvel < 0 then
		oliver.yvel = 5
	end
end
A better solution would probably be to have your character jump in a parabola. Set a y vel when he jumps, which decreases over time. It'll make the jump bell shaped instead of triangle-shaped. Here's an example of that:

Code: Select all

function love.load()
	ground = 500
	jumpspeed = 250
	gravity = 125
	p = {
		x = 400,
		y = 300,
		r = 15,
		v = jumpspeed
	}
end
function love.update(dt)
	if p.v ~= 0 then
		p.v = p.v + (dt * gravity)
		p.y = p.y + (p.v * dt)
	end
	if p.y > ground - p.r then
		p.y = ground - p.r
		p.v = 0
	end
end
function love.draw()
	love.graphics.circle("line", p.x, p.y, p.r)
	love.graphics.line(0, ground, love.graphics.getWidth(), ground)
end
function love.keypressed(key)
	if key == "up" and p.v == 0 then
		p.v = -jumpspeed
	end
end
Thank You So Much! It works now!:D
Post Reply

Who is online

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