︙ | | | ︙ | |
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
if (d->monsterstate)
return false; // hack
headspace = d->o.z - o->o.z - o->aboveeye - d->eyeheight;
if (headspace < 0)
headspace = 10;
};
return true;
};
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
{
sqr *w = wmip[mip];
int sz = ssize >> mip;
bool stest =
SOLID(SWS(w, x + dx, y, sz)) && SOLID(SWS(w, x, y + dy, sz));
mip++;
x /= 2;
y /= 2;
if (SWS(wmip[mip], x, y, ssize >> mip)->type == CORNER) {
bx = x << mip;
by = y << mip;
bs = 1 << mip;
return cornertest(mip, x, y, dx, dy, bx, by, bs);
};
return stest;
};
void
mmcollide(dynent *d, float &hi, float &lo) // collide with a mapmodel
{
loopv(ents)
{
entity &e = ents[i];
|
<
>
<
>
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
if (d->monsterstate)
return false; // hack
headspace = d->o.z - o->o.z - o->aboveeye - d->eyeheight;
if (headspace < 0)
headspace = 10;
};
return true;
}
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
{
sqr *w = wmip[mip];
int sz = ssize >> mip;
bool stest =
SOLID(SWS(w, x + dx, y, sz)) && SOLID(SWS(w, x, y + dy, sz));
mip++;
x /= 2;
y /= 2;
if (SWS(wmip[mip], x, y, ssize >> mip)->type == CORNER) {
bx = x << mip;
by = y << mip;
bs = 1 << mip;
return cornertest(mip, x, y, dx, dy, bx, by, bs);
};
return stest;
}
void
mmcollide(dynent *d, float &hi, float &lo) // collide with a mapmodel
{
loopv(ents)
{
entity &e = ents[i];
|
︙ | | | ︙ | |
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
const float fy2 = d->o.y + d->radius;
const int x1 = fast_f2nat(fx1);
const int y1 = fast_f2nat(fy1);
const int x2 = fast_f2nat(fx2);
const int y2 = fast_f2nat(fy2);
float hi = 127, lo = -128;
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 :)
for (int x = x1; x <= x2; x++)
for (int y = y1; y <= y2; y++) // collide with map
{
if (OUTBORD(x, y))
return false;
sqr *s = S(x, y);
|
|
|
|
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
const float fy2 = d->o.y + d->radius;
const int x1 = fast_f2nat(fx1);
const int y1 = fast_f2nat(fy1);
const int x2 = fast_f2nat(fx2);
const int y2 = fast_f2nat(fy2);
float hi = 127, lo = -128;
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 :)
for (int x = x1; x <= x2; x++)
for (int y = y1; y <= y2; y++) // collide with map
{
if (OUTBORD(x, y))
return false;
sqr *s = S(x, y);
|
︙ | | | ︙ | |
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
};
case FHF: // FIXME: too simplistic collision with
// slopes, makes it feels like tiny stairs
floor -= (s->vdelta + S(x + 1, y)->vdelta +
S(x, y + 1)->vdelta +
S(x + 1, y + 1)->vdelta) /
16.0f;
break;
case CHF:
ceil += (s->vdelta + S(x + 1, y)->vdelta +
S(x, y + 1)->vdelta +
S(x + 1, y + 1)->vdelta) /
16.0f;
};
if (ceil < hi)
hi = ceil;
if (floor > lo)
lo = floor;
if (floor < minfloor)
return false;
};
if (hi - lo < d->eyeheight + d->aboveeye)
return false;
float headspace = 10;
loopv(players) // collide with other players
{
dynent *o = players[i];
if (!o || o == d)
continue;
if (!plcollide(d, o, headspace, hi, lo))
return false;
};
if (d != player1)
if (!plcollide(d, player1, headspace, hi, lo))
return false;
dvector &v = getmonsters();
// this loop can be a performance bottleneck with many monster on a slow
// cpu, should replace with a blockmap but seems mostly fast enough
loopv(v) if (!vreject(d->o, v[i]->o, 7.0f) && d != v[i] &&
!plcollide(d, v[i], headspace, hi, lo)) return false;
headspace -= 0.01f;
mmcollide(d, hi, lo); // collide with map models
if (spawn) {
d->o.z = lo + d->eyeheight; // just drop to floor (sideeffect)
d->onfloor = true;
|
|
|
<
>
|
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
};
case FHF: // FIXME: too simplistic collision with
// slopes, makes it feels like tiny stairs
floor -= (s->vdelta + S(x + 1, y)->vdelta +
S(x, y + 1)->vdelta +
S(x + 1, y + 1)->vdelta) /
16.0f;
break;
case CHF:
ceil += (s->vdelta + S(x + 1, y)->vdelta +
S(x, y + 1)->vdelta +
S(x + 1, y + 1)->vdelta) /
16.0f;
};
if (ceil < hi)
hi = ceil;
if (floor > lo)
lo = floor;
if (floor < minfloor)
return false;
};
if (hi - lo < d->eyeheight + d->aboveeye)
return false;
float headspace = 10;
loopv(players) // collide with other players
{
dynent *o = players[i];
if (!o || o == d)
continue;
if (!plcollide(d, o, headspace, hi, lo))
return false;
}
if (d != player1)
if (!plcollide(d, player1, headspace, hi, lo))
return false;
dvector &v = getmonsters();
// this loop can be a performance bottleneck with many monster on a slow
// cpu, should replace with a blockmap but seems mostly fast enough
loopv(v) if (!vreject(d->o, v[i]->o, 7.0f) && d != v[i] &&
!plcollide(d, v[i], headspace, hi, lo)) return false;
headspace -= 0.01f;
mmcollide(d, hi, lo); // collide with map models
if (spawn) {
d->o.z = lo + d->eyeheight; // just drop to floor (sideeffect)
d->onfloor = true;
|
︙ | | | ︙ | |
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
return true;
}
float
rad(float x)
{
return x * 3.14159f / 180;
};
VARP(maxroll, 0, 3, 20);
int physicsfraction = 0, physicsrepeat = 0;
const int MINFRAMETIME = 20; // physics always simulated at 50fps or better
void
physicsframe() // optimally schedule physics frames inside the graphics frames
{
if (curtime >= MINFRAMETIME) {
int faketime = curtime + physicsfraction;
physicsrepeat = faketime / MINFRAMETIME;
physicsfraction = faketime - physicsrepeat * MINFRAMETIME;
} else {
physicsrepeat = 1;
};
};
// main physics routine, moves a player/monster for a curtime step
// moveres indicated the physics precision (which is lower for monsters and
// multiplayer prediction) local is false for multiplayer prediction
void
moveplayer(dynent *pl, int moveres, bool local, int curtime)
|
<
>
<
>
|
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
return true;
}
float
rad(float x)
{
return x * 3.14159f / 180;
}
VARP(maxroll, 0, 3, 20);
int physicsfraction = 0, physicsrepeat = 0;
const int MINFRAMETIME = 20; // physics always simulated at 50fps or better
void
physicsframe() // optimally schedule physics frames inside the graphics frames
{
if (curtime >= MINFRAMETIME) {
int faketime = curtime + physicsfraction;
physicsrepeat = faketime / MINFRAMETIME;
physicsfraction = faketime - physicsrepeat * MINFRAMETIME;
} else {
physicsrepeat = 1;
};
}
// main physics routine, moves a player/monster for a curtime step
// moveres indicated the physics precision (which is lower for monsters and
// multiplayer prediction) local is false for multiplayer prediction
void
moveplayer(dynent *pl, int moveres, bool local, int curtime)
|
︙ | | | ︙ | |
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
}; // dampen velocity change even harder, gives
// correct water feel
if (local)
playsoundc(S_JUMP);
else if (pl->monsterstate)
playsound(S_JUMP, &pl->o);
} else if (pl->timeinair >
800) // if we land after long time must have
// been a high jump, make thud sound
{
if (local)
playsoundc(S_LAND);
else if (pl->monsterstate)
playsound(S_LAND, &pl->o);
};
pl->timeinair = 0;
} else {
pl->timeinair += curtime;
};
const float gravity = 20;
const float f = 1.0f / moveres;
float dropf =
((gravity - 1) +
pl->timeinair / 15.0f); // incorrect, but works fine
if (water) {
dropf = 5;
pl->timeinair = 0;
}; // float slowly down in water
const float drop =
dropf * curtime / gravity / 100 /
moveres; // at high fps, gravity kicks in too fast
const float rise =
speed / moveres /
1.2f; // extra smoothness when lifting up stairs
loopi(moveres) // discrete steps collision detection & sliding
{
// try move forward
pl->o.x += f * d.x;
pl->o.y += f * d.y;
|
|
|
|
<
|
<
|
|
<
|
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
}; // dampen velocity change even harder, gives
// correct water feel
if (local)
playsoundc(S_JUMP);
else if (pl->monsterstate)
playsound(S_JUMP, &pl->o);
} else if (pl->timeinair >
800) // if we land after long time must have
// been a high jump, make thud sound
{
if (local)
playsoundc(S_LAND);
else if (pl->monsterstate)
playsound(S_LAND, &pl->o);
};
pl->timeinair = 0;
} else {
pl->timeinair += curtime;
};
const float gravity = 20;
const float f = 1.0f / moveres;
float dropf = ((gravity - 1) +
pl->timeinair / 15.0f); // incorrect, but works fine
if (water) {
dropf = 5;
pl->timeinair = 0;
}; // float slowly down in water
const float drop = dropf * curtime / gravity / 100 /
moveres; // at high fps, gravity kicks in too fast
const float rise = speed / moveres /
1.2f; // extra smoothness when lifting up stairs
loopi(moveres) // discrete steps collision detection & sliding
{
// try move forward
pl->o.x += f * d.x;
pl->o.y += f * d.y;
|
︙ | | | ︙ | |
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
pl->o.y -= f * d.y;
if (collide(pl, false, drop, rise)) {
d.y = d.x = 0;
continue;
};
pl->o.z -= f * d.z;
break;
};
};
// detect wether player is outside map, used for skipping zbuffer clear
// mostly
if (pl->o.x < 0 || pl->o.x >= ssize || pl->o.y < 0 || pl->o.y > ssize) {
pl->outsidemap = true;
} else {
sqr *s = S((int)pl->o.x, (int)pl->o.y);
pl->outsidemap =
SOLID(s) ||
pl->o.z < s->floor - (s->type == FHF ? s->vdelta / 4 : 0) ||
pl->o.z > s->ceil + (s->type == CHF ? s->vdelta / 4 : 0);
};
// automatically apply smooth roll when strafing
if (pl->strafe == 0) {
|
<
>
|
<
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
pl->o.y -= f * d.y;
if (collide(pl, false, drop, rise)) {
d.y = d.x = 0;
continue;
};
pl->o.z -= f * d.z;
break;
}
};
// detect wether player is outside map, used for skipping zbuffer clear
// mostly
if (pl->o.x < 0 || pl->o.x >= ssize || pl->o.y < 0 || pl->o.y > ssize) {
pl->outsidemap = true;
} else {
sqr *s = S((int)pl->o.x, (int)pl->o.y);
pl->outsidemap = SOLID(s) ||
pl->o.z < s->floor - (s->type == FHF ? s->vdelta / 4 : 0) ||
pl->o.z > s->ceil + (s->type == CHF ? s->vdelta / 4 : 0);
};
// automatically apply smooth roll when strafing
if (pl->strafe == 0) {
|
︙ | | | ︙ | |
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
if (!pl->inwater && water) {
playsound(S_SPLASH2, &pl->o);
pl->vel.z = 0;
} else if (pl->inwater && !water)
playsound(S_SPLASH1, &pl->o);
pl->inwater = water;
};
void
moveplayer(dynent *pl, int moveres, bool local)
{
loopi(physicsrepeat) moveplayer(pl, moveres, local,
i ? curtime / physicsrepeat
: curtime - curtime / physicsrepeat * (physicsrepeat - 1));
};
|
<
>
<
>
|
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
if (!pl->inwater && water) {
playsound(S_SPLASH2, &pl->o);
pl->vel.z = 0;
} else if (pl->inwater && !water)
playsound(S_SPLASH1, &pl->o);
pl->inwater = water;
}
void
moveplayer(dynent *pl, int moveres, bool local)
{
loopi(physicsrepeat) moveplayer(pl, moveres, local,
i ? curtime / physicsrepeat
: curtime - curtime / physicsrepeat * (physicsrepeat - 1));
}
|