This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-04 07:10:10

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

How to simulate gravity (tutorial)

I've seen a lot of topics recently asking how to add the effect of gravity in their game. I thought "This is really cluttering up the forums". So from now on, you can just link to this topic whenever someone asks that question!  wink
_______________________________________________________________________________

First, you need to create a variable. To do this, click on the orange tab called 'Variables' in the bottom right corner of the tabs.
http://i41.tinypic.com/wmgdxy.png

Then, click on the 'Make a variable' button, and make a variable called 'Y velocity'. Make sure you check the box saying 'For this sprite only', and not 'For all sprites'.
http://i44.tinypic.com/fc90nt.png

Then, on the sprite that you want to jump, put this script:
http://i42.tinypic.com/35ic3es.png

You can change the 15 in this to another number, depending on how high you wan it to jump:
http://i41.tinypic.com/2mfosk.png
(higher number=higher jump, lower number=lower jump)

What's happening?
If the sprite that's jumping is touching the colour of the ground, then one of two things will happen; if the up arrow key is being pressed (or another key of your choice), it will set the variable 'Y velocity' to 15; this is what makes it jump. If the key isn't being pressed, it will set the variable 'Y velocity' to 1; this is so that the sprite will always rise to the top of the surface, instead of partly being underground. If it's not touching the colour of the ground, it will change the variable 'Y velocity' by -1 (decreasing it). This is what gives the effect of gravity. Finally, the sprite changes it's y position by the variable 'Y velocity'.
_______________________________________________________________________________

Alternatively, if you want the sprite to bounce, you can change the previous script to one like this:
http://i39.tinypic.com/157c3lc.png

What's happening?
This script is almost the same, apart from one part of it. That one difference in the script makes a big difference in the project though. The difference is this:
http://i40.tinypic.com/1okio6.png
Instead of this:
http://i42.tinypic.com/8x2ao0.png

If you don't know what 'abs' means, it's short for 'absolute value', and it basically turns a negative number into it's positive equivalent, but if the number's positive it will stay the same. So abs of -5 = 5, and abs of 5 = 5. Another way to remember the meaning is that it's the distance from zero.
Anyway, back to the tutorial. When the sprite is touching the ground colour, it will set the variable 'Y velocity' to 'abs of Y velocty * 0.8'. You can change the 0.8 to whatever number you want; changing it so that it's higher means that it will take longer to settle on the ground, while decreasing the number will mean it takes less time to settle. Play around with it and see if you can find the perfect amount for you!

And that's it! If you use this method, I don't mind if you don't give me credit in your game, but I'd appreciate it if you do!  smile
Also, tell me if there's any mistakes I need to correct!  big_smile

Last edited by RedRocker227 (2011-12-06 10:41:16)


Why

Offline

 

#2 2011-12-05 02:57:22

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

Bump


Why

Offline

 

#3 2011-12-05 08:57:15

mrwalker81
New Scratcher
Registered: 2010-11-03
Posts: 5

Re: How to simulate gravity (tutorial)

not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.

Offline

 

#4 2011-12-05 10:49:04

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

mrwalker81 wrote:

not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.

Sorry, could you maybe simplify that a bit?  big_smile


Why

Offline

 

#5 2011-12-05 10:57:50

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: How to simulate gravity (tutorial)

Sweet!


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#6 2011-12-05 11:00:10

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

Pecola1 wrote:

Sweet!

Thanks  smile


Why

Offline

 

#7 2011-12-05 20:17:13

mrwalker81
New Scratcher
Registered: 2010-11-03
Posts: 5

Re: How to simulate gravity (tutorial)

RedRocker227 wrote:

mrwalker81 wrote:

not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.

Sorry, could you maybe simplify that a bit?  big_smile

Haha. yeah. Falling objects accelerate. For the first second an object is falling, it is traveling around -9.8m/s. By the second second, this is doubled, and so on until it reaches it's terminal of velocity(max rate of speed by mass). So by creating a timer that counts up from zero, you can multiply the timer variable by -9.8 (or whatever rate you want), you will get a realistic gravitational fall.

Offline

 

#8 2011-12-06 02:39:44

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

mrwalker81 wrote:

RedRocker227 wrote:

mrwalker81 wrote:

not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.

Sorry, could you maybe simplify that a bit?  big_smile

Haha. yeah. Falling objects accelerate. For the first second an object is falling, it is traveling around -9.8m/s. By the second second, this is doubled, and so on until it reaches it's terminal of velocity(max rate of speed by mass). So by creating a timer that counts up from zero, you can multiply the timer variable by -9.8 (or whatever rate you want), you will get a realistic gravitational fall.

