1 // physics.cpp: no physics books were hurt nor consulted in the construction of this code.
2 // All physics computations and constants were invented on the fly and simply tweaked until
3 // they "felt right", and have no basis in reality. Collision detection is simplistic but
4 // very robust (uses discrete steps at fixed fps).
5 
6 #include "cube.h"
7 
plcollide(dynent * d,dynent * o,float & headspace,float & hi,float & lo)8 bool plcollide(dynent *d, dynent *o, float &headspace, float &hi, float &lo) // collide with player or monster
9 {
10     if(o->state!=CS_ALIVE) return true;
11     const float r = o->radius+d->radius;
12     if(fabs(o->o.x-d->o.x)<r && fabs(o->o.y-d->o.y)<r)
13     {
14         if(d->o.z-d->eyeheight<o->o.z-o->eyeheight) { if(o->o.z-o->eyeheight<hi) hi = o->o.z-o->eyeheight-1; }
15         else if(o->o.z+o->aboveeye>lo) lo = o->o.z+o->aboveeye+1;
16 
17         if(fabs(o->o.z-d->o.z)<o->aboveeye+d->eyeheight) return false;
18         if(d->monsterstate) return false; // hack
19         headspace = d->o.z-o->o.z-o->aboveeye-d->eyeheight;
20         if(headspace<0) headspace = 10;
21     };
22     return true;
23 };
24 
cornertest(int mip,int x,int y,int dx,int dy,int & bx,int & by,int & bs)25 bool cornertest(int mip, int x, int y, int dx, int dy, int &bx, int &by, int &bs)    // recursively collide with a mipmapped corner cube
26 {
27     sqr *w = wmip[mip];
28     int sz = ssize>>mip;
29     bool stest = SOLID(SWS(w, x+dx, y, sz)) && SOLID(SWS(w, x, y+dy, sz));
30     mip++;
31     x /= 2;
32     y /= 2;
33     if(SWS(wmip[mip], x, y, ssize>>mip)->type==CORNER)
34     {
35         bx = x<<mip;
36         by = y<<mip;
37         bs = 1<<mip;
38         return cornertest(mip, x, y, dx, dy, bx, by, bs);
39     };
40     return stest;
41 };
42 
mmcollide(dynent * d,float & hi,float & lo)43 void mmcollide(dynent *d, float &hi, float &lo)           // collide with a mapmodel
44 {
45     loopv(ents)
46     {
47         entity &e = ents[i];
48         if(e.type!=MAPMODEL) continue;
49         mapmodelinfo *mmi = getmminfo(e.attr2);
50         if(!mmi || !mmi->h) continue;
51         const float r = mmi->rad+d->radius;
52         if(fabs(e.x-d->o.x)<r && fabs(e.y-d->o.y)<r)
53         {
54             float mmz = (float)(S(e.x, e.y)->floor+mmi->zoff+e.attr3);
55             if(d->o.z-d->eyeheight<mmz) { if(mmz<hi) hi = mmz; }
56             else if(mmz+mmi->h>lo) lo = mmz+mmi->h;
57         };
58     };
59 };
60 
61 // all collision happens here
62 // spawn is a dirty side effect used in spawning
63 // drop & rise are supplied by the physics below to indicate gravity/push for current mini-timestep
64 
collide(dynent * d,bool spawn,float drop,float rise)65 bool collide(dynent *d, bool spawn, float drop, float rise)
66 {
67     const float fx1 = d->o.x-d->radius;     // figure out integer cube rectangle this entity covers in map
68     const float fy1 = d->o.y-d->radius;
69     const float fx2 = d->o.x+d->radius;
70     const float fy2 = d->o.y+d->radius;
71     const int x1 = fast_f2nat(fx1);
72     const int y1 = fast_f2nat(fy1);
73     const int x2 = fast_f2nat(fx2);
74     const int y2 = fast_f2nat(fy2);
75     float hi = 127, lo = -128;
76     float minfloor = (d->monsterstate && !spawn && d->health>100) ? d->o.z-d->eyeheight-4.5f : -1000.0f;  // big monsters are afraid of heights, unless angry :)
77 
78     for(int x = x1; x<=x2; x++) for(int y = y1; y<=y2; y++)     // collide with map
79     {
80         if(OUTBORD(x,y)) return false;
81         sqr *s = S(x,y);
82         float ceil = s->ceil;
83         float floor = s->floor;
84         switch(s->type)
85         {
86             case SOLID:
87                 return false;
88 
89             case CORNER:
90             {
91                 int bx = x, by = y, bs = 1;
92                 if(x==x1 && y==y1 && cornertest(0, x, y, -1, -1, bx, by, bs) && fx1-bx+fy1-by<=bs
93                 || x==x2 && y==y1 && cornertest(0, x, y,  1, -1, bx, by, bs) && fx2-bx>=fy1-by
94                 || x==x1 && y==y2 && cornertest(0, x, y, -1,  1, bx, by, bs) && fx1-bx<=fy2-by
95                 || x==x2 && y==y2 && cornertest(0, x, y,  1,  1, bx, by, bs) && fx2-bx+fy2-by>=bs)
96                    return false;
97                 break;
98             };
99 
100             case FHF:       // FIXME: too simplistic collision with slopes, makes it feels like tiny stairs
101                 floor -= (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
102                 break;
103 
104             case CHF:
105                 ceil += (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
106 
107         };
108         if(ceil<hi) hi = ceil;
109         if(floor>lo) lo = floor;
110         if(floor<minfloor) return false;
111     };
112 
113     if(hi-lo < d->eyeheight+d->aboveeye) return false;
114 
115     float headspace = 10;
116     loopv(players)       // collide with other players
117     {
118         dynent *o = players[i];
119         if(!o || o==d) continue;
120         if(!plcollide(d, o, headspace, hi, lo)) return false;
121     };
122     if(d!=player1) if(!plcollide(d, player1, headspace, hi, lo)) return false;
123     dvector &v = getmonsters();
124     // this loop can be a performance bottleneck with many monster on a slow cpu,
125     // should replace with a blockmap but seems mostly fast enough
126     loopv(v) if(!vreject(d->o, v[i]->o, 7.0f) && d!=v[i] && !plcollide(d, v[i], headspace, hi, lo)) return false;
127     headspace -= 0.01f;
128 
129     mmcollide(d, hi, lo);    // collide with map models
130 
131     if(spawn)
132     {
133         d->o.z = lo+d->eyeheight;       // just drop to floor (sideeffect)
134         d->onfloor = true;
135     }
136     else
137     {
138         const float space = d->o.z-d->eyeheight-lo;
139         if(space<0)
140         {
141             if(space>-0.01) d->o.z = lo+d->eyeheight;   // stick on step
142             else if(space>-1.26f) d->o.z += rise;       // rise thru stair
143             else return false;
144         }
145         else
146         {
147             d->o.z -= min(min(drop, space), headspace);       // gravity
148         };
149 
150         const float space2 = hi-(d->o.z+d->aboveeye);
151         if(space2<0)
152         {
153             if(space2<-0.1) return false;     // hack alert!
154             d->o.z = hi-d->aboveeye;          // glue to ceiling
155             d->vel.z = 0;                     // cancel out jumping velocity
156         };
157 
158         d->onfloor = d->o.z-d->eyeheight-lo<0.001f;
159     };
160     return true;
161 }
162 
rad(float x)163 float rad(float x) { return x*3.14159f/180; };
164 
165 VARP(maxroll, 0, 3, 20);
166 
167 int physicsfraction = 0, physicsrepeat = 0;
168 const int MINFRAMETIME = 20; // physics always simulated at 50fps or better
169 
physicsframe()170 void physicsframe()          // optimally schedule physics frames inside the graphics frames
171 {
172     if(curtime>=MINFRAMETIME)
173     {
174         int faketime = curtime+physicsfraction;
175         physicsrepeat = faketime/MINFRAMETIME;
176         physicsfraction = faketime-physicsrepeat*MINFRAMETIME;
177     }
178     else
179     {
180         physicsrepeat = 1;
181     };
182 };
183 
184 // main physics routine, moves a player/monster for a curtime step
185 // moveres indicated the physics precision (which is lower for monsters and multiplayer prediction)
186 // local is false for multiplayer prediction
187 
moveplayer(dynent * pl,int moveres,bool local,int curtime)188 void moveplayer(dynent *pl, int moveres, bool local, int curtime)
189 {
190     const bool water = hdr.waterlevel>pl->o.z-0.5f;
191     const bool floating = (editmode && local) || pl->state==CS_EDITING;
192 
193     vec d;      // vector of direction we ideally want to move in
194 
195     d.x = (float)(pl->move*cos(rad(pl->yaw-90)));
196     d.y = (float)(pl->move*sin(rad(pl->yaw-90)));
197     d.z = 0;
198 
199     if(floating || water)
200     {
201         d.x *= (float)cos(rad(pl->pitch));
202         d.y *= (float)cos(rad(pl->pitch));
203         d.z = (float)(pl->move*sin(rad(pl->pitch)));
204     };
205 
206     d.x += (float)(pl->strafe*cos(rad(pl->yaw-180)));
207     d.y += (float)(pl->strafe*sin(rad(pl->yaw-180)));
208 
209     const float speed = curtime/(water ? 2000.0f : 1000.0f)*pl->maxspeed;
210     const float friction = water ? 20.0f : (pl->onfloor || floating ? 6.0f : 30.0f);
211 
212     const float fpsfric = friction/curtime*20.0f;
213 
214     vmul(pl->vel, fpsfric-1);   // slowly apply friction and direction to velocity, gives a smooth movement
215     vadd(pl->vel, d);
216     vdiv(pl->vel, fpsfric);
217     d = pl->vel;
218     vmul(d, speed);             // d is now frametime based velocity vector
219 
220     pl->blocked = false;
221     pl->moving = true;
222 
223     if(floating)                // just apply velocity
224     {
225         vadd(pl->o, d);
226         if(pl->jumpnext) { pl->jumpnext = false; pl->vel.z = 2;    }
227     }
228     else                        // apply velocity with collision
229     {
230         if(pl->onfloor || water)
231         {
232             if(pl->jumpnext)
233             {
234                 pl->jumpnext = false;
235                 pl->vel.z = 1.7f;       // physics impulse upwards
236                 if(water) { pl->vel.x /= 8; pl->vel.y /= 8; };      // dampen velocity change even harder, gives correct water feel
237                 if(local) playsoundc(S_JUMP);
238                 else if(pl->monsterstate) playsound(S_JUMP, &pl->o);
239             }
240             else if(pl->timeinair>800)  // if we land after long time must have been a high jump, make thud sound
241             {
242                 if(local) playsoundc(S_LAND);
243                 else if(pl->monsterstate) playsound(S_LAND, &pl->o);
244             };
245             pl->timeinair = 0;
246         }
247         else
248         {
249             pl->timeinair += curtime;
250         };
251 
252         const float gravity = 20;
253         const float f = 1.0f/moveres;
254         float dropf = ((gravity-1)+pl->timeinair/15.0f);        // incorrect, but works fine
255         if(water) { dropf = 5; pl->timeinair = 0; };            // float slowly down in water
256         const float drop = dropf*curtime/gravity/100/moveres;   // at high fps, gravity kicks in too fast
257         const float rise = speed/moveres/1.2f;                  // extra smoothness when lifting up stairs
258 
259         loopi(moveres)                                          // discrete steps collision detection & sliding
260         {
261             // try move forward
262             pl->o.x += f*d.x;
263             pl->o.y += f*d.y;
264             pl->o.z += f*d.z;
265             if(collide(pl, false, drop, rise)) continue;
266             // player stuck, try slide along y axis
267             pl->blocked = true;
268             pl->o.x -= f*d.x;
269             if(collide(pl, false, drop, rise)) { d.x = 0; continue; };
270             pl->o.x += f*d.x;
271             // still stuck, try x axis
272             pl->o.y -= f*d.y;
273             if(collide(pl, false, drop, rise)) { d.y = 0; continue; };
274             pl->o.y += f*d.y;
275             // try just dropping down
276             pl->moving = false;
277             pl->o.x -= f*d.x;
278             pl->o.y -= f*d.y;
279             if(collide(pl, false, drop, rise)) { d.y = d.x = 0; continue; };
280             pl->o.z -= f*d.z;
281             break;
282         };
283     };
284 
285     // detect wether player is outside map, used for skipping zbuffer clear mostly
286 
287     if(pl->o.x < 0 || pl->o.x >= ssize || pl->o.y <0 || pl->o.y > ssize)
288     {
289         pl->outsidemap = true;
290     }
291     else
292     {
293         sqr *s = S((int)pl->o.x, (int)pl->o.y);
294         pl->outsidemap = SOLID(s)
295            || pl->o.z < s->floor - (s->type==FHF ? s->vdelta/4 : 0)
296            || pl->o.z > s->ceil  + (s->type==CHF ? s->vdelta/4 : 0);
297     };
298 
299     // automatically apply smooth roll when strafing
300 
301     if(pl->strafe==0)
302     {
303         pl->roll = pl->roll/(1+(float)sqrt((float)curtime)/25);
304     }
305     else
306     {
307         pl->roll += pl->strafe*curtime/-30.0f;
308         if(pl->roll>maxroll) pl->roll = (float)maxroll;
309         if(pl->roll<-maxroll) pl->roll = (float)-maxroll;
310     };
311 
312     // play sounds on water transitions
313 
314     if(!pl->inwater && water) { playsound(S_SPLASH2, &pl->o); pl->vel.z = 0; }
315     else if(pl->inwater && !water) playsound(S_SPLASH1, &pl->o);
316     pl->inwater = water;
317 };
318 
moveplayer(dynent * pl,int moveres,bool local)319 void moveplayer(dynent *pl, int moveres, bool local)
320 {
321     loopi(physicsrepeat) moveplayer(pl, moveres, local, i ? curtime/physicsrepeat : curtime-curtime/physicsrepeat*(physicsrepeat-1));
322 };
323 
324