2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <SDL_rwops.h>
17 #include <SDL_image.h>
26 #include "geom.h" /* Only for height constants! */
27 #include "base_image.h"
29 #include "base_config.h"
32 #define MAGIC 0x4c4f53af
37 /*---------------------------------------------------------------------------*/
39 static float erp(float t)
41 return 3.0f * t * t - 2.0f * t * t * t;
44 static float derp(float t)
46 return 6.0f * t - 6.0f * t * t;
49 static void sol_body_v(float v[3],
50 const struct s_file *fp,
51 const struct s_body *bp)
53 if (bp->pi >= 0 && fp->pv[bp->pi].f)
55 const struct s_path *pp = fp->pv + bp->pi;
56 const struct s_path *pq = fp->pv + pp->pi;
58 v_sub(v, pq->p, pp->p);
59 v_scl(v, v, 1.0f / pp->t);
61 v_scl(v, v, derp(bp->t / pp->t));
71 void sol_body_p(float p[3],
72 const struct s_file *fp,
73 const struct s_body *bp)
79 const struct s_path *pp = fp->pv + bp->pi;
80 const struct s_path *pq = fp->pv + pp->pi;
82 v_sub(v, pq->p, pp->p);
83 v_mad(p, pp->p, v, erp(bp->t / pp->t));
93 /*---------------------------------------------------------------------------*/
95 static void sol_load_mtrl(FILE *fin, struct s_mtrl *mp)
97 get_array(fin, mp->a, 4);
98 get_array(fin, mp->d, 4);
99 get_array(fin, mp->s, 4);
100 get_array(fin, mp->e, 4);
101 get_array(fin, mp->h, 1);
102 get_index(fin, &mp->fl);
104 fread(mp->f, 1, PATHMAX, fin);
107 static void sol_load_vert(FILE *fin, struct s_vert *vp)
109 get_array(fin, vp->p, 3);
112 static void sol_load_edge(FILE *fin, struct s_edge *ep)
114 get_index(fin, &ep->vi);
115 get_index(fin, &ep->vj);
118 static void sol_load_side(FILE *fin, struct s_side *sp)
120 get_array(fin, sp->n, 3);
121 get_float(fin, &sp->d);
124 static void sol_load_texc(FILE *fin, struct s_texc *tp)
126 get_array(fin, tp->u, 2);
129 static void sol_load_geom(FILE *fin, struct s_geom *gp)
131 get_index(fin, &gp->mi);
132 get_index(fin, &gp->ti);
133 get_index(fin, &gp->si);
134 get_index(fin, &gp->vi);
135 get_index(fin, &gp->tj);
136 get_index(fin, &gp->sj);
137 get_index(fin, &gp->vj);
138 get_index(fin, &gp->tk);
139 get_index(fin, &gp->sk);
140 get_index(fin, &gp->vk);
143 static void sol_load_lump(FILE *fin, struct s_lump *lp)
145 get_index(fin, &lp->fl);
146 get_index(fin, &lp->v0);
147 get_index(fin, &lp->vc);
148 get_index(fin, &lp->e0);
149 get_index(fin, &lp->ec);
150 get_index(fin, &lp->g0);
151 get_index(fin, &lp->gc);
152 get_index(fin, &lp->s0);
153 get_index(fin, &lp->sc);
156 static void sol_load_node(FILE *fin, struct s_node *np)
158 get_index(fin, &np->si);
159 get_index(fin, &np->ni);
160 get_index(fin, &np->nj);
161 get_index(fin, &np->l0);
162 get_index(fin, &np->lc);
165 static void sol_load_path(FILE *fin, struct s_path *pp)
167 get_array(fin, pp->p, 3);
168 get_float(fin, &pp->t);
169 get_index(fin, &pp->pi);
170 get_index(fin, &pp->f);
173 static void sol_load_body(FILE *fin, struct s_body *bp)
175 get_index(fin, &bp->pi);
176 get_index(fin, &bp->ni);
177 get_index(fin, &bp->l0);
178 get_index(fin, &bp->lc);
179 get_index(fin, &bp->g0);
180 get_index(fin, &bp->gc);
183 static void sol_load_coin(FILE *fin, struct s_coin *cp)
185 get_array(fin, cp->p, 3);
186 get_index(fin, &cp->n);
189 static void sol_load_goal(FILE *fin, struct s_goal *zp)
191 get_array(fin, zp->p, 3);
192 get_float(fin, &zp->r);
193 get_index(fin, &zp->s);
194 get_index(fin, &zp->c);
197 static void sol_load_swch(FILE *fin, struct s_swch *xp)
199 get_array(fin, xp->p, 3);
200 get_float(fin, &xp->r);
201 get_index(fin, &xp->pi);
202 get_float(fin, &xp->t0);
203 get_float(fin, &xp->t);
204 get_index(fin, &xp->f0);
205 get_index(fin, &xp->f);
206 get_index(fin, &xp->i);
209 static void sol_load_bill(FILE *fin, struct s_bill *rp)
211 get_index(fin, &rp->fl);
212 get_index(fin, &rp->mi);
213 get_float(fin, &rp->t);
214 get_float(fin, &rp->d);
215 get_array(fin, rp->w, 3);
216 get_array(fin, rp->h, 3);
217 get_array(fin, rp->rx, 3);
218 get_array(fin, rp->ry, 3);
219 get_array(fin, rp->rz, 3);
222 static void sol_load_jump(FILE *fin, struct s_jump *jp)
224 get_array(fin, jp->p, 3);
225 get_array(fin, jp->q, 3);
226 get_float(fin, &jp->r);
229 static void sol_load_ball(FILE *fin, struct s_ball *bp)
231 get_array(fin, bp->e[0], 3);
232 get_array(fin, bp->e[1], 3);
233 get_array(fin, bp->e[2], 3);
234 get_array(fin, bp->p, 3);
235 get_float(fin, &bp->r);
238 static void sol_load_view(FILE *fin, struct s_view *wp)
240 get_array(fin, wp->p, 3);
241 get_array(fin, wp->q, 3);
244 static int sol_load_file(FILE *fin, struct s_file *fp)
250 get_index(fin, &magic);
251 get_index(fin, &version);
253 if (magic != MAGIC || version != SOL_VERSION)
256 get_index(fin, &fp->ac);
257 get_index(fin, &fp->mc);
258 get_index(fin, &fp->vc);
259 get_index(fin, &fp->ec);
260 get_index(fin, &fp->sc);
261 get_index(fin, &fp->tc);
262 get_index(fin, &fp->gc);
263 get_index(fin, &fp->lc);
264 get_index(fin, &fp->nc);
265 get_index(fin, &fp->pc);
266 get_index(fin, &fp->bc);
267 get_index(fin, &fp->cc);
268 get_index(fin, &fp->zc);
269 get_index(fin, &fp->jc);
270 get_index(fin, &fp->xc);
271 get_index(fin, &fp->rc);
272 get_index(fin, &fp->uc);
273 get_index(fin, &fp->wc);
274 get_index(fin, &fp->ic);
277 fp->av = (char *) calloc(fp->ac, sizeof (char));
279 fp->mv = (struct s_mtrl *) calloc(fp->mc, sizeof (struct s_mtrl));
281 fp->vv = (struct s_vert *) calloc(fp->vc, sizeof (struct s_vert));
283 fp->ev = (struct s_edge *) calloc(fp->ec, sizeof (struct s_edge));
285 fp->sv = (struct s_side *) calloc(fp->sc, sizeof (struct s_side));
287 fp->tv = (struct s_texc *) calloc(fp->tc, sizeof (struct s_texc));
289 fp->gv = (struct s_geom *) calloc(fp->gc, sizeof (struct s_geom));
291 fp->lv = (struct s_lump *) calloc(fp->lc, sizeof (struct s_lump));
293 fp->nv = (struct s_node *) calloc(fp->nc, sizeof (struct s_node));
295 fp->pv = (struct s_path *) calloc(fp->pc, sizeof (struct s_path));
297 fp->bv = (struct s_body *) calloc(fp->bc, sizeof (struct s_body));
299 fp->cv = (struct s_coin *) calloc(fp->cc, sizeof (struct s_coin));
301 fp->zv = (struct s_goal *) calloc(fp->zc, sizeof (struct s_goal));
303 fp->jv = (struct s_jump *) calloc(fp->jc, sizeof (struct s_jump));
305 fp->xv = (struct s_swch *) calloc(fp->xc, sizeof (struct s_swch));
307 fp->rv = (struct s_bill *) calloc(fp->rc, sizeof (struct s_bill));
309 fp->uv = (struct s_ball *) calloc(fp->uc, sizeof (struct s_ball));
311 fp->wv = (struct s_view *) calloc(fp->wc, sizeof (struct s_view));
313 fp->iv = (int *) calloc(fp->ic, sizeof (int));
316 fread(fp->av, 1, fp->ac, fin);
318 for (i = 0; i < fp->mc; i++) sol_load_mtrl(fin, fp->mv + i);
319 for (i = 0; i < fp->vc; i++) sol_load_vert(fin, fp->vv + i);
320 for (i = 0; i < fp->ec; i++) sol_load_edge(fin, fp->ev + i);
321 for (i = 0; i < fp->sc; i++) sol_load_side(fin, fp->sv + i);
322 for (i = 0; i < fp->tc; i++) sol_load_texc(fin, fp->tv + i);
323 for (i = 0; i < fp->gc; i++) sol_load_geom(fin, fp->gv + i);
324 for (i = 0; i < fp->lc; i++) sol_load_lump(fin, fp->lv + i);
325 for (i = 0; i < fp->nc; i++) sol_load_node(fin, fp->nv + i);
326 for (i = 0; i < fp->pc; i++) sol_load_path(fin, fp->pv + i);
327 for (i = 0; i < fp->bc; i++) sol_load_body(fin, fp->bv + i);
328 for (i = 0; i < fp->cc; i++) sol_load_coin(fin, fp->cv + i);
329 for (i = 0; i < fp->zc; i++) sol_load_goal(fin, fp->zv + i);
330 for (i = 0; i < fp->jc; i++) sol_load_jump(fin, fp->jv + i);
331 for (i = 0; i < fp->xc; i++) sol_load_swch(fin, fp->xv + i);
332 for (i = 0; i < fp->rc; i++) sol_load_bill(fin, fp->rv + i);
333 for (i = 0; i < fp->uc; i++) sol_load_ball(fin, fp->uv + i);
334 for (i = 0; i < fp->wc; i++) sol_load_view(fin, fp->wv + i);
335 for (i = 0; i < fp->ic; i++) get_index(fin, fp->iv + i);
340 static int sol_load_head(FILE *fin, struct s_file *fp)
345 get_index(fin, &magic);
346 get_index(fin, &version);
348 if (magic != MAGIC || version != SOL_VERSION)
351 get_index(fin, &fp->ac);
354 get_index(fin, &fp->mc);
355 get_index(fin, &fp->vc);
356 get_index(fin, &fp->ec);
357 get_index(fin, &fp->sc);
358 get_index(fin, &fp->tc);
359 get_index(fin, &fp->gc);
360 get_index(fin, &fp->lc);
361 get_index(fin, &fp->nc);
362 get_index(fin, &fp->pc);
363 get_index(fin, &fp->bc);
364 get_index(fin, &fp->cc);
365 get_index(fin, &fp->zc);
366 get_index(fin, &fp->jc);
367 get_index(fin, &fp->xc);
368 get_index(fin, &fp->rc);
369 get_index(fin, &fp->uc);
370 get_index(fin, &fp->wc);
371 get_index(fin, &fp->ic);
373 fseek(fin, 18 * 4, SEEK_CUR);
377 fp->av = (char *) calloc(fp->ac, sizeof (char));
378 fread(fp->av, 1, fp->ac, fin);
384 int sol_load_only_file(struct s_file *fp, const char *filename)
389 if ((fin = fopen(filename, FMODE_RB)))
391 res = sol_load_file(fin, fp);
397 int sol_load_only_head(struct s_file *fp, const char *filename)
402 if ((fin = fopen(filename, FMODE_RB)))
404 res = sol_load_head(fin, fp);
410 /*---------------------------------------------------------------------------*/
412 static void sol_stor_mtrl(FILE *fout, struct s_mtrl *mp)
414 put_array(fout, mp->a, 4);
415 put_array(fout, mp->d, 4);
416 put_array(fout, mp->s, 4);
417 put_array(fout, mp->e, 4);
418 put_array(fout, mp->h, 1);
419 put_index(fout, &mp->fl);
421 fwrite(mp->f, 1, PATHMAX, fout);
424 static void sol_stor_vert(FILE *fout, struct s_vert *vp)
426 put_array(fout, vp->p, 3);
429 static void sol_stor_edge(FILE *fout, struct s_edge *ep)
431 put_index(fout, &ep->vi);
432 put_index(fout, &ep->vj);
435 static void sol_stor_side(FILE *fout, struct s_side *sp)
437 put_array(fout, sp->n, 3);
438 put_float(fout, &sp->d);
441 static void sol_stor_texc(FILE *fout, struct s_texc *tp)
443 put_array(fout, tp->u, 2);
446 static void sol_stor_geom(FILE *fout, struct s_geom *gp)
448 put_index(fout, &gp->mi);
449 put_index(fout, &gp->ti);
450 put_index(fout, &gp->si);
451 put_index(fout, &gp->vi);
452 put_index(fout, &gp->tj);
453 put_index(fout, &gp->sj);
454 put_index(fout, &gp->vj);
455 put_index(fout, &gp->tk);
456 put_index(fout, &gp->sk);
457 put_index(fout, &gp->vk);
460 static void sol_stor_lump(FILE *fout, struct s_lump *lp)
462 put_index(fout, &lp->fl);
463 put_index(fout, &lp->v0);
464 put_index(fout, &lp->vc);
465 put_index(fout, &lp->e0);
466 put_index(fout, &lp->ec);
467 put_index(fout, &lp->g0);
468 put_index(fout, &lp->gc);
469 put_index(fout, &lp->s0);
470 put_index(fout, &lp->sc);
473 static void sol_stor_node(FILE *fout, struct s_node *np)
475 put_index(fout, &np->si);
476 put_index(fout, &np->ni);
477 put_index(fout, &np->nj);
478 put_index(fout, &np->l0);
479 put_index(fout, &np->lc);
482 static void sol_stor_path(FILE *fout, struct s_path *pp)
484 put_array(fout, pp->p, 3);
485 put_float(fout, &pp->t);
486 put_index(fout, &pp->pi);
487 put_index(fout, &pp->f);
490 static void sol_stor_body(FILE *fout, struct s_body *bp)
492 put_index(fout, &bp->pi);
493 put_index(fout, &bp->ni);
494 put_index(fout, &bp->l0);
495 put_index(fout, &bp->lc);
496 put_index(fout, &bp->g0);
497 put_index(fout, &bp->gc);
500 static void sol_stor_coin(FILE *fout, struct s_coin *cp)
502 put_array(fout, cp->p, 3);
503 put_index(fout, &cp->n);
506 static void sol_stor_goal(FILE *fout, struct s_goal *zp)
508 put_array(fout, zp->p, 3);
509 put_float(fout, &zp->r);
510 put_index(fout, &zp->s);
511 put_index(fout, &zp->c);
514 static void sol_stor_swch(FILE *fout, struct s_swch *xp)
516 put_array(fout, xp->p, 3);
517 put_float(fout, &xp->r);
518 put_index(fout, &xp->pi);
519 put_float(fout, &xp->t0);
520 put_float(fout, &xp->t);
521 put_index(fout, &xp->f0);
522 put_index(fout, &xp->f);
523 put_index(fout, &xp->i);
526 static void sol_stor_bill(FILE *fout, struct s_bill *rp)
528 put_index(fout, &rp->fl);
529 put_index(fout, &rp->mi);
530 put_float(fout, &rp->t);
531 put_float(fout, &rp->d);
532 put_array(fout, rp->w, 3);
533 put_array(fout, rp->h, 3);
534 put_array(fout, rp->rx, 3);
535 put_array(fout, rp->ry, 3);
536 put_array(fout, rp->rz, 3);
539 static void sol_stor_jump(FILE *fout, struct s_jump *jp)
541 put_array(fout, jp->p, 3);
542 put_array(fout, jp->q, 3);
543 put_float(fout, &jp->r);
546 static void sol_stor_ball(FILE *fout, struct s_ball *bp)
548 put_array(fout, bp->e[0], 3);
549 put_array(fout, bp->e[1], 3);
550 put_array(fout, bp->e[2], 3);
551 put_array(fout, bp->p, 3);
552 put_float(fout, &bp->r);
555 static void sol_stor_view(FILE *fout, struct s_view *wp)
557 put_array(fout, wp->p, 3);
558 put_array(fout, wp->q, 3);
561 static void sol_stor_file(FILE *fin, struct s_file *fp)
565 int version = SOL_VERSION;
567 put_index(fin, &magic);
568 put_index(fin, &version);
570 put_index(fin, &fp->ac);
571 put_index(fin, &fp->mc);
572 put_index(fin, &fp->vc);
573 put_index(fin, &fp->ec);
574 put_index(fin, &fp->sc);
575 put_index(fin, &fp->tc);
576 put_index(fin, &fp->gc);
577 put_index(fin, &fp->lc);
578 put_index(fin, &fp->nc);
579 put_index(fin, &fp->pc);
580 put_index(fin, &fp->bc);
581 put_index(fin, &fp->cc);
582 put_index(fin, &fp->zc);
583 put_index(fin, &fp->jc);
584 put_index(fin, &fp->xc);
585 put_index(fin, &fp->rc);
586 put_index(fin, &fp->uc);
587 put_index(fin, &fp->wc);
588 put_index(fin, &fp->ic);
590 fwrite(fp->av, 1, fp->ac, fin);
591 for (i = 0; i < fp->mc; i++) sol_stor_mtrl(fin, fp->mv + i);
592 for (i = 0; i < fp->vc; i++) sol_stor_vert(fin, fp->vv + i);
593 for (i = 0; i < fp->ec; i++) sol_stor_edge(fin, fp->ev + i);
594 for (i = 0; i < fp->sc; i++) sol_stor_side(fin, fp->sv + i);
595 for (i = 0; i < fp->tc; i++) sol_stor_texc(fin, fp->tv + i);
596 for (i = 0; i < fp->gc; i++) sol_stor_geom(fin, fp->gv + i);
597 for (i = 0; i < fp->lc; i++) sol_stor_lump(fin, fp->lv + i);
598 for (i = 0; i < fp->nc; i++) sol_stor_node(fin, fp->nv + i);
599 for (i = 0; i < fp->pc; i++) sol_stor_path(fin, fp->pv + i);
600 for (i = 0; i < fp->bc; i++) sol_stor_body(fin, fp->bv + i);
601 for (i = 0; i < fp->cc; i++) sol_stor_coin(fin, fp->cv + i);
602 for (i = 0; i < fp->zc; i++) sol_stor_goal(fin, fp->zv + i);
603 for (i = 0; i < fp->jc; i++) sol_stor_jump(fin, fp->jv + i);
604 for (i = 0; i < fp->xc; i++) sol_stor_swch(fin, fp->xv + i);
605 for (i = 0; i < fp->rc; i++) sol_stor_bill(fin, fp->rv + i);
606 for (i = 0; i < fp->uc; i++) sol_stor_ball(fin, fp->uv + i);
607 for (i = 0; i < fp->wc; i++) sol_stor_view(fin, fp->wv + i);
608 for (i = 0; i < fp->ic; i++) put_index(fin, fp->iv + i);
611 /*---------------------------------------------------------------------------*/
613 int sol_stor(struct s_file *fp, const char *filename)
617 if ((fout = fopen(filename, FMODE_WB)))
619 sol_stor_file(fout, fp);
627 void sol_free(struct s_file *fp)
629 if (fp->mv) free(fp->mv);
630 if (fp->vv) free(fp->vv);
631 if (fp->ev) free(fp->ev);
632 if (fp->sv) free(fp->sv);
633 if (fp->tv) free(fp->tv);
634 if (fp->gv) free(fp->gv);
635 if (fp->lv) free(fp->lv);
636 if (fp->nv) free(fp->nv);
637 if (fp->pv) free(fp->pv);
638 if (fp->bv) free(fp->bv);
639 if (fp->cv) free(fp->cv);
640 if (fp->zv) free(fp->zv);
641 if (fp->jv) free(fp->jv);
642 if (fp->xv) free(fp->xv);
643 if (fp->rv) free(fp->rv);
644 if (fp->uv) free(fp->uv);
645 if (fp->wv) free(fp->wv);
646 if (fp->av) free(fp->av);
647 if (fp->iv) free(fp->iv);
649 memset(fp, 0, sizeof (struct s_file));
652 /*---------------------------------------------------------------------------*/
653 /* Solves (p + v * t) . (p + v * t) == r * r for smallest t. */
655 static float v_sol(const float p[3], const float v[3], float r)
657 float a = v_dot(v, v);
658 float b = v_dot(v, p) * 2.0f;
659 float c = v_dot(p, p) - r * r;
660 float d = b * b - 4.0f * a * c;
662 if (a == 0.0f) return LARGE;
663 if (d < 0.0f) return LARGE;
666 return -b * 0.5f / a;
669 float t0 = 0.5f * (-b - fsqrtf(d)) / a;
670 float t1 = 0.5f * (-b + fsqrtf(d)) / a;
671 float t = (t0 < t1) ? t0 : t1;
673 return (t < 0.0f) ? LARGE : t;
677 /*---------------------------------------------------------------------------*/
680 * Compute the earliest time and position of the intersection of a
681 * sphere and a vertex.
683 * The sphere has radius R and moves along vector V from point P. The
684 * vertex moves along vector W from point Q in a coordinate system
687 static float v_vert(float Q[3],
692 const float v[3], float r)
694 float O[3], P[3], V[3];
701 if (v_dot(P, V) < 0.0f)
712 * Compute the earliest time and position of the intersection of a
713 * sphere and an edge.
715 * The sphere has radius R and moves along vector V from point P. The
716 * edge moves along vector W from point Q in a coordinate system based
717 * at O. The edge extends along the length of vector U.
719 static float v_edge(float Q[3],
725 const float v[3], float r)
729 float du, eu, uu, s, t;
739 v_mad(P, d, u, -du / uu);
740 v_mad(V, e, u, -eu / uu);
743 s = (du + eu * t) / uu;
745 if (0.0f < t && t < LARGE && 0.0f < s && s < 1.0f)
758 * Compute the earlist time and position of the intersection of a
759 * sphere and a plane.
761 * The sphere has radius R and moves along vector V from point P. The
762 * plane oves along vector W. The plane has normal N and is
763 * positioned at distance D from the origin O along that normal.
765 static float v_side(float Q[3],
768 const float n[3], float d,
770 const float v[3], float r)
772 float vn = v_dot(v, n);
773 float wn = v_dot(w, n);
778 float on = v_dot(o, n);
779 float pn = v_dot(p, n);
781 float u = (r + d + on - pn) / (vn - wn);
782 float a = ( d + on - pn) / (vn - wn);
802 /*---------------------------------------------------------------------------*/
805 * Compute the new linear and angular velocities of a bouncing ball.
806 * Q gives the position of the point of impact and W gives the
807 * velocity of the object being impacted.
809 static float sol_bounce(struct s_ball *up,
811 const float w[3], float dt)
813 const float kb = 1.10f;
814 const float ke = 0.70f;
815 const float km = 0.20f;
817 float n[3], r[3], d[3], u[3], vn, wn, xn, yn;
821 /* Find the normal of the impact. */
827 /* Find the new angular velocity. */
830 v_scl(up->w, up->w, -1.0f / (up->r * up->r));
832 /* Find the new linear velocity. */
836 xn = (vn < 0.0f) ? -vn * ke : vn;
837 yn = (wn > 0.0f) ? wn * kb : wn;
841 v_mad(v, v, u, +km * dt);
842 v_mad(v, v, n, xn + yn);
844 v_mad(p, q, n, up->r);
846 /* Return the "energy" of the impact, to determine the sound amplitude. */
848 return fabsf(v_dot(n, d));
851 /*---------------------------------------------------------------------------*/
854 * Compute the states of all switches after DT seconds have passed.
856 static void sol_swch_step(struct s_file *fp, float dt)
860 for (xi = 0; xi < fp->xc; xi++)
862 struct s_swch *xp = fp->xv + xi;
873 do /* Tortoise and hare cycle traverser. */
875 fp->pv[pi].f = xp->f0;
876 fp->pv[pj].f = xp->f0;
891 * Compute the positions of all bodies after DT seconds have passed.
893 static void sol_body_step(struct s_file *fp, float dt)
897 for (i = 0; i < fp->bc; i++)
899 struct s_body *bp = fp->bv + i;
900 struct s_path *pp = fp->pv + bp->pi;
902 if (bp->pi >= 0 && pp->f)
916 * Compute the positions of all balls after DT seconds have passed.
918 static void sol_ball_step(struct s_file *fp, float dt)
922 for (i = 0; i < fp->uc; i++)
924 struct s_ball *up = fp->uv + i;
926 v_mad(up->p, up->p, up->v, dt);
928 if (v_len(up->w) > 0.0f)
935 m_rot(M, w, v_len(up->w) * dt);
937 m_vxfm(e[0], M, up->e[0]);
938 m_vxfm(e[1], M, up->e[1]);
939 m_vxfm(e[2], M, up->e[2]);
941 v_crs(up->e[2], e[0], e[1]);
942 v_crs(up->e[1], e[2], e[0]);
943 v_crs(up->e[0], e[1], e[2]);
945 v_nrm(up->e[0], up->e[0]);
946 v_nrm(up->e[1], up->e[1]);
947 v_nrm(up->e[2], up->e[2]);
952 /*---------------------------------------------------------------------------*/
954 static float sol_test_vert(float dt,
956 const struct s_ball *up,
957 const struct s_vert *vp,
961 return v_vert(T, o, vp->p, w, up->p, up->v, up->r);
964 static float sol_test_edge(float dt,
966 const struct s_ball *up,
967 const struct s_file *fp,
968 const struct s_edge *ep,
975 v_cpy(q, fp->vv[ep->vi].p);
976 v_sub(u, fp->vv[ep->vj].p,
979 return v_edge(T, o, q, u, w, up->p, up->v, up->r);
982 static float sol_test_side(float dt,
984 const struct s_ball *up,
985 const struct s_file *fp,
986 const struct s_lump *lp,
987 const struct s_side *sp,
991 float t = v_side(T, o, w, sp->n, sp->d, up->p, up->v, up->r);
995 for (i = 0; i < lp->sc; i++)
997 const struct s_side *sq = fp->sv + fp->iv[lp->s0 + i];
1002 v_dot(w, sq->n) * t > sq->d)
1008 /*---------------------------------------------------------------------------*/
1010 static float sol_test_fore(float dt,
1011 const struct s_ball *up,
1012 const struct s_side *sp,
1018 /* If the ball is not behind the plane, the test passes. */
1022 if (v_dot(q, sp->n) - sp->d + up->r >= 0)
1025 /* if the ball is behind the plane but will hit before dt, test passes. */
1027 if (v_side(q, o, w, sp->n, sp->d, up->p, up->v, up->r) < dt)
1030 /* If the ball is behind but moving toward the plane, test passes. */
1032 if (v_dot(up->v, sp->n) > 0)
1036 /* Else, test fails. */
1041 static float sol_test_back(float dt,
1042 const struct s_ball *up,
1043 const struct s_side *sp,
1049 /* If the ball is not in front of the plane, the test passes. */
1053 if (v_dot(q, sp->n) - sp->d - up->r <= 0)
1056 /* if the ball is behind the plane but will hit before dt, test passes. */
1058 if (v_side(q, o, w, sp->n, sp->d, up->p, up->v, up->r) < dt)
1061 /* If the ball is in front but moving toward the plane, test passes. */
1063 if (v_dot(up->v, sp->n) < 0)
1067 /* Else, test fails. */
1072 /*---------------------------------------------------------------------------*/
1074 static float sol_test_lump(float dt,
1076 const struct s_ball *up,
1077 const struct s_file *fp,
1078 const struct s_lump *lp,
1082 float U[3] = {0.0f, 0.0f, 0.0f}; /* init value only to avoid gcc warnings */
1086 /* Short circuit a non-solid lump. */
1088 if (lp->fl & L_DETAIL) return t;
1090 /* Test all verts */
1093 for (i = 0; i < lp->vc; i++)
1095 const struct s_vert *vp = fp->vv + fp->iv[lp->v0 + i];
1097 if ((u = sol_test_vert(t, U, up, vp, o, w)) < t)
1104 /* Test all edges */
1107 for (i = 0; i < lp->ec; i++)
1109 const struct s_edge *ep = fp->ev + fp->iv[lp->e0 + i];
1111 if ((u = sol_test_edge(t, U, up, fp, ep, o, w)) < t)
1118 /* Test all sides */
1120 for (i = 0; i < lp->sc; i++)
1122 const struct s_side *sp = fp->sv + fp->iv[lp->s0 + i];
1124 if ((u = sol_test_side(t, U, up, fp, lp, sp, o, w)) < t)
1133 static float sol_test_node(float dt,
1135 const struct s_ball *up,
1136 const struct s_file *fp,
1137 const struct s_node *np,
1141 float U[3], u, t = dt;
1144 /* Test all lumps */
1146 for (i = 0; i < np->lc; i++)
1148 const struct s_lump *lp = fp->lv + np->l0 + i;
1150 if ((u = sol_test_lump(t, U, up, fp, lp, o, w)) < t)
1157 /* Test in front of this node */
1159 if (np->ni >= 0 && sol_test_fore(t, up, fp->sv + np->si, o, w))
1161 const struct s_node *nq = fp->nv + np->ni;
1163 if ((u = sol_test_node(t, U, up, fp, nq, o, w)) < t)
1170 /* Test behind this node */
1172 if (np->nj >= 0 && sol_test_back(t, up, fp->sv + np->si, o, w))
1174 const struct s_node *nq = fp->nv + np->nj;
1176 if ((u = sol_test_node(t, U, up, fp, nq, o, w)) < t)
1186 static float sol_test_body(float dt,
1187 float T[3], float V[3],
1188 const struct s_ball *up,
1189 const struct s_file *fp,
1190 const struct s_body *bp)
1192 float U[3], O[3], W[3], u, t = dt;
1194 const struct s_node *np = fp->nv + bp->ni;
1196 sol_body_p(O, fp, bp);
1197 sol_body_v(W, fp, bp);
1199 if ((u = sol_test_node(t, U, up, fp, np, O, W)) < t)
1208 static float sol_test_file(float dt,
1209 float T[3], float V[3],
1210 const struct s_ball *up,
1211 const struct s_file *fp)
1213 float U[3], W[3], u, t = dt;
1216 for (i = 0; i < fp->bc; i++)
1218 const struct s_body *bp = fp->bv + i;
1220 if ((u = sol_test_body(t, U, W, up, fp, bp)) < t)
1230 /*---------------------------------------------------------------------------*/
1233 * Step the physics forward DT seconds under the influence of gravity
1234 * vector G. If the ball gets pinched between two moving solids, this
1235 * loop might not terminate. It is better to do something physically
1236 * impossible than to lock up the game. So, if we make more than C
1237 * iterations, punt it.
1240 float sol_step(struct s_file *fp, const float *g, float dt, int ui, int *m)
1242 float P[3], V[3], v[3], r[3], d, e, nt, b = 0.0f, tt = dt;
1247 struct s_ball *up = fp->uv + ui;
1249 /* If the ball is in contact with a surface, apply friction. */
1254 if (m && sol_test_file(tt, P, V, up, fp) < 0.0005f)
1259 if ((d = v_dot(r, g) / (v_len(r) * v_len(g))) > 0.999f)
1261 if ((e = (v_len(up->v) - dt)) > 0.0f)
1263 /* Scale the linear velocity. */
1265 v_nrm(up->v, up->v);
1266 v_scl(up->v, up->v, e);
1268 /* Scale the angular velocity. */
1272 v_scl(up->w, up->w, -1.0f / (up->r * up->r));
1276 /* Friction has brought the ball to a stop. */
1285 else v_mad(up->v, v, g, tt);
1287 else v_mad(up->v, v, g, tt);
1289 /* Test for collision. */
1291 while (c > 0 && tt > 0 && tt > (nt = sol_test_file(tt, P, V, up, fp)))
1293 sol_body_step(fp, nt);
1294 sol_swch_step(fp, nt);
1295 sol_ball_step(fp, nt);
1299 if (b < (d = sol_bounce(up, P, V, nt)))
1305 sol_body_step(fp, tt);
1306 sol_swch_step(fp, tt);
1307 sol_ball_step(fp, tt);
1312 /*---------------------------------------------------------------------------*/
1314 int sol_coin_test(struct s_file *fp, float *p, float coin_r)
1316 const float *ball_p = fp->uv->p;
1317 const float ball_r = fp->uv->r;
1320 for (ci = 0; ci < fp->cc; ci++)
1324 r[0] = ball_p[0] - fp->cv[ci].p[0];
1325 r[1] = ball_p[1] - fp->cv[ci].p[1];
1326 r[2] = ball_p[2] - fp->cv[ci].p[2];
1328 if (fp->cv[ci].n > 0 && v_len(r) < ball_r + coin_r)
1330 p[0] = fp->cv[ci].p[0];
1331 p[1] = fp->cv[ci].p[1];
1332 p[2] = fp->cv[ci].p[2];
1343 struct s_goal *sol_goal_test(struct s_file *fp, float *p, int ui)
1345 const float *ball_p = fp->uv[ui].p;
1346 const float ball_r = fp->uv[ui].r;
1349 for (zi = 0; zi < fp->zc; zi++)
1353 r[0] = ball_p[0] - fp->zv[zi].p[0];
1354 r[1] = ball_p[2] - fp->zv[zi].p[2];
1357 if (v_len(r) < fp->zv[zi].r * 1.1 - ball_r &&
1358 ball_p[1] > fp->zv[zi].p[1] &&
1359 ball_p[1] < fp->zv[zi].p[1] + GOAL_HEIGHT / 2)
1361 p[0] = fp->zv[zi].p[0];
1362 p[1] = fp->zv[zi].p[1];
1363 p[2] = fp->zv[zi].p[2];
1371 int sol_jump_test(struct s_file *fp, float *p, int ui)
1372 /* Test if the ball ui is inside a jump. */
1373 /* Return 1 if yes and fill p with the destination position. */
1374 /* Return 0 if no. */
1375 /* Return 2 if the ball is on the border of a jump. */
1377 const float *ball_p = fp->uv[ui].p;
1378 const float ball_r = fp->uv[ui].r;
1383 for (ji = 0; ji < fp->jc; ji++)
1387 r[0] = ball_p[0] - fp->jv[ji].p[0];
1388 r[1] = ball_p[2] - fp->jv[ji].p[2];
1391 l = v_len(r) - fp->jv[ji].r;
1393 ball_p[1] > fp->jv[ji].p[1] &&
1394 ball_p[1] < fp->jv[ji].p[1] + JUMP_HEIGHT / 2)
1398 p[0] = fp->jv[ji].q[0] + (ball_p[0] - fp->jv[ji].p[0]);
1399 p[1] = fp->jv[ji].q[1] + (ball_p[1] - fp->jv[ji].p[1]);
1400 p[2] = fp->jv[ji].q[2] + (ball_p[2] - fp->jv[ji].p[2]);
1411 int sol_swch_test(struct s_file *fp, int ui)
1412 /* In the SOL fp, test and process the event the ball ui enters a switch.
1413 * Return 1 if a visible switch is activated, return 0 otherwise (no switch is
1414 * activated or only invisible switchs) */
1416 const float *ball_p = fp->uv[ui].p;
1417 const float ball_r = fp->uv[ui].r;
1420 int res = 0; /* result */
1422 for (xi = 0; xi < fp->xc; xi++)
1424 struct s_swch *xp = fp->xv + xi;
1426 if (xp->t0 == 0 || xp->f == xp->f0)
1430 r[0] = ball_p[0] - xp->p[0];
1431 r[1] = ball_p[2] - xp->p[2];
1434 l = v_len(r) - xp->r;
1436 ball_p[1] > xp->p[1] &&
1437 ball_p[1] < xp->p[1] + SWCH_HEIGHT / 2)
1439 if (!xp->e && l < - ball_r)
1444 /* The ball enter */
1448 /* Toggle the state, update the path. */
1450 xp->f = xp->f ? 0 : 1;
1452 do /* Tortoise and hare cycle traverser. */
1454 fp->pv[pi].f = xp->f;
1455 fp->pv[pj].f = xp->f;
1463 /* It toggled to non-default state, start the timer. */
1465 if (xp->f != xp->f0)
1468 /* If visible, set the result */
1481 /*---------------------------------------------------------------------------*/
1483 void put_file_state(FILE *fout, struct s_file *fp)
1485 /* Write the position and orientation of the ball. */
1487 put_array(fout, fp->uv[0].p, 3);
1488 put_array(fout, fp->uv[0].e[0], 3);
1489 put_array(fout, fp->uv[0].e[1], 3);
1492 void get_file_state(FILE *fin, struct s_file *fp)
1494 /* Read the position and orientation of the ball. */
1496 get_array(fin, fp->uv[0].p, 3);
1497 get_array(fin, fp->uv[0].e[0], 3);
1498 get_array(fin, fp->uv[0].e[1], 3);
1500 /* Compute the 3rd vector of the ball orientatian basis. */
1502 v_crs(fp->uv[0].e[2], fp->uv[0].e[0], fp->uv[0].e[1]);
1505 /*---------------------------------------------------------------------------*/