Ah, okay. This was designed for beginners though, so I didn't really wanna make it too complicated. Good suggestion though!  smile


Why

Offline

 

#9 2011-12-06 07:41:56

XOuter
New Scratcher
Registered: 2011-12-04
Posts: 20

Re: How to simulate gravity (tutorial)

I hate how the object just bounces on the ground though, I'm going to figure out how to fix that.

Offline

 

#10 2011-12-06 07:51:05

XOuter
New Scratcher
Registered: 2011-12-04
Posts: 20

Re: How to simulate gravity (tutorial)

In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0

Offline

 

#11 2011-12-06 09:15:53

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: How to simulate gravity (tutorial)

Is it worth mentioning that the "Y velocity" variable should be set to "this sprite only"?  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#12 2011-12-06 10:37:47

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

blob8108 wrote:

Is it worth mentioning that the "Y velocity" variable should be set to "this sprite only"?  smile

Oh yeah, good suggestion!  smile


Why

Offline

 

#13 2011-12-06 10:38:55

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

XOuter wrote:

In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0

But then it would be partly underground; you need a 1 so that it rises to the top of the ground.


Why

Offline

 

#14 2011-12-06 10:40:06

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

XOuter wrote:

I hate how the object just bounces on the ground though, I'm going to figure out how to fix that.

Yeah, but unfortunately that comes hand in hand with an essential part of the script  hmm  I think you can stop it vibrating using colour sensors, but I've never attempted it.


Why

Offline

 

#15 2011-12-06 11:05:31

Goliath1
Scratcher
Registered: 2010-09-01
Posts: 100+

Re: How to simulate gravity (tutorial)

RedRocker227 wrote:

I've seen a lot of topics recently asking how to add the effect of gravity in their game. I thought "This is really cluttering up the forums". So from now on, you can just link to this topic whenever someone asks that question!  wink
_______________________________________________________________________________

First, you need to create a variable. To do this, click on the orange tab called 'Variables' in the bottom right corner of the tabs.
http://i41.tinypic.com/wmgdxy.png

Then, click on the 'Make a variable' button, and make a variable called 'Y velocity'. Make sure you check the box saying 'For this sprite only', and not 'For all sprites'.
http://i44.tinypic.com/fc90nt.png

Then, on the sprite that you want to jump, put this script:
http://i42.tinypic.com/35ic3es.png

You can change the 15 in this to another number, depending on how high you wan it to jump:
http://i41.tinypic.com/2mfosk.png
(higher number=higher jump, lower number=lower jump)

What's happening?
If the sprite that's jumping is touching the colour of the ground, then one of two things will happen; if the up arrow key is being pressed (or another key of your choice), it will set the variable 'Y velocity' to 15; this is what makes it jump. If the key isn't being pressed, it will set the variable 'Y velocity' to 1; this is so that the sprite will always rise to the top of the surface, instead of partly being underground. If it's not touching the colour of the ground, it will change the variable 'Y velocity' by -1 (decreasing it). This is what gives the effect of gravity. Finally, the sprite changes it's y position by the variable 'Y velocity'.
_______________________________________________________________________________

Alternatively, if you want the sprite to bounce, you can change the previous script to one like this:
http://i39.tinypic.com/157c3lc.png

What's happening?
This script is almost the same, apart from one part of it. That one difference in the script makes a big difference in the project though. The difference is this:
http://i40.tinypic.com/1okio6.png
Instead of this:
http://i42.tinypic.com/8x2ao0.png

If you don't know what 'abs' means, it's short for 'absolute value', and it basically turns a negative number into it's positive equivalent, but if the number's positive it will stay the same. So abs of -5 = 5, and abs of 5 = 5. Another way to remember the meaning is that it's the distance from zero.
Anyway, back to the tutorial. When the sprite is touching the ground colour, it will set the variable 'Y velocity' to 'abs of Y velocty * 0.8'. You can change the 0.8 to whatever number you want; changing it so that it's higher means that it will take longer to settle on the ground, while decreasing the number will mean it takes less time to settle. Play around with it and see if you can find the perfect amount for you!

And that's it! If you use this method, I don't mind if you don't give me credit in your game, but I'd appreciate it if you do!  smile
Also, tell me if there's any mistakes I need to correct!  big_smile

I figured it out the exact same way! XD


http://8t39t548rug88j.files.wordpress.com/2012/09/imagesft.jpg

Offline

 

#16 2011-12-07 16:31:57

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: How to simulate gravity (tutorial)

