Two-dimensional ball collisions with corners

I am trying to write a 2D simulation of a ball that bounces from fixed vertical and horizontal walls. Modeling collisions with wall faces was pretty simple - just negate the speed X for a vertical wall or the Y-speed for a horizontal wall. The problem is that the ball can also collide with the corners of the walls where a horizontal wall meets a vertical wall. I already understood how to detect when a collision with an angle occurs. My question is how the ball should react to this collision - that is, how its speeds X and Y will change.

Here is a list of what I already know or know how to find:

  • X and Y coordinates of the center of the ball during the frame when a collision is detected
  • X and Y components of ball speed
  • X and Y coordinates of the angle
  • The angle between the center of the ball and the angle
  • The angle at which the ball moves just before the collision
  • The amount at which the ball crosses the angle when a collision is detected.

I suggest that it’s best to pretend that the angle is an infinitely small circle, so I can see the collision between the ball and this circle as if the ball were in contact with a wall that touches the circles tangent to the collision point. It seems to me that all I need to do is rotate the coordinate system to align with this imaginary wall, flip the X-component of the speed of the ball in this system and rotate the coordinates back to the original system. The problem is that I have no idea how to program this.

, . - . Objective-C, .

+3
2

, . :

float nx = ballX - cornerX;
float ny = ballY - cornerY;
const float length = sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;

, :

const float projection = velocityX * nx + velocityY * ny;
velocityX = velocityX - 2 * projection * nx;
velocityY = velocityY - 2 * projection * ny;
+3

, retrorefector , . ( )

+2

Source: https://habr.com/ru/post/1726331/


All Articles