Page 1 of 1

Jumping

Posted: Sun Sep 16, 2012 11:02 pm
by epichat
Hello!
I'm new to this online community, although I've been using this framework for a little while. I'm trying to make a platformer but I can't seem to be able to get jumping right (it's actually very sad). I already tried a tutorial on the wiki, but it was "incomplete" and it didn't work. I tried

Code: Select all

for i = 1, 20 do
playerY = playerY - 0.3
end
and then some very similar code for coming down. In retrospect it doesn't seem like the best way of doing things, but I had already tried love.keypressed(key) and love.keyboard.isDown(" ") but nothing seemed to work. Sadly, I deleted that part of the code so I can't put it up here.

Basically I tried making a square "jump" but nothing I tried work. How would you guys do it?

Re: Jumping

Posted: Mon Sep 17, 2012 3:21 am
by verilog
Hello man, and welcome!

Check out this post for detailed information:

viewtopic.php?f=4&t=8831&hilit=jump#p54562

Some tips you could find useful:

Depending on what you would like to accomplish, you typically do not want to modify the player's Y position inside a loop, and then execute that loop in more than one frame. The reason behind this is that you want, instead, to modify the player's vertical position throughout a number of fixed frames, loop-free, since each frame is actually modifying the previous vertical position - that is, the player increases (or decreases) his position on each frame.

To achieve a behavior like this, you must include the dt (delta time) variable in your calculations. dt is a variable that quantifies the time elapsed between drawing and computing frames.

For instance:

Code: Select all

playerY = playerY + playerSpeed*dt
If placed inside an update block, executes every frame, increasing the variable “playerY” by playerSpeed * dt times. This increment depends on dt and a fixed “speed” variable, if you try initially a playerSpeed of 100, then, playerY will be increased by 100 every frame.

Some things to consider:

dt can be a tricky concept to understand, what you must be aware of is that it is the temporal variable that you must include in computations that somewhat must “know” the time elapsed between one (frame) event and another.

dt is defined as the inverse of the frame rate, and varies depending on the hardware where the code is being executed. This could lead to nasty bugs, where your player moves faster or slower, depending on the host computer. To avoid this, set a constant frame rate (eg. 60) inside your program.

Hope it helps! :awesome:

Re: Jumping

Posted: Mon Sep 17, 2012 8:18 am
by Roland_Yonaba
Hi,
Just to link to this tutorial series about making platforming games with Love2d, which also covers how-to make a jump.