Equation for Ball Deflection at Corners in a Breakout Game

  • Context: High School 
  • Thread starter Thread starter Kiltedboy
  • Start date Start date
  • Tags Tags
    Ball Deflection
Click For Summary

Discussion Overview

The discussion revolves around determining an equation for the deflection of a ball in a breakout game when it collides with blocks or paddles. Participants explore the mechanics of collision angles and how to implement these in a programming context, specifically using Scratch. The focus is on ensuring that the angle of the ball after a collision is equal and opposite to its angle before the collision, applicable to all sides of the blocks.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant suggests defining the sides of the blocks as "edges" to reuse existing functions for collision detection.
  • Another participant proposes that if the ball's velocity has horizontal and vertical components, only one component needs to be inverted depending on which edge is hit: the horizontal component for vertical edges and the vertical component for horizontal edges.
  • A different viewpoint emphasizes that the angle of incidence equals the angle of reflection, indicating that the angle at which the ball approaches a block will be the same as the angle at which it deflects.
  • One participant discusses the possibility of modifying the angle of deflection when the ball hits the corners of blocks or paddles, suggesting the use of the radius of curvature and the ball's radius to calculate the new angle.
  • A proposed mathematical approach includes using the atan2 function to determine the angle based on the ball's position relative to the corner, followed by equations to calculate the final velocity components after a corner collision.

Areas of Agreement / Disagreement

Participants express various approaches to handling collisions, with no consensus on a single equation or method that works universally for all scenarios. Multiple competing views on how to handle angles and velocities remain present throughout the discussion.

Contextual Notes

Some limitations include the dependence on how angles are defined within the programming environment and the need for clarity on the implementation of the proposed mathematical functions. There are also unresolved aspects regarding the handling of corner collisions and the potential need for adjustments to the proposed equations.

Kiltedboy
Messages
1
Reaction score
0
Hi First Post so go easy.

I am teaching a programming course for kids between 10-14.

We are using scratch which is a sprite animator.

We are trying to make a breakout game (you know the one where you break block with a ball)

We have a ball sprite that we can easily bounce around the screen as there is a build in function called "If on edge,bounce".

The issue we are having is when the ball collides with another sprite (a block or the paddle).

We would like to know if there is an equation that would work for all collisions so that,

The angle of the ball after the collision is equal and opposite to the angle of the ball before the collision.

e.g. If the ball is moving from left to right and up the screen and collides with block at a 25 degree angle, it continues left to right but leaves the block at a 25 degree angle but is now moving in a downward direction.

Here is what we know ,

The direction of the sprite in degrees. This is measured between (-179 and 180)

0 degrees is North (up)
90 degrees is East (right)
180 degrees is South (down)
-90 degrees is West (left)

We can turn a sprite either left or right by a number of degrees

So in my 25 degree example above the spirtes direction would be 25 degrees (NE) before the collision and 115 degrees (SE) after the collision,

If the sprite were to be traveling right to left :

Its direction would be -25 degrees (NW) before the collision and -115 degrees (SW) after the collision

Can one of you Physics Geniuses come up with an equation that would work for all collisions?

i.e. on all 4 sides of the block from all possible directions ?

Thanks in advance,

Drumbrae Coder Dojo Pupils
 
Physics news on Phys.org
Can you define the sides of the blocks as "edges" and reuse the function you already have?
 
If the velocity of the ball has horizontal and vertical components then I think you only need to change the sign of one of them depending on the orientation of the face it hits. eg if it hits a vertical edge then change the sign of the horizontal component. Likewise if it hits a horizontal edge change the sign of the vertical component...
 

Attachments

  • Bounce.jpg
    Bounce.jpg
    10.1 KB · Views: 559
It would have to depend on what you have defined as 180 and 0 degrees. The only thing you need to know is that angle of incidence is equal to the angle of reflection. The angle the ball comes in on will equal the angle that the ball will deflect. In your program your class definition (I do not know syntax for scratch) you could simple take the supplement or you could just take the opposite.
 
The easiest thing to do is just reflect the horizontal or vertical velocity, depending on which side of the block or paddle was hit. If you want to give the player a little more control, you can change the angle by hitting the corner of a block or paddle. To handle this case, you need the radius of curvature of the corner plus the radius of the ball. In the simple case, only the ball is curved.

Something like the following should work for a corner collision..

R = radius of ball
(x0, y0) = position of ball center
(x1, y1) = position of corner
most programming languages have a function called atan2 which gives the angle based on a vertical and horizontal displacement.
angle = atan2(y1-y0, x1-x0)
(vxi, vyi) = initial velocity of ball
(vxf, vyf) = final velocity of ball
vxf = -vxi*cos(2*angle) + vyi*sin(2*angle)
vyf = vxi*sin(2*angle) + vyi*cos(2*angle)

You might need to fiddle with the signs if I messed them up.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 53 ·
2
Replies
53
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
34
Views
3K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K