Discuss Scratch

Trixie987
Scratcher
100+ posts

Pong Direction Change

When i make the pong game while following the turtorial, the ball code has
if <touching [paddle v] ?> then
turn ccw (180) degrees
end
How do i make it so when i hit it on the edge of the paddle it moves it that direction?

Last edited by Trixie987 (May 14, 2022 08:19:58)

ggenije2
Scratcher
100+ posts

Pong Direction Change

if <touching [paddle1 v] ?> then
point towards [paddle1 v]
turn cw (180) degrees
end
if <touching [paddle2 v] ?> then
point towards [paddle2 v]
turn cw (180) degrees
end
Trixie987
Scratcher
100+ posts

Pong Direction Change

ggenije2 wrote:

if <touching [paddle1 v] ?> then
point towards [paddle1 v]
turn cw (180) degrees
end
if <touching [paddle2 v] ?> then
point towards [paddle2 v]
turn cw (180) degrees
end

Thanks, i didn't think about that!
bgvikings08
Scratcher
100+ posts

Pong Direction Change

If you are looking for it to just move realistically with different bounces you could try:

if <touching [Paddle] ?> then
point in direction ((x position) - ([x position] of [Paddle]))
end

The sprite that would be touching the paddle is the ball.

Last edited by bgvikings08 (May 14, 2022 21:10:53)

Trixie987
Scratcher
100+ posts

Pong Direction Change

What about bouncing the ball. How would you bounce it without toching the edge and getting lower and lower?
Trixie987
Scratcher
100+ posts

Pong Direction Change

Trixie987 wrote:

What about bouncing the ball. How would you bounce it without toching the edge and getting lower and lower?
Actually forget that. I can't edit it anymore. But how would you skim it across the paddle?
Jlerpy
Scratcher
1000+ posts

Pong Direction Change

Wait, why can't you edit it any more?
bgvikings08
Scratcher
100+ posts

Pong Direction Change

Trixie987 wrote:

Trixie987 wrote:

What about bouncing the ball. How would you bounce it without toching the edge and getting lower and lower?
Actually forget that. I can't edit it anymore. But how would you skim it across the paddle?
I don't understand what you want. Im sorry…
Trixie987
Scratcher
100+ posts

Pong Direction Change

Jlerpy wrote:

Wait, why can't you edit it any more?
I don't know. The button disappeared
Trixie987
Scratcher
100+ posts

Pong Direction Change

bgvikings08 wrote:

Trixie987 wrote:

Trixie987 wrote:

What about bouncing the ball. How would you bounce it without toching the edge and getting lower and lower?
Actually forget that. I can't edit it anymore. But how would you skim it across the paddle?
I don't understand what you want. Im sorry…
Say the ball touched the edge. Instead of going the completely opposite way, how would you make it so the ball continues going forwards but the height changes. Like if you skim a rock it continues going forward but jumps up.
If you don't know what i mean just forget about it. I can't explain it any more than this.
MysteriousCoder093
Scratcher
37 posts

Pong Direction Change

repeat until <(direction) = [180]>
turn cw (5) degrees
end

Last edited by MysteriousCoder093 (May 15, 2022 22:35:03)

RT_Borg
Scratcher
1000+ posts

Pong Direction Change

Hi Trixie987,

Trixie987 wrote:

Actually forget that. I can't edit it anymore.

Everyone, I'm pretty sure Trixie987 meant “edit the forum post any more”. New scratchers (like me and Trixie987) can only edit a post right after Submit.

Trixie987 wrote:

Say the ball touched the edge. Instead of going the completely opposite way, how would you make it so the ball continues going forwards but the height changes. Like if you skim a rock it continues going forward but jumps up..

Yes, you want it to bounce naturally, instead of reversing direction. That was my first thought when I saw the Pong tutorial too. It takes just a little more work, but isn't too bad.

The trick to what you want to do is to keep track of your own variables for “x_velocity” (horizontal speed) and “y_velocity” (vertical speed). In a movement loop (forever) you can move the ball by updating these:

