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