56698f87e2ecdfd65fd44e083eed6b522d386c88
[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 VG_STATIC struct
18 grind_edge *player_grind_collect_edge( v3f p0, v3f p1,
19 v3f c0, v3f c1, float max_dist )
20 {
21 struct player_phys *phys = &player.phys;
22
23 bh_iter it;
24 bh_iter_init( 0, &it );
25
26 boxf region;
27
28 box_init_inf( region );
29 box_addpt( region, p0 );
30 box_addpt( region, p1 );
31
32 float k_r = max_dist;
33 v3_add( (v3f){ k_r, k_r, k_r}, region[1], region[1] );
34 v3_add( (v3f){-k_r,-k_r,-k_r}, region[0], region[0] );
35
36 float closest = k_r*k_r;
37 struct grind_edge *closest_edge = NULL;
38
39 int idx;
40 while( bh_next( world.grind_bh, &it, region, &idx ) )
41 {
42 struct grind_edge *edge = &world.grind_edges[ idx ];
43
44 float s,t;
45 v3f pa, pb;
46
47 float d2 =
48 closest_segment_segment( p0, p1, edge->p0, edge->p1, &s,&t, pa, pb );
49
50 if( d2 < closest )
51 {
52 closest = d2;
53 closest_edge = edge;
54 v3_copy( pa, c0 );
55 v3_copy( pb, c1 );
56 }
57 }
58
59 return closest_edge;
60 }
61
62 /*
63 * Called when launching into the air to predict and adjust trajectories
64 */
65 VG_STATIC void player_start_air(void)
66 {
67 struct player_phys *phys = &player.phys;
68
69 float pstep = VG_TIMESTEP_FIXED * 10.0f;
70 float best_velocity_delta = -9999.9f;
71 float k_bias = 0.96f;
72
73 v3f axis;
74 v3_cross( phys->rb.up, phys->rb.v, axis );
75 v3_normalize( axis );
76 player.land_log_count = 0;
77
78 m3x3_identity( phys->vr );
79
80 for( int m=-3;m<=12; m++ )
81 {
82 struct land_log *log = &player.land_log[ player.land_log_count ++ ];
83 log->count = 0;
84 log->colour = 0xff000000;
85
86 float vmod = ((float)m / 15.0f)*0.09f;
87
88 v3f pco, pco1, pv;
89 v3_copy( phys->rb.co, pco );
90 v3_muls( phys->rb.v, k_bias, pv );
91
92 /*
93 * Try different 'rotations' of the velocity to find the best possible
94 * landing normal. This conserves magnitude at the expense of slightly
95 * unrealistic results
96 */
97
98 m3x3f vr;
99 v4f vr_q;
100
101 q_axis_angle( vr_q, axis, vmod );
102 q_m3x3( vr_q, vr );
103
104 m3x3_mulv( vr, pv, pv );
105 v3_muladds( pco, pv, pstep, pco );
106
107 struct grind_edge *best_grind = NULL;
108 float closest_grind = INFINITY;
109
110 for( int i=0; i<50; i++ )
111 {
112 v3_copy( pco, pco1 );
113 apply_gravity( pv, pstep );
114
115 m3x3_mulv( vr, pv, pv );
116 v3_muladds( pco, pv, pstep, pco );
117
118 ray_hit contact;
119 v3f vdir;
120
121 v3_sub( pco, pco1, vdir );
122 contact.dist = v3_length( vdir );
123 v3_divs( vdir, contact.dist, vdir);
124
125 v3f c0, c1;
126 struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
127 c0, c1, 0.4f );
128
129 if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
130 {
131 float d2 = v3_dist2( c0, c1 );
132 if( d2 < closest_grind )
133 {
134 closest_grind = d2;
135 best_grind = ge;
136 }
137 }
138
139 if( ray_world( pco1, vdir, &contact ))
140 {
141 float land_delta = v3_dot( pv, contact.normal );
142 u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
143
144 /* Bias prediction towords ramps */
145 if( ray_hit_material( &contact )->info.flags
146 & k_material_flag_skate_surface )
147 {
148 land_delta *= 0.1f;
149 scolour |= 0x0000a000;
150 }
151
152 if( (land_delta < 0.0f) && (land_delta > best_velocity_delta) )
153 {
154 best_velocity_delta = land_delta;
155
156 v3_copy( contact.pos, player.land_target );
157
158 m3x3_copy( vr, phys->vr_pstep );
159 q_axis_angle( vr_q, axis, vmod*0.1f );
160 q_m3x3( vr_q, phys->vr );
161 }
162
163 v3_copy( contact.pos, log->positions[ log->count ++ ] );
164 log->colour = 0xff000000 | scolour;
165 break;
166 }
167
168 v3_copy( pco, log->positions[ log->count ++ ] );
169 }
170
171 if( best_grind )
172 {
173 log->colour = 0xff0000ff;
174
175 float score = -closest_grind * 0.05f;
176
177 if( score > best_velocity_delta )
178 {
179 best_velocity_delta = score;
180
181 m3x3_copy( vr, phys->vr_pstep );
182 q_axis_angle( vr_q, axis, vmod*0.1f );
183 q_m3x3( vr_q, phys->vr );
184 }
185 }
186 }
187 }
188
189
190 VG_STATIC void player_physics_control_passive(void)
191 {
192 struct player_phys *phys = &player.phys;
193 float grabt = player.input_grab->axis.value;
194
195 if( grabt > 0.5f )
196 {
197 v2_muladds( phys->grab_mouse_delta, vg.mouse_delta, 0.02f,
198 phys->grab_mouse_delta );
199 v2_normalize_clamp( phys->grab_mouse_delta );
200
201 if( freecam )
202 v2_zero( phys->grab_mouse_delta );
203 }
204 else
205 v2_zero( phys->grab_mouse_delta );
206
207 phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
208 player.phys.pushing = 0.0f;
209
210 if( !phys->jump_charge || phys->in_air )
211 {
212 phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
213 }
214
215 phys->jump_charge = 0;
216 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
217 }
218
219 /*
220 * Main friction interface model
221 */
222 VG_STATIC void player_physics_control(void)
223 {
224 struct player_phys *phys = &player.phys;
225
226 /*
227 * Computing localized friction forces for controlling the character
228 * Friction across X is significantly more than Z
229 */
230
231 v3f vel;
232 m3x3_mulv( phys->rb.to_local, phys->rb.v, vel );
233 float slip = 0.0f;
234
235 if( fabsf(vel[2]) > 0.01f )
236 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
237
238 if( fabsf( slip ) > 1.2f )
239 slip = vg_signf( slip ) * 1.2f;
240 phys->slip = slip;
241 phys->reverse = -vg_signf(vel[2]);
242
243 float substep = VG_TIMESTEP_FIXED * 0.2f;
244 float fwd_resistance = k_friction_resistance;
245
246 for( int i=0; i<5; i++ )
247 {
248 vel[2] = stable_force( vel[2],vg_signf(vel[2]) * -fwd_resistance*substep);
249 vel[0] = stable_force( vel[0],vg_signf(vel[0]) * -k_friction_lat*substep);
250 }
251
252 if( player.input_jump->button.value )
253 {
254 phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed;
255
256 if( !phys->jump_charge )
257 phys->jump_dir = phys->reverse > 0.0f? 1: 0;
258
259 phys->jump_charge = 1;
260 }
261
262 static int push_thresh_last = 0;
263 float push = player.input_push->button.value;
264 int push_thresh = push>0.15f? 1: 0;
265
266 if( push_thresh && !push_thresh_last )
267 player.phys.start_push = vg.time;
268
269 push_thresh_last = push_thresh;
270
271 if( !player.input_jump->button.value && push_thresh )
272 {
273 player.phys.pushing = 1.0f;
274 player.phys.push_time = vg.time - player.phys.start_push;
275
276 float cycle_time = player.phys.push_time*k_push_cycle_rate,
277 amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED,
278 current = v3_length( vel ),
279 new_vel = vg_minf( current + amt, k_max_push_speed );
280
281 new_vel -= vg_minf(current, k_max_push_speed);
282 vel[2] -= new_vel * phys->reverse;
283 }
284
285 m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
286
287 float input = player.input_js1h->axis.value,
288 grab = player.input_grab->axis.value,
289 steer = input * (1.0f-(phys->jump+grab)*0.4f),
290 steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
291
292 phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
293
294 if( !phys->jump_charge && phys->jump > 0.2f )
295 {
296 v3f jumpdir;
297
298 /* Launch more up if alignment is up else improve velocity */
299 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
300 mod = 0.5f,
301 dir = mod + aup*(1.0f-mod);
302
303 v3_copy( phys->rb.v, jumpdir );
304 v3_normalize( jumpdir );
305 v3_muls( jumpdir, 1.0f-dir, jumpdir );
306 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
307 v3_normalize( jumpdir );
308
309 float force = k_jump_force*phys->jump;
310 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
311 phys->jump = 0.0f;
312
313 player.jump_time = vg.time;
314
315 /* TODO: Move to audio file */
316 audio_lock();
317 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
318 audio_player_set_position( &audio_player_extra, phys->rb.co );
319 audio_player_set_vol( &audio_player_extra, 20.0f );
320 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
321 audio_unlock();
322 }
323 }
324
325 VG_STATIC void player_physics_control_grind(void)
326 {
327 struct player_phys *phys = &player.phys;
328 v2f steer = { player.input_js1h->axis.value,
329 player.input_js1v->axis.value };
330
331 float l2 = v2_length2( steer );
332 if( l2 > 1.0f )
333 v2_muls( steer, 1.0f/sqrtf(l2), steer );
334
335 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
336
337 float iX = steer[1] * phys->reverse * k_steer_air * VG_TIMESTEP_FIXED;
338
339 static float siX = 0.0f;
340 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
341
342 v4f rotate;
343 q_axis_angle( rotate, phys->rb.right, siX );
344 q_mul( rotate, phys->rb.q, phys->rb.q );
345
346 phys->slip = 0.0f;
347 }
348
349 /*
350 * Air control, no real physics
351 */
352 VG_STATIC void player_physics_control_air(void)
353 {
354 struct player_phys *phys = &player.phys;
355
356 m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
357 vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
358
359 ray_hit hit;
360
361 /*
362 * Prediction
363 */
364 float pstep = VG_TIMESTEP_FIXED * 10.0f;
365
366 v3f pco, pco1, pv;
367 v3_copy( phys->rb.co, pco );
368 v3_copy( phys->rb.v, pv );
369
370 float time_to_impact = 0.0f;
371 float limiter = 1.0f;
372
373 struct grind_edge *best_grind = NULL;
374 float closest_grind = INFINITY;
375
376 v3f target_normal = { 0.0f, 1.0f, 0.0f };
377 int has_target = 0;
378
379 for( int i=0; i<50; i++ )
380 {
381 v3_copy( pco, pco1 );
382 m3x3_mulv( phys->vr_pstep, pv, pv );
383 apply_gravity( pv, pstep );
384 v3_muladds( pco, pv, pstep, pco );
385
386 ray_hit contact;
387 v3f vdir;
388
389 v3_sub( pco, pco1, vdir );
390 contact.dist = v3_length( vdir );
391 v3_divs( vdir, contact.dist, vdir);
392
393 v3f c0, c1;
394 struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
395 c0, c1, 0.4f );
396
397 if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
398 {
399 vg_line( ge->p0, ge->p1, 0xff0000ff );
400 vg_line_cross( pco, 0xff0000ff, 0.25f );
401 has_target = 1;
402 break;
403 }
404
405 float orig_dist = contact.dist;
406 if( ray_world( pco1, vdir, &contact ) )
407 {
408 v3_copy( contact.normal, target_normal );
409 has_target = 1;
410 time_to_impact += (contact.dist/orig_dist)*pstep;
411 vg_line_cross( contact.pos, 0xffff0000, 0.25f );
412 break;
413 }
414 time_to_impact += pstep;
415 }
416
417 if( has_target )
418 {
419 float angle = v3_dot( phys->rb.up, target_normal );
420 v3f axis;
421 v3_cross( phys->rb.up, target_normal, axis );
422
423 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
424 limiter = 1.0f-limiter;
425 limiter *= limiter;
426 limiter = 1.0f-limiter;
427
428 if( fabsf(angle) < 0.99f )
429 {
430 v4f correction;
431 q_axis_angle( correction, axis,
432 acosf(angle)*(1.0f-limiter)*3.0f*VG_TIMESTEP_FIXED );
433 q_mul( correction, phys->rb.q, phys->rb.q );
434 }
435 }
436
437 v2f steer = { player.input_js1h->axis.value,
438 player.input_js1v->axis.value };
439
440 float l2 = v2_length2( steer );
441 if( l2 > 1.0f )
442 v2_muls( steer, 1.0f/sqrtf(l2), steer );
443
444 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
445
446 float iX = steer[1] *
447 phys->reverse * k_steer_air * limiter * VG_TIMESTEP_FIXED;
448
449 static float siX = 0.0f;
450 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
451
452 v4f rotate;
453 q_axis_angle( rotate, phys->rb.right, siX );
454 q_mul( rotate, phys->rb.q, phys->rb.q );
455
456 #if 0
457 v2f target = {0.0f,0.0f};
458 v2_muladds( target, (v2f){ vg_get_axis("grabh"), vg_get_axis("grabv") },
459 phys->grab, target );
460 #endif
461 }
462
463 VG_STATIC void player_walk_update_collision(void)
464 {
465 struct player_phys *phys = &player.phys;
466 float h0 = 0.3f,
467 h1 = 0.9f;
468
469 rigidbody *rbf = &player.collide_front,
470 *rbb = &player.collide_back;
471
472 v3_add( phys->rb.co, (v3f){0.0f,h0,0.0f}, rbf->co );
473 v3_add( phys->rb.co, (v3f){0.0f,h1,0.0f}, rbb->co );
474 v3_copy( rbf->co, rbf->to_world[3] );
475 v3_copy( rbb->co, rbb->to_world[3] );
476 m4x3_invert_affine( rbf->to_world, rbf->to_local );
477 m4x3_invert_affine( rbb->to_world, rbb->to_local );
478
479 rb_update_bounds( rbf );
480 rb_update_bounds( rbb );
481 }
482
483 VG_STATIC void player_integrate(void);
484 /*
485 * Entire Walking physics model
486 * TODO: sleep when under certain velotiy
487 */
488 VG_STATIC void player_walk_physics(void)
489 {
490 struct player_phys *phys = &player.phys;
491 rigidbody *rbf = &player.collide_front,
492 *rbb = &player.collide_back;
493
494 m3x3_identity( player.collide_front.to_world );
495 m3x3_identity( player.collide_back.to_world );
496
497 v3_zero( phys->rb.w );
498 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
499
500 rb_ct manifold[64];
501 int len;
502
503 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
504 v3f right_dir = { -forward_dir[2], 0.0f, forward_dir[0] };
505
506 v2f walk = { player.input_walkh->axis.value,
507 player.input_walkv->axis.value };
508
509 if( freecam )
510 v2_zero( walk );
511
512 if( v2_length2(walk) > 0.001f )
513 v2_normalize_clamp( walk );
514
515 if( phys->in_air )
516 {
517 player_walk_update_collision();
518 rb_debug( rbf, 0xff0000ff );
519 rb_debug( rbb, 0xff0000ff );
520
521 /* allow player to accelerate a bit */
522 v3f walk_3d;
523 v3_muls( forward_dir, walk[1], walk_3d );
524 v3_muladds( walk_3d, right_dir, walk[0], walk_3d );
525
526 float current_vel = fabsf(v3_dot( walk_3d, phys->rb.v )),
527 new_vel = current_vel + VG_TIMESTEP_FIXED*k_air_accelerate,
528 clamped_new = vg_clampf( new_vel, 0.0f, k_walkspeed ),
529 vel_diff = vg_maxf( 0.0f, clamped_new - current_vel );
530
531 v3_muladds( phys->rb.v, right_dir, walk[0] * vel_diff, phys->rb.v );
532 v3_muladds( phys->rb.v, forward_dir, walk[1] * vel_diff, phys->rb.v );
533
534
535 len = 0;
536 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
537 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
538 rb_presolve_contacts( manifold, len );
539
540 for( int i=0; i<len; i++ )
541 {
542 struct contact *ct = &manifold[i];
543 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
544 phys->in_air = 0;
545 }
546
547 for( int j=0; j<5; j++ )
548 {
549 for( int i=0; i<len; i++ )
550 {
551 struct contact *ct = &manifold[i];
552
553 /*normal */
554 float vn = -v3_dot( phys->rb.v, ct->n );
555 vn += ct->bias;
556
557 float temp = ct->norm_impulse;
558 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
559 vn = ct->norm_impulse - temp;
560
561 v3f impulse;
562 v3_muls( ct->n, vn, impulse );
563
564 v3_add( impulse, phys->rb.v, phys->rb.v );
565
566 /* friction */
567 for( int j=0; j<2; j++ )
568 {
569 float f = k_friction * ct->norm_impulse,
570 vt = v3_dot( phys->rb.v, ct->t[j] ),
571 lambda = -vt;
572
573 float temp = ct->tangent_impulse[j];
574 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
575 lambda = ct->tangent_impulse[j] - temp;
576
577 v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
578 }
579 }
580 }
581
582 player_integrate();
583 }
584 else
585 {
586 player.walk = v2_length( walk );
587
588 if( player.input_walk->button.value )
589 v2_muls( walk, 0.5f, walk );
590
591 v2_muls( walk, k_walkspeed * VG_TIMESTEP_FIXED, walk );
592
593 v3f walk_apply;
594 v3_zero( walk_apply );
595
596 /* Do XY translation */
597 v3_muladds( walk_apply, right_dir, walk[0], walk_apply );
598 v3_muladds( walk_apply, forward_dir, walk[1], walk_apply );
599 v3_add( walk_apply, phys->rb.co, phys->rb.co );
600 v3_divs( walk_apply, VG_TIMESTEP_FIXED, phys->rb.v );
601
602 /* Directly resolve collisions */
603 player_walk_update_collision();
604 rb_debug( rbf, 0xffffff00 );
605 rb_debug( rbb, 0xffffff00 );
606
607 len = 0;
608 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
609 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
610
611 v3f dt;
612 v3_zero( dt );
613 for( int j=0; j<3; j++ )
614 {
615 for( int i=0; i<len; i++ )
616 {
617 struct contact *ct = &manifold[i];
618
619 float p = vg_maxf( 0.0f, ct->p - 0.00f ),
620 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
621 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
622 }
623 }
624 v3_add( dt, phys->rb.co, phys->rb.co );
625
626 if( len )
627 {
628 struct world_material *surface_mat = world_contact_material(manifold);
629 player.surface_prop = surface_mat->info.surface_prop;
630 }
631
632 /* jump */
633 if( player.input_jump->button.value )
634 {
635 phys->rb.v[1] = 5.0f;
636 phys->in_air = 1;
637 return;
638 }
639
640 /* if we've put us in the air, step down slowly */
641 phys->in_air = 1;
642 float max_dist = 0.3f,
643 start_y = phys->rb.co[1];
644
645 for( int j=0; j<8; j++ )
646 {
647 for( int i=0; i<len; i++ )
648 {
649 struct contact *ct = &manifold[i];
650 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
651 {
652 phys->in_air = 0;
653 if( j == 0 )
654 return;
655
656 v3f dt;
657 v3_zero( dt );
658 for( int j=0; j<3; j++ )
659 {
660 for( int i=0; i<len; i++ )
661 {
662 struct contact *ct = &manifold[i];
663
664 float p = vg_maxf( 0.0f, ct->p - 0.0025f ),
665 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
666 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
667 }
668 }
669 v3_add( dt, phys->rb.co, phys->rb.co );
670 return;
671 }
672 }
673
674 phys->rb.co[1] -= max_dist * 0.125f;
675
676 player_walk_update_collision();
677 len = 0;
678 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
679 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
680 }
681
682 /* Transitioning into air mode */
683 phys->rb.co[1] = start_y;
684 }
685 }
686
687 VG_STATIC void player_grind(void)
688 {
689 struct player_phys *phys = &player.phys;
690
691 v3f closest;
692 int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
693 if( idx == -1 )
694 return;
695
696 struct grind_edge *edge = &world.grind_edges[ idx ];
697
698 vg_line( phys->rb.co, closest, 0xff000000 );
699 vg_line_cross( closest, 0xff000000, 0.3f );
700 vg_line( edge->p0, edge->p1, 0xff000000 );
701
702 v3f grind_delta;
703 v3_sub( closest, phys->rb.co, grind_delta );
704
705 float p = v3_dot( phys->rb.forward, grind_delta );
706 v3_muladds( grind_delta, phys->rb.forward, -p, grind_delta );
707
708 float a = vg_maxf( 0.0f, 4.0f-v3_dist2( closest, phys->rb.co ) );
709 v3_muladds( phys->rb.v, grind_delta, a*0.2f, phys->rb.v );
710 }
711
712 VG_STATIC int player_update_grind_collision( rb_ct *contact )
713 {
714 struct player_phys *phys = &player.phys;
715
716 v3f p0, p1, c0, c1;
717 v3_muladds( phys->rb.co, phys->rb.forward, 0.5f, p0 );
718 v3_muladds( phys->rb.co, phys->rb.forward, -0.5f, p1 );
719 v3_muladds( p0, phys->rb.up, 0.125f, p0 );
720 v3_muladds( p1, phys->rb.up, 0.125f, p1 );
721
722 float const k_r = 0.25f;
723 struct grind_edge *closest_edge = player_grind_collect_edge( p0, p1,
724 c0, c1, k_r );
725
726
727 vg_line( p0, p1, 0xff0000ff );
728
729 if( closest_edge )
730 {
731 vg_line_cross( c0, 0xff000000, 0.1f );
732 vg_line_cross( c1, 0xff000000, 0.1f );
733 vg_line( c0, c1, 0xff000000 );
734
735 v3f delta;
736 v3_sub( c1, c0, delta );
737
738 if( v3_dot( delta, phys->rb.up ) > 0.0001f )
739 {
740 contact->p = v3_length( delta );
741 contact->type = k_contact_type_edge;
742 contact->element_id = 0;
743 v3_copy( c1, contact->co );
744 contact->rba = &player.phys.rb;
745 contact->rbb = &world.rb_geo;
746
747 v3f edge_dir, axis_dir;
748 v3_sub( closest_edge->p1, closest_edge->p0, edge_dir );
749 v3_normalize( edge_dir );
750 v3_cross( (v3f){0.0f,1.0f,0.0f}, edge_dir, axis_dir );
751 v3_cross( edge_dir, axis_dir, contact->n );
752
753 vg_info( "%f %f\n", v3_length( contact->n ), contact->p );
754
755 return 1;
756 }
757 else
758 return -1;
759 }
760
761 return 0;
762 }
763
764 /* Manifold must be able to hold at least 64 elements */
765 VG_STATIC int player_update_collision_manifold( rb_ct *manifold )
766 {
767 struct player_phys *phys = &player.phys;
768
769 rigidbody *rbf = &player.collide_front,
770 *rbb = &player.collide_back;
771
772 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
773 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
774
775 player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
776 float h = player.air_blend*0.2f;
777
778 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
779 v3_copy( rbf->co, rbf->to_world[3] );
780 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
781 v3_copy( rbb->co, rbb->to_world[3] );
782
783 m4x3_invert_affine( rbf->to_world, rbf->to_local );
784 m4x3_invert_affine( rbb->to_world, rbb->to_local );
785
786 rb_update_bounds( rbf );
787 rb_update_bounds( rbb );
788
789 rb_debug( rbf, 0xff00ffff );
790 rb_debug( rbb, 0xffffff00 );
791
792 int len_f = 0,
793 len_b = 0;
794
795 len_f = rb_sphere_scene( rbf, &world.rb_geo, manifold );
796 rb_manifold_filter_coplanar( manifold, len_f, 0.05f );
797 if( len_f > 1 )
798 {
799 rb_manifold_filter_backface( manifold, len_f );
800 rb_manifold_filter_joint_edges( manifold, len_f, 0.05f );
801 rb_manifold_filter_pairs( manifold, len_f, 0.05f );
802 }
803 int new_len_f = rb_manifold_apply_filtered( manifold, len_f );
804 if( len_f && !new_len_f )
805 len_f = 1;
806 else
807 len_f = new_len_f;
808
809 rb_ct *man_b = &manifold[len_f];
810 len_b = rb_sphere_scene( rbb, &world.rb_geo, man_b );
811 rb_manifold_filter_coplanar( man_b, len_b, 0.05f );
812 if( len_b > 1 )
813 {
814 rb_manifold_filter_backface( man_b, len_b );
815 rb_manifold_filter_joint_edges( man_b, len_b, 0.05f );
816 rb_manifold_filter_pairs( man_b, len_b, 0.05f );
817 }
818 int new_len_b = rb_manifold_apply_filtered( man_b, len_b );
819 if( len_b && !new_len_b )
820 len_b = 1;
821 else
822 len_b = new_len_b;
823 #if 0
824 /*
825 * Preprocess collision points, and create a surface picture.
826 * we want contacts that are within our 'capsule's internal line to be
827 * clamped so that they face the line and do not oppose, to stop the
828 * player hanging up on stuff
829 */
830 for( int i=0; i<len; i++ )
831 {
832 v3f dfront, dback;
833 v3_sub( manifold[i].co, rbf->co, dfront );
834 v3_sub( manifold[i].co, rbb->co, dback );
835
836 if( (v3_dot( dfront, phys->rb.forward ) < -0.02f) &&
837 (v3_dot( dback, phys->rb.forward ) > 0.02f))
838 {
839 float p = v3_dot( manifold[i].n, phys->rb.forward );
840 v3_muladds( manifold[i].n, phys->rb.forward, -p, manifold[i].n );
841 v3_normalize( manifold[i].n );
842 }
843 }
844 #endif
845
846 return len_f + len_b;
847 }
848
849 VG_STATIC void player_adhere_ground( rb_ct *manifold, int len )
850 {
851 struct player_phys *phys = &player.phys;
852 int was_in_air = phys->in_air;
853
854 v3f surface_avg;
855 v3_zero( surface_avg );
856
857 if( len == 0 )
858 {
859 phys->lift_frames ++;
860
861 if( phys->lift_frames >= 8 )
862 phys->in_air = 1;
863 }
864 else
865 {
866 for( int i=0; i<len; i++ )
867 v3_add( surface_avg, manifold[i].n, surface_avg );
868 v3_normalize( surface_avg );
869
870 if( v3_dot( phys->rb.v, surface_avg ) > 0.7f )
871 {
872 phys->lift_frames ++;
873
874 if( phys->lift_frames >= 8 )
875 phys->in_air = 1;
876 }
877 else
878 {
879 phys->in_air = 0;
880 phys->lift_frames = 0;
881 v3f projected, axis;
882
883 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
884 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
885
886 float d = v3_dot( phys->rb.forward, surface_avg );
887 v3_muladds( surface_avg, phys->rb.forward, -d, projected );
888 v3_normalize( projected );
889
890 float angle = v3_dot( phys->rb.up, projected );
891 v3_cross( phys->rb.up, projected, axis );
892
893 v3f p0, p1;
894 v3_add( phys->rb.co, projected, p0 );
895 v3_add( phys->rb.co, phys->rb.up, p1 );
896 vg_line( phys->rb.co, p0, 0xff00ff00 );
897 vg_line( phys->rb.co, p1, 0xff000fff );
898
899 if( fabsf(angle) < 0.999f )
900 {
901 v4f correction;
902 q_axis_angle( correction, axis,
903 acosf(angle)*4.0f*VG_TIMESTEP_FIXED );
904 q_mul( correction, phys->rb.q, phys->rb.q );
905 }
906 }
907 }
908
909 if( !was_in_air && phys->in_air )
910 player_start_air();
911 }
912
913 VG_STATIC void player_collision_response( rb_ct *manifold, int len )
914 {
915 struct player_phys *phys = &player.phys;
916
917 for( int j=0; j<5; j++ )
918 {
919 for( int i=0; i<len; i++ )
920 {
921 struct contact *ct = &manifold[i];
922
923 v3f dv, delta;
924 v3_sub( ct->co, phys->rb.co, delta );
925 v3_cross( phys->rb.w, delta, dv );
926 v3_add( phys->rb.v, dv, dv );
927
928 float vn = -v3_dot( dv, ct->n );
929 vn += ct->bias;
930
931 float temp = ct->norm_impulse;
932 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
933 vn = ct->norm_impulse - temp;
934
935 v3f impulse;
936 v3_muls( ct->n, vn, impulse );
937
938 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
939 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
940 {
941 player_kill();
942 return;
943 }
944
945 v3_add( impulse, phys->rb.v, phys->rb.v );
946 v3_cross( delta, impulse, impulse );
947
948 /*
949 * W Impulses are limited to the Y and X axises, we don't really want
950 * roll angular velocities being included.
951 *
952 * Can also tweak the resistance of each axis here by scaling the wx,wy
953 * components.
954 */
955
956 float wy = v3_dot( phys->rb.up, impulse ) * 0.8f,
957 wx = v3_dot( phys->rb.right, impulse )*1.0f;
958
959 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
960 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
961 }
962 }
963 }
964
965 VG_STATIC void player_save_frame(void)
966 {
967 player.phys_gate_frame = player.phys;
968 }
969
970 VG_STATIC void player_restore_frame(void)
971 {
972 player.phys = player.phys_gate_frame;
973 rb_update_transform( &player.phys.rb );
974 }
975
976 VG_STATIC void player_integrate(void)
977 {
978 struct player_phys *phys = &player.phys;
979 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
980 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
981 }
982
983 VG_STATIC void player_do_motion(void)
984 {
985 struct player_phys *phys = &player.phys;
986
987 if( world.water.enabled )
988 {
989 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
990 {
991 audio_lock();
992 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
993 audio_player_set_position( &audio_player_extra, phys->rb.co );
994 audio_player_set_vol( &audio_player_extra, 20.0f );
995 audio_player_playclip( &audio_player_extra, &audio_splash );
996 audio_unlock();
997
998 player_kill();
999 }
1000 }
1001
1002 v3f prevco;
1003 v3_copy( phys->rb.co, prevco );
1004
1005 if( phys->on_board )
1006 {
1007 rb_ct manifold[72];
1008 int len = player_update_collision_manifold( manifold );
1009 int grind_col = player_update_grind_collision( &manifold[len] );
1010
1011 static int _grind_col_pre = 0;
1012
1013 if( grind_col )
1014 {
1015 phys->grind = 1;
1016 v3f up = { 0.0f, 1.0f, 0.0f };
1017 float angle = v3_dot( phys->rb.up, up );
1018
1019 if( fabsf(angle) < 0.99f )
1020 {
1021 v3f axis;
1022 v3_cross( phys->rb.up, up, axis );
1023
1024 v4f correction;
1025 q_axis_angle( correction, axis,
1026 VG_TIMESTEP_FIXED * 10.0f * acosf(angle) );
1027 q_mul( correction, phys->rb.q, phys->rb.q );
1028 }
1029
1030 float const DOWNFORCE = -k_downforce*1.2f*VG_TIMESTEP_FIXED;
1031 v3_muladds( phys->rb.v, manifold[len].n, DOWNFORCE, phys->rb.v );
1032 m3x3_identity( phys->vr );
1033 m3x3_identity( phys->vr_pstep );
1034
1035 if( !_grind_col_pre )
1036 {
1037 audio_lock();
1038 audio_player_set_flags( &audio_player_extra,
1039 AUDIO_FLAG_SPACIAL_3D );
1040 audio_player_set_position( &audio_player_extra, phys->rb.co );
1041 audio_player_set_vol( &audio_player_extra, 20.0f );
1042 audio_player_playclip( &audio_player_extra, &audio_board[5] );
1043 audio_unlock();
1044 }
1045 }
1046 else
1047 {
1048 phys->grind = 0;
1049 player_adhere_ground( manifold, len );
1050
1051 if( _grind_col_pre )
1052 {
1053 audio_lock();
1054 audio_player_set_flags( &audio_player_extra,
1055 AUDIO_FLAG_SPACIAL_3D );
1056 audio_player_set_position( &audio_player_extra, phys->rb.co );
1057 audio_player_set_vol( &audio_player_extra, 20.0f );
1058 audio_player_playclip( &audio_player_extra, &audio_board[6] );
1059 audio_unlock();
1060 }
1061 }
1062
1063 _grind_col_pre = grind_col;
1064
1065 rb_presolve_contacts( manifold, len+ VG_MAX(0,grind_col) );
1066 player_collision_response( manifold, len+ VG_MAX(0,grind_col) );
1067
1068 player_physics_control_passive();
1069
1070 if( grind_col )
1071 {
1072 phys->in_air = 0;
1073 player_physics_control_grind();
1074 }
1075 else
1076 {
1077 if( phys->in_air )
1078 player_physics_control_air();
1079 else
1080 player_physics_control();
1081 }
1082
1083 player_integrate();
1084 }
1085 else
1086 player_walk_physics();
1087
1088
1089 /* Real angular velocity integration */
1090 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f*0.5f, phys->rb.w );
1091 if( v3_length2( phys->rb.w ) > 0.0f )
1092 {
1093 v4f rotation;
1094 v3f axis;
1095 v3_copy( phys->rb.w, axis );
1096
1097 float mag = v3_length( axis );
1098 v3_divs( axis, mag, axis );
1099 q_axis_angle( rotation, axis, mag*k_rb_delta );
1100 q_mul( rotation, phys->rb.q, phys->rb.q );
1101 }
1102
1103 /* Faux angular velocity */
1104 v4f rotate;
1105
1106 float lerpq = phys->in_air? 0.04f: 0.3f;
1107 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
1108
1109 q_axis_angle( rotate, phys->rb.up, phys->siY );
1110 q_mul( rotate, phys->rb.q, phys->rb.q );
1111 phys->iY = 0.0f;
1112
1113 /*
1114 * Gate intersection, by tracing a line over the gate planes
1115 */
1116 for( int i=0; i<world.gate_count; i++ )
1117 {
1118 struct route_gate *rg = &world.gates[i];
1119 teleport_gate *gate = &rg->gate;
1120
1121 if( gate_intersect( gate, phys->rb.co, prevco ) )
1122 {
1123 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
1124 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
1125 m3x3_mulv( gate->transport, phys->vl, phys->vl );
1126 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
1127 m3x3_mulv( gate->transport, phys->m, phys->m );
1128 m3x3_mulv( gate->transport, phys->bob, phys->bob );
1129
1130 /* Pre-emptively edit the camera matrices so that the motion vectors
1131 * are correct */
1132 m4x3f transport_i;
1133 m4x4f transport_4;
1134 m4x3_invert_affine( gate->transport, transport_i );
1135 m4x3_expand( transport_i, transport_4 );
1136 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
1137 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
1138
1139 v4f transport_rotation;
1140 m3x3_q( gate->transport, transport_rotation );
1141 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
1142
1143 world_routes_activate_gate( i );
1144
1145 if( !phys->on_board )
1146 {
1147 v3f fwd_dir = {cosf(player.angles[0]),
1148 0.0f,
1149 sinf(player.angles[0])};
1150 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
1151
1152 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
1153 }
1154
1155 player.rewind_length = 0;
1156 player.rewind_total_length = 0.0f;
1157 player.rewind_incrementer = 10000;
1158 player_save_frame();
1159
1160 audio_lock();
1161 audio_play_oneshot( &audio_gate_pass, 1.0f );
1162 audio_unlock();
1163 break;
1164 }
1165 }
1166
1167 rb_update_transform( &phys->rb );
1168 }
1169
1170 VG_STATIC void player_freecam(void)
1171 {
1172 player_mouseview();
1173
1174 float movespeed = fc_speed * VG_TIMESTEP_FIXED;
1175 v3f lookdir = { 0.0f, 0.0f, -1.0f },
1176 sidedir = { 1.0f, 0.0f, 0.0f };
1177
1178 m3x3_mulv( main_camera.transform, lookdir, lookdir );
1179 m3x3_mulv( main_camera.transform, sidedir, sidedir );
1180
1181 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
1182
1183 v2f steer = { player.input_js1h->axis.value,
1184 player.input_js1v->axis.value };
1185
1186 v3_muladds( move_vel, sidedir, movespeed*steer[0], move_vel );
1187 v3_muladds( move_vel, lookdir, -movespeed*steer[1], move_vel );
1188
1189 v3_muls( move_vel, 0.7f, move_vel );
1190 v3_add( move_vel, player.camera_pos, player.camera_pos );
1191 }
1192
1193 VG_STATIC int reset_player( int argc, char const *argv[] )
1194 {
1195 struct player_phys *phys = &player.phys;
1196 struct respawn_point *rp = NULL, *r;
1197
1198 if( argc == 1 )
1199 {
1200 for( int i=0; i<world.spawn_count; i++ )
1201 {
1202 r = &world.spawns[i];
1203 if( !strcmp( r->name, argv[0] ) )
1204 {
1205 rp = r;
1206 break;
1207 }
1208 }
1209
1210 if( !rp )
1211 vg_warn( "No spawn named '%s'\n", argv[0] );
1212 }
1213
1214 if( !rp )
1215 {
1216 float min_dist = INFINITY;
1217
1218 for( int i=0; i<world.spawn_count; i++ )
1219 {
1220 r = &world.spawns[i];
1221 float d = v3_dist2( r->co, phys->rb.co );
1222
1223 vg_info( "Dist %s : %f\n", r->name, d );
1224 if( d < min_dist )
1225 {
1226 min_dist = d;
1227 rp = r;
1228 }
1229 }
1230 }
1231
1232 if( !rp )
1233 {
1234 vg_error( "No spawn found\n" );
1235 vg_info( "Player position: %f %f %f\n", player.phys.rb.co[0],
1236 player.phys.rb.co[1],
1237 player.phys.rb.co[2] );
1238 vg_info( "Player velocity: %f %f %f\n", player.phys.rb.v[0],
1239 player.phys.rb.v[1],
1240 player.phys.rb.v[2] );
1241
1242 if( !world.spawn_count )
1243 return 0;
1244
1245 rp = &world.spawns[0];
1246 }
1247
1248 player.is_dead = 0;
1249
1250 m3x3f the_long_way;
1251 q_m3x3( rp->q, the_long_way );
1252
1253 v3f delta = {1.0f,0.0f,0.0f};
1254 m3x3_mulv( the_long_way, delta, delta );
1255
1256 player.angles[0] = atan2f( delta[0], -delta[2] );
1257 player.angles[1] = -asinf( delta[1] );
1258
1259
1260 v4_copy( rp->q, phys->rb.q );
1261 v3_copy( rp->co, phys->rb.co );
1262 v3_zero( phys->rb.v );
1263
1264 phys->vswitch = 1.0f;
1265 phys->slip_last = 0.0f;
1266 phys->in_air = 1;
1267 phys->on_board = 0;
1268 m3x3_identity( phys->vr );
1269
1270 player.mdl.shoes[0] = 1;
1271 player.mdl.shoes[1] = 1;
1272
1273 rb_update_transform( &phys->rb );
1274 player_save_frame();
1275 return 1;
1276 }
1277
1278 #endif /* PLAYER_PHYSICS_H */