Page 1 of 1

Gravity

Posted: Mon Oct 23, 2023 1:38 pm
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 ?

Re: Gravity

Posted: Mon Oct 23, 2023 3:13 pm
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.

Re: Gravity

Posted: Tue Oct 24, 2023 1:03 am
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.

Re: Gravity

Posted: Tue Oct 24, 2023 8:39 am
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

Re: Gravity

Posted: Tue Oct 24, 2023 11:23 am
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 :)

Re: Gravity

Posted: Tue Oct 24, 2023 8:49 pm
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.

Re: Gravity

Posted: Sat Oct 28, 2023 11:33 am
by Pinko
:crazy: Yeah, i a waiting for the first game, with negative gravity. :megagrin: