Page 1 of 1

Bug in love.physics, or bad coding ???

Posted: Tue Apr 12, 2016 12:44 pm
by apatbol
Hello all,

I am learning Löve & Lua (working with love 10.1), and I have a strange behavior in one of my tests...

I created a ball (associated with an image), a ground and a floor (width: 1/3 of the window, in the middle of the window).
When pressing the space bar, the ball has a vertical kick and goes up. When pressing left or right arrows, the ball goes in the direction of the arrow.
(see attached .love file for the code)

Now, the bug I suspect:
When I start the game, I press one or 2 times (or 3 !) to bring the ball on the floor. Once on the floor, I let the ball few seconds without any move (to "stabilize"). Then I move horizontally with the arrows.
***** the bug ****
sometimes (often..), the ball has no gravity anymore. It stays at the height of the floor, and does not fall on the ground.
when giving a new kick, the ball (if not above the floor) has a "normal" behavior (falling on the ground)
***** bug correction **** (but ugly solution...)
when moving horizontally, I add an impulse to the bottom (with very low impulse: 0.01)
(you can test it by enabling the "correction mode" with up arrow (sorry, I haven't added a debounce mode..)
The strange thing is that if I activate the "correction mode" once, then I haven't the bug anymore...

Now my question:
Did I made a mistake in coding, or is it a bug from love.physics (or is there a ghost in my computer...) ?????

Thanks for your answers

Apatbol

Re: Bug in love.physics, or bad coding ???

Posted: Tue Apr 12, 2016 2:01 pm
by Tanner
Your ball body is going to sleep. You can either wake it up by applying some force to it (like you're doing with the impulse) or you can tell it never to go to sleep.

https://love2d.org/wiki/Body:setSleepingAllowed

Notice the warning on that page: "A sleeping body is not simulated unless it collided with an awake body. Be wary that one can end up with a situation like a floating sleeping body if the floor was removed."

You using `setX` to move your physics body is sort of like if the floor was removed! The body is still asleep because there haven't been any new forces applied to it but now the floor isn't under it anymore.

Re: Bug in love.physics, or bad coding ???

Posted: Tue Apr 12, 2016 2:21 pm
by kikito
When you think you have found a bug, it is always useful to remember the first rule of programming.

Re: Bug in love.physics, or bad coding ???

Posted: Tue Apr 12, 2016 7:36 pm
by apatbol
Dear both,

Thanks a lot for the answers !
I apologize, I will not code lazy balls that are sleeping whenever possible, neither I will accuse Löve for my mistakes...
(although I was more expecting a mistake from my side, much more than a bug in Löve..).

Additionnal question as a newbie in game programming: my trick for horizontal move is correct ? or ugly ? (from a "real programmer"'s perspective ?)

Regards

Apatbol

Re: Bug in love.physics, or bad coding ???

Posted: Wed Apr 13, 2016 12:22 am
by Tanner
Personally, I prefer using `Body:applyForce` for moving physics objects like yours. It gives the bodies a nice acceleration/deceleration.

Try it out and if you like it, use it! There's no real right way and I've seen experienced people use both!

Re: Bug in love.physics, or bad coding ???

Posted: Wed Apr 13, 2016 4:00 am
by apatbol
Hello !

Once again, thanks for the answer.
I'll try it !

Regards

Apatbol