Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Realistic Bouncing Script
- bballluke
-
3 posts
Realistic Bouncing Script
Hi, I'm making a game based off of Inkball, a game for Windows Vista and Windows XP. However, I need a script that will make bounces from anywhere look realistic. It's not all 90 degree angles, as the player will be using the pen tool to draw lines for the ball to bounce off of. My friend gave me a sort of bounce script for my first game, Super Marble Challenge, but the ball just bounced backwards, regardless of the angle. This game based off of Inkball is riding on this one script; it would pretty much be the core script. Please help, as I won't be able to make the game any good if I don't get a good script. Thanks in advance. :3
- Locomule
-
1000+ posts
Realistic Bouncing Script
For round object bounces, search for Paddle2See's Alien Pinball remix. It has the vector math formulas you are looking for.
- Locomule
-
1000+ posts
Realistic Bouncing Script
I'll try to explain it in text…
When the ball hits a surface, you've got to know 3 things. The speed of the ball, the ball's direction, and the angle or Normal of the surface it hit. That just means which direction it is pointing.
Normals for a brick would be…
0 for the top
90 for the right side
-90 for the left side
180 for the bottom
Makes sense, right? One way to figure normals is when the ball hits an object, have that object set to not rotate but point at the ball. That direction would be the Normal for the object.
ball bounce script…
Forever…
repeat until Move Ball = False
(place all the following in that repeat)
move Ball Speed steps
if on edge, bounce
set Vx to Ball Speed * sin of direction
set Vy to Ball Speed * cos of direction
set Ball Speed to sqrt of ((Vx * Vx) + (Vy * Vy))
set Angle to asin Vx / Ball Speed
…if Vy < 0 then
……if Vx > 0 then
……set Ang to (180 - Ang)
……else
……set Ang to (0 - (Ang + 180))
(end of both If brackets)
point in direction Ang
turn 180 degrees
wait until Point Ball = True
set Local SN to Surface Normal
set Diff to direction - Local SN
…repeat until not Diff < -180
…change Diff by 360
(end of repeat)
…repeat until not Diff > 180
…change Diff by -360
(end of repeat)
…if abs of Diff < 90 then
…point in direction Local SN - Diff
…else
…turn 180 degrees
(end of If/else bracket)
(end of original repeat block)
set Move Ball to True
set Point Ball to False
(end of Forever block)
This will move the ball, bouncing it off the edges of the screen until it hits your object.
When the object touches the ball, it sets Move Ball to False. The ball stops moving and waits for the object to return its Normal via the Surface Normal variable. Once the object sets that variable, it sets the variable Point Ball to True, triggering the ball script to continue. It uses the Normal to figure Diff, a component of the desired bounce angle. Then it turns aroundmakes sure that it is facing the correct direction, offsets its angle again using Diff and taadaa, the bounce is done.
When the ball hits a surface, you've got to know 3 things. The speed of the ball, the ball's direction, and the angle or Normal of the surface it hit. That just means which direction it is pointing.
Normals for a brick would be…
0 for the top
90 for the right side
-90 for the left side
180 for the bottom
Makes sense, right? One way to figure normals is when the ball hits an object, have that object set to not rotate but point at the ball. That direction would be the Normal for the object.
ball bounce script…
Forever…
repeat until Move Ball = False
(place all the following in that repeat)
move Ball Speed steps
if on edge, bounce
set Vx to Ball Speed * sin of direction
set Vy to Ball Speed * cos of direction
set Ball Speed to sqrt of ((Vx * Vx) + (Vy * Vy))
set Angle to asin Vx / Ball Speed
…if Vy < 0 then
……if Vx > 0 then
……set Ang to (180 - Ang)
……else
……set Ang to (0 - (Ang + 180))
(end of both If brackets)
point in direction Ang
turn 180 degrees
wait until Point Ball = True
set Local SN to Surface Normal
set Diff to direction - Local SN
…repeat until not Diff < -180
…change Diff by 360
(end of repeat)
…repeat until not Diff > 180
…change Diff by -360
(end of repeat)
…if abs of Diff < 90 then
…point in direction Local SN - Diff
…else
…turn 180 degrees
(end of If/else bracket)
(end of original repeat block)
set Move Ball to True
set Point Ball to False
(end of Forever block)
This will move the ball, bouncing it off the edges of the screen until it hits your object.
When the object touches the ball, it sets Move Ball to False. The ball stops moving and waits for the object to return its Normal via the Surface Normal variable. Once the object sets that variable, it sets the variable Point Ball to True, triggering the ball script to continue. It uses the Normal to figure Diff, a component of the desired bounce angle. Then it turns aroundmakes sure that it is facing the correct direction, offsets its angle again using Diff and taadaa, the bounce is done.
- Locomule
-
1000+ posts
Realistic Bouncing Script
I removed the Drag feature that slowed ball movement, and the Kick feature that made the pinball bumpers speed the ball up.
Also, this script specifically works for a moving ball hitting a stationary object. If the object were moving, we would need to take its speed into account.
Another thing this formula ignores is mass. For example, if a huge ball of lead runs into a small ball of lead headon, with both balls traveling the same speed, it is easy to imagine the small ball bouncing backwards faster and the big ball continuing forward slightly slower.
If you are like me, and have never had this math in school, don't get too worried. Just find a Scratch project that uses the math you need and adapt it.
While working on my Breakout (brick breaker) game, I too needed this math. So I looked up Billiards and Pinball projects by most faves and most views. I am adapting the Pinball (moving object bouncing off of non-moving object) right now. Later, I wll go back and use the Billiards formulas (two moving objects bouncing off each other)
Hope all this helps. As with most things Scratch, using it makes it a lot less confusing
Also, this script specifically works for a moving ball hitting a stationary object. If the object were moving, we would need to take its speed into account.
Another thing this formula ignores is mass. For example, if a huge ball of lead runs into a small ball of lead headon, with both balls traveling the same speed, it is easy to imagine the small ball bouncing backwards faster and the big ball continuing forward slightly slower.
If you are like me, and have never had this math in school, don't get too worried. Just find a Scratch project that uses the math you need and adapt it.
While working on my Breakout (brick breaker) game, I too needed this math. So I looked up Billiards and Pinball projects by most faves and most views. I am adapting the Pinball (moving object bouncing off of non-moving object) right now. Later, I wll go back and use the Billiards formulas (two moving objects bouncing off each other)
Hope all this helps. As with most things Scratch, using it makes it a lot less confusing

