Discuss Scratch

PoIygon
Scratcher
1000+ posts

How do i make it so it doesnt do this?

when im playing my game (633465485) there is a bug where sometimes the ball goes into the paddle while the paddle is moving and it starts having a stroke.

how do i fix this?










awesome-llama
Scratcher
1000+ posts

How do i make it so it doesnt do this?

One easy way to fix this is to make sure that the rotation blocks only run a single time, not constantly (which keeps the ball stuck in the paddle). You can split up the scripts so that you have 2 forever loops - this new script will only handle the ball rotation (and detect when it should happen). If you add a wait until block after the rotation, you can have the script wait until the paddle is no longer touching the ball so it will never get stuck.

There is one problem which I found when testing it - if the ball goes towards the paddle at a shallow angle, almost parallel to it, the bounce may send it in the wrong direction and aligned with the paddle and the wait until will not let another bounce happen again. In this situation, the ball will exit the circle and you will lose.
My solution is to instead have the ball use a different script to rotate - have it first point towards the centre of the screen, then add some random variation for difficulty. A bounce will always send it somewhere near the centre of the screen.
For pointing towards the centre, you can use trigonometry, or if you want to be lazy, use a sprite at 0,0 and point to that sprite instead.

Here are the scripts that I used:



Additional note: Now the original forever loop can be replaced with a repeat until (touching edge).


PoIygon
Scratcher
1000+ posts

How do i make it so it doesnt do this?

awesome-llama wrote:

One easy way to fix this is to make sure that the rotation blocks only run a single time, not constantly (which keeps the ball stuck in the paddle). You can split up the scripts so that you have 2 forever loops - this new script will only handle the ball rotation (and detect when it should happen). If you add a wait until block after the rotation, you can have the script wait until the paddle is no longer touching the ball so it will never get stuck.

There is one problem which I found when testing it - if the ball goes towards the paddle at a shallow angle, almost parallel to it, the bounce may send it in the wrong direction and aligned with the paddle and the wait until will not let another bounce happen again. In this situation, the ball will exit the circle and you will lose.
My solution is to instead have the ball use a different script to rotate - have it first point towards the centre of the screen, then add some random variation for difficulty. A bounce will always send it somewhere near the centre of the screen.
For pointing towards the centre, you can use trigonometry, or if you want to be lazy, use a sprite at 0,0 and point to that sprite instead.

Here are the scripts that I used:



Additional note: Now the original forever loop can be replaced with a repeat until (touching edge).
Ok thanks










PoIygon
Scratcher
1000+ posts

How do i make it so it doesnt do this?

awesome-llama wrote:

One easy way to fix this is to make sure that the rotation blocks only run a single time, not constantly (which keeps the ball stuck in the paddle). You can split up the scripts so that you have 2 forever loops - this new script will only handle the ball rotation (and detect when it should happen). If you add a wait until block after the rotation, you can have the script wait until the paddle is no longer touching the ball so it will never get stuck.

There is one problem which I found when testing it - if the ball goes towards the paddle at a shallow angle, almost parallel to it, the bounce may send it in the wrong direction and aligned with the paddle and the wait until will not let another bounce happen again. In this situation, the ball will exit the circle and you will lose.
My solution is to instead have the ball use a different script to rotate - have it first point towards the centre of the screen, then add some random variation for difficulty. A bounce will always send it somewhere near the centre of the screen.
For pointing towards the centre, you can use trigonometry, or if you want to be lazy, use a sprite at 0,0 and point to that sprite instead.

Here are the scripts that I used:



Additional note: Now the original forever loop can be replaced with a repeat until (touching edge).
The script that you use doesn’t work. It makes the ball go through the paddle










deck26
Scratcher
1000+ posts

How do i make it so it doesnt do this?

Two quick comments.

In my opinion a ball should bounce off the paddle at a natural angle which depends on whether the paddle is vertical or horizontal - see https://scratch.mit.edu/projects/99412537 - rather than just turning 180 degrees or some random amount. Adding or subtracting a small random amount from the natural bounce is the most you should do and perhaps only if a certain distance from the centre of the paddle. I accept that's my opinion and others may not agree but it does give the player some control over the bounce.

Secondly, to avoid problems with the ball sticking in the paddle. This is often caused by the ball not being centred properly or the change in direction needing a move or more than one move to leave the paddle - so the paddle contact is detected more than once. Simple fix, for a horizontal paddle at the bottom of the screen (for example), is to only change direction if touching the paddle and the abs value of the direction is > 90. So the ball only bounces off the paddle if moving downwards (any angle below horizontal). That can be modified for a paddle on any side of the playing area - eg for a paddle on the right you only bounce if touching and direction > 0. That should always give a clean bounce.
awesome-llama
Scratcher
1000+ posts

How do i make it so it doesnt do this?

PoIygon wrote:

The script that you use doesn’t work. It makes the ball go through the paddle
But the ball does bounce toward the centre, right? It never gets stuck?

If you want the edge of the paddle to make the ball bounce off it realistically, it requires a bit more work. I recommend looking at Griffpatch's ball bounce physics tutorial. There's a part in it which explains how you can do correct reflections: https://www.youtube.com/watch?v=Ge1DbXkyMKo


Powered by DjangoBB