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

#1 2010-07-14 01:25:56

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Simulating Ball Bounces

How do I simulate a ball being bounced? Like, if the ball hits a wall, it bounces back, and if it hits the floor, it bounces at a 90º angle from the previous bounce or whatever. So, how do I simulate a ball being bounced?

Edit: Just want to make sure you know this- the only floor in the project is a moving platform.

Last edited by Sunrise-Moon (2010-07-14 02:14:01)


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#2 2010-07-14 01:34:11

Flait7
Scratcher
Registered: 2008-04-14
Posts: 100+

Re: Simulating Ball Bounces

One way could be using variables such as speed and gravity, speed being how fast the ball is moving in whatever direction while gravity is a constant pull downward


http://scratch.mit.edu/static/projects/Flait7/2248016_sm.pnghttp://scratch.mit.edu/static/projects/Flait7/1827934_sm.png

Offline

 

#3 2010-07-14 01:34:54

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Simulating Ball Bounces

Flait7 wrote:

One way could be using variables such as speed and gravity, speed being how fast the ball is moving in whatever direction while gravity is a constant pull downward

You mean basically velocity?


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#4 2010-07-14 10:07:10

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: Simulating Ball Bounces

my solution in an old project here

here is a snapshot of the code...
http://a.imageshack.us/img714/3678/pongscript.gif

First it picks a random direction but not straight horizontal or vertical cause that would be a boring bounce to demonstrate.

Then it goes to the middle and aims itself to the direction.

The Forever loop first checks to see if the ball has hit any of the four sides. If it has, it changes the applicable variable, and then figures the new direction. Lastly it aims itself and then moves in that direction before looping again.


Things to notice are that due to the way Scratch handles X and Y coordinates, you cannot use one simple formula to figure all angles. Instead you must use two formulas, one for horizontal wall bounces and another for vertical bounces.
Also, you set the walls with the 4 X and Y checks. I set mine along the edges of the screen but you could move them around. I used to have a project with a ball that bounced around inside a box that also bounced around the screen.

ps. this was one of my first scripts. I can see a lot of ways to make it better now. Maybe I will update it and re-post the new version.

Last edited by Locomule (2010-07-14 10:39:20)


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

#5 2010-07-14 11:42:59

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Simulating Ball Bounces

Locomule wrote:

my solution in an old project here

here is a snapshot of the code...
http://a.imageshack.us/img714/3678/pongscript.gif

First it picks a random direction but not straight horizontal or vertical cause that would be a boring bounce to demonstrate.

Then it goes to the middle and aims itself to the direction.

The Forever loop first checks to see if the ball has hit any of the four sides. If it has, it changes the applicable variable, and then figures the new direction. Lastly it aims itself and then moves in that direction before looping again.


Things to notice are that due to the way Scratch handles X and Y coordinates, you cannot use one simple formula to figure all angles. Instead you must use two formulas, one for horizontal wall bounces and another for vertical bounces.
Also, you set the walls with the 4 X and Y checks. I set mine along the edges of the screen but you could move them around. I used to have a project with a ball that bounced around inside a box that also bounced around the screen.

ps. this was one of my first scripts. I can see a lot of ways to make it better now. Maybe I will update it and re-post the new version.

Thanks! I sort of got it all down, but maybe your script is better. I'll try it probably  smile


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#6 2010-07-14 12:17:34

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: Simulating Ball Bounces

My script was just meant to be a simple demo. As long as you understand the technique, that is all that matters.


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

#7 2010-07-14 12:24:18

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Simulating Ball Bounces

Locomule wrote:

My script was just meant to be a simple demo. As long as you understand the technique, that is all that matters.

lol I kind of understand it. I ended up using velocity that never slows down and multiplying the velocity by -1 when it hit a wall.


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

#8 2010-07-14 16:28:41

Locomule
Scratcher
Registered: 2009-08-24
Posts: 500+

Re: Simulating Ball Bounces

Hmm, as goonlon pointed out to me privately, you could just use the "if on edge bounce" block. I dunno why I just assumed you didn't want to use the edges of the screen.

"Loco fumbles but goonlon picks it up and scores!!" heh


aka Pain from DragonSpires, Delrith Online, BotBattle, Urban Dead etc etc lol

Offline

 

#9 2010-07-14 16:36:56

Sunrise-Moon
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Simulating Ball Bounces

Locomule wrote:

Hmm, as goonlon pointed out to me privately, you could just use the "if on edge bounce" block. I dunno why I just assumed you didn't want to use the edges of the screen.

"Loco fumbles but goonlon picks it up and scores!!" heh

lol I never tried it  tongue . Oh well, this way works better  big_smile


http://i1067.photobucket.com/albums/u427/HulKDzN/RebornBlade.png

Offline

 

Board footer