Gravity

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
chocolat_en_poudre
Prole
Posts: 2
Joined: Mon Oct 23, 2023 1:33 pm

Gravity

Post by chocolat_en_poudre »

Hi ! I just started learning love2d and im making a smash bros like game with bump.lua but it hasn't gravity fonctionality.
Can someone help me how to implement it please ?
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Gravity

Post by darkfrei »

chocolat_en_poudre wrote: Mon Oct 23, 2023 1:38 pm Hi ! I just started learning love2d and im making a smash bros like game with bump.lua but it hasn't gravity fonctionality.
Can someone help me how to implement it please ?
Just add the vertical acceleration multiple dt to you vertical velocity and than vertical velocity multiple dt to your vertical position.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
tourgen
Citizen
Posts: 53
Joined: Sat Mar 18, 2023 12:45 am

Re: Gravity

Post by tourgen »

darkfrei wrote: Mon Oct 23, 2023 3:13 pm
chocolat_en_poudre wrote: Mon Oct 23, 2023 1:38 pm Hi ! I just started learning love2d and im making a smash bros like game with bump.lua but it hasn't gravity fonctionality.
Can someone help me how to implement it please ?
Just add the vertical acceleration multiple dt to you vertical velocity and than vertical velocity multiple dt to your vertical position.

F = m * a. m = mass. a = -9.8m/s^2 at ~ sea level (gravity). -> a = F/m. Given mass of your player, F is solved for.

velocity is the integral of acceleration. If you are not familiar with calculus, calculate velocity incrementally each timestep (using dt) i.e. v = dt * a, where a (accel due to gravity) is negative (see above, -9.8). the negative denotes down. positive is up. The second integral of accel is position, FYI.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Gravity

Post by darkfrei »

tourgen wrote: Tue Oct 24, 2023 1:03 am F = m * a. m = mass. a = -9.8m/s^2 at ~ sea level (gravity). -> a = F/m. Given mass of your player, F is solved for.

velocity is the integral of acceleration. If you are not familiar with calculus, calculate velocity incrementally each timestep (using dt) i.e. v = dt * a, where a (accel due to gravity) is negative (see above, -9.8). the negative denotes down. positive is up. The second integral of accel is position, FYI.
Almost right :)
But the gravity is an acceleration without a = F/m, this acceleration is absolute same for heavy and light masses, you can skip here any mass using. It's enough:

Code: Select all

vy = vy + dt*g -- positive g to fall down (positive Y direction)
y = y + dt*vy
Of if you want to be more precise:

Code: Select all

-- see https://love2d.org/forums/viewtopic.php?p=256873#p256873
y = y + dt*(vy + dt*g/2) -- changing position to half changed velocity
vy = vy + dt*g -- full changed velocity
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
chocolat_en_poudre
Prole
Posts: 2
Joined: Mon Oct 23, 2023 1:33 pm

Re: Gravity

Post by chocolat_en_poudre »

darkfrei wrote: Tue Oct 24, 2023 8:39 am
tourgen wrote: Tue Oct 24, 2023 1:03 am F = m * a. m = mass. a = -9.8m/s^2 at ~ sea level (gravity). -> a = F/m. Given mass of your player, F is solved for.

velocity is the integral of acceleration. If you are not familiar with calculus, calculate velocity incrementally each timestep (using dt) i.e. v = dt * a, where a (accel due to gravity) is negative (see above, -9.8). the negative denotes down. positive is up. The second integral of accel is position, FYI.
Almost right :)
But the gravity is an acceleration without a = F/m, this acceleration is absolute same for heavy and light masses, you can skip here any mass using. It's enough:

Code: Select all

vy = vy + dt*g -- positive g to fall down (positive Y direction)
y = y + dt*vy
Of if you want to be more precise:

Code: Select all

-- see https://love2d.org/forums/viewtopic.php?p=256873#p256873
y = y + dt*(vy + dt*g/2) -- changing position to half changed velocity
vy = vy + dt*g -- full changed velocity
thanks :)
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Gravity

Post by pgimeno »

tourgen has a point IF you are going to need more forces to be applied to the object. You first calculate the force due to gravity times mass (i.e. the weight), then add it to all other forces acting on the object, and then obtain the final acceleration of the object by dividing the resulting force by the object's mass.

In many platformers, you can do away without any of these as darkfrei points out.

But watch out: acceleration is a vector. It has a magnitude of 9.8 m/s² and a direction pointing down. Problem is, in Löve, "down" is positive y, therefore the sign of the y coordinate of the acceleration needs to be positive too. Either that, or you build a system with coordinate transforms and everything just to change the vertical sign convention, which sounds a bit overkill.
User avatar
Pinko
Prole
Posts: 46
Joined: Sat Jul 29, 2023 3:22 pm
Contact:

Re: Gravity

Post by Pinko »

:crazy: Yeah, i a waiting for the first game, with negative gravity. :megagrin:
Author of the vape knigge. I have thought of you! is much better than "i have waited for you". :cool:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests