Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » HELP- Gravity
- Hololly
-
Scratcher
9 posts
HELP- Gravity
So basically, I don't know what to make on Scratch next but I REALLY want to know how to make gravity. But I don't just want to make it, I want to understand it, most of all. I got a script for velocity which I actually do understand but my family isn't really into coding so I have nobody to tell me. I feel like when I get this code and understand it it will be like the key to SO many things and I could make SO MUCH!! So I really need help! Thank you so much if you try and please could you explain it in a simpler way- if possible! Thank you so much!
- awesome-llama
-
Scratcher
1000+ posts
HELP- Gravity
Firstly, a change in distance/displacement over a period of time is known as velocity. A change in velocity over a period of time is known as acceleration.
The three concepts are related in that way.
Gravity applies a constant force on objects which results in an acceleration of 9.8 metres per second, per second. So every second, the velocity increases downwards by 9.8 metres per second. In Scrach, though, you can make up your own values.
So, with a constant acceleration, the velocity always changes by a specific amount. You will see often that motion is done with x and y positions and x and y velocity. Acceleration as a variable is not usually used as it will always be known as a constant number.
Also, you need to remember the direction of gravity. For most purposes, it will be only part of the vertical direction, so it only affects the y components of the position, velocity and acceleration.
In Scratch, gravity can be done in the following basic way:
The three concepts are related in that way.
Gravity applies a constant force on objects which results in an acceleration of 9.8 metres per second, per second. So every second, the velocity increases downwards by 9.8 metres per second. In Scrach, though, you can make up your own values.
So, with a constant acceleration, the velocity always changes by a specific amount. You will see often that motion is done with x and y positions and x and y velocity. Acceleration as a variable is not usually used as it will always be known as a constant number.
Also, you need to remember the direction of gravity. For most purposes, it will be only part of the vertical direction, so it only affects the y components of the position, velocity and acceleration.
In Scratch, gravity can be done in the following basic way:
forever
change [y velocity v] by (-9.8) // Downwards is negative. Note that the input is the acceleration, which is constant.
change [y position v] by (y velocity) // Could be replaced with a motion block (change y by _).
end
- MathPuppy314
-
Scratcher
500+ posts
HELP- Gravity
The code example above is a little messed up.
The -9.8 would only work if you want each pixel to represent 1/294 metres.(about 3mm) It would be much more realistic to use a value between -0.5 and -1 to get more realistic results for most instances.
The -9.8 would only work if you want each pixel to represent 1/294 metres.(about 3mm) It would be much more realistic to use a value between -0.5 and -1 to get more realistic results for most instances.
Last edited by MathPuppy314 (Nov. 7, 2020 16:58:09)
- awesome-llama
-
Scratcher
1000+ posts
HELP- Gravity
The code example above is a little messed up.I wouldn't describe the alternative as “realistic”, but more “functional” for use in Scratch projects at a basic level.
The -9.8 would only work if you want each pixel to represent 1/294 meters. It would be much more realistic to use a value between -0.5 and -1 to get more realistic results for most instances.
Why I used the 9.8 example was because I tend to use the real values for making projects, which are a bunch of physics related things. I do the actual conversion to pixels right at the end (in the motion blocks) as this makes it much easier to understand what's going on in the maths. Knowing speed in metres per second makes more sense, so I would know what is fast or not instantly.
To relate to the original question, the values can go any way. What the variables store doesn't really matter, if you know what they are and you are scaling them correctly by at the end for what is being displayed.
- Hololly
-
Scratcher
9 posts
HELP- Gravity
Ok thank you so so much for helping! You don't know how much this has helped me! Gravity is like the key to all of the projects I want to make 

- TMaster706
-
Scratcher
15 posts
HELP- Gravity
One way you could do it is with this script:
But may not be so accurate but really close!
The change y velocity block that says -2.5 you may change since its the gravity acceleration per millisecond
But may not be so accurate but really close!
when green flag clicked
forever
if <touching [Level v] ?> then
set [Y Velocity v] to [0]
else
change [Y Velocity v] by (-2.5)
end
change y by (Y Velocity)
end
The change y velocity block that says -2.5 you may change since its the gravity acceleration per millisecond

Last edited by TMaster706 (Jan. 10, 2024 12:56:09)
- Progameergast
-
Scratcher
7 posts
HELP- Gravity
Just do this in the player sprite:
when green flag clicked
set [ Speed Y] to [0]
forever
if <touching [ level] ?> then
set [ Speed Y] to [0]
else
change [ Speed Y] by (How much gravity you want)
change y by (Speed Y)
end
end
Last edited by Progameergast (Jan. 10, 2024 15:33:00)
- MJ-Fights
-
Scratcher
100+ posts
HELP- Gravity
The idea is that when you jump your y increases by gravity and gravity increases through a variable, then when you fall the variable decreases
- deck26
-
Scratcher
1000+ posts
HELP- Gravity
The idea is that when you jump your y increases by gravity and gravity increases through a variable, then when you fall the variable decreasesVelocity decreases as you rise and increases as you fall but the value for gravity shouldn't need to change.
- Discussion Forums
- » Help with Scripts
-
» HELP- Gravity