Discuss Scratch

MrSharky09
Scratcher
12 posts

How to make if on edge, bounce

How do I make an
ifonedge,bounce
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
Scratcher
1000+ posts

How to make if on edge, bounce

Hello! You can use these blocks:

pointindirectiondirection*-1This is used for bouncing off of vertical walls (the left and right edges)turn90degreesThis is used for bouncing off of horizontal walls (the top and bottom edges)pointindirectiondirection*-1turn90degrees

Rectangular bouncing is simple, but bouncing off of non-rectangular things is much trickier. Do the above blocks work for you?
MrSharky09
Scratcher
12 posts

How to make if on edge, bounce

legendary34678 wrote:

Hello! You can use these blocks:

pointindirectiondirection*-1This is used for bouncing off of vertical walls (the left and right edges)turn90degreesThis is used for bouncing off of horizontal walls (the top and bottom edges)pointindirectiondirection*-1turn90degrees

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
Scratcher
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:

Remember to check the "run without screen refresh" box!definewallcheckchangeyby-1ifnottouchingwallthenchangeyby1. . .Bounce off of top code goes herestopthis scriptIf we are at the topchangeyby1changexby-1ifnottouchingwallthenchangexby1. . .Bounce off of right code goes hereIf we are at the rightchangexby1changeyby1ifnottouchingwallthenchangeyby-1. . .Bounce off of bottom code goes hereIf we are at the bottomchangeyby-1changexby1ifnottouchingwallthenchangexby-1. . .Bounce off of left code goes hereIf we are at the leftchangexby-1

Hope this helped!

Last edited by legendary34678 (Aug. 23, 2022 04:38:53)

MrSharky09
Scratcher
12 posts

How to make if on edge, bounce

legendary34678 wrote:

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:

Remember to check the "run without screen refresh" box!definewallcheckchangeyby-1ifnottouchingwallthenchangeyby1. . .Bounce off of top code goes herestopthis scriptIf we are at the topchangeyby1changexby-1ifnottouchingwallthenchangexby1. . .Bounce off of right code goes hereIf we are at the rightchangexby1changeyby1ifnottouchingwallthenchangeyby-1. . .Bounce off of bottom code goes hereIf we are at the bottomchangeyby-1changexby1ifnottouchingwallthenchangexby-1. . .Bounce off of left code goes hereIf we are at the leftchangexby-1

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
ifnottouchingwallthen
to
iftouchingwall?then
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
Scratcher
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
Scratcher
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

Last edited by RT_Borg (Aug. 23, 2022 06:59:36)

legendary34678
Scratcher
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:

Remember to check the "run without screen refresh" box!definewallcheckchangeyby-1ifnottouchingwallthenchangeyby1. . .Bounce off of top code goes herestopthis scriptIf we are at the topchangeyby1changexby-1ifnottouchingwallthenchangexby1. . .Bounce off of right code goes herestopthis scriptIf we are at the rightchangexby1changeyby1ifnottouchingwallthenchangeyby-1. . .Bounce off of bottom code goes herestopthis scriptIf we are at the bottomchangeyby-1changexby1ifnottouchingwallthenchangexby-1. . .Bounce off of left code goes herestopthis scriptIf we are at the leftchangexby-1broadcastwall checkandwaitUse this block to check all four wallswhenIreceivewall checkwallcheck

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
Scratcher
12 posts

How to make if on edge, bounce

legendary34678 wrote:

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:

Remember to check the "run without screen refresh" box!definewallcheckchangeyby-1ifnottouchingwallthenchangeyby1. . .Bounce off of top code goes herestopthis scriptIf we are at the topchangeyby1changexby-1ifnottouchingwallthenchangexby1. . .Bounce off of right code goes herestopthis scriptIf we are at the rightchangexby1changeyby1ifnottouchingwallthenchangeyby-1. . .Bounce off of bottom code goes herestopthis scriptIf we are at the bottomchangeyby-1changexby1ifnottouchingwallthenchangexby-1. . .Bounce off of left code goes herestopthis scriptIf we are at the leftchangexby-1broadcastwall checkandwaitUse this block to check all four wallswhenIreceivewall checkwallcheck

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
Scratcher
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
Scratcher
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:

foreverchangexbyspeedxiftouchingwallthensetspeed xto-1*speedxWhen the ball hits the right wall, it reflects its x value to start moving left.changexbyspeedxMoves away from the wall so the loop doesn't trigger infinitely.changeybyspeedyiftouchingwallthensetspeed yto-1*speedyWhen the ball hits the top wall, it reflects its y value to start moving down.changeybyspeedyMoves away from the wall so the loop doesn't trigger infinitely.Instead of using a move block, you can use variables in combination with the change x or y blocks for easy bouncing.

RT_Borg
Scratcher
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
NeonG4
Scratcher
1000+ posts

How to make if on edge, bounce

This needs math, an if statement
iftouchingsprite?then
and
pointindirection0 I will have to look up math, but I can find some. Comment on my profile to remind me.
legendary34678
Scratcher
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
Scratcher
28 posts

How to make if on edge, bounce

legendary34678 wrote:

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:

foreverchangexbyspeedxiftouchingwallthensetspeed xto-1*speedxWhen the ball hits the right wall, it reflects its x value to start moving left.changexbyspeedxMoves away from the wall so the loop doesn't trigger infinitely.changeybyspeedyiftouchingwallthensetspeed yto-1*speedyWhen the ball hits the top wall, it reflects its y value to start moving down.changeybyspeedyMoves away from the wall so the loop doesn't trigger infinitely.Instead of using a move block, you can use variables in combination with the change x or y blocks for easy bouncing.

Yes yes that is not simple at all
TayloxArt
Scratcher
7 posts

How to make if on edge, bounce

iftouchingSprite1,bounce
ExtraMental
Scratcher
100+ posts

How to make if on edge, bounce

Please do not necropost.
arabicGF
Scratcher
1 post

How to make if on edge, bounce

whenclickedpointindirection45foreverifonedge,bouncemove10stepsiftouchingcolorblackthenturn270degrees
NeonG4
Scratcher
1000+ posts

How to make if on edge, bounce

arabicGF wrote:

snip
Please don't necropost. (posting on old inactive topics)

Powered by DjangoBB