0
\$\begingroup\$

I am trying to figure out the velocity of ball A after collision, which is V2.A as seen in the figure. I know that the velocity of ball A is perpendicular to the line connecting centre of ball A and B when the collision occurs.

Diagram of two colliding balls (Image taken from: http://www.real-world-physics-problems.com/physics-of-billiards.html)

The issue is, my V2.A is sometimes flipped in the opposite direction, and I can't seem to figure out the issue here. Here's my V2.A calculation

Vector2 diff = (b - a).normalized;  
Vector2 perpendiular = Vector2.Perpendicular(diff);
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

See Vector2.Perpendicular:

Returns the 2D vector perpendicular to this 2D vector. The result is always rotated 90-degrees in a counter-clockwise direction for a 2D coordinate system where the positive Y axis goes up.

In this case, just judge whether the unsigned angle between the velocity vector of ball A and the result vector is greater than 90 degrees:

Vector2 diff = (b - a).normalized;
Vector2 perpendiular = Vector2.Perpendicular(diff);
if (Vector2.Angle(directionOfA, perpendiular) > 90)
{
    perpendiular = -perpendiular;
}
\$\endgroup\$
1
  • \$\begingroup\$ thanks a lot! that fixed it \$\endgroup\$
    – ahmedkl
    Oct 19, 2022 at 12:40
2
\$\begingroup\$

You can also treat this as a vector projection.

Vector2 collisionAxis = (b - a).normalized;

// Get the component of the velocity vector pointing into B.
float parallelSpeed = Vector2.Dot(collisionAxis, velocityA);

// Subtract that component to leave only the portion perpendicular.
Vector2 remainingVelocity = velocityA - collisionAxis * parallelSpeed;

This method works without computing an angle or branching.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .