### [Pong game] How do I make the ball bounce off with an unpredictable angle?

#### Bhoopalan

• Posts: 1019
I just did it like this. The paddle is on the bottom of my screen, facing up. That is why the velocity angles are between 0 and -180.
I don't really understand the code, what does the "[x of self]+[number]>[x-center] of [other actor]" means?

[x of self] means the x position in screen of the actor.

I'll give example for [x-center] of [other actor].

Say the other actor is 40 px (x) x 40 px (y) . The screen is 320 px (x) x 480 px (y). x center of other actor is 20 px from the x of the actor.

Say the other actor is placed at origin of the screen (0,0). x center of the other actor would then be 20 px.

Did I make myself clear?
If I helped you at anytime, help me back build my twitter followers

#### treaxen

• Posts: 29

[x of self] means the x position in screen of the actor.

I'll give example for [x-center] of [other actor].

Say the other actor is 40 px (x) x 40 px (y) . The screen is 320 px (x) x 480 px (y). x center of other actor is 20 px from the x of the actor.

Say the other actor is placed at origin of the screen (0,0). x center of the other actor would then be 20 px.

Did I make myself clear?
So if I refer to ceosol's blocks, for his first line, does it translate to "if the x position of the center of the ball is more than the x position of the paddle+49, which which is the 100th pixel of the paddle(given that his paddle is >=pixel wide), does it mean this way?

#### ceosol

• Posts: 2275
So if I refer to ceosol's blocks, for his first line, does it translate to "if the x position of the center of the ball is more than the x position of the paddle+49, which which is the 100th pixel of the paddle(given that his paddle is >=pixel wide), does it mean this way?

My paddle is 128 pixels wide. The x-center of the paddle is at pixel 64. The xcenter+49 is pixel 113. So basically, the first line is saying:

"If the center of the ball lands between pixel 113 and 128 of the paddle,
set the ball's velocity direction to -15 degree".

#### treaxen

• Posts: 29
it didn't work for me... the ball goes right through the paddle (sensor's on, but it worked fine when i was using the basic reflection mechanics)

• Posts: 733

#### treaxen

• Posts: 29
mind elaborating on how it was done?

• Posts: 733

#### treaxen

• Posts: 29
http://www.stencyl.com/help/view/collisions-and-groups/

Go to the "shapes" section here.
I don't really understand how do I change the collision box's shape at the middle of the game..

#### yoplalala

• Posts: 1611
Quote
it didn't work for me... the ball goes right through the paddle (sensor's on, but it worked fine when i was using the basic reflection mechanics)

you have to enable continuous collision detection

http://www.stencyl.com/help/view/continuous-collision-detection/

And don't put sensor ! Sensor  is when you don't want your actor to physically reac ( http://www.stencyl.com/help/view/collisions-and-groups/    sensors part)'

#### tigerteeth

• Posts: 733
http://www.stencyl.com/help/view/collisions-and-groups/

Go to the "shapes" section here.
I don't really understand how do I change the collision box's shape at the middle of the game..

Create 5 or 6 different animations with the same image, but give each one a different collision polygon. Then, in an "created" event, put the code "every 1 seconds change animation to..." etc.

#### treaxen

• Posts: 29
Quote
it didn't work for me... the ball goes right through the paddle (sensor's on, but it worked fine when i was using the basic reflection mechanics)

you have to enable continuous collision detection

http://www.stencyl.com/help/view/continuous-collision-detection/

And don't put sensor ! Sensor  is when you don't want your actor to physically reac ( http://www.stencyl.com/help/view/collisions-and-groups/    sensors part)'

Without sensor, the moment it hits something the velocity(speed) goes down to zero. For my bounding area and destructable blocks, I used the command [negate[x-speed]], so if the velo goes down to zero, the x-speed will be zero as well. Which means that the ball won;t bounce....

#### treaxen

• Posts: 29
http://www.stencyl.com/help/view/collisions-and-groups/

Go to the "shapes" section here.
I don't really understand how do I change the collision box's shape at the middle of the game..

Create 5 or 6 different animations with the same image, but give each one a different collision polygon. Then, in an "created" event, put the code "every 1 seconds change animation to..." etc.
So you mean to create polygons that has bumps or slants?

#### ceosol

• Posts: 2275
I would suggest taking a step back from making this game. Go play around with making some basic prototypes. Once you have a better understanding of how the mechanics work, you can come back to this game a bang it out in 1/100th of the time.

#### tigerteeth

• Posts: 733
http://www.stencyl.com/help/view/collisions-and-groups/

Go to the "shapes" section here.
I don't really understand how do I change the collision box's shape at the middle of the game..

Create 5 or 6 different animations with the same image, but give each one a different collision polygon. Then, in an "created" event, put the code "every 1 seconds change animation to..." etc.
So you mean to create polygons that has bumps or slants?

CEOsol is right, you'd be able to solve this problem really quickly if you had a better understanding of the basics of Stencyl. The only problem is, getting that basic understanding has taken me about 6 months....

But, yes, the most intuitive way of solving this problem would be to create a slanted or curved or jagged paddle. Collisions are supposed to be automatic, you shouldn't have to do all this maths and coding for such a "simple" problem (i suppose it depends how unpredictable you want your bouncing to be, but...)

#### ceosol

• Posts: 2275
But, yes, the most intuitive way of solving this problem would be to create a slanted or curved or jagged paddle. Collisions are supposed to be automatic, you shouldn't have to do all this maths and coding for such a "simple" problem (i suppose it depends how unpredictable you want your bouncing to be, but...)

The problem with doing that is that a moving paddle (e.g. the player moving the paddle at the same moment of deflection) can cause unwanted side effects upon impact. A standardized deflection pattern minimizes the chance of those unwanted occurrences.