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;
}
}