Discuss Scratch

lizardvenom64
Scratcher
5 posts

How to create gravity

How can I create realistic gravity in scratch? Like, an object falls if it's in the air, and it lands on the ground, and it falls faster over time in the air. Also maybe have the ability to roll down slopes (idk if thats even possible but if someone knows a way please tell me)

Edit: thanks for the help everyone. I have figured out how to do it now

Last edited by lizardvenom64 (March 1, 2021 21:18:36)

awesome-llama
Scratcher
1000+ posts

How to create gravity

Look at gravity first.

Gravity is a constant acceleration downwards. That means, your velocity when falling will continuously increase downwards (just ignore air resistance).

We can simulate gravity like this.

change [y velocity v] by (-0.5) // in this example, gravity is -0.5, remember it is downwards so it is negative

Finally, since you have velocity, you need to convert it in to positions that the sprites can use. Since velocity is a change in position, we can change the position like this:

change y by (y velocity)

Since these need to run continuously, put them in a loop.

forever
change [y velocity v] by (-0.5)
change y by (y velocity)
end

Make sure to reset the y velocity when the project is started too. You don't want to start and suddenly fall through the ground really fast.
Tiger_Person
Scratcher
100+ posts

How to create gravity

I have project that can help you further https://scratch.mit.edu/projects/485079594/
A_riordan
Scratcher
32 posts

How to create gravity

when green flag clicked
set [ Yvel] to [0]
forever
change [ Yvel] by (-1)
change y by ( Yvel)
Detect ground
end

define Detect ground
repeat until <not <touching [ ground sprite] ?>>
change y by (1)
set [ Yvel] to [0]
end
[scratchblocks]
Just make sure the "Detect ground" block is "run without screen refresh".

Last edited by A_riordan (March 1, 2021 14:50:14)

gdfsgdfsgdfg
Scratcher
1000+ posts

How to create gravity

BeachSun10 wrote:

when this sprite clicked
when this sprite clicked
when backdrop switches to [ v]
when [ v] key pressed
when backdrop switches to [ v]
when [ v] > (10)


don't necropost

Powered by DjangoBB