Discuss Scratch

LittleBigStinker
Scratcher
57 posts

if on edge bounce

So, let's say I want to make a game where there is a ball, and it bounces off of other sprites. Doing the exact same thing as the “if on edge bounce” block but with another sprite instead of the edge. What is the code for the if on edge bounce block?

Example:
There is a ball moving towards the top wall at a 45-degree angle. It should turn 45 degrees counterclockwise. “Okay easy, just make the ball turn 45 degrees when it hits a wall!” See this is where it gets confusing because how do I know to either turn clockwise or counterclockwise? Or how can I know how much of an angle to turn because what if it is the same scenario, but the ball is at a 30-degree angle. It should turn, well, 30 degrees! But what if it is the right wall! Because if the ball is heading towards the right wall, I can't just make the ball turn the direction of the ball because the angle of the ball would be 120.

Please help.

Last edited by LittleBigStinker (Jan. 3, 2024 22:19:47)

p-p-p-p-p-p-p-p-p-p-
Scratcher
1000+ posts

if on edge bounce

does the sprite have a set position? Cause if not, then it's not easy to code something like that (at least I don't think)
Scratch-Minion
Scratcher
1000+ posts

if on edge bounce

Here is a Rebound Directions project that should help: https://scratch.mit.edu/projects/138052902/
RethinkingVoxels
Scratcher
1000+ posts

if on edge bounce

what about this?
when green flag clicked
forever
if <touching [sprite v]?> then
bounce ::custom
end
end
define bounce
set [last x v] to (0) //block needs to be run without screen refresh
change x by (9999999)
if on edge, bounce
set x to (last x)
LittleBigStinker
Scratcher
57 posts

if on edge bounce

I just want to know how “if on edge bounce” works so I can make a ball bounce off of other sprites
LittleBigStinker
Scratcher
57 posts

if on edge bounce

Scratch-Minion wrote:

Here is a Rebound Directions project that should help: https://scratch.mit.edu/projects/138052902/
this is exactly what I wanted to find, but how would this work with my game? I'm trying to find how to make something bounce off of a clone of a sprite.

https://scratch.mit.edu/projects/946135072/
PixelGuy_1234
Scratcher
7 posts

if on edge bounce

HMM…

It seems that it wouldn't work with a clone, but maybe use sin cos tan?
Scratch-Minion
Scratcher
1000+ posts

if on edge bounce

LittleBigStinker wrote:

Scratch-Minion wrote:

Here is a Rebound Directions project that should help: https://scratch.mit.edu/projects/138052902/
this is exactly what I wanted to find, but how would this work with my game? I'm trying to find how to make something bounce off of a clone of a sprite.

https://scratch.mit.edu/projects/946135072/

In the Ball sprite, you can use “touching Block” to check if the ball is touching any block clone.
Then broadcast “Block Hit” and wait.
All the Block clones will receive this message, so check if the Block clone is touching the ball.

ie. code like below goes inside the Block sprite:

when I receive [Block Hit v]
if <touching [Ball v] ?> then
...
delete this clone
end


Note: In the costume editor, you could colour the sides of the blocks with one colour for the top and bottom and another colour for the left and right sides. Then when the Ball hits a Block, you could check which colour it is touching to determine the rebound calculation.

Alternatively, you could have special sprites and clones just for the horizontal and vertical edges.
TheUltimateHoodie
Scratcher
1000+ posts

if on edge bounce

sure,
define if on edge bounce 
side note: run without screen refresh or i dont think it will work because it will not run within one frame
if <touching [the thing v] ?> then
turn cw (180) degrees

end
note: use

move () steps

Last edited by TheUltimateHoodie (Jan. 7, 2024 08:53:44)

deck26
Scratcher
1000+ posts

if on edge bounce

If on edge bounce only has to work with vertical and horizontal edges but the bounce ange depends on the angle of the surface and the approach direction. So to bounce off a sprite will depend on the angle of the surface at the collision point. If the sprite is a simple pong paddle that's easy but if it's a circle, for example, the angle at the point of contact could be any value in the 360 degree range (although bouncing off angle x is the same as bouncing off angle x+180 so the range is actually half of 360).

https://scratch.mit.edu/projects/99412537 may help you understand.

Strictly speaking the shape of the moving object is also relevant. A square that can rotate hitting a flat surface may rotate and skid or just bounce depending on how it hits the surface.

The ‘on edge bounce’ block doesn't care about any of this and just does a very basic bounce which is fine for balls but not for everything. The code for the block is also not written in Scratch so i don't see how it would help but at the end of the day it just does what my demo project shows but calculates the bounce according to whether you're hitting a side or top/bottom.
flyingteleportingc
Scratcher
6 posts

if on edge bounce

define scratch my blocks are stupid

bugged blocks are better
flyingteleportingc
Scratcher
6 posts

if on edge bounce

 

Last edited by flyingteleportingc (April 27, 2024 17:52:56)

SebGamer2009
Scratcher
61 posts

if on edge bounce

This conversation is more than 2 months old. If you have nothing to contribute, please don't comment on this topic. Doing so is called necroposting.

Powered by DjangoBB