split out RecursiveHullCheck and related code into collision.c (shared by client...
[xonotic/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3
4 /*
5 // not yet used
6 typedef struct physentity_s
7 {
8         // this may be a entity_t, or a edict_t, or whatever
9         void *realentity;
10
11         // can be NULL if it is a bbox object
12         model_t *bmodel;
13
14         // node this entity crosses
15         // for avoiding unnecessary collisions
16         physnode_t *node;
17
18         // matrix for converting from model to world coordinates
19         double modeltoworldmatrix[3][4];
20
21         // matrix for converting from world to model coordinates
22         double worldtomodelmatrix[3][4];
23
24         // if this is a bmodel, this is used for culling it quickly
25         // if this is not a bmodel, this is used for actual collisions
26         double mins[3], maxs[3];
27 }
28 physentity_t;
29 */
30
31 static entity_render_t *traceline_entity[MAX_EDICTS];
32 static int traceline_entities;
33
34 // builds list of entities for TraceLine to check later
35 void CL_TraceLine_ScanForBModels(void)
36 {
37         int i;
38         entity_render_t *ent;
39         model_t *model;
40         traceline_entities = 0;
41         for (i = 1;i < MAX_EDICTS;i++)
42         {
43                 ent = &cl_entities[i].render;
44                 model = ent->model;
45                 // look for embedded brush models only
46                 if (model && model->name[0] == '*')
47                 {
48                         // this does nothing for * models currently...
49                         //Mod_CheckLoaded(model);
50                         if (model->type == mod_brush)
51                         {
52                                 traceline_entity[traceline_entities++] = ent;
53                                 if (ent->angles[0] || ent->angles[2])
54                                 {
55                                         // pitch or roll
56                                         VectorAdd(ent->origin, model->rotatedmins, ent->mins);
57                                         VectorAdd(ent->origin, model->rotatedmaxs, ent->maxs);
58                                 }
59                                 else if (ent->angles[1])
60                                 {
61                                         // yaw
62                                         VectorAdd(ent->origin, model->yawmins, ent->mins);
63                                         VectorAdd(ent->origin, model->yawmaxs, ent->maxs);
64                                 }
65                                 else
66                                 {
67                                         VectorAdd(ent->origin, model->normalmins, ent->mins);
68                                         VectorAdd(ent->origin, model->normalmaxs, ent->maxs);
69                                 }
70                         }
71                 }
72         }
73 }
74
75 int cl_traceline_endcontents;
76
77 float CL_TraceLine (vec3_t start, vec3_t end, vec3_t impact, vec3_t normal, int contents, int hitbmodels)
78 {
79         double maxfrac;
80         trace_t trace;
81
82         Mod_CheckLoaded(cl.worldmodel);
83         Collision_ClipTrace(&trace, NULL, cl.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, start, vec3_origin, vec3_origin, end);
84
85         if (impact)
86                 VectorCopy (trace.endpos, impact);
87         if (normal)
88                 VectorCopy (trace.plane.normal, normal);
89         cl_traceline_endcontents = trace.endcontents;
90         maxfrac = trace.fraction;
91
92         if (hitbmodels && traceline_entities)
93         {
94                 int n;
95                 entity_render_t *ent;
96                 double /*start2[3], end2[3], */tracemins[3], tracemaxs[3];
97                 tracemins[0] = min(start[0], end[0]);
98                 tracemaxs[0] = max(start[0], end[0]);
99                 tracemins[1] = min(start[1], end[1]);
100                 tracemaxs[1] = max(start[1], end[1]);
101                 tracemins[2] = min(start[2], end[2]);
102                 tracemaxs[2] = max(start[2], end[2]);
103
104                 // look for embedded bmodels
105                 for (n = 0;n < traceline_entities;n++)
106                 {
107                         ent = traceline_entity[n];
108                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
109                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
110                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
111                                 continue;
112
113                         Collision_ClipTrace(&trace, ent, ent->model, ent->origin, ent->angles, ent->mins, ent->maxs, start, vec3_origin, vec3_origin, end);
114
115                         if (trace.allsolid || trace.startsolid || trace.fraction < maxfrac)
116                         {
117                                 maxfrac = trace.fraction;
118                                 if (impact)
119                                         VectorCopy(trace.endpos, impact);
120                                 if (normal)
121                                         VectorCopy(trace.plane.normal, normal);
122                                 cl_traceline_endcontents = trace.endcontents;
123                         }
124                 }
125         }
126         return maxfrac;
127 }