If I have two circles (pool balls) and I know where I want one of them (the target ball) to end up (in a pocket) by whacking the other one (the cue ball) into it, is there a reasonable way of determining the impulse required to apply to the second circle (the cue ball) to hit it into the first (the target) to make it move towards the given point (the pocket)?
I have something set up that works, for the most part. It calculates all the right angles and offsets, but as the angle between the cue ball, target ball, and pocket deviates from 180 degrees, it gets less and less accurate. It's still somewhat accurate, as it seems that the angle is off by just a few degrees, and if I adjust it manually by only a little bit, I can still get the ball into the pocket.
At this point, I'm not sure if this is due to angular velocity, or friction, (i.e. it's not shooting off in the exact direction I calculated) or just some inaccuracy when calculating the angle to shoot at (i.e. intermediate double precision floating point values don't hold enough precision to keep the angle precise enough).
Does anybody have any suggestions to tweak this calculation to make it better? Note that I'm using doubles wherever I can.
I have something set up that works, for the most part. It calculates all the right angles and offsets, but as the angle between the cue ball, target ball, and pocket deviates from 180 degrees, it gets less and less accurate. It's still somewhat accurate, as it seems that the angle is off by just a few degrees, and if I adjust it manually by only a little bit, I can still get the ball into the pocket.
At this point, I'm not sure if this is due to angular velocity, or friction, (i.e. it's not shooting off in the exact direction I calculated) or just some inaccuracy when calculating the angle to shoot at (i.e. intermediate double precision floating point values don't hold enough precision to keep the angle precise enough).
Does anybody have any suggestions to tweak this calculation to make it better? Note that I'm using doubles wherever I can.
Code: Select all
private static Vec2 sideA, sideB, sideC;
public double testBallForPockets(Vec2 cuePos, Vec2 ballPos, Vec2 pocketPos) {
sideA.set(cuePos);
sideA.subLocal(ballPos);
sideB.set(pocketPos);
sideB.subLocal(ballPos);
sideC.set(pocketPos);
sideC.subLocal(cuePos);
// Apply rule of cosine to find angle created by the cue ball, the object ball, and the pocket
double cosAngle = getCosAngleC(ls(sideA), ls(sideB),ls(sideC));
if (0 <= cosAngle && cosAngle <= 1) {
continue;
}
double angle = MathUtilities.acos(cosAngle);
double angleOffset = Math.PI - angle;
if (sideA.x * sideB.y - sideA.y * sideB.x < 0) {
angleOffset = -angleOffset;
}
//Add this angle to the angle required to shoot the cue ball straight at the target ball to get the target ball to travel in the desired direction
return angleOffset;
}
private static double ls(Vec2 v) {
double x = v.x, y = v.y;
return x * x + y*y;
}
private static double getCosAngleC(double sideASquared, double sideBSquared, double sideCSquared) {
double sideA = Math.sqrt(sideASquared);
double sideB = Math.sqrt(sideBSquared);
double numerator = sideASquared + sideBSquared - sideCSquared;
double denominator = 2 * sideA * sideB;
double cosAngleC = numerator / denominator;
return cosAngleC;
}
private static double getSinAngleB(double sideA, double sideB, double angleC) {
double sideCSquared = sideA * sideA + sideB * sideB - 2 * sideA * sideB * Math.cos(angleC);
double sideC = Math.sqrt(sideCSquared);
double sinAngleB = sideB * Math.sin(angleC) / sideC;
return sinAngleB;
}
private static void takeShot(Vec2 cuePos, Vec2 ballPos, double angleOffset) {
Vec2 v = new Vec2(ballPos);
v.subLocal(cuePos);
double sin = getSinAngleB(Math.sqrt(v.x*v.x+v.y*v.y),BALL_RADIUS * 2,angleOffset);
double cos = Math.sqrt(1-sin*sin);
v.set((float)(v.x * cos - v.y * sin),(float)(v.x * sin + v.y * cos));
v.normalize();
v.mulLocal(4.375f);
cueBall.applyImpulse(impulse, cuePos);
}