0

So in my intro C++ class I'm building a game of pool. I have everything working well so far. Currently I am figuring out the angle between the two balls upon hit, sending the hit ball off at the angle of impact, and the striking ball off at the appropriate reflect angle. What I would really like is to have glancing hits impart less speed to the ball being hit, and direct hits to impart more speed. My idea that I don't know how to put into code is if the struckBall has the same end angle as the original strikerBall, then it gets something like 90% of it's speed. As the angle changes though, let's say it's 45 degrees off from original angle, then it would be around a 50/50 split of speed at their new angles.

Relevant code:

private: void checkBallCollision(BilliardBall^ striker, BilliardBall^ struck)
                {
                    if (distanceBetweenBalls(striker, struck))
                    {
                        double velocityStrike = 0;
                        double velocityStruck = 0;
                        double struckBallAngle = GetAngle(striker->Location, struck->Location);
                        double strikerBallAngle = GetAngle(struck->Location, striker->Location);
                        striker->Angle = striker->Angle;
                        if (strikerBallAngle > striker->Angle)
                        {
                            striker->Angle = striker->Angle + (1 / (strikerBallAngle - striker->Angle));
                        } 
                        else
                        {
                            striker->Angle = striker->Angle - (1 / (striker->Angle - strikerBallAngle));
                        }
                        struck->Angle = struckBallAngle;


                        double distance = GetDistance(striker->Location, struck->Location);
                        struck->PosX += (BALL_WIDTH - distance) * Math::Cos(struckBallAngle);
                        struck->PosY += (BALL_WIDTH - distance) * Math::Sin(struckBallAngle);

                        struck->Location = Point(struck->PosX, struck->PosY);

                        struck->Distance = striker->Distance * .7;
                        striker->Distance *= .2;
                    }
                }

2 Answers2

1

You lost me at if... That is, 'if' statements have no place in a physics problem. There is a mathematical function (a formula) that will describe this. 'If' you didn't know this already, here are a couple tips:

  1. The two balls will move perpendicular to each other.
  2. One ball will move perpendicular to the tangent line between the two balls, the other will move parallel.

http://www.real-world-physics-problems.com/physics-of-billiards.html

neurotik
  • 1,390
  • 11
  • 15
0

It is a often repeated fallacy that you need to compute the collision angle for elastic collisions. It makes for a nice geometric exploration of the situation, but the actual formula only requires the center coordinates and velocity vectors (and mass and radii) at the moment of collision, see my answer https://stackoverflow.com/a/28252655/3088138 to a similar problem or the wikipedia page on elastic collisions.

Community
  • 1
  • 1
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51