Page 1 of 1

Velocity in Y axis never gets 0

Posted: Tue Jun 02, 2009 5:46 am
by eliasaif
I'm making a platform game where I limit the jumping. I set a variable to 1 if the jumping button is pressed and then if the velocity in Y axis is 0, the variable is set to 0. The problem is that the velocity never gets exactly 0. When I draw the Y axis velocity value of the player, it shows some different values changing really fast. Is there anyone who knows why it is like this? Is there anything to do about it? The player is a circle shape and the ground is a rectangle.

Re: Velocity in Y axis never gets 0

Posted: Tue Jun 02, 2009 6:08 am
by bartbes
That's because you're working with dt (the physics engine is, anyway), so you're going to have to round the numbers yourself, try something like:

Code: Select all

if math.abs(yvel) <= 0.1 then
This gives you 0.1 positive and 0.1 negative.

Re: Velocity in Y axis never gets 0

Posted: Tue Jun 02, 2009 6:45 am
by eliasaif
Ah! Thanks!

Re: Velocity in Y axis never gets 0

Posted: Wed Jun 17, 2009 7:58 am
by eliasaif
I thought that the code in the earlier post solved my problem. But it didn't. It works fine as long as you just press the jump button once. But if you press the jump button one more time when the jumping object is in the "air", the object reaches the ground as it should, but when you try to jump again, it doesn't. So, I want the object to be able to jump instantly after reaching the ground. Does anyone have a solution for this?

Re: Velocity in Y axis never gets 0

Posted: Wed Jun 17, 2009 11:26 am
by Robin
Let's review the important part of the code:

Code: Select all

jumppress = false

function keypressed(k)

    vx, vy = rect:getVelocity()
    
    if k == love.key_space then
        if jumppress == false then
            rect:applyImpulse(0, 2)
            jumppress = true
        end
        if math.abs(vy) <= 0.1 then
            jumppress = false
        end
    end
    (snip)
end
Now, what does that do? If you press Space once, you apply some impulse, and since math.abs(vy) <= 0.1 at that point, jumppress is set to false, so the next time you press space, you doublejump. However, if you doublejump, the second time you press space math.abs(vy) is no longer <= 0.1, so jumppress is still true. It will only be set to false if you press space and the velocity is small enough. Placing the jumppress=false somewhere else (i.e. in update()) should probably fix it.

EDIT: placing the jumppress=false code in update() worked.