ive seen a lot of projects with gravity in them, like
http://scratch.mit.edu/projects/munkeeb/43938
http://scratch.mit.edu/projects/Lucario621/49695
http://scratch.mit.edu/projects/OJY321/40448
they all have gravity. i was wondering how you programmed it into a project. do you make the variable gravity, or is there already a gravity block? also, someone said that they had to use the [blocks] <abs( [/blocks] block to use gravity. what does it do? i know, im being kind of confusing, but if anyone could answer my questions, it would help the new game im working on a lot.
Offline
Refer to this forum thread. http://scratch.mit.edu/forums/viewtopic.php?id=1887
Also the abs block will turn a negative number into a positive number and it is not needed for gravity.
Last edited by archmage (2007-11-23 09:44:52)
Offline
thanks so much!
Offline
Here is a way to do gravity:
<forever if> <not> <touching color[ black
<change{ gravity }by( -0.5
<end>
<forever if> <touching color[ black
<set{ gravity }to( 0
<end>
<forever if> <not> <touching color[ black
<set y to( <{ gravity }>
<end>
<forever>
<if> <key[ up arrow ]pressed?>
<change y by( 3
<set{ gravity }to( 8
<wait until> <touching color[ black
<end>
<end>
That is an easy way to make gravity. Hope this works for you!
Last edited by music_man (2007-11-23 15:27:35)
Offline
music_man wrote:
That is an easy way to make gravity. Hope this works for you!
That is not a very efficient way of doing gravity and by the looks of it the script will make a sprite move at a constant speed which may not be something you want for realistic looking gravity.
The cleaner the code the better
Last edited by archmage (2007-11-23 16:24:14)
Offline
ok, im going to try out your way of gravity music_man. and maybe this will make what i want more clear: in real life, when you play with a ball, you can bounce it and stuff, and i'm making a game where you can bounce a ball against a sprite, almost like soccer. can you do that on scratch?
Offline
I have made a simple gravity demo for you which includes bounce.
http://scratch.mit.edu/projects/archmage/58354
Now bouncing on angles in more difficult because it involves trigonometry.
Offline
basically what i want is a way to use gravity on something without using the arrow keys. archmage, your way is perfect, without the up arrow part, if only it could bounce. i think thats where i could use parts of music_man's way.
[blocks] <when green flag clicked>
<set{ yVelocity }to( 0
<forever>
<change{ yVelocity }by( -1
<change y by( yVelocity
<end>
<when green flag clicked>
<forever if><touching color[ black
<set{ yVelocity }to( 0
<end>[/blocks]
would that make any sense at all to make it bounce?
Last edited by funkymonkey (2007-11-23 17:11:39)
Offline
Check out http://scratch.mit.edu/projects/archmage/58354 for a simple bouncing script.
Offline
For more complicated bouncing off of objects, check out the pinball game at
http://scratch.mit.edu/projects/kevin_and_abe/54377
It only works in scratch v1.2, but it has pretty good bounces with slow gravity.
Offline
To bounce the ball, do this:
<forever if> <touching color[ black
<set{ gravity }to( (( <{ gravity }> <*> -1 ))
<end>
funkymonkey's way to clean it up is good. This one will make it bounce.
This may cause some problems just to let you know. I have a project I will let you use. It shows you how to make a ball bounce. It is not on the web sight yet but it will soon.
Last edited by music_man (2007-11-23 23:13:48)
Offline
archmage wrote:
Check out http://scratch.mit.edu/projects/archmage/58354 for a simple bouncing script.
cool, but could you tell me what that script for bouncing is? my scratch is being stupid and not letting me open your project.
also music_man, that script does make it move at a constant speed.
Offline
music_man wrote:
To bounce the ball, do this:
<forever if> <touching color[ black
<set{ gravity }to( (( <{ gravity }> <*> -1 ))
<end>
funkymonkey's way to clean it up is good. This one will make it bounce.
This may cause some problems just to let you know. I have a project I will let you use. It shows you how to make a ball bounce. It is not on the web sight yet but it will soon.
If you want it to lose power do this
<forever if> <touching[ ground
<set{ gravity }to( (( <{ yVelocity }> <*> -0.95 ))
<change{ yVelocity }by( 1
<end>
This is what I have in my demo
Music_man's way was pretty similar but the object will not lose it's bounce power.
Last edited by archmage (2007-11-24 11:27:35)
Offline
so that would make it like what you had in your project? for me to make something bounce i should use these 2 scripts:
[blocks] <when green flag clicked>
<set{ yVelocity }to( 0
<forever>
<change{ yVelocity }by( -1
<change y by( yVelocity
<end>
<when green flag clicked>
<forever if> <touching[ ground
<set{ gravity }to( (( <{ yVelocity }> <*> -0.95 ))
<change{ yVelocity }by( 1
<end>
[/blocks]
i would need the variables gravity and yVelocity, and also a sprite called ground.
just clearing it up. thanks!
Last edited by funkymonkey (2007-11-25 15:23:51)
Offline
funkymonkey wrote:
so that would make it like what you had in your project? for me to make something bounce i should use these 2 scripts:
[blocks] <when green flag clicked>
<set{ yVelocity }to( 0
<forever>
<change{ yVelocity }by( -1
<change y by( yVelocity
<end>
<when green flag clicked>
<forever if> <touching[ ground
<set{ gravity }to( (( <{ yVelocity }> <*> -0.95 ))
<change{ yVelocity }by( 1
<end>
[/blocks]
i would need the variables gravity and yVelocity, and also a sprite called ground.
just clearing it up. thanks!
Oh, very sorry a part of the script is wrong.
It should have the yVelocity variable instead of the gravity variable
//Like this
<when green flag clicked>
<forever if> <touching[ ground
<set{ yVelocity }to( (( <{ yVelocity }> <*> -0.95 ))
<change{ yVelocity }by( 1
<end>
It would also be a good idea to set the yVelocity to 0 when the flag is clicked.
Offline
thanks so much!
Offline
can you make the ball bounce other ways that just up and down? like since im making a soccer game, i need to be able to make the ball be able to move when it is hit.
Offline
This could also be applied to left and right movement if you recreate the scripts to affect y position.
If you want the object to bounce off of any angle you need to use trigonometry.
Check out kevin's project to see how it is done.
http://scratch.mit.edu/projects/kevin_and_abe/54377
Offline
kevin_karplus wrote:
For more complicated bouncing off of objects, check out the pinball game at
http://scratch.mit.edu/projects/kevin_and_abe/54377
It only works in scratch v1.2, but it has pretty good bounces with slow gravity.
what was the script that you used to make it bounce like that?
Offline
funkymonkey wrote:
basically what i want is a way to use gravity on something without using the arrow keys. archmage, your way is perfect, without the up arrow part, if only it could bounce. i think thats where i could use parts of music_man's way.
[blocks] <when green flag clicked>
<set{ yVelocity }to( 0
<forever>
<change{ yVelocity }by( -1
<change y by( yVelocity
<end>
<when green flag clicked>
<forever if><touching color[ black
<set{ yVelocity }to( 0
<end>[/blocks]
would that make any sense at all to make it bounce?
No.
Offline
music_man wrote:
No.
actually thats basically what you said would make it bounce. archmage's scripts were the only ones that made it actually bounce.
Last edited by funkymonkey (2007-11-25 20:52:33)
Offline
funkymonkey wrote:
kevin_karplus wrote:
For more complicated bouncing off of objects, check out the pinball game at
http://scratch.mit.edu/projects/kevin_and_abe/54377
It only works in scratch v1.2, but it has pretty good bounces with slow gravity.what was the script that you used to make it bounce like that?
never mind
Offline
music_man wrote:
Here is a way to do gravity:
<forever if> <not> <touching color[ black
<change{ gravity }by( -0.5
<end>
<forever if> <touching color[ black
<set{ gravity }to( 0
<end>
<forever if> <not> <touching color[ black
<set y to( <{ gravity }>
<end>
<forever>
<if> <key[ up arrow ]pressed?>
<change y by( 3
<set{ gravity }to( 8
<wait until> <touching color[ black
<end>
<end>
That is an easy way to make gravity. Hope this works for you!
does this work for gravity? just for making the jump look more realistic. thats what i need now.
Offline
That code won't produce a realistic looking gravity. That code will make a sprite move downwards at a uniform speed if not touching colour black. Try using the codes I posted, it's the easiest way to do things.
Offline
You might also want to look at the ball sprite in "School pinball"
http://scratch.mit.edu/projects/kevin_and_abe/54377
Offline