]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_collision.c
now only does stair smoothing when onground (thanks to Sajt for noticing this bug)
[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_startsupercontents;
33
34 float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int hitbmodels, entity_render_t **hitent, int hitsupercontentsmask)
35 {
36         float maxfrac, maxrealfrac;
37         int n;
38         entity_render_t *ent;
39         float tracemins[3], tracemaxs[3];
40         trace_t trace;
41         matrix4x4_t matrix, imatrix;
42         float tempnormal[3], starttransformed[3], endtransformed[3];
43
44         memset (&trace, 0 , sizeof(trace_t));
45         trace.fraction = 1;
46         trace.realfraction = 1;
47         VectorCopy (end, trace.endpos);
48
49         if (hitent)
50                 *hitent = &cl_entities[0].render;
51         Mod_CheckLoaded(cl.worldmodel);
52         if (cl.worldmodel && cl.worldmodel->TraceBox)
53                 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, hitsupercontentsmask);
54
55         if (normal)
56                 VectorCopy(trace.plane.normal, normal);
57         cl_traceline_startsupercontents = trace.startsupercontents;
58         maxfrac = trace.fraction;
59         maxrealfrac = trace.realfraction;
60
61         if (hitbmodels && cl_num_brushmodel_entities)
62         {
63                 tracemins[0] = min(start[0], end[0]);
64                 tracemaxs[0] = max(start[0], end[0]);
65                 tracemins[1] = min(start[1], end[1]);
66                 tracemaxs[1] = max(start[1], end[1]);
67                 tracemins[2] = min(start[2], end[2]);
68                 tracemaxs[2] = max(start[2], end[2]);
69
70                 // look for embedded bmodels
71                 for (n = 0;n < cl_num_brushmodel_entities;n++)
72                 {
73                         ent = cl_brushmodel_entities[n];
74                         if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
75                          || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
76                          || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
77                                 continue;
78
79                         Matrix4x4_CreateFromQuakeEntity(&matrix, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2], 1);
80                         Matrix4x4_Invert_Simple(&imatrix, &matrix);
81                         Matrix4x4_Transform(&imatrix, start, starttransformed);
82                         Matrix4x4_Transform(&imatrix, end, endtransformed);
83
84                         if (ent->model && ent->model->TraceBox)
85                                 ent->model->TraceBox(ent->model, 0, &trace, starttransformed, starttransformed, endtransformed, endtransformed, hitsupercontentsmask);
86
87                         cl_traceline_startsupercontents |= trace.startsupercontents;
88                         if (maxrealfrac > trace.realfraction)
89                         {
90                                 if (hitent)
91                                         *hitent = ent;
92                                 maxfrac = trace.fraction;
93                                 maxrealfrac = trace.realfraction;
94                                 if (normal)
95                                 {
96                                         VectorCopy(trace.plane.normal, tempnormal);
97                                         Matrix4x4_Transform3x3(&matrix, tempnormal, normal);
98                                 }
99                         }
100                 }
101         }
102         if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
103         if (impact)
104                 VectorLerp(start, maxfrac, end, impact);
105         return maxfrac;
106 }
107
108 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
109 {
110         // FIXME: check multiple brush models
111         if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
112                 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
113 }
114
115 int CL_PointQ1Contents(const vec3_t p)
116 {
117         CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
118         return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, cl_traceline_startsupercontents);
119         /*
120         // FIXME: check multiple brush models
121         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
122                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
123         return 0;
124         */
125 }
126
127 int CL_PointSuperContents(const vec3_t p)
128 {
129         CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
130         return cl_traceline_startsupercontents;
131         /*
132         // FIXME: check multiple brush models
133         if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
134                 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
135         return 0;
136         */
137 }
138