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)
Offline
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?
Offline
my solution in an old project here
here is a snapshot of the code...
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)
Offline
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
Offline
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.
Offline
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
Offline
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 . Oh well, this way works better
Offline