Velocity in Y axis never gets 0

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
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Velocity in Y axis never gets 0

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Velocity in Y axis never gets 0

Post 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.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Velocity in Y axis never gets 0

Post by eliasaif »

Ah! Thanks!
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Velocity in Y axis never gets 0

Post 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?
Attachments
jumptest.love
(4.72 KiB) Downloaded 158 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Velocity in Y axis never gets 0

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

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