]> icculus.org git repositories - divverent/darkplaces.git/blob - collision.c
no time to explain, more changes on the path to q3bsp support
[divverent/darkplaces.git] / collision.c
1
2 #include "quakedef.h"
3
4 typedef struct
5 {
6         // the hull we're tracing through
7         const hull_t *hull;
8
9         // the trace structure to fill in
10         trace_t *trace;
11
12         // start and end of the trace (in model space)
13         double start[3];
14         double end[3];
15
16         // end - start
17         double dist[3];
18 }
19 RecursiveHullCheckTraceInfo_t;
20
21 // 1/32 epsilon to keep floating point happy
22 #define DIST_EPSILON (0.03125)
23
24 #define HULLCHECKSTATE_EMPTY 0
25 #define HULLCHECKSTATE_SOLID 1
26 #define HULLCHECKSTATE_DONE 2
27
28 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
29 {
30         // status variables, these don't need to be saved on the stack when
31         // recursing...  but are because this should be thread-safe
32         // (note: tracing against a bbox is not thread-safe, yet)
33         int ret;
34         mplane_t *plane;
35         double t1, t2;
36
37         // variables that need to be stored on the stack when recursing
38         dclipnode_t *node;
39         int side;
40         double midf, mid[3];
41
42         // LordHavoc: a goto!  everyone flee in terror... :)
43 loc0:
44         // check for empty
45         if (num < 0)
46         {
47                 t->trace->endcontents = num;
48                 if (t->trace->thiscontents)
49                 {
50                         if (num == t->trace->thiscontents)
51                                 t->trace->allsolid = false;
52                         else
53                         {
54                                 // if the first leaf is solid, set startsolid
55                                 if (t->trace->allsolid)
56                                         t->trace->startsolid = true;
57                                 return HULLCHECKSTATE_SOLID;
58                         }
59                         return HULLCHECKSTATE_EMPTY;
60                 }
61                 else
62                 {
63                         if (num != CONTENTS_SOLID)
64                         {
65                                 t->trace->allsolid = false;
66                                 if (num == CONTENTS_EMPTY)
67                                         t->trace->inopen = true;
68                                 else
69                                         t->trace->inwater = true;
70                         }
71                         else
72                         {
73                                 // if the first leaf is solid, set startsolid
74                                 if (t->trace->allsolid)
75                                         t->trace->startsolid = true;
76                                 return HULLCHECKSTATE_SOLID;
77                         }
78                         return HULLCHECKSTATE_EMPTY;
79                 }
80         }
81
82         // find the point distances
83         node = t->hull->clipnodes + num;
84
85         plane = t->hull->planes + node->planenum;
86         if (plane->type < 3)
87         {
88                 t1 = p1[plane->type] - plane->dist;
89                 t2 = p2[plane->type] - plane->dist;
90         }
91         else
92         {
93                 t1 = DotProduct (plane->normal, p1) - plane->dist;
94                 t2 = DotProduct (plane->normal, p2) - plane->dist;
95         }
96
97         if (t1 < 0)
98         {
99                 if (t2 < 0)
100                 {
101                         num = node->children[1];
102                         goto loc0;
103                 }
104                 side = 1;
105         }
106         else
107         {
108                 if (t2 >= 0)
109                 {
110                         num = node->children[0];
111                         goto loc0;
112                 }
113                 side = 0;
114         }
115
116         // the line intersects, find intersection point
117         // LordHavoc: this uses the original trace for maximum accuracy
118         if (plane->type < 3)
119         {
120                 t1 = t->start[plane->type] - plane->dist;
121                 t2 = t->end[plane->type] - plane->dist;
122         }
123         else
124         {
125                 t1 = DotProduct (plane->normal, t->start) - plane->dist;
126                 t2 = DotProduct (plane->normal, t->end) - plane->dist;
127         }
128
129         midf = t1 / (t1 - t2);
130         midf = bound(p1f, midf, p2f);
131         VectorMA(t->start, midf, t->dist, mid);
132
133         // recurse both sides, front side first
134         ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
135         // if this side is not empty, return what it is (solid or done)
136         if (ret != HULLCHECKSTATE_EMPTY)
137                 return ret;
138
139         ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
140         // if other side is not solid, return what it is (empty or done)
141         if (ret != HULLCHECKSTATE_SOLID)
142                 return ret;
143
144         // front is air and back is solid, this is the impact point...
145         if (side)
146         {
147                 t->trace->plane.dist = -plane->dist;
148                 VectorNegate (plane->normal, t->trace->plane.normal);
149         }
150         else
151         {
152                 t->trace->plane.dist = plane->dist;
153                 VectorCopy (plane->normal, t->trace->plane.normal);
154         }
155
156         // bias away from surface a bit
157         t1 = DotProduct(t->trace->plane.normal, t->start) - (t->trace->plane.dist + DIST_EPSILON);
158         t2 = DotProduct(t->trace->plane.normal, t->end) - (t->trace->plane.dist + DIST_EPSILON);
159
160         midf = t1 / (t1 - t2);
161         t->trace->fraction = bound(0.0f, midf, 1.0);
162
163         VectorMA(t->start, t->trace->fraction, t->dist, t->trace->endpos);
164
165         return HULLCHECKSTATE_DONE;
166 }
167
168 #if 0
169 // used if start and end are the same
170 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
171 {
172         // If you can read this, you understand BSP trees
173         while (num >= 0)
174                 num = t->hull->clipnodes[num].children[((t->hull->planes[t->hull->clipnodes[num].planenum].type < 3) ? (t->start[t->hull->planes[t->hull->clipnodes[num].planenum].type]) : (DotProduct(t->hull->planes[t->hull->clipnodes[num].planenum].normal, t->start))) < t->hull->planes[t->hull->clipnodes[num].planenum].dist];
175
176         // check for empty
177         t->trace->endcontents = num;
178         if (t->trace->thiscontents)
179         {
180                 if (num == t->trace->thiscontents)
181                         t->trace->allsolid = false;
182                 else
183                 {
184                         // if the first leaf is solid, set startsolid
185                         if (t->trace->allsolid)
186                                 t->trace->startsolid = true;
187                 }
188         }
189         else
190         {
191                 if (num != CONTENTS_SOLID)
192                 {
193                         t->trace->allsolid = false;
194                         if (num == CONTENTS_EMPTY)
195                                 t->trace->inopen = true;
196                         else
197                                 t->trace->inwater = true;
198                 }
199                 else
200                 {
201                         // if the first leaf is solid, set startsolid
202                         if (t->trace->allsolid)
203                                 t->trace->startsolid = true;
204                 }
205         }
206 }
207 #endif
208
209 static hull_t box_hull;
210 static dclipnode_t box_clipnodes[6];
211 static mplane_t box_planes[6];
212
213 void Collision_Init (void)
214 {
215         int             i;
216         int             side;
217
218         //Set up the planes and clipnodes so that the six floats of a bounding box
219         //can just be stored out and get a proper hull_t structure.
220
221         box_hull.clipnodes = box_clipnodes;
222         box_hull.planes = box_planes;
223         box_hull.firstclipnode = 0;
224         box_hull.lastclipnode = 5;
225
226         for (i = 0;i < 6;i++)
227         {
228                 box_clipnodes[i].planenum = i;
229
230                 side = i&1;
231
232                 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
233                 if (i != 5)
234                         box_clipnodes[i].children[side^1] = i + 1;
235                 else
236                         box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
237
238                 box_planes[i].type = i>>1;
239                 box_planes[i].normal[i>>1] = 1;
240         }
241 }
242
243 void Collision_ClipTrace_Box(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end)
244 {
245         RecursiveHullCheckTraceInfo_t rhc;
246         // fill in a default trace
247         memset(&rhc, 0, sizeof(rhc));
248         memset(trace, 0, sizeof(trace_t));
249         //To keep everything totally uniform, bounding boxes are turned into small
250         //BSP trees instead of being compared directly.
251         // create a temp hull from bounding box sizes
252         box_planes[0].dist = cmaxs[0] - mins[0];
253         box_planes[1].dist = cmins[0] - maxs[0];
254         box_planes[2].dist = cmaxs[1] - mins[1];
255         box_planes[3].dist = cmins[1] - maxs[1];
256         box_planes[4].dist = cmaxs[2] - mins[2];
257         box_planes[5].dist = cmins[2] - maxs[2];
258         // trace a line through the generated clipping hull
259         rhc.hull = &box_hull;
260         rhc.trace = trace;
261         rhc.trace->fraction = 1;
262         rhc.trace->allsolid = true;
263         VectorCopy(start, rhc.start);
264         VectorCopy(end, rhc.end);
265         VectorSubtract(rhc.end, rhc.start, rhc.dist);
266         RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
284 {
285         int i;
286         Con_Printf("3 %s\n%i\n", name, brush->numpoints);
287         for (i = 0;i < brush->numpoints;i++)
288                 Con_Printf("%g %g %g\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
289         // FIXME: optimize!
290         Con_Printf("4\n%i\n", brush->numplanes);
291         for (i = 0;i < brush->numplanes;i++)
292                 Con_Printf("%g %g %g %g\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
293 }
294
295
296 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes)
297 {
298         colbrushf_t *brush;
299         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes);
300         brush->numpoints = numpoints;
301         brush->numplanes = numplanes;
302         brush->planes = (void *)(brush + 1);
303         brush->points = (void *)(brush->planes + brush->numplanes);
304         return brush;
305 }
306
307 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
308 {
309         int i;
310         float edge0[3], edge1[3], normal[3], dist, bestdist;
311         colpointf_t *p, *p2;
312
313         // choose best surface normal for polygon's plane
314         bestdist = 0;
315         for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
316         {
317                 VectorSubtract(p[-1].v, p[0].v, edge0);
318                 VectorSubtract(p[1].v, p[0].v, edge1);
319                 CrossProduct(edge0, edge1, normal);
320                 dist = DotProduct(normal, normal);
321                 if (i == 0 || bestdist < dist)
322                 {
323                         bestdist = dist;
324                         VectorCopy(normal, brush->planes->normal);
325                 }
326         }
327
328         VectorNormalize(brush->planes->normal);
329         brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
330
331         // negate plane to create other side
332         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
333         brush->planes[1].dist = -brush->planes[0].dist;
334         for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
335         {
336                 VectorSubtract(p->v, p2->v, edge0);
337                 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
338                 VectorNormalize(brush->planes[i + 2].normal);
339                 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
340         }
341
342 #if 1
343         // validity check - will be disabled later
344         for (i = 0;i < brush->numplanes;i++)
345         {
346                 int j;
347                 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
348                         if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
349                                 Con_Printf("Error in brush plane generation, plane %i\n", i);
350         }
351 #endif
352 }
353
354 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points)
355 {
356         colbrushf_t *brush;
357         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
358         brush->numpoints = numpoints;
359         brush->numplanes = numpoints + 2;
360         brush->planes = (void *)(brush + 1);
361         brush->points = (colpointf_t *)points;
362         return brush;
363 }
364
365 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
366 {
367         float dist, bestdist;
368         bestdist = DotProduct(points->v, normal);
369         points++;
370         while(--numpoints)
371         {
372                 dist = DotProduct(points->v, normal);
373                 if (bestdist > dist)
374                         bestdist = dist;
375                 points++;
376         }
377         return bestdist;
378 }
379
380 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
381 {
382         float dist, bestdist;
383         bestdist = DotProduct(points->v, normal);
384         points++;
385         while(--numpoints)
386         {
387                 dist = DotProduct(points->v, normal);
388                 if (bestdist < dist)
389                         bestdist = dist;
390                 points++;
391         }
392         return bestdist;
393 }
394
395 #define COLLISIONEPSILON (1.0f / 32.0f)
396 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
397
398 // NOTE: start and end of each brush pair must have same numplanes/numpoints
399 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
400 {
401         int nplane, nplane2, fstartsolid, fendsolid;
402         float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
403         const colplanef_t *startplane, *endplane;
404
405         enterfrac = -1;
406         leavefrac = 1;
407         fstartsolid = true;
408         fendsolid = true;
409
410         for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
411         {
412                 nplane2 = nplane;
413                 if (nplane2 >= thatbrush_start->numplanes)
414                 {
415                         nplane2 -= thatbrush_start->numplanes;
416                         startplane = thisbrush_start->planes + nplane2;
417                         endplane = thisbrush_end->planes + nplane2;
418                 }
419                 else
420                 {
421                         startplane = thatbrush_start->planes + nplane2;
422                         endplane = thatbrush_end->planes + nplane2;
423                 }
424                 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
425                 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
426                 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
427
428                 f = d1 - d2;
429                 if (f >= 0)
430                 {
431                         // moving into brush
432                         if (d2 > 0)
433                                 return;
434                         if (d1 < 0)
435                                 continue;
436                         // enter
437                         fstartsolid = false;
438                         f = (d1 - COLLISIONEPSILON) / f;
439                         f = bound(0, f, 1);
440                         if (enterfrac < f)
441                         {
442                                 enterfrac = f;
443                                 VectorBlend(startplane->normal, endplane->normal, enterfrac, newimpactnormal);
444                         }
445                 }
446                 else if (f < 0)
447                 {
448                         // moving out of brush
449                         if (d1 > 0)
450                                 return;
451                         if (d2 < 0)
452                                 continue;
453                         // leave
454                         fendsolid = false;
455                         f = (d1 + COLLISIONEPSILON) / f;
456                         f = bound(0, f, 1);
457                         if (leavefrac > f)
458                                 leavefrac = f;
459                 }
460         }
461
462         if (fstartsolid)
463         {
464                 trace->startsolid = true;
465                 if (fendsolid)
466                         trace->allsolid = true;
467         }
468
469         // LordHavoc: we need an epsilon nudge here because for a point trace the
470         // penetrating line segment is normally zero length if this brush was
471         // generated from a polygon (infinitely thin), and could even be slightly
472         // positive or negative due to rounding errors in that case.
473         if (enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
474         {
475                 trace->fraction = bound(0, enterfrac, 1);
476                 VectorCopy(newimpactnormal, trace->plane.normal);
477         }
478 }
479
480 static colplanef_t polyf_planes[256 + 2];
481 static colbrushf_t polyf_brush;
482
483 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points)
484 {
485         if (numpoints > 256)
486         {
487                 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
488                 return;
489         }
490         polyf_brush.numpoints = numpoints;
491         polyf_brush.numplanes = numpoints + 2;
492         polyf_brush.points = (colpointf_t *)points;
493         polyf_brush.planes = polyf_planes;
494         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
495         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
496         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
497 }
498
499 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
500 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
501 static colbrushf_t polyf_brushstart, polyf_brushend;
502
503 void Collision_TraceBrushPolygonTransformFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, const matrix4x4_t *polygonmatrixstart, const matrix4x4_t *polygonmatrixend)
504 {
505         int i;
506         if (numpoints > 256)
507         {
508                 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
509                 return;
510         }
511         polyf_brushstart.numpoints = numpoints;
512         polyf_brushstart.numplanes = numpoints + 2;
513         polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
514         polyf_brushstart.planes = polyf_planesstart;
515         for (i = 0;i < numpoints;i++)
516                 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
517         polyf_brushend.numpoints = numpoints;
518         polyf_brushend.numplanes = numpoints + 2;
519         polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
520         polyf_brushend.planes = polyf_planesend;
521         for (i = 0;i < numpoints;i++)
522                 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
523         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
524         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
525
526         //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
527         //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
528
529         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
530 }
531
532
533
534 #define MAX_BRUSHFORBOX 16
535 static int brushforbox_index = 0;
536 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
537 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
538 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
539
540 void Collision_InitBrushForBox(void)
541 {
542         int i;
543         for (i = 0;i < MAX_BRUSHFORBOX;i++)
544         {
545                 brushforbox_brush[i].numpoints = 8;
546                 brushforbox_brush[i].numplanes = 6;
547                 brushforbox_brush[i].points = brushforbox_point + i * 8;
548                 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
549         }
550 }
551
552 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
553 {
554         int i;
555         vec3_t v;
556         colbrushf_t *brush;
557         if (brushforbox_brush[0].numpoints == 0)
558                 Collision_InitBrushForBox();
559         brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
560         // FIXME: optimize
561         for (i = 0;i < 8;i++)
562         {
563                 v[0] = i & 1 ? maxs[0] : mins[0];
564                 v[1] = i & 2 ? maxs[1] : mins[1];
565                 v[2] = i & 4 ? maxs[2] : mins[2];
566                 Matrix4x4_Transform(matrix, v, brush->points[i].v);
567         }
568         // FIXME: optimize!
569         for (i = 0;i < 6;i++)
570         {
571                 VectorClear(v);
572                 v[i >> 1] = i & 1 ? 1 : -1;
573                 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
574                 VectorNormalize(brush->planes[i].normal);
575                 brush->planes[i].dist = furthestplanedist_float(brush->planes[i].normal, brush->points, brush->numpoints);
576         }
577         return brush;
578 }
579
580 // LordHavoc: currently unused and not yet tested
581 // note: this can be used for tracing a moving sphere vs a stationary sphere,
582 // by simply adding the moving sphere's radius to the sphereradius parameter,
583 // all the results are correct (impactpoint, impactnormal, and fraction)
584 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
585 {
586         double dir[3], scale, v[3], deviationdist, impactdist, linelength;
587         // make sure the impactpoint and impactnormal are valid even if there is
588         // no collision
589         impactpoint[0] = lineend[0];
590         impactpoint[1] = lineend[1];
591         impactpoint[2] = lineend[2];
592         impactnormal[0] = 0;
593         impactnormal[1] = 0;
594         impactnormal[2] = 0;
595         // calculate line direction
596         dir[0] = lineend[0] - linestart[0];
597         dir[1] = lineend[1] - linestart[1];
598         dir[2] = lineend[2] - linestart[2];
599         // normalize direction
600         linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
601         if (linelength)
602         {
603                 scale = 1.0 / linelength;
604                 dir[0] *= scale;
605                 dir[1] *= scale;
606                 dir[2] *= scale;
607         }
608         // this dotproduct calculates the distance along the line at which the
609         // sphere origin is (nearest point to the sphere origin on the line)
610         impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
611         // calculate point on line at that distance, and subtract the
612         // sphereorigin from it, so we have a vector to measure for the distance
613         // of the line from the sphereorigin (deviation, how off-center it is)
614         v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
615         v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
616         v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
617         deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
618         // if outside the radius, it's a miss for sure
619         // (we do this comparison using squared radius to avoid a sqrt)
620         if (deviationdist > sphereradius*sphereradius)
621                 return 1; // miss (off to the side)
622         // nudge back to find the correct impact distance
623         impactdist += (sqrt(deviationdist) - sphereradius);
624         if (impactdist >= linelength)
625                 return 1; // miss (not close enough)
626         if (impactdist < 0)
627                 return 1; // miss (linestart is past or inside sphere)
628         // calculate new impactpoint
629         impactpoint[0] = linestart[0] + impactdist * dir[0];
630         impactpoint[1] = linestart[1] + impactdist * dir[1];
631         impactpoint[2] = linestart[2] + impactdist * dir[2];
632         // calculate impactnormal (surface normal at point of impact)
633         impactnormal[0] = impactpoint[0] - sphereorigin[0];
634         impactnormal[1] = impactpoint[1] - sphereorigin[1];
635         impactnormal[2] = impactpoint[2] - sphereorigin[2];
636         // normalize impactnormal
637         scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
638         if (scale)
639         {
640                 scale = 1.0 / sqrt(scale);
641                 impactnormal[0] *= scale;
642                 impactnormal[1] *= scale;
643                 impactnormal[2] *= scale;
644         }
645         // return fraction of movement distance
646         return impactdist / linelength;
647 }
648