+static double
+find_bounce(jugglestruct *sp,
+ double yo, double yf, double yc, double tc, double cor)
+{
+ double tb, i, dy = 0;
+ const double e = 1; /* permissible error in yc */
+
+ /*
+ tb = time to bounce
+ yt = height at catch time after one bounce
+ one or three roots according to timing
+ find one by interval bisection
+ */
+ tb = tc;
+ for(i = tc / 2; i > 0.0001; i/=2){
+ double dt, yt;
+ if(tb == 0){
+ (void) fprintf(stderr, "juggle: bounce div by zero!\n");
+ break;
+ }
+ dy = (yf - yo)/tb + sp->Gr/2*tb;
+ dt = tc - tb;
+ yt = -cor*dy*dt + sp->Gr/2*dt*dt + yf;
+ if(yt < yc + e){
+ tb-=i;
+ }else if(yt > yc - e){
+ tb+=i;
+ }else{
+ break;
+ }
+ }
+ if(dy*THROW_CATCH_INTERVAL < -200) { /* bounce too hard */
+ tb = -1;
+ }
+ return tb;
+}
+
+static Trajectory*
+new_predictor(const Trajectory *t, int start, int finish, double angle)
+{
+ Trajectory *n;
+ ADD_ELEMENT(Trajectory, n, t->prev);
+ if(n == NULL){
+ return NULL;
+ }
+ DUP_OBJECT(n, t);
+ n->divisions = t->divisions;
+ n->type = Ball;
+ n->status = PREDICTOR;
+
+ n->start = start;
+ n->finish = finish;
+ n->angle = angle;
+ return n;
+}
+
+/* Turn abstract timings into physically appropriate object trajectories. */
+static Bool
+projectile(jugglestruct *sp)
+{
+ Trajectory *t;
+ const int yf = 0; /* Floor height */
+
+ for (t = sp->head->next; t != sp->head; t = t->next) {
+ if (t->status != PTHRATCH || t->action != THROW) {
+ continue;
+ } else if (t->balllink == NULL) { /* Zero Throw */
+ t->status = BPREDICTOR;
+ } else if (t->balllink->handlink == NULL) { /* Incomplete */
+ return True;
+ } else if(t->balllink == t->handlink) {
+ /* '2' height - hold on to ball. Don't need to consider
+ flourishes, 'hands' will do that automatically anyway */
+
+ t->type = Full;
+ /* Zero spin to avoid wrist injuries */
+ t->spin = 0;
+ match_spins_on_catch(t, t);
+ t->dx = t->dy = 0;
+ t->status = BPREDICTOR;
+ continue;
+ } else {
+ if (t->posn == '_') { /* Bounce once */
+
+ const int tb = t->start +
+ find_bounce(sp, t->y, (double) yf, t->balllink->y,
+ (double) (t->balllink->start - t->start),
+ ObjectDefs[t->object->type].cor);
+
+ if(tb < t->start) { /* bounce too hard */
+ t->posn = '+'; /* Use regular throw */
+ } else {
+ Trajectory *n; /* First (throw) trajectory. */
+ double dt; /* Time span of a trajectory */
+ double dy; /* Distance span of a follow-on trajectory.
+ First trajectory uses t->dy */
+ /* dx is constant across both trajectories */
+ t->dx = (t->balllink->x - t->x) / (t->balllink->start - t->start);
+
+ { /* ball follows parabola down */
+ n = new_predictor(t, t->start, tb, t->angle);
+ if(n == NULL) return False;
+ dt = n->finish - n->start;
+ /* Ball rate 4, no flight or matching club turns */
+ n->spin = spinrate(t->object->type, t, 0.0, dt, 4, 0, 0.0);
+ t->dy = (yf - t->y)/dt - sp->Gr/2*dt;
+ makeParabola(n, t->x, t->dx, t->y, t->dy, sp->Gr);
+ }
+
+ { /* ball follows parabola up */
+ Trajectory *m = new_predictor(t, n->finish, t->balllink->start,
+ end_spin(n));
+ if(m == NULL) return False;
+ dt = m->finish - m->start;
+ /* Use previous ball rate, no flight club turns */
+ m->spin = spinrate(t->object->type, t, n->spin, dt, 0, 0,
+ t->balllink->angle - m->angle);
+ match_spins_on_catch(t, m);
+ dy = (t->balllink->y - yf)/dt - sp->Gr/2 * dt;
+ makeParabola(m, t->balllink->x - t->dx * dt,
+ t->dx, (double) yf, dy, sp->Gr);
+ }
+
+ t->status = BPREDICTOR;
+ continue;
+ }
+ } else if (t->posn == 'k') { /* Drop & Kick */
+ Trajectory *n; /* First (drop) trajectory. */
+ Trajectory *o; /* Second (rest) trajectory */
+ Trajectory *m; /* Third (kick) trajectory */
+ const int td = t->start + 2*THROW_CATCH_INTERVAL; /* Drop time */
+ const int tk = t->balllink->start - 5*THROW_CATCH_INTERVAL; /* Kick */
+ double dt, dy;
+
+ { /* Fall to ground */
+ n = new_predictor(t, t->start, td, t->angle);
+ if(n == NULL) return False;
+ dt = n->finish - n->start;
+ /* Ball spin rate 4, no flight club turns */
+ n->spin = spinrate(t->object->type, t, 0.0, dt, 4, 0,
+ t->balllink->angle - n->angle);
+ t->dx = (t->balllink->x - t->x) / dt;
+ t->dy = (yf - t->y)/dt - sp->Gr/2*dt;
+ makeParabola(n, t->x, t->dx, t->y, t->dy, sp->Gr);
+ }
+
+ { /* Rest on ground */
+ o = new_predictor(t, n->finish, tk, end_spin(n));
+ if(o == NULL) return False;
+ o->spin = 0;
+ makeParabola(o, t->balllink->x, 0.0, (double) yf, 0.0, 0.0);
+ }
+
+ /* Kick up */
+ {
+ m = new_predictor(t, o->finish, t->balllink->start, end_spin(o));
+ if(m == NULL) return False;
+ dt = m->finish - m->start;
+ /* Match receiving hand, ball rate 4, one flight club turn */
+ m->spin = spinrate(t->object->type, t->balllink->handlink, 0.0, dt,
+ 4, 1, t->balllink->angle - m->angle);
+ match_spins_on_catch(t, m);
+ dy = (t->balllink->y - yf)/dt - sp->Gr/2 * dt;
+ makeParabola(m, t->balllink->x, 0.0, (double) yf, dy, sp->Gr);
+ }
+
+ t->status = BPREDICTOR;
+ continue;
+ }
+
+ /* Regular flight, no bounce */
+ { /* ball follows parabola */
+ double dt;
+ Trajectory *n = new_predictor(t, t->start,
+ t->balllink->start, t->angle);
+ if(n == NULL) return False;
+ dt = t->balllink->start - t->start;
+ /* Regular spin */
+ n->spin = spinrate(t->object->type, t, 0.0, dt, t->height, t->height/2,
+ t->balllink->angle - n->angle);
+ match_spins_on_catch(t, n);
+ t->dx = (t->balllink->x - t->x) / dt;
+ t->dy = (t->balllink->y - t->y) / dt - sp->Gr/2 * dt;
+ makeParabola(n, t->x, t->dx, t->y, t->dy, sp->Gr);
+ }
+
+ t->status = BPREDICTOR;
+ }
+ }
+ return True;
+}
+
+/* Turn abstract hand motions into cubic splines. */
+static void
+hands(jugglestruct *sp)
+{
+ Trajectory *t, *u, *v;
+
+ for (t = sp->head->next; t != sp->head; t = t->next) {
+ /* no throw => no velocity */
+ if (t->status != BPREDICTOR) {
+ continue;
+ }
+
+ u = t->handlink;
+ if (u == NULL) { /* no next catch */
+ continue;
+ }
+ v = u->handlink;
+ if (v == NULL) { /* no next throw */
+ continue;
+ }
+
+ /* double spline takes hand from throw, thru catch, to
+ next throw */
+
+ t->finish = u->start;
+ t->status = PREDICTOR;
+
+ u->finish = v->start;
+ u->status = PREDICTOR;
+
+
+ /* FIXME: These adjustments leave a small glitch when alternating
+ balls and clubs. Just hope no-one notices. :-) */
+
+ /* make sure empty hand spin matches the thrown object in case it
+ had a handle */
+
+ t->spin = ((t->hand == LEFT)? -1 : 1 ) *
+ fabs((u->angle - t->angle)/(u->start - t->start));
+
+ u->spin = ((v->hand == LEFT) ^ (v->posn == '+')? -1 : 1 ) *
+ fabs((v->angle - u->angle)/(v->start - u->start));
+
+ (void) makeSplinePair(&t->xp, &u->xp,
+ t->x, t->dx, t->start,
+ u->x, u->start,
+ v->x, v->dx, v->start);
+ (void) makeSplinePair(&t->yp, &u->yp,
+ t->y, t->dy, t->start,
+ u->y, u->start,
+ v->y, v->dy, v->start);
+
+ t->status = PREDICTOR;
+ }
+}
+
+/* Given target x, y find_elbow puts hand at target if possible,
+ * otherwise makes hand point to the target */
+static void
+find_elbow(int armlength, DXPoint *h, DXPoint *e, DXPoint *p, DXPoint *s,
+ int z)
+{
+ double r, h2, t;
+ double x = p->x - s->x;
+ double y = p->y - s->y;
+ h2 = x*x + y*y + z*z;
+ if (h2 > 4 * armlength * armlength) {
+ t = armlength/sqrt(h2);
+ e->x = t*x + s->x;
+ e->y = t*y + s->y;
+ h->x = 2 * t * x + s->x;
+ h->y = 2 * t * y + s->y;
+ } else {
+ r = sqrt((double)(x*x + z*z));
+ t = sqrt(4 * armlength * armlength / h2 - 1);
+ e->x = x*(1 + y*t/r)/2 + s->x;
+ e->y = (y - r*t)/2 + s->y;
+ h->x = x + s->x;
+ h->y = y + s->y;
+ }