- ILoveSoda247
-
11 posts
Realistic Bouncing Script
I hope you know that there are these I'll try to explain it in text…
When the ball hits a surface, you've got to know 3 things. The speed of the ball, the ball's direction, and the angle or Normal of the surface it hit. That just means which direction it is pointing.
Normals for a brick would be…
0 for the top
90 for the right side
-90 for the left side
180 for the bottom
Makes sense, right? One way to figure normals is when the ball hits an object, have that object set to not rotate but point at the ball. That direction would be the Normal for the object.
ball bounce script…
Forever…
repeat until Move Ball = False
(place all the following in that repeat)
move Ball Speed steps
if on edge, bounce
set Vx to Ball Speed * sin of direction
set Vy to Ball Speed * cos of direction
set Ball Speed to sqrt of ((Vx * Vx) + (Vy * Vy))
set Angle to asin Vx / Ball Speed
…if Vy < 0 then
……if Vx > 0 then
……set Ang to (180 - Ang)
……else
……set Ang to (0 - (Ang + 180))
(end of both If brackets)
point in direction Ang
turn 180 degrees
wait until Point Ball = True
set Local SN to Surface Normal
set Diff to direction - Local SN
…repeat until not Diff < -180
…change Diff by 360
(end of repeat)
…repeat until not Diff > 180
…change Diff by -360
(end of repeat)
…if abs of Diff < 90 then
…point in direction Local SN - Diff
…else
…turn 180 degrees
(end of If/else bracket)
(end of original repeat block)
set Move Ball to True
set Point Ball to False
(end of Forever block)
This will move the ball, bouncing it off the edges of the screen until it hits your object.
When the object touches the ball, it sets Move Ball to False. The ball stops moving and waits for the object to return its Normal via the Surface Normal variable. Once the object sets that variable, it sets the variable Point Ball to True, triggering the ball script to continue. It uses the Normal to figure Diff, a component of the desired bounce angle. Then it turns around makes sure that it is facing the correct direction, offsets its angle again using Diff and taadaa, the bounce is done.
stuff like that.
look above the message box for something that looks like this
Last edited by ILoveSoda247 (July 22, 2014 12:48:36)
- crabo212
-
8 posts
Realistic Bouncing Script
hey guys I'm trying to make a maze game and I want the mouse to bounce off the walls. I have the collision detection thing but I need to know how to make the mouse bound off the walls of the maze.
- kateg11
-
3 posts
Realistic Bouncing Script
Hi People I am making a soccer game.
I've been wanting to know how can I make the ball bounce when my SPRITE touches it.
I you know plz send me a message or something that can help or a script. I really need to finish this game so plz help me with this.
THANKS!
I've been wanting to know how can I make the ball bounce when my SPRITE touches it.
I you know plz send me a message or something that can help or a script. I really need to finish this game so plz help me with this.
THANKS!
Last edited by kateg11 (Feb. 10, 2016 22:06:28)
- deck26
-
1000+ posts
Realistic Bouncing Script
Hi People I am making a soccer game.Please start your own topic rather than posting on an old one. The New Topic button is at the top of the forum.
I've been wanting to know how can I make the ball bounce when my SPRITE touches it.
I you know plz send me a message or something that can help or a script. I really need to finish this game so plz help me with this.
THANKS!
- FireyDeath4
-
100+ posts
Realistic Bouncing Script
Me too! Hi, I'm making a game based off of Inkball, a game for Windows Vista and Windows XP. However, I need a script that will make bounces from anywhere look realistic. It's not all 90 degree angles, as the player will be using the pen tool to draw lines for the ball to bounce off of. My friend gave me a sort of bounce script for my first game, Super Marble Challenge, but the ball just bounced backwards, regardless of the angle. This game based off of Inkball is riding on this one script; it would pretty much be the core script. Please help, as I won't be able to make the game any good if I don't get a good script. Thanks in advance. :3
- deck26
-
1000+ posts
Realistic Bouncing Script
Please don't necropost. Your comment doesn't add anything useful to this thread.Me too! Hi, I'm making a game based off of Inkball, a game for Windows Vista and Windows XP. However, I need a script that will make bounces from anywhere look realistic. It's not all 90 degree angles, as the player will be using the pen tool to draw lines for the ball to bounce off of. My friend gave me a sort of bounce script for my first game, Super Marble Challenge, but the ball just bounced backwards, regardless of the angle. This game based off of Inkball is riding on this one script; it would pretty much be the core script. Please help, as I won't be able to make the game any good if I don't get a good script. Thanks in advance. :3
- Discussion Forums
- » Help with Scripts
-
» Realistic Bouncing Script