Discuss Scratch

MrMcCoder
Scratcher
100+ posts

How would I code "gravity" in Scratch?

Hey, folks!

I've been playing this game on Roblox recently, called “Decent Planet Game.” It's really fun, and it is inspiring me to make my own spin on it in Scratch, especially after also having played Thebenjiball's game MOONWALK ( https://scratch.mit.edu/projects/775596408/ ). However, I'm kind of at a loss with how to simulate gravity in these types of games.

I know how to make gravity, period, like in platformers and stuff, but how would I program a game that would involve gravities of different planets and moons? Thanks! ^^

Last edited by MrMcCoder (March 26, 2025 03:10:22)

NMario84
Scratcher
1000+ posts

How would I code "gravity" in Scratch?

You do what you would do normal gravity in platformers, but reverse the situation.

Normal Gravity:
forever
change [y gravity v] by (-1)
change y by (y gravity)
end

Reverse Gravity:
forever
change [y gravity v] by (1)
change y by (y gravity)
end

Forward Gravity:
forever
change [x gravity v] by (1)
change x by (x gravity)
end

Backward Gravity:
forever
change [x gravity v] by (-1)
change x by (x gravity)
end
Konserverad_gran
Scratcher
78 posts

How would I code "gravity" in Scratch?

the newtonian gravity equation is a = G * m / (r * r) so lets pry that apart

- a is the acceleration, the value you want to find out and change the velocity by
- G is just a constant number, so you can try a few different ones and pick whichever gives the best feel in the game
- m is the mass of the planet
- r is the distance between the player and the center of the planet

if you dont have access to r directly, you can swap out (r * r) for ( (player x - planet x)*(player x - planet x) + (player y - planet y)*(player y - planet y) )

now this only gives the one-dimensional magnitude of the acceleration rather than the vector, so to implement it in 2d we will need to make a few adjustments. if we're using the scratch system of direction pointing with 0 degrees at the top, the acceleration components should be:
x-wise acceleration = a * sin(direction to planet)
y-wise acceleration = a * cos(direction to planet)
or if you dont have the direction at hand:
x-wise acceleration = a * (planet x - player x) / r
y-wise acceleration = a * (planet y - player y) / r

now all you need to do is change x by the x-wise acceleration and y by the y-wise acceleration

and repeat the process for every planet.

lets hope i didnt make a mistake but that should become apparent upon experiment i suppose
MrMcCoder
Scratcher
100+ posts

How would I code "gravity" in Scratch?

Konserverad_gran wrote:

the newtonian gravity equation is a = G * m / (r * r) so lets pry that apart

- a is the acceleration, the value you want to find out and change the velocity by
- G is just a constant number, so you can try a few different ones and pick whichever gives the best feel in the game
- m is the mass of the planet
- r is the distance between the player and the center of the planet

if you dont have access to r directly, you can swap out (r * r) for ( (player x - planet x)*(player x - planet x) + (player y - planet y)*(player y - planet y) )

now this only gives the one-dimensional magnitude of the acceleration rather than the vector, so to implement it in 2d we will need to make a few adjustments. if we're using the scratch system of direction pointing with 0 degrees at the top, the acceleration components should be:
x-wise acceleration = a * sin(direction to planet)
y-wise acceleration = a * cos(direction to planet)
or if you dont have the direction at hand:
x-wise acceleration = a * (planet x - player x) / r
y-wise acceleration = a * (planet y - player y) / r

now all you need to do is change x by the x-wise acceleration and y by the y-wise acceleration

and repeat the process for every planet.

lets hope i didnt make a mistake but that should become apparent upon experiment i suppose
Thanks! For some reason I didn't get notifications about this, lol, I'll try it when I can! ^^

Gee, I never thought I'd have to bring Newtonian physics into things

Powered by DjangoBB