forever
change x by (x_velocity)
change y by (y_velocity)
end

and elsewhere, change x_velocity or y_velocity when you hit walls or the paddle. (So not using “direction” or “if on edge, bounce” at all.)

There's fancy math and physics engines if you have weird shapes and angles, but for vertical and horizontal obstacles you don't need the fancy stuff.

You're thinking about the problem in exactly the right way when you say “the ball continues going forwards but the height changes”. To state the thought in terms of these variables: When you bounce off the paddle, you want the x_velocity to stay the same, but you want the y_velocity to to be the same speed in the opposite direction:

forever
if <touching [paddle v] ?> then
set [y_velocity v] to ((0) - (y_velocity)) // and no change to x_velocity
end
end

Because you're keeping track of your own variables for x_velocity and y_velocity, you won't want to just bounce off the top and edges of the screen (because “if on edge, bounce” won't update your x_velocity and y_velocity when the motion of the ball changes).

So you can put your own wall sprites on the top, left, right and bottom and reflect off them just like you do with the paddle.

Horizontal surfaces like top (and bottom if you don't just stop when it lands) are just like bouncing off a paddle. Vertical surfaces (left and right walls) are similar, but you want x_velocity to become - x_velocity and leave y_velocity unchanged.

I made a sample project that shows this technique: https://scratch.mit.edu/projects/690905887

I hope this helps. Happy to answer any questions.

- RT_Borg
Trixie987
Scratcher
100+ posts

Pong Direction Change

RT_Borg wrote:

Hi Trixie987,

Trixie987 wrote:

Actually forget that. I can't edit it anymore.

Everyone, I'm pretty sure Trixie987 meant “edit the forum post any more”. New scratchers (like me and Trixie987) can only edit a post right after Submit.

Trixie987 wrote:

Say the ball touched the edge. Instead of going the completely opposite way, how would you make it so the ball continues going forwards but the height changes. Like if you skim a rock it continues going forward but jumps up..

Yes, you want it to bounce naturally, instead of reversing direction. That was my first thought when I saw the Pong tutorial too. It takes just a little more work, but isn't too bad.

The trick to what you want to do is to keep track of your own variables for “x_velocity” (horizontal speed) and “y_velocity” (vertical speed). In a movement loop (forever) you can move the ball by updating these:

forever
change x by (x_velocity)
change y by (y_velocity)
end

and elsewhere, change x_velocity or y_velocity when you hit walls or the paddle. (So not using “direction” or “if on edge, bounce” at all.)

There's fancy math and physics engines if you have weird shapes and angles, but for vertical and horizontal obstacles you don't need the fancy stuff.

You're thinking about the problem in exactly the right way when you say “the ball continues going forwards but the height changes”. To state the thought in terms of these variables: When you bounce off the paddle, you want the x_velocity to stay the same, but you want the y_velocity to to be the same speed in the opposite direction:

forever
if <touching [paddle v] ?> then
set [y_velocity v] to ((0) - (y_velocity)) // and no change to x_velocity
end
end

Because you're keeping track of your own variables for x_velocity and y_velocity, you won't want to just bounce off the top and edges of the screen (because “if on edge, bounce” won't update your x_velocity and y_velocity when the motion of the ball changes).

So you can put your own wall sprites on the top, left, right and bottom and reflect off them just like you do with the paddle.

Horizontal surfaces like top (and bottom if you don't just stop when it lands) are just like bouncing off a paddle. Vertical surfaces (left and right walls) are similar, but you want x_velocity to become - x_velocity and leave y_velocity unchanged.

I made a sample project that shows this technique: https://scratch.mit.edu/projects/690905887

I hope this helps. Happy to answer any questions.

- RT_Borg
Thanks man! The rest of you also thanks for reminding me that i needed to explain more and some of you solutions works but not in skimming.
Thanks!

Powered by DjangoBB