Page 1 of 1

Decrease velocity

Posted: Tue Oct 08, 2019 7:25 pm
by TheSoftWaffle
Is there a built-in thing that gradually slows down the object, its linear velocity?

Re: Decrease velocity

Posted: Tue Oct 08, 2019 9:58 pm
by akopyl
Usually we call it friction.
Here's a simple way to do it:

Code: Select all

player.xVel = player.xVel * (1 - math.min(player.friction*dt, 1))

Re: Decrease velocity

Posted: Wed Oct 09, 2019 4:54 pm
by TheSoftWaffle
Do bodies have xVel?

i tried to do

Code: Select all

ball.fixture:setFriction(10)
but it didn't slow the ball down.

Re: Decrease velocity

Posted: Wed Oct 09, 2019 6:20 pm
by pgimeno
See Body:setLinearDamping.

Friction applies to contact between two bodies.

Re: Decrease velocity

Posted: Thu Oct 10, 2019 5:24 pm
by TheSoftWaffle
Thanks, will check out
It does that with the y coords too.
Can i do that with only x?
Btw i am new to lua, how can i only get the y value from getLinearVelocity()?

Re: Decrease velocity

Posted: Fri Oct 11, 2019 9:42 am
by pgimeno
TheSoftWaffle wrote: Thu Oct 10, 2019 5:24 pm Can i do that with only x?
Not with any existing function. You need to implement damping yourself, using e.g. applyForce proportional to current x velocity.
TheSoftWaffle wrote: Thu Oct 10, 2019 5:24 pm Btw i am new to lua, how can i only get the y value from getLinearVelocity()?
If you absolutely must, you can use select(2, yourbody:getLinearVelocity()).