Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make if on edge, bounce
- MrSharky09
-
12 posts
How to make if on edge, bounce
How do I make an
Im hoping for a sprite called walls that the ball bounces off no matter what, so I don't have vertical walls and horizontal walls on different sprites
please and thank you
Here is a project im using to show how things are working so far
https://scratch.mit.edu/projects/724082783/
without using that block, so I can have it bounce of things like sprites as well instead of just edge
Im hoping for a sprite called walls that the ball bounces off no matter what, so I don't have vertical walls and horizontal walls on different sprites
please and thank you
Here is a project im using to show how things are working so far
https://scratch.mit.edu/projects/724082783/
Last edited by MrSharky09 (Aug. 23, 2022 03:37:43)
- legendary34678
-
1000+ posts
How to make if on edge, bounce
Hello! You can use these blocks:
Rectangular bouncing is simple, but bouncing off of non-rectangular things is much trickier. Do the above blocks work for you?
Rectangular bouncing is simple, but bouncing off of non-rectangular things is much trickier. Do the above blocks work for you?
- MrSharky09
-
12 posts
How to make if on edge, bounce
Hello! You can use these blocks:
Rectangular bouncing is simple, but bouncing off of non-rectangular things is much trickier. Do the above blocks work for you?
It works, but I was mainly hoping for a way to have the walls all be one sprite, or all be together. I know it can be done with like blocks and wall detection stuff but that stuff is weird, so im probably gonna edit the discussion
- legendary34678
-
1000+ posts
How to make if on edge, bounce
What you could do is to create a custom block (that runs without screen refresh) that checks which side it is on when it touches a wall. It would go something along the lines of:
Hope this helped!
Hope this helped!
Last edited by legendary34678 (Aug. 23, 2022 04:38:53)
- MrSharky09
-
12 posts
How to make if on edge, bounce
What you could do is to create a custom block (that runs without screen refresh) that checks which side it is on when it touches a wall. It would go something along the lines of:
Hope this helped!
I attempted this, but all I got was a weird bug where it sometimes gets stuck in the walls. There was one change I made, but it was only changing your script from
to
Because the <not touching> didn't work
here's the project if you want to see the bug
https://scratch.mit.edu/projects/724082783/
Also I appreciate the help
Last edited by MrSharky09 (Aug. 23, 2022 05:17:01)
- legendary34678
-
1000+ posts
How to make if on edge, bounce
Hmm… This might be due to the fact that I haven't tested this code out. Maybe someone else can provide a better solution than the one I thought of.
- RT_Borg
-
1000+ posts
How to make if on edge, bounce
Hi MrSharky09,
You might also be interested in the way griffpatch does bouncing. It's fancy ball physics, but it might be right for what you're doing:
griffpatch Ball Projectile Physics Tutorial
https://www.youtube.com/watch?v=Ge1DbXkyMKo
and this starter project by reevo_56 (that griffpatch included in his studio)
https://scratch.mit.edu/projects/514523898
@legendary34678 clever idea for doing all the walls at once like that. It may need to shift by more than 1, depending on how precisely adjacent to the wall the object is before checking. There might also be a very rare case where you're precisely in a corner and bounce one direction instead of both. But very cool approach!
– RT_Borg
You might also be interested in the way griffpatch does bouncing. It's fancy ball physics, but it might be right for what you're doing:
griffpatch Ball Projectile Physics Tutorial
https://www.youtube.com/watch?v=Ge1DbXkyMKo
and this starter project by reevo_56 (that griffpatch included in his studio)
https://scratch.mit.edu/projects/514523898
@legendary34678 clever idea for doing all the walls at once like that. It may need to shift by more than 1, depending on how precisely adjacent to the wall the object is before checking. There might also be a very rare case where you're precisely in a corner and bounce one direction instead of both. But very cool approach!
– RT_Borg
Last edited by RT_Borg (Aug. 23, 2022 06:59:36)
- legendary34678
-
1000+ posts
How to make if on edge, bounce
I noticed an error in my code which is that it will continue to check even after the correct bouncing code is run. So, I think it should be modified like this:
Now the error I realized was that after figuring out where the sprite is, it should stop checking for the other directions. I think you could use RT_Borg's suggestion though!
Now the error I realized was that after figuring out where the sprite is, it should stop checking for the other directions. I think you could use RT_Borg's suggestion though!
- MrSharky09
-
12 posts
How to make if on edge, bounce
I noticed an error in my code which is that it will continue to check even after the correct bouncing code is run. So, I think it should be modified like this:
Now the error I realized was that after figuring out where the sprite is, it should stop checking for the other directions. I think you could use RT_Borg's suggestion though!
I tried that but you didn't really say where to put the brodcast block so im not sure I did it right, but now it always collides with top and bottom walls but never collides with left and right walls
- legendary34678
-
1000+ posts
How to make if on edge, bounce
I think I made it work in this remix, although it sometimes breaks if the ball is going too fast. I think that breaks the collision, but I'm not sure.
Last edited by legendary34678 (Aug. 23, 2022 21:27:45)
- legendary34678
-
1000+ posts
How to make if on edge, bounce
I just took a look at griffpatch's method, and wow is it super simple (and elegant too might I add). You would be much, much better off using his method since you're only doing rectangular bouncing. I summarized it here:
- RT_Borg
-
1000+ posts
How to make if on edge, bounce
Hey legendary34678 and Hi MrSharky09,
I remixed again. The problem with legendary34678's remix was that the custom Move split a large move into small “move 1 steps” sub-moves, but the bounce test/repair happened outside that. Instead of the broadcast/receive, I moved the test/bounce right into the custom Move block.
Now that test/bounce happens after every move 1, the distance for change x/y during bounce can be small. (I replaced the +/- 5 with a pair of variables for easy trial. Works fine with +/- 1, since that's the most you could have traveled in a vertical/horizontal direction with move 1.)
Ball Thing remix remix
https://scratch.mit.edu/projects/724349828/
Still not sure about what happens in a perfect corner shot. Might still need some thought there.
This would be a good general use tool now.

– RT_Borg
- legendary34678
-
1000+ posts
How to make if on edge, bounce
Yes, I already provided the scripts for turning in the right direction to simulate bouncing here. It would be nice if you could please read the discussion to see what has already been suggested!
- JM827
-
28 posts
How to make if on edge, bounce
wow is it super simple (and elegant too might I add). You would be much, much better off using his method since you're only doing rectangular bouncing. I summarized it here:Yes yes that is not simple at all I just took a look at griffpatch's method, and
- NeonG4
-
1000+ posts
How to make if on edge, bounce
Please don't necropost. (posting on old inactive topics) snip
- Discussion Forums
- » Help with Scripts
-
» How to make if on edge, bounce