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

#1 2007-11-23 09:27:17

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

gravity in projects

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.


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#2 2007-11-23 09:43:19

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#3 2007-11-23 09:48:39

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

thanks so much!


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#4 2007-11-23 15:26:23

music_man
Scratcher
Registered: 2007-09-22
Posts: 100+

Re: gravity in projects

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)


The fool has said in his heart, "There is no God."...  Psalms 14:1

Offline

 

#5 2007-11-23 16:23:15

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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  wink

Last edited by archmage (2007-11-23 16:24:14)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#6 2007-11-23 16:49:38

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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?


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#7 2007-11-23 16:58:33

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#8 2007-11-23 17:02:09

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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)


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#9 2007-11-23 17:07:36

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

Check out http://scratch.mit.edu/projects/archmage/58354 for a simple bouncing script.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#10 2007-11-23 17:35:09

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: gravity in projects

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

 

#11 2007-11-23 22:59:41

music_man
Scratcher
Registered: 2007-09-22
Posts: 100+

Re: gravity in projects

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)


The fool has said in his heart, "There is no God."...  Psalms 14:1

Offline

 

#12 2007-11-24 10:00:40

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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.


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#13 2007-11-24 11:18:23

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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)


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#14 2007-11-25 15:22:57

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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)


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#15 2007-11-25 15:42:36

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#16 2007-11-25 16:18:39

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

thanks so much!


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#17 2007-11-25 16:48:54

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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.


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#18 2007-11-25 16:56:55

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#19 2007-11-25 17:14:02

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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?


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#20 2007-11-25 19:30:12

music_man
Scratcher
Registered: 2007-09-22
Posts: 100+

Re: gravity in projects

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.


The fool has said in his heart, "There is no God."...  Psalms 14:1

Offline

 

#21 2007-11-25 20:51:25

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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)


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#22 2007-11-26 20:19:01

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#23 2007-12-19 20:13:10

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: gravity in projects

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.


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#24 2007-12-19 20:37:42

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: gravity in projects

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.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#25 2007-12-20 01:04:06

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: gravity in projects

You might also want to look at the ball sprite in "School pinball"
http://scratch.mit.edu/projects/kevin_and_abe/54377

Offline

 

Board footer