Decrease velocity

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
TheSoftWaffle
Prole
Posts: 3
Joined: Mon Oct 07, 2019 4:17 pm

Decrease velocity

Post by TheSoftWaffle » Tue Oct 08, 2019 7:25 pm

Is there a built-in thing that gradually slows down the object, its linear velocity?

User avatar
akopyl
Prole
Posts: 21
Joined: Fri Jun 27, 2014 1:20 pm

Re: Decrease velocity

Post by akopyl » Tue Oct 08, 2019 9:58 pm

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))

TheSoftWaffle
Prole
Posts: 3
Joined: Mon Oct 07, 2019 4:17 pm

Re: Decrease velocity

Post by TheSoftWaffle » Wed Oct 09, 2019 4:54 pm

Do bodies have xVel?

i tried to do

Code: Select all

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

User avatar
pgimeno
Party member
Posts: 1818
Joined: Sun Oct 18, 2015 2:58 pm

Re: Decrease velocity

Post by pgimeno » Wed Oct 09, 2019 6:20 pm

See Body:setLinearDamping.

Friction applies to contact between two bodies.

TheSoftWaffle
Prole
Posts: 3
Joined: Mon Oct 07, 2019 4:17 pm

Re: Decrease velocity

Post by TheSoftWaffle » Thu Oct 10, 2019 5:24 pm

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()?

User avatar
pgimeno
Party member
Posts: 1818
Joined: Sun Oct 18, 2015 2:58 pm

Re: Decrease velocity

Post by pgimeno » Fri Oct 11, 2019 9:42 am

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()).

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests