Calculating trajectory

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.

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

There's a couple of reasons why this might be off:

* Friction. You can set friction to 0, but that won't be very realistic, as a lot of shots involve spin
* Restitution. This doesn't really have to do with the angle though
* To make the engine operate in real-time, the physics is approximated and estimated, so the collisions aren't going to exactly follow correct math trajectories. For more accurate results decrease the timestep and increase position/velocity iterations
Hi durnurd! This is an old thread but EXACTLY the same problem I'm having with my pool simulator! I have a sneaking suspicion this is the result of not getting the exact point of collision since time is stepped and not continuous but its a difficult problem to isolate. I'm worried that to get a perfectly accurate system a custom physics implementation has to be designed specifically for this scenario but I'm trying to avoid that.

Can you please tell me if you were able to clear this up and what you did??
Yeah, sounds like it is caused by the cue ball moving in discrete steps, instead of continuous movement.
Since you know the time step length, you could adjust the speed of the cue ball a little to make sure that one of those step positions along the way will put it exactly where you want it.
Box2D is a pretty bad pool simulator, due to a lot of the approximations it uses (the circle shapes don't bounce off at quite the right angles). Circle-circle and circle-plane discrete collisions are very easy to do yourself, I would suggest just rolling your own physics 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=5300