]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
decals added back due to popular demand, currently not at all optimized (they're...
[divverent/darkplaces.git] / cl_collision.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 /*
6 // not yet used
7 typedef struct physentity_s
8 {
9         // this may be a entity_t, or a edict_t, or whatever
10         void *realentity;
11
12         // can be NULL if it is a bbox object
13         model_t *bmodel;
14
15         // node this entity crosses
16         // for avoiding unnecessary collisions
17         physnode_t *node;
18
19         // matrix for converting from model to world coordinates
20         double modeltoworldmatrix[3][4];
21
22         // matrix for converting from world to model coordinates
23         double worldtomodelmatrix[3][4];
24
25         // if this is a bmodel, this is used for culling it quickly
26         // if this is not a bmodel, this is used for actual collisions
27         double mins[3], maxs[3];
28 }
29 physentity_t;
30 */
31
32 int cl_traceline_endcontents;
33
34 float CL_TraceLine (const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int contents, int hitbmodels, entity_render_t **hitent)
35 {
36         float maxfrac;
37         int n;
38         entity_render_t *ent;
39         float tracemins[3], tracemaxs[3];
40         trace_t trace;
41
42         if (hitent)
43                 *hitent = NULL;
44         Mod_CheckLoaded(cl.worldmodel);
45         Collision_ClipTrace(&trace, NULL, cl.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, start, vec3_origin, vec3_origin, end);
46
47         if (impact)
48                 VectorCopy (trace.endpos, impact);
49         if (normal)
50                 VectorCopy (trace.plane.normal, normal);
51         cl_traceline_endcontents = trace.endcontents;
52         maxfrac = trace.fraction;
53         if (hitent && trace.fraction < 1)
54                 *hitent = &cl_entities[0].render;
55
56         if (hitbmodels && cl_num_brushmodel_entities)
57         {
58                 tracemins[0] = min(start[0], end[0]);
59                 tracemaxs[0] = max(start[0], end[0]);
60                 tracemins[1] = min(start[1], end[1]);
61                 tracemaxs[1] = max(start[1], end[1]);
62                 tracemins[2] = min(start[2], end[2]);
63                 tracemaxs[2] = max(start[2], end[2]);
64
65                 // look for embedded bmodels
66                 for (n = 0;n < cl_num_brushmodel_entities;n++)
67                 {
68                         ent = cl_brushmodel_entities[n];
69                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
70                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
71                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
72                                 continue;
73
74                         Collision_ClipTrace(&trace, ent, ent->model, ent->origin, ent->angles, ent->mins, ent->maxs, start, vec3_origin, vec3_origin, end);
75
76                         if (trace.allsolid || trace.startsolid || trace.fraction < maxfrac)
77                         {
78                                 maxfrac = trace.fraction;
79                                 if (impact)
80                                         VectorCopy(trace.endpos, impact);
81                                 if (normal)
82                                         VectorCopy(trace.plane.normal, normal);
83                                 cl_traceline_endcontents = trace.endcontents;
84                                 if (hitent)
85                                         *hitent = ent;
86                         }
87                 }
88         }
89         if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __LINE__, __FILE__);
90         return maxfrac;
91 }
92