latest
[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 * Cast a sphere from a to b and see what time it hits
64 */
65 VG_STATIC int spherecast_world( v3f pa, v3f pb, float r, float *t, v3f n )
66 {
67 struct player_phys *phys = &player.phys;
68
69 bh_iter it;
70 bh_iter_init( 0, &it );
71
72 boxf region;
73 box_init_inf( region );
74 box_addpt( region, pa );
75 box_addpt( region, pb );
76
77 v3_add( (v3f){ r, r, r}, region[1], region[1] );
78 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
79
80 v3f dir;
81 v3_sub( pb, pa, dir );
82
83 int hit = -1;
84 float min_t = 1.0f;
85
86 int idx;
87 while( bh_next( world.geo_bh, &it, region, &idx ) )
88 {
89 u32 *ptri = &world.scene_geo->arrindices[ idx*3 ];
90 v3f tri[3];
91
92 boxf box;
93 box_init_inf( box );
94
95 for( int j=0; j<3; j++ )
96 {
97 v3_copy( world.scene_geo->arrvertices[ptri[j]].co, tri[j] );
98 box_addpt( box, tri[j] );
99 }
100
101 v3_add( (v3f){ r, r, r}, box[1], box[1] );
102 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
103 if( !ray_aabb( box, pa, dir, 1.0f ) )
104 continue;
105
106 float t;
107 v3f n1;
108 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
109 {
110 if( t < min_t )
111 {
112 min_t = t;
113 hit = idx;
114 v3_copy( n1, n );
115 }
116 }
117 }
118
119 *t = min_t;
120 return hit;
121 }
122
123 /*
124 * Trace a path given a velocity rotation.
125 * Closest to 0 is best.
126 */
127 VG_STATIC void player_predict_land( m3x3f vr,
128 struct land_prediction *prediction )
129 {
130 struct player_phys *phys = &player.phys;
131
132 float pstep = VG_TIMESTEP_FIXED * 10.0f;
133 float k_bias = 0.96f;
134
135 v3f pco, pco1, pv;
136 v3_copy( phys->rb.co, pco );
137 v3_muls( phys->rb.v, k_bias, pv );
138
139 m3x3_mulv( vr, pv, pv );
140 v3_muladds( pco, pv, pstep, pco );
141
142 struct grind_edge *best_grind = NULL;
143 float closest_grind = INFINITY;
144
145 float grind_score = INFINITY,
146 air_score = INFINITY;
147
148 prediction->log_length = 0;
149
150 for( int i=0; i<vg_list_size(prediction->log); i++ )
151 {
152 v3_copy( pco, pco1 );
153 apply_gravity( pv, pstep );
154
155 m3x3_mulv( vr, pv, pv );
156 v3_muladds( pco, pv, pstep, pco );
157
158 v3f vdir;
159
160 v3_sub( pco, pco1, vdir );
161
162 float l = v3_length( vdir );
163 v3_muls( vdir, 1.0f/l, vdir );
164
165 v3f c0, c1;
166 struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
167 c0, c1, 0.4f );
168
169 if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
170 {
171 float d2 = v3_dist2( c0, c1 );
172 if( d2 < closest_grind )
173 {
174 closest_grind = d2;
175 best_grind = ge;
176 grind_score = closest_grind * 0.05f;
177 }
178 }
179
180 v3f n1;
181
182 float t1;
183 int idx = spherecast_world( pco1, pco, 0.4f, &t1, n1 );
184 if( idx != -1 )
185 {
186 v3_copy( n1, prediction->n );
187 air_score = -v3_dot( pv, n1 );
188
189 u32 vert_index = world.scene_geo->arrindices[ idx*3 ];
190 struct world_material *mat = world_tri_index_material( vert_index );
191
192 /* Bias prediction towords ramps */
193 if( mat->info.flags & k_material_flag_skate_surface )
194 air_score *= 0.1f;
195
196 v3_lerp( pco1, pco, t1, prediction->log[ prediction->log_length ++ ] );
197 break;
198 }
199
200 v3_copy( pco, prediction->log[ prediction->log_length ++ ] );
201 }
202
203 if( grind_score < air_score )
204 {
205 prediction->score = grind_score;
206 prediction->type = k_prediction_grind;
207 }
208 else if( air_score < INFINITY )
209 {
210 prediction->score = air_score;
211 prediction->type = k_prediction_land;
212 }
213 else
214 {
215 prediction->score = INFINITY;
216 prediction->type = k_prediction_none;
217 }
218 }
219
220 /*
221 * Called when launching into the air to predict and adjust trajectories
222 */
223 VG_STATIC void player_start_air(void)
224 {
225 struct player_phys *phys = &player.phys;
226
227 float pstep = VG_TIMESTEP_FIXED * 10.0f;
228 float best_velocity_delta = -9999.9f;
229
230 v3f axis;
231 v3_cross( phys->rb.up, phys->rb.v, axis );
232 v3_normalize( axis );
233 player.prediction_count = 0;
234
235 m3x3_identity( phys->vr );
236
237 float
238 best_vmod = 0.0f,
239 min_score = INFINITY,
240 max_score = -INFINITY;
241
242 /*
243 * Search a broad selection of futures
244 */
245 for( int m=-3;m<=12; m++ )
246 {
247 struct land_prediction *p =
248 &player.predictions[ player.prediction_count ++ ];
249
250 float vmod = ((float)m / 15.0f)*0.09f;
251
252 m3x3f vr;
253 v4f vr_q;
254
255 q_axis_angle( vr_q, axis, vmod );
256 q_m3x3( vr_q, vr );
257
258 player_predict_land( vr, p );
259
260 if( p->type != k_prediction_none )
261 {
262 if( p->score < min_score )
263 {
264 min_score = p->score;
265 best_vmod = vmod;
266 }
267
268 if( p->score > max_score )
269 max_score = p->score;
270 }
271 }
272
273 v4f vr_q;
274 q_axis_angle( vr_q, axis, best_vmod*0.1f );
275 q_m3x3( vr_q, phys->vr );
276
277 q_axis_angle( vr_q, axis, best_vmod );
278 q_m3x3( vr_q, phys->vr_pstep );
279
280 /*
281 * Logging
282 */
283 for( int i=0; i<player.prediction_count; i ++ )
284 {
285 struct land_prediction *p = &player.predictions[i];
286
287 float l = p->score;
288
289 if( l < 0.0f )
290 {
291 vg_error( "negative score! (%f)\n", l );
292 }
293
294 l -= min_score;
295 l /= (max_score-min_score);
296 l = 1.0f - l;
297 l *= 255.0f;
298
299 p->colour = l;
300 p->colour <<= 8;
301 p->colour |= 0xff000000;
302 }
303 }
304
305
306 VG_STATIC void player_physics_control_passive(void)
307 {
308 struct player_phys *phys = &player.phys;
309 float grabt = player.input_grab->axis.value;
310
311 if( grabt > 0.5f )
312 {
313 v2_muladds( phys->grab_mouse_delta, vg.mouse_delta, 0.02f,
314 phys->grab_mouse_delta );
315 v2_normalize_clamp( phys->grab_mouse_delta );
316
317 if( freecam )
318 v2_zero( phys->grab_mouse_delta );
319 }
320 else
321 v2_zero( phys->grab_mouse_delta );
322
323 phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
324 player.phys.pushing = 0.0f;
325
326 if( !phys->jump_charge || phys->in_air )
327 {
328 phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
329 }
330
331 phys->jump_charge = 0;
332 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
333 }
334
335 /*
336 * Main friction interface model
337 */
338 VG_STATIC void player_physics_control(void)
339 {
340 struct player_phys *phys = &player.phys;
341
342 /*
343 * Computing localized friction forces for controlling the character
344 * Friction across X is significantly more than Z
345 */
346
347 v3f vel;
348 m3x3_mulv( phys->rb.to_local, phys->rb.v, vel );
349 float slip = 0.0f;
350
351 if( fabsf(vel[2]) > 0.01f )
352 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
353
354 if( fabsf( slip ) > 1.2f )
355 slip = vg_signf( slip ) * 1.2f;
356 phys->slip = slip;
357 phys->reverse = -vg_signf(vel[2]);
358
359 float substep = VG_TIMESTEP_FIXED;
360 float fwd_resistance = k_friction_resistance;
361
362 vel[2] = stable_force( vel[2],vg_signf(vel[2]) * -fwd_resistance*substep);
363 vel[0] = stable_force( vel[0],vg_signf(vel[0]) * -k_friction_lat*substep);
364
365 if( player.input_jump->button.value )
366 {
367 phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed;
368
369 if( !phys->jump_charge )
370 phys->jump_dir = phys->reverse > 0.0f? 1: 0;
371
372 phys->jump_charge = 1;
373 }
374
375 static int push_thresh_last = 0;
376 float push = player.input_push->button.value;
377 int push_thresh = push>0.15f? 1: 0;
378
379 if( push_thresh && !push_thresh_last )
380 player.phys.start_push = vg.time;
381
382 push_thresh_last = push_thresh;
383
384 if( !player.input_jump->button.value && push_thresh )
385 {
386 player.phys.pushing = 1.0f;
387 player.phys.push_time = vg.time - player.phys.start_push;
388
389 float cycle_time = player.phys.push_time*k_push_cycle_rate,
390 amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED,
391 current = v3_length( vel ),
392 new_vel = vg_minf( current + amt, k_max_push_speed );
393
394 new_vel -= vg_minf(current, k_max_push_speed);
395 vel[2] -= new_vel * phys->reverse;
396 }
397
398 m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
399
400 float input = player.input_js1h->axis.value,
401 grab = player.input_grab->axis.value,
402 steer = input * (1.0f-(phys->jump+grab)*0.4f),
403 steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
404
405 phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
406
407
408 /*
409 * EXPERIMENTAL
410 * ===============================================
411 */
412 #if 0
413 v3f cog_ideal, diff;
414
415 v3_muladds( phys->rb.co, phys->rb.up, 1.0f-grab, cog_ideal );
416 v3_sub( cog_ideal, phys->cog, diff );
417
418 /* temp */
419 if( v3_length2( diff ) > 20.0f*20.0f )
420 v3_copy( cog_ideal, phys->cog );
421 else
422 {
423 float rate_lat = k_cog_spring_lat * VG_TIMESTEP_FIXED,
424 rate_vert = k_cog_spring_vert * VG_TIMESTEP_FIXED,
425 vert_amt = v3_dot( diff, phys->rb.up );
426
427 /* Split into vert/lat springs */
428 v3f diff_vert, diff_lat;
429 v3_muladds( diff, phys->rb.up, -vert_amt, diff_lat );
430 v3_muls( phys->rb.up, vert_amt, diff_vert );
431
432
433 if( diff[1] > 0.0f )
434 rate_vert *= k_cog_boost_multiplier;
435
436 float ap_a = k_cog_mass_ratio,
437 ap_b = -(1.0f-k_cog_mass_ratio);
438
439 v3_muladds( phys->cog_v, diff_lat, rate_lat * ap_a, phys->cog_v );
440 v3_muladds( phys->cog_v, diff_vert, rate_vert * ap_a, phys->cog_v );
441
442 //v3_muladds( phys->rb.v, diff_lat, rate_lat * ap_b, phys->rb.v );
443 v3_muladds( phys->rb.v, diff_vert, rate_vert * ap_b, phys->rb.v );
444
445 /* dampen */
446 v3_muls( phys->cog_v, 1.0f-(VG_TIMESTEP_FIXED*k_cog_damp), phys->cog_v );
447
448 /* integrate */
449 v3_muladds( phys->cog, phys->cog_v, VG_TIMESTEP_FIXED, phys->cog );
450 }
451
452
453 /*
454 * EXPERIMENTAL
455 * ===============================================
456 */
457 #endif
458
459
460 if( !phys->jump_charge && phys->jump > 0.2f )
461 {
462 v3f jumpdir;
463
464 /* Launch more up if alignment is up else improve velocity */
465 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
466 mod = 0.5f,
467 dir = mod + aup*(1.0f-mod);
468
469 v3_copy( phys->rb.v, jumpdir );
470 v3_normalize( jumpdir );
471 v3_muls( jumpdir, 1.0f-dir, jumpdir );
472 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
473 v3_normalize( jumpdir );
474
475 float force = k_jump_force*phys->jump;
476 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
477 phys->jump = 0.0f;
478
479 player.jump_time = vg.time;
480
481 /* TODO: Move to audio file */
482 audio_lock();
483 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
484 audio_player_set_position( &audio_player_extra, phys->rb.co );
485 audio_player_set_vol( &audio_player_extra, 20.0f );
486 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
487 audio_unlock();
488 }
489 }
490
491 VG_STATIC void player_physics_control_grind(void)
492 {
493 struct player_phys *phys = &player.phys;
494 v2f steer = { player.input_js1h->axis.value,
495 player.input_js1v->axis.value };
496
497 float l2 = v2_length2( steer );
498 if( l2 > 1.0f )
499 v2_muls( steer, 1.0f/sqrtf(l2), steer );
500
501 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
502
503 float iX = steer[1] * phys->reverse * k_steer_air * VG_TIMESTEP_FIXED;
504
505 static float siX = 0.0f;
506 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
507
508 v4f rotate;
509 q_axis_angle( rotate, phys->rb.right, siX );
510 q_mul( rotate, phys->rb.q, phys->rb.q );
511
512 phys->slip = 0.0f;
513 }
514
515 /*
516 * Air control, no real physics
517 */
518 VG_STATIC void player_physics_control_air(void)
519 {
520 struct player_phys *phys = &player.phys;
521
522 m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
523 //vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
524
525 ray_hit hit;
526
527 /*
528 * Prediction
529 */
530 float pstep = VG_TIMESTEP_FIXED * 1.0f;
531 float k_bias = 0.98f;
532
533 v3f pco, pco1, pv;
534 v3_copy( phys->rb.co, pco );
535 v3_muls( phys->rb.v, 1.0f, pv );
536
537 float time_to_impact = 0.0f;
538 float limiter = 1.0f;
539
540 struct grind_edge *best_grind = NULL;
541 float closest_grind = INFINITY;
542
543 v3f target_normal = { 0.0f, 1.0f, 0.0f };
544 int has_target = 0;
545
546 for( int i=0; i<250; i++ )
547 {
548 v3_copy( pco, pco1 );
549 m3x3_mulv( phys->vr, pv, pv );
550 apply_gravity( pv, pstep );
551 v3_muladds( pco, pv, pstep, pco );
552
553 ray_hit contact;
554 v3f vdir;
555
556 v3_sub( pco, pco1, vdir );
557 contact.dist = v3_length( vdir );
558 v3_divs( vdir, contact.dist, vdir);
559
560 v3f c0, c1;
561 struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
562 c0, c1, 0.4f );
563
564 if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
565 {
566 vg_line( ge->p0, ge->p1, 0xff0000ff );
567 vg_line_cross( pco, 0xff0000ff, 0.25f );
568 has_target = 1;
569 break;
570 }
571
572 float orig_dist = contact.dist;
573 if( ray_world( pco1, vdir, &contact ) )
574 {
575 v3_copy( contact.normal, target_normal );
576 has_target = 1;
577 time_to_impact += (contact.dist/orig_dist)*pstep;
578 vg_line_cross( contact.pos, 0xffff0000, 0.25f );
579 break;
580 }
581 time_to_impact += pstep;
582 }
583
584 if( has_target )
585 {
586 float angle = v3_dot( phys->rb.up, target_normal );
587 v3f axis;
588 v3_cross( phys->rb.up, target_normal, axis );
589
590 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
591 limiter = 1.0f-limiter;
592 limiter *= limiter;
593 limiter = 1.0f-limiter;
594
595 if( fabsf(angle) < 0.99f )
596 {
597 v4f correction;
598 q_axis_angle( correction, axis,
599 acosf(angle)*(1.0f-limiter)*3.0f*VG_TIMESTEP_FIXED );
600 q_mul( correction, phys->rb.q, phys->rb.q );
601 }
602 }
603
604 v2f steer = { player.input_js1h->axis.value,
605 player.input_js1v->axis.value };
606
607 float l2 = v2_length2( steer );
608 if( l2 > 1.0f )
609 v2_muls( steer, 1.0f/sqrtf(l2), steer );
610
611 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
612
613 float iX = steer[1] *
614 phys->reverse * k_steer_air * limiter * VG_TIMESTEP_FIXED;
615
616 static float siX = 0.0f;
617 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
618
619 v4f rotate;
620 q_axis_angle( rotate, phys->rb.right, siX );
621 q_mul( rotate, phys->rb.q, phys->rb.q );
622
623 #if 0
624 v2f target = {0.0f,0.0f};
625 v2_muladds( target, (v2f){ vg_get_axis("grabh"), vg_get_axis("grabv") },
626 phys->grab, target );
627 #endif
628 }
629
630 VG_STATIC void player_walk_collider_configuration(void)
631 {
632 struct player_phys *phys = &player.phys;
633 float h0 = 0.3f,
634 h1 = 0.9f;
635
636 rigidbody *rbf = &player.collide_front,
637 *rbb = &player.collide_back;
638
639 v3_add( phys->rb.co, (v3f){0.0f,h0,0.0f}, rbf->co );
640 v3_add( phys->rb.co, (v3f){0.0f,h1,0.0f}, rbb->co );
641 v3_copy( rbf->co, rbf->to_world[3] );
642 v3_copy( rbb->co, rbb->to_world[3] );
643 m4x3_invert_affine( rbf->to_world, rbf->to_local );
644 m4x3_invert_affine( rbb->to_world, rbb->to_local );
645
646 rb_update_bounds( rbf );
647 rb_update_bounds( rbb );
648 }
649
650 VG_STATIC void player_regular_collider_configuration(void)
651 {
652 struct player_phys *phys = &player.phys;
653
654 /* Standard ground configuration */
655 rigidbody *rbf = &player.collide_front,
656 *rbb = &player.collide_back;
657
658 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
659 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
660
661 player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
662 float h = player.air_blend*0.0f;
663
664 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
665 v3_copy( rbf->co, rbf->to_world[3] );
666 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
667 v3_copy( rbb->co, rbb->to_world[3] );
668
669 m4x3_invert_affine( rbf->to_world, rbf->to_local );
670 m4x3_invert_affine( rbb->to_world, rbb->to_local );
671
672 rb_update_bounds( rbf );
673 rb_update_bounds( rbb );
674 }
675
676 VG_STATIC void player_integrate(void);
677
678 VG_STATIC int player_walk_surface_standable( v3f n )
679 {
680 return v3_dot( n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f;
681 }
682
683 VG_STATIC void player_walk_stepdown(void)
684 {
685 struct player_phys *phys = &player.phys;
686 float max_dist = 0.4f;
687
688 v3f pa, pb;
689 v3_copy( phys->rb.co, pa );
690 pa[1] += 0.3f;
691
692 v3_muladds( pa, (v3f){0.01f,1.0f,0.01f}, -max_dist, pb );
693 vg_line( pa, pb, 0xff000000 );
694
695 /* TODO: Make #define */
696 float r = 0.3f,
697 t;
698
699 v3f n;
700 if( spherecast_world( pa, pb, r, &t, n ) != -1 )
701 {
702 if( player_walk_surface_standable( n ) )
703 {
704 phys->in_air = 0;
705 v3_lerp( pa, pb, t+0.001f, phys->rb.co );
706 phys->rb.co[1] -= 0.3f;
707 }
708 }
709 }
710
711 VG_STATIC int player_update_collision_manifold( rb_ct *manifold );
712 VG_STATIC void player_walk_physics(void)
713 {
714 struct player_phys *phys = &player.phys;
715 rigidbody *rbf = &player.collide_front,
716 *rbb = &player.collide_back;
717
718 m3x3_identity( player.collide_front.to_world );
719 m3x3_identity( player.collide_back.to_world );
720
721 v3_zero( phys->rb.w );
722 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
723
724 rb_ct manifold[64];
725 int len;
726
727 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
728 v3f right_dir = { -forward_dir[2], 0.0f, forward_dir[0] };
729
730 v2f walk = { player.input_walkh->axis.value,
731 player.input_walkv->axis.value };
732
733 if( freecam )
734 v2_zero( walk );
735
736 if( v2_length2(walk) > 0.001f )
737 v2_normalize_clamp( walk );
738
739 if( phys->in_air )
740 {
741 player_walk_collider_configuration();
742
743 /* allow player to accelerate a bit */
744 v3f walk_3d;
745 v3_muls( forward_dir, walk[1], walk_3d );
746 v3_muladds( walk_3d, right_dir, walk[0], walk_3d );
747
748 float current_vel = fabsf(v3_dot( walk_3d, phys->rb.v )),
749 new_vel = current_vel + VG_TIMESTEP_FIXED*k_air_accelerate,
750 clamped_new = vg_clampf( new_vel, 0.0f, k_walkspeed ),
751 vel_diff = vg_maxf( 0.0f, clamped_new - current_vel );
752
753 v3_muladds( phys->rb.v, right_dir, walk[0] * vel_diff, phys->rb.v );
754 v3_muladds( phys->rb.v, forward_dir, walk[1] * vel_diff, phys->rb.v );
755
756
757 len = player_update_collision_manifold( manifold );
758 rb_presolve_contacts( manifold, len );
759
760 for( int i=0; i<len; i++ )
761 {
762 struct contact *ct = &manifold[i];
763 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
764 phys->in_air = 0;
765 }
766
767 for( int j=0; j<5; j++ )
768 {
769 for( int i=0; i<len; i++ )
770 {
771 struct contact *ct = &manifold[i];
772
773 /*normal */
774 float vn = -v3_dot( phys->rb.v, ct->n );
775 vn += ct->bias;
776
777 float temp = ct->norm_impulse;
778 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
779 vn = ct->norm_impulse - temp;
780
781 v3f impulse;
782 v3_muls( ct->n, vn, impulse );
783
784 v3_add( impulse, phys->rb.v, phys->rb.v );
785
786 /* friction */
787 for( int j=0; j<2; j++ )
788 {
789 float f = k_friction * ct->norm_impulse,
790 vt = v3_dot( phys->rb.v, ct->t[j] ),
791 lambda = -vt;
792
793 float temp = ct->tangent_impulse[j];
794 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
795 lambda = ct->tangent_impulse[j] - temp;
796
797 v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
798 }
799 }
800 }
801
802 player_integrate();
803 }
804 else
805 {
806 player.walk = v2_length( walk );
807
808 if( player.input_walk->button.value )
809 v2_muls( walk, 0.5f, walk );
810
811 v2_muls( walk, k_walkspeed * VG_TIMESTEP_FIXED, walk );
812
813 /* Do XY translation */
814 v3f walk_apply, walk_measured;
815 v3_zero( walk_apply );
816 v3_muladds( walk_apply, right_dir, walk[0], walk_apply );
817 v3_muladds( walk_apply, forward_dir, walk[1], walk_apply );
818 v3_add( walk_apply, phys->rb.co, phys->rb.co );
819
820 /* Directly resolve collisions */
821 player_walk_collider_configuration();
822 len = player_update_collision_manifold( manifold );
823
824 v3f dt;
825 v3_zero( dt );
826 for( int j=0; j<8; j++ )
827 {
828 for( int i=0; i<len; i++ )
829 {
830 struct contact *ct = &manifold[i];
831
832 float resolved_amt = v3_dot( ct->n, dt ),
833 remaining = (ct->p-k_penetration_slop) - resolved_amt,
834 apply = vg_maxf( remaining, 0.0f ) * 0.3f;
835
836 v3_muladds( dt, ct->n, apply, dt );
837 }
838 }
839 v3_add( dt, phys->rb.co, phys->rb.co );
840
841 v3_add( dt, walk_apply, walk_measured );
842 v3_divs( walk_measured, VG_TIMESTEP_FIXED, phys->rb.v );
843
844 if( len )
845 {
846 struct world_material *surface_mat = world_contact_material(manifold);
847 player.surface_prop = surface_mat->info.surface_prop;
848 }
849
850 /* jump */
851 if( player.input_jump->button.value )
852 {
853 phys->rb.v[1] = 5.0f;
854 phys->in_air = 1;
855 return;
856 }
857
858 /* Check if grounded by current manifold */
859 phys->in_air = 1;
860 for( int i=0; i<len; i++ )
861 {
862 struct contact *ct = &manifold[i];
863 if( player_walk_surface_standable( ct->n ) )
864 phys->in_air = 0;
865 }
866
867 /* otherwise... */
868 if( phys->in_air )
869 player_walk_stepdown();
870 }
871 }
872
873 VG_STATIC void player_grind(void)
874 {
875 struct player_phys *phys = &player.phys;
876
877 v3f closest;
878 int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
879 if( idx == -1 )
880 return;
881
882 struct grind_edge *edge = &world.grind_edges[ idx ];
883
884 vg_line( phys->rb.co, closest, 0xff000000 );
885 vg_line_cross( closest, 0xff000000, 0.3f );
886 vg_line( edge->p0, edge->p1, 0xff000000 );
887
888 v3f grind_delta;
889 v3_sub( closest, phys->rb.co, grind_delta );
890
891 float p = v3_dot( phys->rb.forward, grind_delta );
892 v3_muladds( grind_delta, phys->rb.forward, -p, grind_delta );
893
894 float a = vg_maxf( 0.0f, 4.0f-v3_dist2( closest, phys->rb.co ) );
895 v3_muladds( phys->rb.v, grind_delta, a*0.2f, phys->rb.v );
896 }
897
898 VG_STATIC int player_update_grind_collision( rb_ct *contact )
899 {
900 struct player_phys *phys = &player.phys;
901
902 v3f p0, p1, c0, c1;
903 v3_muladds( phys->rb.co, phys->rb.forward, 0.5f, p0 );
904 v3_muladds( phys->rb.co, phys->rb.forward, -0.5f, p1 );
905 v3_muladds( p0, phys->rb.up, 0.125f-0.15f, p0 );
906 v3_muladds( p1, phys->rb.up, 0.125f-0.15f, p1 );
907
908 float const k_r = 0.25f;
909 struct grind_edge *closest_edge = player_grind_collect_edge( p0, p1,
910 c0, c1, k_r );
911
912
913 #if 0
914 vg_line( p0, p1, 0xff0000ff );
915 #endif
916
917 if( closest_edge )
918 {
919 #if 0
920 vg_line_cross( c0, 0xff000000, 0.1f );
921 vg_line_cross( c1, 0xff000000, 0.1f );
922 vg_line( c0, c1, 0xff000000 );
923 #endif
924
925 v3f delta;
926 v3_sub( c1, c0, delta );
927
928 if( v3_dot( delta, phys->rb.up ) > 0.0001f )
929 {
930 contact->p = v3_length( delta );
931 contact->type = k_contact_type_edge;
932 contact->element_id = 0;
933 v3_copy( c1, contact->co );
934 contact->rba = &player.phys.rb;
935 contact->rbb = &world.rb_geo;
936
937 v3f edge_dir, axis_dir;
938 v3_sub( closest_edge->p1, closest_edge->p0, edge_dir );
939 v3_normalize( edge_dir );
940 v3_cross( (v3f){0.0f,1.0f,0.0f}, edge_dir, axis_dir );
941 v3_cross( edge_dir, axis_dir, contact->n );
942
943 #if 0
944 vg_info( "%f %f\n", v3_length( contact->n ), contact->p );
945 #endif
946
947 return 1;
948 }
949 else
950 return -1;
951 }
952
953 return 0;
954 }
955
956 /* Manifold must be able to hold at least 64 elements */
957 VG_STATIC int player_update_collision_manifold( rb_ct *manifold )
958 {
959 struct player_phys *phys = &player.phys;
960
961 rigidbody *rbf = &player.collide_front,
962 *rbb = &player.collide_back;
963
964 rb_debug( rbf, 0xff00ffff );
965 rb_debug( rbb, 0xffffff00 );
966
967
968 #if 0
969 phys->rise = vg_lerpf( phys->rise, phys->in_air? -0.25f: 0.0f,
970 VG_TIMESTEP_FIXED );
971 #endif
972
973 int len_f = 0,
974 len_b = 0;
975
976 len_f = rb_sphere_scene( rbf, &world.rb_geo, manifold );
977 rb_manifold_filter_coplanar( manifold, len_f, 0.05f );
978 if( len_f > 1 )
979 {
980 rb_manifold_filter_backface( manifold, len_f );
981 rb_manifold_filter_joint_edges( manifold, len_f, 0.05f );
982 rb_manifold_filter_pairs( manifold, len_f, 0.05f );
983 }
984 int new_len_f = rb_manifold_apply_filtered( manifold, len_f );
985 if( len_f && !new_len_f )
986 len_f = 1;
987 else
988 len_f = new_len_f;
989
990 rb_ct *man_b = &manifold[len_f];
991 len_b = rb_sphere_scene( rbb, &world.rb_geo, man_b );
992 rb_manifold_filter_coplanar( man_b, len_b, 0.05f );
993 if( len_b > 1 )
994 {
995 rb_manifold_filter_backface( man_b, len_b );
996 rb_manifold_filter_joint_edges( man_b, len_b, 0.05f );
997 rb_manifold_filter_pairs( man_b, len_b, 0.05f );
998 }
999 int new_len_b = rb_manifold_apply_filtered( man_b, len_b );
1000 if( len_b && !new_len_b )
1001 len_b = 1;
1002 else
1003 len_b = new_len_b;
1004
1005 return len_f + len_b;
1006 }
1007
1008 VG_STATIC void player_adhere_ground( rb_ct *manifold, int len )
1009 {
1010 struct player_phys *phys = &player.phys;
1011 int was_in_air = phys->in_air;
1012
1013 v3f surface_avg;
1014 v3_zero( surface_avg );
1015
1016 /*
1017 *
1018 * EXPERIMENTAL
1019 * ================================================================
1020 */
1021 if( phys->in_air )
1022 player.normal_pressure = 0.0f;
1023 else
1024 player.normal_pressure = v3_dot( phys->rb.up, phys->rb.v );
1025
1026 v3f p0_0, p0_1,
1027 p1_0, p1_1,
1028 n0, n1;
1029 float t0, t1;
1030
1031 float mod = 0.7f * player.input_grab->axis.value + 0.3f,
1032 spring_k = mod * k_spring_force,
1033 damp_K = mod * k_spring_dampener,
1034 disp_k = 0.4f;
1035
1036 v3_copy( player.collide_front.co, p0_0 );
1037 v3_copy( player.collide_back.co, p1_0 );
1038
1039 v3_muladds( p0_0, phys->rb.up, -disp_k, p0_1 );
1040 v3_muladds( p1_0, phys->rb.up, -disp_k, p1_1 );
1041
1042 int cast0 = spherecast_world( p0_0, p0_1, 0.2f, &t0, n0 ),
1043 cast1 = spherecast_world( p1_0, p1_1, 0.2f, &t1, n1 );
1044
1045 v3f animp0, animp1;
1046
1047 m4x3f temp;
1048 m3x3_copy( phys->rb.to_world, temp );
1049 if( cast0 != -1 )
1050 {
1051 v3_lerp( p0_0, p0_1, t0, temp[3] );
1052 v3_copy( temp[3], animp0 );
1053 debug_sphere( temp, 0.2f, VG__PINK );
1054
1055 v3f F, delta;
1056 v3_sub( p0_0, phys->rb.co, delta );
1057
1058 float displacement = vg_clampf( 1.0f-t0, 0.0f, 1.0f ),
1059 damp = vg_maxf( 0.0f, v3_dot( phys->rb.up, phys->rb.v ) );
1060 v3_muls( phys->rb.up, displacement*spring_k*k_rb_delta -
1061 damp*damp_K*k_rb_delta, F );
1062
1063 v3_muladds( phys->rb.v, F, 1.0f, phys->rb.v );
1064
1065 /* Angular velocity */
1066 v3f wa;
1067 v3_cross( delta, F, wa );
1068 v3_muladds( phys->rb.w, wa, k_spring_angular, phys->rb.w );
1069 }
1070 else
1071 v3_copy( p0_1, animp0 );
1072
1073 if( cast1 != -1 )
1074 {
1075 v3_lerp( p1_0, p1_1, t1, temp[3] );
1076 v3_copy( temp[3], animp1 );
1077 debug_sphere( temp, 0.2f, VG__PINK );
1078
1079 v3f F, delta;
1080 v3_sub( p1_0, phys->rb.co, delta );
1081
1082 float displacement = vg_clampf( 1.0f-t1, 0.0f, 1.0f ),
1083 damp = vg_maxf( 0.0f, v3_dot( phys->rb.up, phys->rb.v ) );
1084 v3_muls( phys->rb.up, displacement*spring_k*k_rb_delta -
1085 damp*damp_K*k_rb_delta, F );
1086
1087 v3_muladds( phys->rb.v, F, 1.0f, phys->rb.v );
1088
1089 /* Angular velocity */
1090 v3f wa;
1091 v3_cross( delta, F, wa );
1092 v3_muladds( phys->rb.w, wa, k_spring_angular, phys->rb.w );
1093 }
1094 else
1095 v3_copy( p1_1, animp1 );
1096
1097 v3f animavg, animdelta;
1098 v3_add( animp0, animp1, animavg );
1099 v3_muls( animavg, 0.5f, animavg );
1100
1101 v3_sub( animp1, animp0, animdelta );
1102 v3_normalize( animdelta );
1103
1104 m4x3_mulv( phys->rb.to_local, animavg, player.board_offset );
1105
1106 float dx = -v3_dot( animdelta, phys->rb.forward ),
1107 dy = v3_dot( animdelta, phys->rb.up );
1108
1109 float angle = -atan2f( dy, dx );
1110 q_axis_angle( player.board_rotation, (v3f){ 1.0f, 0.0f, 0.0f }, angle );
1111
1112 /*
1113 * ================================================================
1114 * EXPERIMENTAL
1115 */
1116
1117 if( len == 0 && !((cast0 !=-1)&&(cast1!=-1)) )
1118 {
1119 phys->lift_frames ++;
1120
1121 if( phys->lift_frames >= 8 )
1122 phys->in_air = 1;
1123 }
1124 else
1125 {
1126 for( int i=0; i<len; i++ )
1127 v3_add( surface_avg, manifold[i].n, surface_avg );
1128 v3_normalize( surface_avg );
1129
1130 if( v3_dot( phys->rb.v, surface_avg ) > 0.7f )
1131 {
1132 phys->lift_frames ++;
1133
1134 if( phys->lift_frames >= 8 )
1135 phys->in_air = 1;
1136 }
1137 else
1138 {
1139 phys->in_air = 0;
1140 phys->lift_frames = 0;
1141 v3f projected, axis;
1142
1143 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
1144 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
1145
1146 float d = v3_dot( phys->rb.forward, surface_avg );
1147 v3_muladds( surface_avg, phys->rb.forward, -d, projected );
1148 v3_normalize( projected );
1149
1150 float angle = v3_dot( phys->rb.up, projected );
1151 v3_cross( phys->rb.up, projected, axis );
1152
1153 #if 0
1154 v3f p0, p1;
1155 v3_add( phys->rb.co, projected, p0 );
1156 v3_add( phys->rb.co, phys->rb.up, p1 );
1157 vg_line( phys->rb.co, p0, 0xff00ff00 );
1158 vg_line( phys->rb.co, p1, 0xff000fff );
1159 #endif
1160
1161 if( fabsf(angle) < 0.999f )
1162 {
1163 v4f correction;
1164 q_axis_angle( correction, axis,
1165 acosf(angle)*4.0f*VG_TIMESTEP_FIXED );
1166 q_mul( correction, phys->rb.q, phys->rb.q );
1167 }
1168 }
1169 }
1170
1171 if( !was_in_air && phys->in_air )
1172 player_start_air();
1173 }
1174
1175 VG_STATIC void player_collision_response( rb_ct *manifold, int len )
1176 {
1177 struct player_phys *phys = &player.phys;
1178
1179 /*
1180 * EXPERIMENTAL
1181 * ===============================================
1182 */
1183
1184 #if 0
1185 player.normal_pressure = v3_dot( phys->rb.up, phys->rb.v );
1186
1187 {
1188 float ideal = 1.0f-player.input_grab->axis.value,
1189 diff = phys->spring - ideal,
1190 Fspring = -k_cog_spring_lat * diff,
1191 Fdamp = -k_cog_damp * phys->springv,
1192 F = (Fspring + Fdamp) * k_rb_delta;
1193
1194 phys->springv += F;
1195 phys->spring += phys->springv * k_rb_delta;
1196
1197 if( phys->springv > 0.0f )
1198 v3_muladds( phys->rb.v, phys->rb.up, F*k_cog_spring_vert, phys->rb.v );
1199
1200 if( phys->in_air )
1201 player.normal_pressure = 0.0f;
1202 else
1203 player.normal_pressure = v3_dot( phys->rb.up, phys->rb.v );
1204 }
1205 #endif
1206
1207 if( player.input_grab->axis.value > 0.5f )
1208 {
1209 if( !phys->in_air )
1210 {
1211 /* Throw */
1212 v3_muls( phys->rb.up, k_mmthrow_scale, phys->throw_v );
1213 }
1214 }
1215 else
1216 {
1217 /* Collect */
1218 float doty = v3_dot( phys->rb.up, phys->throw_v );
1219
1220 v3f Fl, Fv;
1221 v3_muladds( phys->throw_v, phys->rb.up, -doty, Fl );
1222
1223 if( !phys->in_air )
1224 {
1225 v3_muladds( phys->rb.v, Fl, k_mmcollect_lat, phys->rb.v );
1226 v3_muladds( phys->throw_v, Fl, -k_mmcollect_lat, phys->throw_v );
1227 }
1228
1229 v3_muls( phys->rb.up, -doty, Fv );
1230 v3_muladds( phys->rb.v, Fv, k_mmcollect_vert, phys->rb.v );
1231 v3_muladds( phys->throw_v, Fv, k_mmcollect_vert, phys->throw_v );
1232
1233 v3_copy( Fl, player.debug_mmcollect_lat );
1234 v3_copy( Fv, player.debug_mmcollect_vert );
1235 }
1236
1237 /* Decay */
1238 if( v3_length2( phys->throw_v ) > 0.0001f )
1239 {
1240 v3f dir;
1241 v3_copy( phys->throw_v, dir );
1242 v3_normalize( dir );
1243
1244 float max = v3_dot( dir, phys->throw_v ),
1245 amt = vg_minf( k_mmdecay * k_rb_delta, max );
1246
1247 v3_muladds( phys->throw_v, dir, -amt, phys->throw_v );
1248 }
1249
1250
1251 /* TODO: RElocate */
1252 {
1253
1254 v3f ideal_cog, ideal_diff;
1255 v3_muladds( phys->rb.co, phys->rb.up,
1256 1.0f-player.input_grab->axis.value, ideal_cog );
1257 v3_sub( ideal_cog, phys->cog, ideal_diff );
1258
1259 /* Apply velocities */
1260 v3f rv;
1261 v3_sub( phys->rb.v, phys->cog_v, rv );
1262
1263 v3f F;
1264 v3_muls( ideal_diff, -k_cog_spring * k_rb_rate, F );
1265 v3_muladds( F, rv, -k_cog_damp * k_rb_rate, F );
1266
1267 float ra = k_cog_mass_ratio,
1268 rb = 1.0f-k_cog_mass_ratio;
1269
1270 v3_muladds( phys->cog_v, F, -rb, phys->cog_v );
1271 }
1272
1273 /*
1274 * EXPERIMENTAL
1275 * ===============================================
1276 */
1277
1278 for( int j=0; j<10; j++ )
1279 {
1280 for( int i=0; i<len; i++ )
1281 {
1282 struct contact *ct = &manifold[i];
1283
1284 v3f dv, delta;
1285 v3_sub( ct->co, phys->rb.co, delta );
1286 v3_cross( phys->rb.w, delta, dv );
1287 v3_add( phys->rb.v, dv, dv );
1288
1289 float vn = -v3_dot( dv, ct->n );
1290 vn += ct->bias;
1291
1292 float temp = ct->norm_impulse;
1293 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
1294 vn = ct->norm_impulse - temp;
1295
1296 v3f impulse;
1297 v3_muls( ct->n, vn, impulse );
1298
1299 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
1300 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
1301 {
1302 player_kill();
1303 return;
1304 }
1305
1306 v3_add( impulse, phys->rb.v, phys->rb.v );
1307 v3_cross( delta, impulse, impulse );
1308
1309 /*
1310 * W Impulses are limited to the Y and X axises, we don't really want
1311 * roll angular velocities being included.
1312 *
1313 * Can also tweak the resistance of each axis here by scaling the wx,wy
1314 * components.
1315 */
1316
1317 float wy = v3_dot( phys->rb.up, impulse ) * 0.8f,
1318 wx = v3_dot( phys->rb.right, impulse )*1.0f;
1319
1320 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
1321 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
1322 }
1323 }
1324
1325 /* early integrate this */
1326 phys->cog_v[1] += -9.8f * k_rb_delta;
1327 v3_muladds( phys->cog, phys->cog_v, k_rb_delta, phys->cog );
1328 }
1329
1330 VG_STATIC void player_save_frame(void)
1331 {
1332 player.phys_gate_frame = player.phys;
1333 }
1334
1335 VG_STATIC void player_restore_frame(void)
1336 {
1337 player.phys = player.phys_gate_frame;
1338 rb_update_transform( &player.phys.rb );
1339 }
1340
1341 VG_STATIC void player_integrate(void)
1342 {
1343 struct player_phys *phys = &player.phys;
1344 v3_sub( phys->rb.v, phys->v_last, phys->a );
1345 v3_muls( phys->a, 1.0f/VG_TIMESTEP_FIXED, phys->a );
1346 v3_copy( phys->rb.v, phys->v_last );
1347
1348 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
1349 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
1350 }
1351
1352 VG_STATIC void player_do_motion(void)
1353 {
1354 struct player_phys *phys = &player.phys;
1355
1356 if( world.water.enabled )
1357 {
1358 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
1359 {
1360 audio_lock();
1361 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
1362 audio_player_set_position( &audio_player_extra, phys->rb.co );
1363 audio_player_set_vol( &audio_player_extra, 20.0f );
1364 audio_player_playclip( &audio_player_extra, &audio_splash );
1365 audio_unlock();
1366
1367 player_kill();
1368 }
1369 }
1370
1371 v3f prevco;
1372 v3_copy( phys->rb.co, prevco );
1373
1374 if( phys->on_board )
1375 {
1376 rb_ct manifold[72];
1377
1378 player_regular_collider_configuration();
1379 int len = player_update_collision_manifold( manifold );
1380 int grind_col = player_update_grind_collision( &manifold[len] );
1381
1382 static int _grind_col_pre = 0;
1383
1384 if( grind_col )
1385 {
1386 phys->grind = 1;
1387 v3f up = { 0.0f, 1.0f, 0.0f };
1388 float angle = v3_dot( phys->rb.up, up );
1389
1390 if( fabsf(angle) < 0.99f )
1391 {
1392 v3f axis;
1393 v3_cross( phys->rb.up, up, axis );
1394
1395 v4f correction;
1396 q_axis_angle( correction, axis,
1397 VG_TIMESTEP_FIXED * 10.0f * acosf(angle) );
1398 q_mul( correction, phys->rb.q, phys->rb.q );
1399 }
1400
1401 float const DOWNFORCE = -k_downforce*1.2f*VG_TIMESTEP_FIXED;
1402 v3_muladds( phys->rb.v, manifold[len].n, DOWNFORCE, phys->rb.v );
1403 m3x3_identity( phys->vr );
1404 m3x3_identity( phys->vr_pstep );
1405
1406 if( !_grind_col_pre )
1407 {
1408 audio_lock();
1409 audio_player_set_flags( &audio_player_extra,
1410 AUDIO_FLAG_SPACIAL_3D );
1411 audio_player_set_position( &audio_player_extra, phys->rb.co );
1412 audio_player_set_vol( &audio_player_extra, 20.0f );
1413 audio_player_playclip( &audio_player_extra, &audio_board[5] );
1414 audio_unlock();
1415 }
1416 }
1417 else
1418 {
1419 phys->grind = 0;
1420 player_adhere_ground( manifold, len );
1421
1422 if( _grind_col_pre )
1423 {
1424 audio_lock();
1425 audio_player_set_flags( &audio_player_extra,
1426 AUDIO_FLAG_SPACIAL_3D );
1427 audio_player_set_position( &audio_player_extra, phys->rb.co );
1428 audio_player_set_vol( &audio_player_extra, 20.0f );
1429 audio_player_playclip( &audio_player_extra, &audio_board[6] );
1430 audio_unlock();
1431 }
1432 }
1433
1434 _grind_col_pre = grind_col;
1435
1436 rb_presolve_contacts( manifold, len+ VG_MAX(0,grind_col) );
1437 player_collision_response( manifold, len+ VG_MAX(0,grind_col) );
1438
1439 player_physics_control_passive();
1440
1441 if( grind_col )
1442 {
1443 phys->in_air = 0;
1444 player_physics_control_grind();
1445 }
1446 else
1447 {
1448 if( phys->in_air )
1449 player_physics_control_air();
1450 else
1451 player_physics_control();
1452 }
1453
1454 player_integrate();
1455 }
1456 else
1457 player_walk_physics();
1458
1459
1460 /* Real angular velocity integration */
1461 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f*0.5f, phys->rb.w );
1462 if( v3_length2( phys->rb.w ) > 0.0f )
1463 {
1464 v4f rotation;
1465 v3f axis;
1466 v3_copy( phys->rb.w, axis );
1467
1468 float mag = v3_length( axis );
1469 v3_divs( axis, mag, axis );
1470 q_axis_angle( rotation, axis, mag*k_rb_delta );
1471 q_mul( rotation, phys->rb.q, phys->rb.q );
1472 }
1473
1474 /* Faux angular velocity */
1475 v4f rotate;
1476
1477 float lerpq = phys->in_air? 0.04f: 0.3f;
1478 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
1479
1480 q_axis_angle( rotate, phys->rb.up, phys->siY );
1481 q_mul( rotate, phys->rb.q, phys->rb.q );
1482 phys->iY = 0.0f;
1483
1484 /*
1485 * Gate intersection, by tracing a line over the gate planes
1486 */
1487 for( int i=0; i<world.gate_count; i++ )
1488 {
1489 struct route_gate *rg = &world.gates[i];
1490 teleport_gate *gate = &rg->gate;
1491
1492 if( gate_intersect( gate, phys->rb.co, prevco ) )
1493 {
1494 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
1495 m4x3_mulv( gate->transport, phys->cog, phys->cog );
1496 m3x3_mulv( gate->transport, phys->cog_v, phys->cog_v );
1497 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
1498 m3x3_mulv( gate->transport, phys->vl, phys->vl );
1499 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
1500 m3x3_mulv( gate->transport, phys->m, phys->m );
1501 m3x3_mulv( gate->transport, phys->bob, phys->bob );
1502
1503 /* Pre-emptively edit the camera matrices so that the motion vectors
1504 * are correct */
1505 m4x3f transport_i;
1506 m4x4f transport_4;
1507 m4x3_invert_affine( gate->transport, transport_i );
1508 m4x3_expand( transport_i, transport_4 );
1509 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
1510 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
1511
1512 v4f transport_rotation;
1513 m3x3_q( gate->transport, transport_rotation );
1514 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
1515
1516 world_routes_activate_gate( i );
1517
1518 if( !phys->on_board )
1519 {
1520 v3f fwd_dir = {cosf(player.angles[0]),
1521 0.0f,
1522 sinf(player.angles[0])};
1523 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
1524
1525 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
1526 }
1527
1528 player.rewind_length = 0;
1529 player.rewind_total_length = 0.0f;
1530 player.rewind_incrementer = 10000;
1531 player_save_frame();
1532
1533 audio_lock();
1534 audio_play_oneshot( &audio_gate_pass, 1.0f );
1535 audio_unlock();
1536 break;
1537 }
1538 }
1539
1540 rb_update_transform( &phys->rb );
1541 }
1542
1543 VG_STATIC void player_freecam(void)
1544 {
1545 player_mouseview();
1546
1547 float movespeed = fc_speed * VG_TIMESTEP_FIXED;
1548 v3f lookdir = { 0.0f, 0.0f, -1.0f },
1549 sidedir = { 1.0f, 0.0f, 0.0f };
1550
1551 m3x3_mulv( main_camera.transform, lookdir, lookdir );
1552 m3x3_mulv( main_camera.transform, sidedir, sidedir );
1553
1554 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
1555
1556 v2f steer = { player.input_js1h->axis.value,
1557 player.input_js1v->axis.value };
1558
1559 v3_muladds( move_vel, sidedir, movespeed*steer[0], move_vel );
1560 v3_muladds( move_vel, lookdir, -movespeed*steer[1], move_vel );
1561
1562 v3_muls( move_vel, 0.7f, move_vel );
1563 v3_add( move_vel, player.camera_pos, player.camera_pos );
1564 }
1565
1566 VG_STATIC int kill_player( int argc, char const *argv[] )
1567 {
1568 player_kill();
1569 return 0;
1570 }
1571
1572 VG_STATIC int reset_player( int argc, char const *argv[] )
1573 {
1574 struct player_phys *phys = &player.phys;
1575 struct respawn_point *rp = NULL, *r;
1576
1577 if( argc == 1 )
1578 {
1579 for( int i=0; i<world.spawn_count; i++ )
1580 {
1581 r = &world.spawns[i];
1582 if( !strcmp( r->name, argv[0] ) )
1583 {
1584 rp = r;
1585 break;
1586 }
1587 }
1588
1589 if( !rp )
1590 vg_warn( "No spawn named '%s'\n", argv[0] );
1591 }
1592
1593 if( !rp )
1594 {
1595 float min_dist = INFINITY;
1596
1597 for( int i=0; i<world.spawn_count; i++ )
1598 {
1599 r = &world.spawns[i];
1600 float d = v3_dist2( r->co, phys->rb.co );
1601
1602 vg_info( "Dist %s : %f\n", r->name, d );
1603 if( d < min_dist )
1604 {
1605 min_dist = d;
1606 rp = r;
1607 }
1608 }
1609 }
1610
1611 if( !rp )
1612 {
1613 vg_error( "No spawn found\n" );
1614 vg_info( "Player position: %f %f %f\n", player.phys.rb.co[0],
1615 player.phys.rb.co[1],
1616 player.phys.rb.co[2] );
1617 vg_info( "Player velocity: %f %f %f\n", player.phys.rb.v[0],
1618 player.phys.rb.v[1],
1619 player.phys.rb.v[2] );
1620
1621 if( !world.spawn_count )
1622 return 0;
1623
1624 rp = &world.spawns[0];
1625 }
1626
1627 player.is_dead = 0;
1628
1629 m3x3f the_long_way;
1630 q_m3x3( rp->q, the_long_way );
1631
1632 v3f delta = {1.0f,0.0f,0.0f};
1633 m3x3_mulv( the_long_way, delta, delta );
1634
1635 if( !freecam )
1636 {
1637 player.angles[0] = atan2f( delta[0], -delta[2] );
1638 player.angles[1] = -asinf( delta[1] );
1639 }
1640
1641 v4_copy( rp->q, phys->rb.q );
1642 v3_copy( rp->co, phys->rb.co );
1643 v3_zero( phys->rb.v );
1644
1645 phys->vswitch = 1.0f;
1646 phys->slip_last = 0.0f;
1647 phys->in_air = 1;
1648 phys->on_board = 0;
1649 m3x3_identity( phys->vr );
1650
1651 player.mdl.shoes[0] = 1;
1652 player.mdl.shoes[1] = 1;
1653
1654 rb_update_transform( &phys->rb );
1655
1656 v3_add( phys->rb.up, phys->rb.co, phys->cog );
1657 v3_zero( phys->cog_v );
1658
1659 player_save_frame();
1660 return 1;
1661 }
1662
1663 VG_STATIC void reset_player_poll( int argc, char const *argv[] )
1664 {
1665 if( argc == 1 )
1666 {
1667 for( int i=0; i<world.spawn_count; i++ )
1668 {
1669 struct respawn_point *r = &world.spawns[i];
1670
1671 console_suggest_score_text( r->name, argv[argc-1], 0 );
1672 }
1673 }
1674 }
1675
1676 VG_STATIC void player_physics_gui(void)
1677 {
1678 return;
1679
1680 vg_uictx.cursor[0] = 0;
1681 vg_uictx.cursor[1] = vg.window_y - 128;
1682 vg_uictx.cursor[3] = 14;
1683 ui_fill_x();
1684
1685 char buf[128];
1686
1687 snprintf( buf, 127, "v: %6.3f %6.3f %6.3f\n", player.phys.rb.v[0],
1688 player.phys.rb.v[1],
1689 player.phys.rb.v[2] );
1690
1691 ui_text( vg_uictx.cursor, buf, 1, 0 );
1692 vg_uictx.cursor[1] += 14;
1693
1694
1695 snprintf( buf, 127, "a: %6.3f %6.3f %6.3f (%6.3f)\n", player.phys.a[0],
1696 player.phys.a[1],
1697 player.phys.a[2],
1698 v3_length(player.phys.a));
1699 ui_text( vg_uictx.cursor, buf, 1, 0 );
1700 vg_uictx.cursor[1] += 14;
1701
1702 float normal_acceleration = v3_dot( player.phys.a, player.phys.rb.up );
1703 snprintf( buf, 127, "Normal acceleration: %6.3f\n", normal_acceleration );
1704
1705 ui_text( vg_uictx.cursor, buf, 1, 0 );
1706 vg_uictx.cursor[1] += 14;
1707
1708 snprintf( buf, 127, "Normal Pressure: %6.3f\n", player.normal_pressure );
1709 ui_text( vg_uictx.cursor, buf, 1, 0 );
1710 vg_uictx.cursor[1] += 14;
1711
1712
1713 }
1714
1715 #endif /* PLAYER_PHYSICS_H */