Thanks  tongue  Now can i have a tutorial on how to make X velocity??? (I REALLY do not know how ._.)


I'm back.
Maybe.

Offline

 

#17 2011-12-07 16:36:12

Venazard
Scratcher
Registered: 2009-12-15
Posts: 100+

Re: How to simulate gravity (tutorial)

DigiTechs wrote:

Thanks  tongue  Now can i have a tutorial on how to make X velocity??? (I REALLY do not know how ._.)

I figured XVelocity out with the example project MarbleRacer.


http://nitrostudios.weebly.com/uploads/6/8/1/2/6812574/1323469851.png

Offline

 

#18 2011-12-07 16:41:34

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: How to simulate gravity (tutorial)

RedRocker227 wrote:

XOuter wrote:

In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0

But then it would be partly underground; you need a 1 so that it rises to the top of the ground.

I just realised, You can use invisible sprites to do this job; make an invisible sprite the colour you want the ground to be - and make the ground a shade darker or lighter
That, would probbably make the non-vibrate thingy script work  big_smile


I'm back.
Maybe.

Offline

 

#19 2011-12-07 16:47:56

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: How to simulate gravity (tutorial)

DigiTechs wrote:

RedRocker227 wrote:

XOuter wrote:

In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0

But then it would be partly underground; you need a 1 so that it rises to the top of the ground.

I just realised, You can use invisible sprites to do this job; make an invisible sprite the colour you want the ground to be - and make the ground a shade darker or lighter
That, would probbably make the non-vibrate thingy script work  big_smile

Also, I found another way - Set the Costume Center to the BOTTOM of the costume, and it seems to appear to stand properly..


I'm back.
Maybe.

Offline

 

#20 2011-12-07 20:12:15

XOuter
New Scratcher
Registered: 2011-12-04
Posts: 20

Re: How to simulate gravity (tutorial)

RedRocker227 wrote:

XOuter wrote:

In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0

But then it would be partly underground; you need a 1 so that it rises to the top of the ground.

Hmm... Using variables you may be able to make it change from 1 to 0 if its on the top of the ground, but that would require some color sensing. I'll see if I can do something, or you can change the costume center, but then if you had the player have a sword or something like that, it wouldn't work.

Offline

 

#21 2011-12-07 20:26:08

XOuter
New Scratcher
Registered: 2011-12-04
Posts: 20

Re: How to simulate gravity (tutorial)

Woot! Got a perfect working version! No changes of costume center or anything like that, It works perfectly too! You have to make a thin line of a different color at the top of the platform though. I tested it and you can use the smallest marker size so you can make it almost unnoticable. This is it, I'm a new scratcher on this account still so I can't use BBcode: http://i.imgur.com/sM6hn.gif

Offline

 

#22 2011-12-09 09:22:03

BOBBYBOB3
Scratcher
Registered: 2011-11-25
Posts: 500+

Re: How to simulate gravity (tutorial)

That just made my sprite go to the top of the screen.
                                                                                BOBBYBOB3. S sad


http://i89.photobucket.com/albums/k219/jackdelamare/JPIV/JPIV-Userbar2.png after seven years of development fails I'm impatient... IT MUST COME

Offline

 

#23 2011-12-09 09:26:44

BOBBYBOB3
Scratcher
Registered: 2011-11-25
Posts: 500+

Re: How to simulate gravity (tutorial)

No, I wasn't trying to make a bad word, I was trying to make a baffled emoticon. Sorry, RedRocker227, I don't mean to be mean, I was just telling you it doesn't work.


http://i89.photobucket.com/albums/k219/jackdelamare/JPIV/JPIV-Userbar2.png after seven years of development fails I'm impatient... IT MUST COME

Offline

 

#24 2011-12-09 10:20:24

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How to simulate gravity (tutorial)

BOBBYBOB3 wrote:

No, I wasn't trying to make a bad word, I was trying to make a baffled emoticon. Sorry, RedRocker227, I don't mean to be mean, I was just telling you it doesn't work.

Are you sure you put -1 and not 1 in the else bit of 'If touching colour [colour of ground]'?


Why

Offline

 

#25 2011-12-10 11:15:49

BOBBYBOB3
Scratcher
Registered: 2011-11-25
Posts: 500+

Re: How to simulate gravity (tutorial)

Oh! Maybe I DID mess up! Considering I'm new to this, probably!
                                                                                        BOBBYBOB3.  smile


http://i89.photobucket.com/albums/k219/jackdelamare/JPIV/JPIV-Userbar2.png after seven years of development fails I'm impatient... IT MUST COME

Offline

 

Board footer