2D Collision between Ball and Immovable object(wall) (Elastic collision?)

562 Views Asked by At

So I am making a simple pool game (no ball to ball collisions only ball to wall) and I'm kind of struggling (especially with an actual ball (diameter>0)). (After I figure out the logic and physics I am going to make it on c++ code)

So basically I have a coordinate system with the 4 edges of the pool table, ball coordinates, direction of hit and power of the hit.

So far I've semi-figured out 2 ways of calculating the end position of the ball when it meets a collision with the wall with a ball with 0 diameter:

  1. A 5 formula algorithm combining vectors and lines to figure out the formula of the result line that the ball will travel on when it has collided with the wall. The problem here is that after I got this line I have no idea how to find the exact end position of the ball on this line(coordinate). (I use a perpendicular to the wall line to calculate the result angle at which its gonna bounce and then using some formulas and sin() cos() of that angle I find a and b for the ax+b = y formula of the line. To be more exact I find the perpendicular line formula with a system of 2 equations, then cos() of angle between 2 vectors, then sin() of that same angle to use it again in the same formulas but reversed in order to find the other coordinate, then using the two points I have I make a line)
  2. I basically find where it's gonna hit the wall and since the power and direction of the hit is given I will just have 8 cases where the ball will hit each of the four walls and check which case I'm in and just reverse whatever x and y I need to. The problem with this is that if the ball has a diameter > 0 (so its an actual ball and not just a point) the ball is going to collide with the wall not at its center but at the side of the ball and I have no idea how to find exactly where the ball will collide with the wall and how that will change the line that it's gonna travel on/angle of the collision and the end position respectfully. (This method is likely to work but it would be really hard to implement, because of 8 cases to check which wall and what direction its going to hit it at also calculate the formulas of the lines of the edges and check where its going to hit exactly and a lot of other stuff) EDIT: This is going to be a little different since I'm changing a few things in the original logic.

Examples(I already made this part on code):

Ball is at (10, 10). A hit is given with direction (20,20) and power 1. Output: End Ball Pos = (20, 20)

Ball is at (10, 10). A hit is given with direction (20,20) and power 2. Output: End Ball Pos = (30, 30)

Example with wall being hit(playing field in this case is (0,0);(320,0);(320,160);(0,160):

Ball(300, 60), hit(250, 30), power 3 diameter 0: Output(150,30) (hits wall(200, 0))

Same case, but diameter 10: Output(150, 40) (hits wall(208.3333333, 5))

I don't need to find where it hits the wall only where the ball is going to end up after a hit(multiple wall collision are possible in one hit).

More info(playing field is always 1:2; isn't always parallel to x and y axis; ball can have any diameter; power ranges from 1 - 10; hitting corner is a different case not included in this question; friction is ignored and I I just need to move the ball to the designated position, doesn't mean that it will travel forever)

TL;DR need to find coordinates of ball after a collision with a wall.

Any help is appreciated! Thanks in advance!

1

There are 1 best solutions below

0
On

Going from a point-like ball to a ball with a diameter is quite simple.

(I can't prepare images easily, so I must rely on your imagination.)

You have already experimented with a point-like ball bouncing off a wall. For example, if the wall is at x=W, and the "ball" approaches from the x<W side, with positive x-velocity, it will rebound at x=W and take on negative x-velocity.

Now consider a ball with radius r approaching the same wall, again from the x<W side with positive x-velocity. The collision will occur when the center of the ball is at x=W-r. Notice that the center point of the ball will move exactly as if it were a point-like ball striking a wall at x=W-r.

So to get the motion of a ball with radius r, simply move the walls in a distance r, and calculate the motion of a point-like ball. When you have that, draw a ball of radius r centered on that moving point.