better physics
[carveJwlIkooP6JGAAIwe30JlM.git] / player_physics.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef PLAYER_PHYSICS_H
6 #define PLAYER_PHYSICS_H
7
8 #include "player.h"
9 #include "camera.h"
10
11 VG_STATIC void apply_gravity( v3f vel, float const timestep )
12 {
13 v3f gravity = { 0.0f, -9.6f, 0.0f };
14 v3_muladds( vel, gravity, timestep, vel );
15 }
16
17 /*
18 * Called when launching into the air to predict and adjust trajectories
19 */
20 VG_STATIC void player_start_air(void)
21 {
22 struct player_phys *phys = &player.phys;
23
24 if( phys->in_air )
25 return;
26
27 phys->in_air = 1;
28
29 float pstep = VG_TIMESTEP_FIXED * 10.0f;
30 float best_velocity_delta = -9999.9f;
31 float k_bias = 0.96f;
32
33 v3f axis;
34 v3_cross( phys->rb.up, phys->rb.v, axis );
35 v3_normalize( axis );
36 player.land_log_count = 0;
37
38 m3x3_identity( phys->vr );
39
40 for( int m=-3;m<=12; m++ )
41 {
42 float vmod = ((float)m / 15.0f)*0.09f;
43
44 v3f pco, pco1, pv;
45 v3_copy( phys->rb.co, pco );
46 v3_muls( phys->rb.v, k_bias, pv );
47
48 /*
49 * Try different 'rotations' of the velocity to find the best possible
50 * landing normal. This conserves magnitude at the expense of slightly
51 * unrealistic results
52 */
53
54 m3x3f vr;
55 v4f vr_q;
56
57 q_axis_angle( vr_q, axis, vmod );
58 q_m3x3( vr_q, vr );
59
60 m3x3_mulv( vr, pv, pv );
61 v3_muladds( pco, pv, pstep, pco );
62
63 for( int i=0; i<50; i++ )
64 {
65 v3_copy( pco, pco1 );
66 apply_gravity( pv, pstep );
67
68 m3x3_mulv( vr, pv, pv );
69 v3_muladds( pco, pv, pstep, pco );
70
71 ray_hit contact;
72 v3f vdir;
73
74 v3_sub( pco, pco1, vdir );
75 contact.dist = v3_length( vdir );
76 v3_divs( vdir, contact.dist, vdir);
77
78 if( ray_world( pco1, vdir, &contact ))
79 {
80 float land_delta = v3_dot( pv, contact.normal );
81 u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
82
83 /* Bias prediction towords ramps */
84 if( ray_hit_material( &contact )->info.flags
85 & k_material_flag_skate_surface )
86 {
87 land_delta *= 0.1f;
88 scolour |= 0x0000a000;
89 }
90
91 if( (land_delta < 0.0f) && (land_delta > best_velocity_delta) )
92 {
93 best_velocity_delta = land_delta;
94
95 v3_copy( contact.pos, player.land_target );
96
97 m3x3_copy( vr, phys->vr_pstep );
98 q_axis_angle( vr_q, axis, vmod*0.1f );
99 q_m3x3( vr_q, phys->vr );
100 }
101
102 v3_copy( contact.pos,
103 player.land_target_log[player.land_log_count] );
104 player.land_target_colours[player.land_log_count] =
105 0xff000000 | scolour;
106
107 player.land_log_count ++;
108
109 break;
110 }
111 }
112 }
113 }
114
115 /*
116 * Main friction interface model
117 */
118 VG_STATIC void player_physics_control(void)
119 {
120 struct player_phys *phys = &player.phys;
121
122 /*
123 * Computing localized friction forces for controlling the character
124 * Friction across X is significantly more than Z
125 */
126
127 v3f vel;
128 m3x3_mulv( phys->rb.to_local, phys->rb.v, vel );
129 float slip = 0.0f;
130
131 if( fabsf(vel[2]) > 0.01f )
132 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
133
134 if( fabsf( slip ) > 1.2f )
135 slip = vg_signf( slip ) * 1.2f;
136 phys->slip = slip;
137 phys->reverse = -vg_signf(vel[2]);
138
139 float substep = VG_TIMESTEP_FIXED * 0.2f;
140
141 #if 0
142 float fwd_resistance = vg_get_button( "break" )? 5.0f: k_friction_resistance;
143 #else
144 float fwd_resistance = k_friction_resistance;
145 #endif
146
147 for( int i=0; i<5; i++ )
148 {
149 vel[2] = stable_force( vel[2],vg_signf(vel[2]) * -fwd_resistance*substep);
150 vel[0] = stable_force( vel[0],vg_signf(vel[0]) * -k_friction_lat*substep);
151 }
152
153 if( player.input_jump->button.value )
154 {
155 phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed;
156
157 if( !phys->jump_charge )
158 phys->jump_dir = phys->reverse > 0.0f? 1: 0;
159
160 phys->jump_charge = 1;
161 }
162
163 static int push_thresh_last = 0;
164 float push = player.input_push->axis.value;
165 int push_thresh = push>0.15f? 1: 0;
166
167 if( push_thresh && !push_thresh_last )
168 player.phys.start_push = vg.time;
169
170 push_thresh_last = push_thresh;
171
172 if( !player.input_jump->button.value && push_thresh )
173 {
174 player.phys.pushing = 1.0f;
175 player.phys.push_time = vg.time - player.phys.start_push;
176
177 float cycle_time = player.phys.push_time*k_push_cycle_rate,
178 amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED,
179 current = v3_length( vel ),
180 new_vel = vg_minf( current + amt, k_max_push_speed );
181
182 new_vel -= vg_minf(current, k_max_push_speed);
183 vel[2] -= new_vel * phys->reverse;
184 }
185
186 /* Pumping */
187 static float previous = 0.0f;
188 float delta = previous - phys->grab,
189 pump = delta * k_pump_force * VG_TIMESTEP_FIXED;
190 previous = phys->grab;
191
192 v3f p1;
193 v3_muladds( phys->rb.co, phys->rb.up, pump, p1 );
194 vg_line( phys->rb.co, p1, 0xff0000ff );
195
196 vel[1] += pump;
197
198
199 m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
200
201 float steer = player.input_js1h->axis.value,
202 steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
203
204 phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
205 }
206
207 /*
208 * Air control, no real physics
209 */
210 VG_STATIC void player_physics_control_air(void)
211 {
212 struct player_phys *phys = &player.phys;
213
214 m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
215 vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
216
217 ray_hit hit;
218
219 /*
220 * Prediction
221 */
222 float pstep = VG_TIMESTEP_FIXED * 10.0f;
223
224 v3f pco, pco1, pv;
225 v3_copy( phys->rb.co, pco );
226 v3_copy( phys->rb.v, pv );
227
228 float time_to_impact = 0.0f;
229 float limiter = 1.0f;
230
231 for( int i=0; i<50; i++ )
232 {
233 v3_copy( pco, pco1 );
234 m3x3_mulv( phys->vr_pstep, pv, pv );
235 apply_gravity( pv, pstep );
236 v3_muladds( pco, pv, pstep, pco );
237
238 ray_hit contact;
239 v3f vdir;
240
241 v3_sub( pco, pco1, vdir );
242 contact.dist = v3_length( vdir );
243 v3_divs( vdir, contact.dist, vdir);
244
245 float orig_dist = contact.dist;
246 if( ray_world( pco1, vdir, &contact ))
247 {
248 float angle = v3_dot( phys->rb.up, contact.normal );
249 v3f axis;
250 v3_cross( phys->rb.up, contact.normal, axis );
251
252 time_to_impact += (contact.dist/orig_dist)*pstep;
253 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
254 limiter = 1.0f-limiter;
255 limiter *= limiter;
256 limiter = 1.0f-limiter;
257
258 if( angle < 0.99f )
259 {
260 v4f correction;
261 q_axis_angle( correction, axis,
262 acosf(angle)*(1.0f-limiter)*3.0f*VG_TIMESTEP_FIXED );
263 q_mul( correction, phys->rb.q, phys->rb.q );
264 }
265
266 vg_line_cross( contact.pos, 0xffff0000, 0.25f );
267 break;
268 }
269 time_to_impact += pstep;
270 }
271
272 float steerh = player.input_js1h->axis.value,
273 steerv = player.input_js1v->axis.value;
274
275 phys->iY -= steerh * k_steer_air * VG_TIMESTEP_FIXED;
276
277 {
278 float iX = steerv *
279 phys->reverse * k_steer_air * limiter * VG_TIMESTEP_FIXED;
280
281 static float siX = 0.0f;
282 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
283
284 v4f rotate;
285 q_axis_angle( rotate, phys->rb.right, siX );
286 q_mul( rotate, phys->rb.q, phys->rb.q );
287 }
288
289 #if 0
290 v2f target = {0.0f,0.0f};
291 v2_muladds( target, (v2f){ vg_get_axis("grabh"), vg_get_axis("grabv") },
292 phys->grab, target );
293 #endif
294 }
295
296 VG_STATIC void player_walk_update_collision(void)
297 {
298 struct player_phys *phys = &player.phys;
299 float h0 = 0.3f,
300 h1 = 0.9f;
301
302 rigidbody *rbf = &player.collide_front,
303 *rbb = &player.collide_back;
304
305 v3_add( phys->rb.co, (v3f){0.0f,h0,0.0f}, rbf->co );
306 v3_add( phys->rb.co, (v3f){0.0f,h1,0.0f}, rbb->co );
307 v3_copy( rbf->co, rbf->to_world[3] );
308 v3_copy( rbb->co, rbb->to_world[3] );
309 m4x3_invert_affine( rbf->to_world, rbf->to_local );
310 m4x3_invert_affine( rbb->to_world, rbb->to_local );
311
312 rb_update_bounds( rbf );
313 rb_update_bounds( rbb );
314 }
315
316 VG_STATIC void player_integrate(void);
317 /*
318 * Entire Walking physics model
319 * TODO: sleep when under certain velotiy
320 */
321 VG_STATIC void player_walk_physics(void)
322 {
323 struct player_phys *phys = &player.phys;
324 rigidbody *rbf = &player.collide_front,
325 *rbb = &player.collide_back;
326
327 m3x3_identity( player.collide_front.to_world );
328 m3x3_identity( player.collide_back.to_world );
329
330 v3_zero( phys->rb.w );
331 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
332
333 rb_ct manifold[64];
334 int len;
335
336 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
337 v3f right_dir = { -forward_dir[2], 0.0f, forward_dir[0] };
338
339 if( phys->in_air )
340 {
341 player_walk_update_collision();
342 rb_debug( rbf, 0xff0000ff );
343 rb_debug( rbb, 0xff0000ff );
344
345 len = 0;
346 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
347 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
348 rb_presolve_contacts( manifold, len );
349
350 for( int i=0; i<len; i++ )
351 {
352 struct contact *ct = &manifold[i];
353 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
354 phys->in_air = 0;
355 }
356
357 for( int j=0; j<5; j++ )
358 {
359 for( int i=0; i<len; i++ )
360 {
361 struct contact *ct = &manifold[i];
362
363 /*normal */
364 float vn = -v3_dot( phys->rb.v, ct->n );
365 vn += ct->bias;
366
367 float temp = ct->norm_impulse;
368 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
369 vn = ct->norm_impulse - temp;
370
371 v3f impulse;
372 v3_muls( ct->n, vn, impulse );
373
374 v3_add( impulse, phys->rb.v, phys->rb.v );
375
376 /* friction */
377 for( int j=0; j<2; j++ )
378 {
379 float f = k_friction * ct->norm_impulse,
380 vt = v3_dot( phys->rb.v, ct->t[j] ),
381 lambda = -vt;
382
383 float temp = ct->tangent_impulse[j];
384 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
385 lambda = ct->tangent_impulse[j] - temp;
386
387 v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
388 }
389 }
390 }
391
392 player_integrate();
393 }
394 else
395 {
396 /* translate player */
397 v2f walk = { player.input_walkh->axis.value,
398 player.input_walkv->axis.value };
399
400 if( v2_length2(walk) > 0.001f )
401 v2_normalize( walk );
402
403 v2_muls( walk, vg_maxf( player.input_push->axis.value, 0.5f ) *
404 k_walkspeed * VG_TIMESTEP_FIXED, walk );
405
406 v3f walk_apply;
407 v3_zero( walk_apply );
408
409 /* Do XY translation */
410 v3_muladds( walk_apply, right_dir, walk[0], walk_apply );
411 v3_muladds( walk_apply, forward_dir, walk[1], walk_apply );
412 v3_add( walk_apply, phys->rb.co, phys->rb.co );
413 v3_divs( walk_apply, VG_TIMESTEP_FIXED, phys->rb.v );
414
415 /* Directly resolve collisions */
416 player_walk_update_collision();
417 rb_debug( rbf, 0xffffff00 );
418 rb_debug( rbb, 0xffffff00 );
419
420 len = 0;
421 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
422 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
423
424 v3f dt;
425 v3_zero( dt );
426 for( int j=0; j<3; j++ )
427 {
428 for( int i=0; i<len; i++ )
429 {
430 struct contact *ct = &manifold[i];
431
432 float p = vg_maxf( 0.0f, ct->p - 0.00f ),
433 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
434 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
435 }
436 }
437 v3_add( dt, phys->rb.co, phys->rb.co );
438
439 /* jump */
440 if( player.input_jump->button.value )
441 {
442 phys->rb.v[1] = 5.0f;
443 phys->in_air = 1;
444 return;
445 }
446
447 /* if we've put us in the air, step down slowly */
448 phys->in_air = 1;
449 float max_dist = 0.3f,
450 start_y = phys->rb.co[1];
451
452 for( int j=0; j<8; j++ )
453 {
454 for( int i=0; i<len; i++ )
455 {
456 struct contact *ct = &manifold[i];
457 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
458 {
459 phys->in_air = 0;
460 if( j == 0 )
461 return;
462
463 v3f dt;
464 v3_zero( dt );
465 for( int j=0; j<3; j++ )
466 {
467 for( int i=0; i<len; i++ )
468 {
469 struct contact *ct = &manifold[i];
470
471 float p = vg_maxf( 0.0f, ct->p - 0.0025f ),
472 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
473 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
474 }
475 }
476 v3_add( dt, phys->rb.co, phys->rb.co );
477 return;
478 }
479 }
480
481 phys->rb.co[1] -= max_dist * 0.125f;
482
483 player_walk_update_collision();
484 len = 0;
485 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
486 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
487 }
488
489 /* Transitioning into air mode */
490 phys->rb.co[1] = start_y;
491 }
492 }
493
494 /*
495 * Physics collision detection, and control
496 */
497 VG_STATIC void player_physics(void)
498 {
499 struct player_phys *phys = &player.phys;
500 /*
501 * Update collision fronts
502 */
503
504 rigidbody *rbf = &player.collide_front,
505 *rbb = &player.collide_back;
506
507 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
508 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
509
510 player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
511 float h = player.air_blend*0.2f;
512
513 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
514 v3_copy( rbf->co, rbf->to_world[3] );
515 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
516 v3_copy( rbb->co, rbb->to_world[3] );
517
518 m4x3_invert_affine( rbf->to_world, rbf->to_local );
519 m4x3_invert_affine( rbb->to_world, rbb->to_local );
520
521 rb_update_bounds( rbf );
522 rb_update_bounds( rbb );
523
524 rb_debug( rbf, 0xff00ffff );
525 rb_debug( rbb, 0xffffff00 );
526
527 rb_ct manifold[64];
528 int len = 0;
529
530 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
531 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
532
533 rb_presolve_contacts( manifold, len );
534 v3f surface_avg = {0.0f, 0.0f, 0.0f};
535
536 if( !len )
537 {
538 player_start_air();
539 }
540 else
541 {
542 for( int i=0; i<len; i++ )
543 {
544 v3_add( manifold[i].n, surface_avg, surface_avg );
545 }
546
547 v3_normalize( surface_avg );
548
549 if( v3_dot( phys->rb.v, surface_avg ) > 0.5f )
550 {
551 player_start_air();
552 }
553 else
554 {
555 phys->in_air = 0;
556 }
557 }
558
559 for( int j=0; j<5; j++ )
560 {
561 for( int i=0; i<len; i++ )
562 {
563 struct contact *ct = &manifold[i];
564
565 v3f dv, delta;
566 v3_sub( ct->co, phys->rb.co, delta );
567 v3_cross( phys->rb.w, delta, dv );
568 v3_add( phys->rb.v, dv, dv );
569
570 float vn = -v3_dot( dv, ct->n );
571 vn += ct->bias;
572
573 float temp = ct->norm_impulse;
574 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
575 vn = ct->norm_impulse - temp;
576
577 v3f impulse;
578 v3_muls( ct->n, vn, impulse );
579
580 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
581 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
582 {
583 player_kill();
584 return;
585 }
586
587 v3_add( impulse, phys->rb.v, phys->rb.v );
588 v3_cross( delta, impulse, impulse );
589
590 /*
591 * W Impulses are limited to the Y and X axises, we don't really want
592 * roll angular velocities being included.
593 *
594 * Can also tweak the resistance of each axis here by scaling the wx,wy
595 * components.
596 */
597
598 float wy = v3_dot( phys->rb.up, impulse ),
599 wx = v3_dot( phys->rb.right, impulse )*1.5f;
600
601 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
602 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
603 }
604 }
605
606 float grabt = vg_maxf( player.input_grab->axis.value,
607 vg_maxf( fabsf( player.input_emjs2h->axis.value ),
608 fabsf( player.input_emjs2v->axis.value ) )
609 );
610
611 phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
612 player.phys.pushing = 0.0f;
613
614 if( !phys->in_air )
615 {
616 #if 0
617 v3f axis;
618 float angle = v3_dot( phys->rb.up, surface_avg );
619 v3_cross( phys->rb.up, surface_avg, axis );
620
621 //float cz = v3_dot( player.rb.forward, axis );
622 //v3_muls( player.rb.forward, cz, axis );
623
624 if( angle < 0.999f )
625 {
626 v4f correction;
627 q_axis_angle( correction, axis, acosf(angle)*18.0f*VG_TIMESTEP_FIXED );
628 q_mul( correction, phys->rb.q, phys->rb.q );
629 }
630 #else
631
632 /* 20/10/22: make this only go axisways instead, may effect velocities. */
633
634 v3f projected, axis;
635
636 float d = v3_dot( phys->rb.forward, surface_avg );
637 v3_muladds( surface_avg, phys->rb.forward, -d, projected );
638 v3_normalize( projected );
639
640 float angle = v3_dot( phys->rb.up, projected );
641 v3_cross( phys->rb.up, projected, axis );
642
643 v3f p0, p1;
644 v3_add( phys->rb.co, projected, p0 );
645 v3_add( phys->rb.co, phys->rb.up, p1 );
646 vg_line( phys->rb.co, p0, 0xff00ff00 );
647 vg_line( phys->rb.co, p1, 0xff000fff );
648
649 if( fabsf(angle) < 0.999f )
650 {
651 v4f correction;
652 q_axis_angle( correction, axis, acosf(angle)*4.0f*VG_TIMESTEP_FIXED );
653 q_mul( correction, phys->rb.q, phys->rb.q );
654 }
655
656
657 #endif
658
659 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
660 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
661
662 player_physics_control();
663
664 if( !phys->jump_charge && phys->jump > 0.2f )
665 {
666 v3f jumpdir;
667
668 /* Launch more up if alignment is up else improve velocity */
669 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
670 mod = 0.5f,
671 dir = mod + aup*(1.0f-mod);
672
673 v3_copy( phys->rb.v, jumpdir );
674 v3_normalize( jumpdir );
675 v3_muls( jumpdir, 1.0f-dir, jumpdir );
676 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
677 v3_normalize( jumpdir );
678
679 float force = k_jump_force*phys->jump;
680 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
681 phys->jump = 0.0f;
682
683 player.jump_time = vg.time;
684
685 /* TODO: Move to audio file */
686 audio_lock();
687 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
688 audio_player_set_position( &audio_player_extra, phys->rb.co );
689 audio_player_set_vol( &audio_player_extra, 20.0f );
690 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
691 audio_unlock();
692 }
693 }
694 else
695 {
696 player_physics_control_air();
697 }
698
699 if( !phys->jump_charge )
700 {
701 phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
702 }
703
704 phys->jump_charge = 0;
705 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
706 }
707
708 VG_STATIC void player_save_frame(void)
709 {
710 player.phys_gate_frame = player.phys;
711 }
712
713 VG_STATIC void player_restore_frame(void)
714 {
715 player.phys = player.phys_gate_frame;
716 rb_update_transform( &player.phys.rb );
717 }
718
719 VG_STATIC void player_integrate(void)
720 {
721 struct player_phys *phys = &player.phys;
722 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
723 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
724 }
725
726 VG_STATIC void player_do_motion(void)
727 {
728 struct player_phys *phys = &player.phys;
729
730 if( world.water.enabled )
731 {
732 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
733 {
734 audio_lock();
735 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
736 audio_player_set_position( &audio_player_extra, phys->rb.co );
737 audio_player_set_vol( &audio_player_extra, 20.0f );
738 audio_player_playclip( &audio_player_extra, &audio_splash );
739 audio_unlock();
740
741 player_kill();
742 }
743 }
744
745
746 v3f prevco;
747 v3_copy( phys->rb.co, prevco );
748
749 if( phys->on_board )
750 {
751 player_physics();
752 player_integrate();
753 }
754 else
755 player_walk_physics();
756
757
758 /* Real angular velocity integration */
759 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, phys->rb.w );
760 if( v3_length2( phys->rb.w ) > 0.0f )
761 {
762 v4f rotation;
763 v3f axis;
764 v3_copy( phys->rb.w, axis );
765
766 float mag = v3_length( axis );
767 v3_divs( axis, mag, axis );
768 q_axis_angle( rotation, axis, mag*k_rb_delta );
769 q_mul( rotation, phys->rb.q, phys->rb.q );
770 }
771
772 /* Faux angular velocity */
773 v4f rotate;
774
775 float lerpq = phys->in_air? 0.04f: 0.3f;
776 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
777
778 q_axis_angle( rotate, phys->rb.up, phys->siY );
779 q_mul( rotate, phys->rb.q, phys->rb.q );
780 phys->iY = 0.0f;
781
782 /*
783 * Gate intersection, by tracing a line over the gate planes
784 */
785 for( int i=0; i<world.gate_count; i++ )
786 {
787 struct route_gate *rg = &world.gates[i];
788 teleport_gate *gate = &rg->gate;
789
790 if( gate_intersect( gate, phys->rb.co, prevco ) )
791 {
792 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
793 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
794 m3x3_mulv( gate->transport, phys->vl, phys->vl );
795 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
796 m3x3_mulv( gate->transport, phys->m, phys->m );
797 m3x3_mulv( gate->transport, phys->bob, phys->bob );
798
799 v4f transport_rotation;
800 m3x3_q( gate->transport, transport_rotation );
801 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
802
803 world_routes_activate_gate( i );
804
805 if( !phys->on_board )
806 {
807 v3f fwd_dir = {cosf(player.angles[0]),
808 0.0f,
809 sinf(player.angles[0])};
810 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
811
812 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
813 }
814
815 player.rewind_length = 0;
816 player.rewind_total_length = 0.0f;
817 player.rewind_incrementer = 10000;
818 player_save_frame();
819
820 audio_lock();
821 audio_play_oneshot( &audio_gate_pass, 1.0f );
822 audio_unlock();
823 break;
824 }
825 }
826
827 rb_update_transform( &phys->rb );
828 }
829
830 VG_STATIC void player_freecam(void)
831 {
832 player_mouseview();
833
834 float movespeed = fc_speed;
835 v3f lookdir = { 0.0f, 0.0f, -1.0f },
836 sidedir = { 1.0f, 0.0f, 0.0f };
837
838 m3x3_mulv( camera_mtx, lookdir, lookdir );
839 m3x3_mulv( camera_mtx, sidedir, sidedir );
840
841 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
842 if( vg_get_button( "forward" ) )
843 v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED * movespeed, move_vel );
844 if( vg_get_button( "back" ) )
845 v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED *-movespeed, move_vel );
846 if( vg_get_button( "left" ) )
847 v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED *-movespeed, move_vel );
848 if( vg_get_button( "right" ) )
849 v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED * movespeed, move_vel );
850
851 v3_muls( move_vel, 0.7f, move_vel );
852 v3_add( move_vel, player.camera_pos, player.camera_pos );
853 }
854
855 VG_STATIC int reset_player( int argc, char const *argv[] )
856 {
857 struct player_phys *phys = &player.phys;
858 struct respawn_point *rp = NULL, *r;
859
860 if( argc == 1 )
861 {
862 for( int i=0; i<world.spawn_count; i++ )
863 {
864 r = &world.spawns[i];
865 if( !strcmp( r->name, argv[0] ) )
866 {
867 rp = r;
868 break;
869 }
870 }
871
872 if( !rp )
873 vg_warn( "No spawn named '%s'\n", argv[0] );
874 }
875
876 if( !rp )
877 {
878 float min_dist = INFINITY;
879
880 for( int i=0; i<world.spawn_count; i++ )
881 {
882 r = &world.spawns[i];
883 float d = v3_dist2( r->co, phys->rb.co );
884
885 vg_info( "Dist %s : %f\n", r->name, d );
886 if( d < min_dist )
887 {
888 min_dist = d;
889 rp = r;
890 }
891 }
892 }
893
894 if( !rp )
895 {
896 vg_error( "No spawn found\n" );
897 if( !world.spawn_count )
898 return 0;
899
900 rp = &world.spawns[0];
901 }
902
903 player.is_dead = 0;
904
905 m3x3f the_long_way;
906 q_m3x3( rp->q, the_long_way );
907
908 v3f delta = {1.0f,0.0f,0.0f};
909 m3x3_mulv( the_long_way, delta, delta );
910
911 player.angles[0] = atan2f( delta[0], -delta[2] );
912 player.angles[1] = -asinf( delta[1] );
913
914
915 v4_copy( rp->q, phys->rb.q );
916 v3_copy( rp->co, phys->rb.co );
917 v3_zero( phys->rb.v );
918
919 phys->vswitch = 1.0f;
920 phys->slip_last = 0.0f;
921 phys->in_air = 1;
922 phys->on_board = 0;
923 m3x3_identity( phys->vr );
924
925 player.mdl.shoes[0] = 1;
926 player.mdl.shoes[1] = 1;
927
928 rb_update_transform( &phys->rb );
929 player_save_frame();
930 return 1;
931 }
932
933 #endif /* PLAYER_PHYSICS_H */