Velocity in Y axis never gets 0
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Velocity in Y axis never gets 0
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.
- 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
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:
This gives you 0.1 positive and 0.1 negative.
Code: Select all
if math.abs(yvel) <= 0.1 then
Re: Velocity in Y axis never gets 0
Ah! Thanks!
Re: Velocity in Y axis never gets 0
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 157 times
- 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
Let's review the important part of the code:
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.
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
EDIT: placing the jumppress=false code in update() worked.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Google [Bot] and 13 guests