]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_collision.c
ea667b654e2d8106558c00f1c481537762776499
[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                         if (model->type == mod_brush)
49                         {
50                                 traceline_entity[traceline_entities++] = ent;
51                                 if (ent->angles[0] || ent->angles[2])
52                                 {
53                                         // pitch or roll
54                                         VectorAdd(ent->origin, model->rotatedmins, ent->mins);
55                                         VectorAdd(ent->origin, model->rotatedmaxs, ent->maxs);
56                                 }
57                                 else if (ent->angles[1])
58                                 {
59                                         // yaw
60                                         VectorAdd(ent->origin, model->yawmins, ent->mins);
61                                         VectorAdd(ent->origin, model->yawmaxs, ent->maxs);
62                                 }
63                                 else
64                                 {
65                                         VectorAdd(ent->origin, model->normalmins, ent->mins);
66                                         VectorAdd(ent->origin, model->normalmaxs, ent->maxs);
67                                 }
68                         }
69                 }
70         }
71 }
72
73 int cl_traceline_endcontents;
74
75 float CL_TraceLine (vec3_t start, vec3_t end, vec3_t impact, vec3_t normal, int contents, int hitbmodels)
76 {
77         double maxfrac;
78         trace_t trace;
79
80         Mod_CheckLoaded(cl.worldmodel);
81         Collision_ClipTrace(&trace, NULL, cl.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, start, vec3_origin, vec3_origin, end);
82
83         if (impact)
84                 VectorCopy (trace.endpos, impact);
85         if (normal)
86                 VectorCopy (trace.plane.normal, normal);
87         cl_traceline_endcontents = trace.endcontents;
88         maxfrac = trace.fraction;
89
90         if (hitbmodels && traceline_entities)
91         {
92                 int n;
93                 entity_render_t *ent;
94                 double tracemins[3], tracemaxs[3];
95                 tracemins[0] = min(start[0], end[0]);
96                 tracemaxs[0] = max(start[0], end[0]);
97                 tracemins[1] = min(start[1], end[1]);
98                 tracemaxs[1] = max(start[1], end[1]);
99                 tracemins[2] = min(start[2], end[2]);
100                 tracemaxs[2] = max(start[2], end[2]);
101
102                 // look for embedded bmodels
103                 for (n = 0;n < traceline_entities;n++)
104                 {
105                         ent = traceline_entity[n];
106                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
107                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
108                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
109                                 continue;
110
111                         Collision_ClipTrace(&trace, ent, ent->model, ent->origin, ent->angles, ent->mins, ent->maxs, start, vec3_origin, vec3_origin, end);
112
113                         if (trace.allsolid || trace.startsolid || trace.fraction < maxfrac)
114                         {
115                                 maxfrac = trace.fraction;
116                                 if (impact)
117                                         VectorCopy(trace.endpos, impact);
118                                 if (normal)
119                                         VectorCopy(trace.plane.normal, normal);
120                                 cl_traceline_endcontents = trace.endcontents;
121                         }
122                 }
123         }
124         return maxfrac;
125 }
126