a fairly major physics update
[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 * 0.2f;
360 float fwd_resistance = k_friction_resistance;
361
362 for( int i=0; i<5; i++ )
363 {
364 vel[2] = stable_force( vel[2],vg_signf(vel[2]) * -fwd_resistance*substep);
365 vel[0] = stable_force( vel[0],vg_signf(vel[0]) * -k_friction_lat*substep);
366 }
367
368 if( player.input_jump->button.value )
369 {
370 phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed;
371
372 if( !phys->jump_charge )
373 phys->jump_dir = phys->reverse > 0.0f? 1: 0;
374
375 phys->jump_charge = 1;
376 }
377
378 static int push_thresh_last = 0;
379 float push = player.input_push->button.value;
380 int push_thresh = push>0.15f? 1: 0;
381
382 if( push_thresh && !push_thresh_last )
383 player.phys.start_push = vg.time;
384
385 push_thresh_last = push_thresh;
386
387 if( !player.input_jump->button.value && push_thresh )
388 {
389 player.phys.pushing = 1.0f;
390 player.phys.push_time = vg.time - player.phys.start_push;
391
392 float cycle_time = player.phys.push_time*k_push_cycle_rate,
393 amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED,
394 current = v3_length( vel ),
395 new_vel = vg_minf( current + amt, k_max_push_speed );
396
397 new_vel -= vg_minf(current, k_max_push_speed);
398 vel[2] -= new_vel * phys->reverse;
399 }
400
401 m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
402
403 float input = player.input_js1h->axis.value,
404 grab = player.input_grab->axis.value,
405 steer = input * (1.0f-(phys->jump+grab)*0.4f),
406 steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
407
408 phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
409
410 if( !phys->jump_charge && phys->jump > 0.2f )
411 {
412 v3f jumpdir;
413
414 /* Launch more up if alignment is up else improve velocity */
415 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
416 mod = 0.5f,
417 dir = mod + aup*(1.0f-mod);
418
419 v3_copy( phys->rb.v, jumpdir );
420 v3_normalize( jumpdir );
421 v3_muls( jumpdir, 1.0f-dir, jumpdir );
422 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
423 v3_normalize( jumpdir );
424
425 float force = k_jump_force*phys->jump;
426 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
427 phys->jump = 0.0f;
428
429 player.jump_time = vg.time;
430
431 /* TODO: Move to audio file */
432 audio_lock();
433 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
434 audio_player_set_position( &audio_player_extra, phys->rb.co );
435 audio_player_set_vol( &audio_player_extra, 20.0f );
436 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
437 audio_unlock();
438 }
439 }
440
441 VG_STATIC void player_physics_control_grind(void)
442 {
443 struct player_phys *phys = &player.phys;
444 v2f steer = { player.input_js1h->axis.value,
445 player.input_js1v->axis.value };
446
447 float l2 = v2_length2( steer );
448 if( l2 > 1.0f )
449 v2_muls( steer, 1.0f/sqrtf(l2), steer );
450
451 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
452
453 float iX = steer[1] * phys->reverse * k_steer_air * VG_TIMESTEP_FIXED;
454
455 static float siX = 0.0f;
456 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
457
458 v4f rotate;
459 q_axis_angle( rotate, phys->rb.right, siX );
460 q_mul( rotate, phys->rb.q, phys->rb.q );
461
462 phys->slip = 0.0f;
463 }
464
465 /*
466 * Air control, no real physics
467 */
468 VG_STATIC void player_physics_control_air(void)
469 {
470 struct player_phys *phys = &player.phys;
471
472 m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
473 //vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
474
475 ray_hit hit;
476
477 /*
478 * Prediction
479 */
480 float pstep = VG_TIMESTEP_FIXED * 1.0f;
481 float k_bias = 0.98f;
482
483 v3f pco, pco1, pv;
484 v3_copy( phys->rb.co, pco );
485 v3_muls( phys->rb.v, 1.0f, pv );
486
487 float time_to_impact = 0.0f;
488 float limiter = 1.0f;
489
490 struct grind_edge *best_grind = NULL;
491 float closest_grind = INFINITY;
492
493 v3f target_normal = { 0.0f, 1.0f, 0.0f };
494 int has_target = 0;
495
496 for( int i=0; i<250; i++ )
497 {
498 v3_copy( pco, pco1 );
499 m3x3_mulv( phys->vr, pv, pv );
500 apply_gravity( pv, pstep );
501 v3_muladds( pco, pv, pstep, pco );
502
503 ray_hit contact;
504 v3f vdir;
505
506 v3_sub( pco, pco1, vdir );
507 contact.dist = v3_length( vdir );
508 v3_divs( vdir, contact.dist, vdir);
509
510 v3f c0, c1;
511 struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
512 c0, c1, 0.4f );
513
514 if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
515 {
516 vg_line( ge->p0, ge->p1, 0xff0000ff );
517 vg_line_cross( pco, 0xff0000ff, 0.25f );
518 has_target = 1;
519 break;
520 }
521
522 float orig_dist = contact.dist;
523 if( ray_world( pco1, vdir, &contact ) )
524 {
525 v3_copy( contact.normal, target_normal );
526 has_target = 1;
527 time_to_impact += (contact.dist/orig_dist)*pstep;
528 vg_line_cross( contact.pos, 0xffff0000, 0.25f );
529 break;
530 }
531 time_to_impact += pstep;
532 }
533
534 if( has_target )
535 {
536 float angle = v3_dot( phys->rb.up, target_normal );
537 v3f axis;
538 v3_cross( phys->rb.up, target_normal, axis );
539
540 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
541 limiter = 1.0f-limiter;
542 limiter *= limiter;
543 limiter = 1.0f-limiter;
544
545 if( fabsf(angle) < 0.99f )
546 {
547 v4f correction;
548 q_axis_angle( correction, axis,
549 acosf(angle)*(1.0f-limiter)*3.0f*VG_TIMESTEP_FIXED );
550 q_mul( correction, phys->rb.q, phys->rb.q );
551 }
552 }
553
554 v2f steer = { player.input_js1h->axis.value,
555 player.input_js1v->axis.value };
556
557 float l2 = v2_length2( steer );
558 if( l2 > 1.0f )
559 v2_muls( steer, 1.0f/sqrtf(l2), steer );
560
561 phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
562
563 float iX = steer[1] *
564 phys->reverse * k_steer_air * limiter * VG_TIMESTEP_FIXED;
565
566 static float siX = 0.0f;
567 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
568
569 v4f rotate;
570 q_axis_angle( rotate, phys->rb.right, siX );
571 q_mul( rotate, phys->rb.q, phys->rb.q );
572
573 #if 0
574 v2f target = {0.0f,0.0f};
575 v2_muladds( target, (v2f){ vg_get_axis("grabh"), vg_get_axis("grabv") },
576 phys->grab, target );
577 #endif
578 }
579
580 VG_STATIC void player_walk_update_collision(void)
581 {
582 struct player_phys *phys = &player.phys;
583 float h0 = 0.3f,
584 h1 = 0.9f;
585
586 rigidbody *rbf = &player.collide_front,
587 *rbb = &player.collide_back;
588
589 v3_add( phys->rb.co, (v3f){0.0f,h0,0.0f}, rbf->co );
590 v3_add( phys->rb.co, (v3f){0.0f,h1,0.0f}, rbb->co );
591 v3_copy( rbf->co, rbf->to_world[3] );
592 v3_copy( rbb->co, rbb->to_world[3] );
593 m4x3_invert_affine( rbf->to_world, rbf->to_local );
594 m4x3_invert_affine( rbb->to_world, rbb->to_local );
595
596 rb_update_bounds( rbf );
597 rb_update_bounds( rbb );
598 }
599
600 VG_STATIC void player_integrate(void);
601
602 VG_STATIC int player_walk_surface_standable( v3f n )
603 {
604 return v3_dot( n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f;
605 }
606
607 VG_STATIC void player_walk_stepdown(void)
608 {
609 struct player_phys *phys = &player.phys;
610 float max_dist = 0.4f;
611
612 v3f pa, pb;
613 v3_copy( phys->rb.co, pa );
614 pa[1] += 0.3f;
615
616 v3_muladds( pa, (v3f){0.01f,1.0f,0.01f}, -max_dist, pb );
617 vg_line( pa, pb, 0xff000000 );
618
619 /* TODO: Make #define */
620 float r = 0.3f,
621 t;
622
623 v3f n;
624 if( spherecast_world( pa, pb, r, &t, n ) != -1 )
625 {
626 if( player_walk_surface_standable( n ) )
627 {
628 phys->in_air = 0;
629 v3_lerp( pa, pb, t+0.001f, phys->rb.co );
630 phys->rb.co[1] -= 0.3f;
631 }
632 }
633 }
634
635 /*
636 * Entire Walking physics model
637 * TODO: sleep when under certain velotiy
638 */
639 VG_STATIC void player_walk_physics(void)
640 {
641 struct player_phys *phys = &player.phys;
642 rigidbody *rbf = &player.collide_front,
643 *rbb = &player.collide_back;
644
645 m3x3_identity( player.collide_front.to_world );
646 m3x3_identity( player.collide_back.to_world );
647
648 v3_zero( phys->rb.w );
649 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
650
651 rb_ct manifold[64];
652 int len;
653
654 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
655 v3f right_dir = { -forward_dir[2], 0.0f, forward_dir[0] };
656
657 v2f walk = { player.input_walkh->axis.value,
658 player.input_walkv->axis.value };
659
660 if( freecam )
661 v2_zero( walk );
662
663 if( v2_length2(walk) > 0.001f )
664 v2_normalize_clamp( walk );
665
666 if( phys->in_air )
667 {
668 player_walk_update_collision();
669 rb_debug( rbf, 0xff0000ff );
670 rb_debug( rbb, 0xff0000ff );
671
672 /* allow player to accelerate a bit */
673 v3f walk_3d;
674 v3_muls( forward_dir, walk[1], walk_3d );
675 v3_muladds( walk_3d, right_dir, walk[0], walk_3d );
676
677 float current_vel = fabsf(v3_dot( walk_3d, phys->rb.v )),
678 new_vel = current_vel + VG_TIMESTEP_FIXED*k_air_accelerate,
679 clamped_new = vg_clampf( new_vel, 0.0f, k_walkspeed ),
680 vel_diff = vg_maxf( 0.0f, clamped_new - current_vel );
681
682 v3_muladds( phys->rb.v, right_dir, walk[0] * vel_diff, phys->rb.v );
683 v3_muladds( phys->rb.v, forward_dir, walk[1] * vel_diff, phys->rb.v );
684
685
686 len = 0;
687 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
688 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
689 rb_presolve_contacts( manifold, len );
690
691 for( int i=0; i<len; i++ )
692 {
693 struct contact *ct = &manifold[i];
694 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
695 phys->in_air = 0;
696 }
697
698 for( int j=0; j<5; j++ )
699 {
700 for( int i=0; i<len; i++ )
701 {
702 struct contact *ct = &manifold[i];
703
704 /*normal */
705 float vn = -v3_dot( phys->rb.v, ct->n );
706 vn += ct->bias;
707
708 float temp = ct->norm_impulse;
709 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
710 vn = ct->norm_impulse - temp;
711
712 v3f impulse;
713 v3_muls( ct->n, vn, impulse );
714
715 v3_add( impulse, phys->rb.v, phys->rb.v );
716
717 /* friction */
718 for( int j=0; j<2; j++ )
719 {
720 float f = k_friction * ct->norm_impulse,
721 vt = v3_dot( phys->rb.v, ct->t[j] ),
722 lambda = -vt;
723
724 float temp = ct->tangent_impulse[j];
725 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
726 lambda = ct->tangent_impulse[j] - temp;
727
728 v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
729 }
730 }
731 }
732
733 player_integrate();
734 }
735 else
736 {
737 player.walk = v2_length( walk );
738
739 if( player.input_walk->button.value )
740 v2_muls( walk, 0.5f, walk );
741
742 v2_muls( walk, k_walkspeed * VG_TIMESTEP_FIXED, walk );
743
744 v3f walk_apply;
745 v3_zero( walk_apply );
746
747 /* Do XY translation */
748 v3_muladds( walk_apply, right_dir, walk[0], walk_apply );
749 v3_muladds( walk_apply, forward_dir, walk[1], walk_apply );
750 v3_add( walk_apply, phys->rb.co, phys->rb.co );
751 v3_divs( walk_apply, VG_TIMESTEP_FIXED, phys->rb.v );
752
753 /* Directly resolve collisions */
754 player_walk_update_collision();
755 rb_debug( rbf, 0xffffff00 );
756 rb_debug( rbb, 0xffffff00 );
757
758 len = 0;
759 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
760 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
761
762 v3f dt;
763 v3_zero( dt );
764 for( int j=0; j<3; j++ )
765 {
766 for( int i=0; i<len; i++ )
767 {
768 struct contact *ct = &manifold[i];
769
770 float p = vg_maxf( 0.0f, ct->p - 0.00f ),
771 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
772 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
773 }
774 }
775 v3_add( dt, phys->rb.co, phys->rb.co );
776
777 if( len )
778 {
779 struct world_material *surface_mat = world_contact_material(manifold);
780 player.surface_prop = surface_mat->info.surface_prop;
781 }
782
783 /* jump */
784 if( player.input_jump->button.value )
785 {
786 phys->rb.v[1] = 5.0f;
787 phys->in_air = 1;
788 return;
789 }
790
791 /* Check if grounded by current manifold */
792 phys->in_air = 1;
793 for( int i=0; i<len; i++ )
794 {
795 struct contact *ct = &manifold[i];
796 if( player_walk_surface_standable( ct->n ) )
797 phys->in_air = 0;
798 }
799
800 /* otherwise... */
801 if( phys->in_air )
802 player_walk_stepdown();
803
804 #if 0
805 /* if we've put us in the air, step down slowly */
806 phys->in_air = 1;
807 float max_dist = 0.3f,
808 start_y = phys->rb.co[1];
809
810 v3f pa, pb;
811 v3_copy( phys->rb.co, pa );
812 v3_muladds( pa, (v3f){0.0f,1.0f,0.0f}, -max_dist, pb );
813
814
815 for( int j=0; j<8; j++ )
816 {
817 for( int i=0; i<len; i++ )
818 {
819 struct contact *ct = &manifold[i];
820 if( v3_dot( ct->n, (v3f){0.0f,1.0f,0.0f} ) > 0.5f )
821 {
822 phys->in_air = 0;
823 if( j == 0 )
824 return;
825
826 v3f dt;
827 v3_zero( dt );
828 for( int j=0; j<3; j++ )
829 {
830 for( int i=0; i<len; i++ )
831 {
832 struct contact *ct = &manifold[i];
833
834 float p = vg_maxf( 0.0f, ct->p - 0.0025f ),
835 cur = vg_clampf( v3_dot( ct->n, dt ), 0.0f, p );
836 v3_muladds( dt, ct->n, (p - cur) * 0.333333333f, dt );
837 }
838 }
839 v3_add( dt, phys->rb.co, phys->rb.co );
840 return;
841 }
842 }
843
844 phys->rb.co[1] -= max_dist * 0.125f;
845
846 player_walk_update_collision();
847 len = 0;
848 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
849 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
850 }
851
852 /* Transitioning into air mode */
853 phys->rb.co[1] = start_y;
854 #endif
855 }
856 }
857
858 VG_STATIC void player_grind(void)
859 {
860 struct player_phys *phys = &player.phys;
861
862 v3f closest;
863 int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
864 if( idx == -1 )
865 return;
866
867 struct grind_edge *edge = &world.grind_edges[ idx ];
868
869 vg_line( phys->rb.co, closest, 0xff000000 );
870 vg_line_cross( closest, 0xff000000, 0.3f );
871 vg_line( edge->p0, edge->p1, 0xff000000 );
872
873 v3f grind_delta;
874 v3_sub( closest, phys->rb.co, grind_delta );
875
876 float p = v3_dot( phys->rb.forward, grind_delta );
877 v3_muladds( grind_delta, phys->rb.forward, -p, grind_delta );
878
879 float a = vg_maxf( 0.0f, 4.0f-v3_dist2( closest, phys->rb.co ) );
880 v3_muladds( phys->rb.v, grind_delta, a*0.2f, phys->rb.v );
881 }
882
883 VG_STATIC int player_update_grind_collision( rb_ct *contact )
884 {
885 struct player_phys *phys = &player.phys;
886
887 v3f p0, p1, c0, c1;
888 v3_muladds( phys->rb.co, phys->rb.forward, 0.5f, p0 );
889 v3_muladds( phys->rb.co, phys->rb.forward, -0.5f, p1 );
890 v3_muladds( p0, phys->rb.up, 0.125f-0.15f, p0 );
891 v3_muladds( p1, phys->rb.up, 0.125f-0.15f, p1 );
892
893 float const k_r = 0.25f;
894 struct grind_edge *closest_edge = player_grind_collect_edge( p0, p1,
895 c0, c1, k_r );
896
897
898 vg_line( p0, p1, 0xff0000ff );
899
900 if( closest_edge )
901 {
902 vg_line_cross( c0, 0xff000000, 0.1f );
903 vg_line_cross( c1, 0xff000000, 0.1f );
904 vg_line( c0, c1, 0xff000000 );
905
906 v3f delta;
907 v3_sub( c1, c0, delta );
908
909 if( v3_dot( delta, phys->rb.up ) > 0.0001f )
910 {
911 contact->p = v3_length( delta );
912 contact->type = k_contact_type_edge;
913 contact->element_id = 0;
914 v3_copy( c1, contact->co );
915 contact->rba = &player.phys.rb;
916 contact->rbb = &world.rb_geo;
917
918 v3f edge_dir, axis_dir;
919 v3_sub( closest_edge->p1, closest_edge->p0, edge_dir );
920 v3_normalize( edge_dir );
921 v3_cross( (v3f){0.0f,1.0f,0.0f}, edge_dir, axis_dir );
922 v3_cross( edge_dir, axis_dir, contact->n );
923
924 #if 0
925 vg_info( "%f %f\n", v3_length( contact->n ), contact->p );
926 #endif
927
928 return 1;
929 }
930 else
931 return -1;
932 }
933
934 return 0;
935 }
936
937 /* Manifold must be able to hold at least 64 elements */
938 VG_STATIC int player_update_collision_manifold( rb_ct *manifold )
939 {
940 struct player_phys *phys = &player.phys;
941
942 phys->rise = vg_lerpf( phys->rise, phys->in_air? -0.25f: 0.0f,
943 VG_TIMESTEP_FIXED );
944
945 rigidbody *rbf = &player.collide_front,
946 *rbb = &player.collide_back;
947
948 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
949 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
950
951 player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
952 float h = player.air_blend*0.0f;
953
954 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
955 v3_copy( rbf->co, rbf->to_world[3] );
956 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
957 v3_copy( rbb->co, rbb->to_world[3] );
958
959 m4x3_invert_affine( rbf->to_world, rbf->to_local );
960 m4x3_invert_affine( rbb->to_world, rbb->to_local );
961
962 rb_update_bounds( rbf );
963 rb_update_bounds( rbb );
964
965 rb_debug( rbf, 0xff00ffff );
966 rb_debug( rbb, 0xffffff00 );
967
968 int len_f = 0,
969 len_b = 0;
970
971 len_f = rb_sphere_scene( rbf, &world.rb_geo, manifold );
972 rb_manifold_filter_coplanar( manifold, len_f, 0.05f );
973 if( len_f > 1 )
974 {
975 rb_manifold_filter_backface( manifold, len_f );
976 rb_manifold_filter_joint_edges( manifold, len_f, 0.05f );
977 rb_manifold_filter_pairs( manifold, len_f, 0.05f );
978 }
979 int new_len_f = rb_manifold_apply_filtered( manifold, len_f );
980 if( len_f && !new_len_f )
981 len_f = 1;
982 else
983 len_f = new_len_f;
984
985 rb_ct *man_b = &manifold[len_f];
986 len_b = rb_sphere_scene( rbb, &world.rb_geo, man_b );
987 rb_manifold_filter_coplanar( man_b, len_b, 0.05f );
988 if( len_b > 1 )
989 {
990 rb_manifold_filter_backface( man_b, len_b );
991 rb_manifold_filter_joint_edges( man_b, len_b, 0.05f );
992 rb_manifold_filter_pairs( man_b, len_b, 0.05f );
993 }
994 int new_len_b = rb_manifold_apply_filtered( man_b, len_b );
995 if( len_b && !new_len_b )
996 len_b = 1;
997 else
998 len_b = new_len_b;
999 #if 0
1000 /*
1001 * Preprocess collision points, and create a surface picture.
1002 * we want contacts that are within our 'capsule's internal line to be
1003 * clamped so that they face the line and do not oppose, to stop the
1004 * player hanging up on stuff
1005 */
1006 for( int i=0; i<len; i++ )
1007 {
1008 v3f dfront, dback;
1009 v3_sub( manifold[i].co, rbf->co, dfront );
1010 v3_sub( manifold[i].co, rbb->co, dback );
1011
1012 if( (v3_dot( dfront, phys->rb.forward ) < -0.02f) &&
1013 (v3_dot( dback, phys->rb.forward ) > 0.02f))
1014 {
1015 float p = v3_dot( manifold[i].n, phys->rb.forward );
1016 v3_muladds( manifold[i].n, phys->rb.forward, -p, manifold[i].n );
1017 v3_normalize( manifold[i].n );
1018 }
1019 }
1020 #endif
1021
1022 return len_f + len_b;
1023 }
1024
1025 VG_STATIC void player_adhere_ground( rb_ct *manifold, int len )
1026 {
1027 struct player_phys *phys = &player.phys;
1028 int was_in_air = phys->in_air;
1029
1030 v3f surface_avg;
1031 v3_zero( surface_avg );
1032
1033 if( len == 0 )
1034 {
1035 phys->lift_frames ++;
1036
1037 if( phys->lift_frames >= 8 )
1038 phys->in_air = 1;
1039 }
1040 else
1041 {
1042 for( int i=0; i<len; i++ )
1043 v3_add( surface_avg, manifold[i].n, surface_avg );
1044 v3_normalize( surface_avg );
1045
1046 if( v3_dot( phys->rb.v, surface_avg ) > 0.7f )
1047 {
1048 phys->lift_frames ++;
1049
1050 if( phys->lift_frames >= 8 )
1051 phys->in_air = 1;
1052 }
1053 else
1054 {
1055 phys->in_air = 0;
1056 phys->lift_frames = 0;
1057 v3f projected, axis;
1058
1059 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
1060 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
1061
1062 float d = v3_dot( phys->rb.forward, surface_avg );
1063 v3_muladds( surface_avg, phys->rb.forward, -d, projected );
1064 v3_normalize( projected );
1065
1066 float angle = v3_dot( phys->rb.up, projected );
1067 v3_cross( phys->rb.up, projected, axis );
1068
1069 v3f p0, p1;
1070 v3_add( phys->rb.co, projected, p0 );
1071 v3_add( phys->rb.co, phys->rb.up, p1 );
1072 vg_line( phys->rb.co, p0, 0xff00ff00 );
1073 vg_line( phys->rb.co, p1, 0xff000fff );
1074
1075 if( fabsf(angle) < 0.999f )
1076 {
1077 v4f correction;
1078 q_axis_angle( correction, axis,
1079 acosf(angle)*4.0f*VG_TIMESTEP_FIXED );
1080 q_mul( correction, phys->rb.q, phys->rb.q );
1081 }
1082 }
1083 }
1084
1085 if( !was_in_air && phys->in_air )
1086 player_start_air();
1087 }
1088
1089 VG_STATIC void player_collision_response( rb_ct *manifold, int len )
1090 {
1091 struct player_phys *phys = &player.phys;
1092
1093 for( int j=0; j<5; j++ )
1094 {
1095 for( int i=0; i<len; i++ )
1096 {
1097 struct contact *ct = &manifold[i];
1098
1099 v3f dv, delta;
1100 v3_sub( ct->co, phys->rb.co, delta );
1101 v3_cross( phys->rb.w, delta, dv );
1102 v3_add( phys->rb.v, dv, dv );
1103
1104 float vn = -v3_dot( dv, ct->n );
1105 vn += ct->bias;
1106
1107 float temp = ct->norm_impulse;
1108 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
1109 vn = ct->norm_impulse - temp;
1110
1111 v3f impulse;
1112 v3_muls( ct->n, vn, impulse );
1113
1114 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
1115 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
1116 {
1117 player_kill();
1118 return;
1119 }
1120
1121 v3_add( impulse, phys->rb.v, phys->rb.v );
1122 v3_cross( delta, impulse, impulse );
1123
1124 /*
1125 * W Impulses are limited to the Y and X axises, we don't really want
1126 * roll angular velocities being included.
1127 *
1128 * Can also tweak the resistance of each axis here by scaling the wx,wy
1129 * components.
1130 */
1131
1132 float wy = v3_dot( phys->rb.up, impulse ) * 0.8f,
1133 wx = v3_dot( phys->rb.right, impulse )*1.0f;
1134
1135 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
1136 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
1137 }
1138 }
1139 }
1140
1141 VG_STATIC void player_save_frame(void)
1142 {
1143 player.phys_gate_frame = player.phys;
1144 }
1145
1146 VG_STATIC void player_restore_frame(void)
1147 {
1148 player.phys = player.phys_gate_frame;
1149 rb_update_transform( &player.phys.rb );
1150 }
1151
1152 VG_STATIC void player_integrate(void)
1153 {
1154 struct player_phys *phys = &player.phys;
1155 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
1156 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
1157 }
1158
1159 VG_STATIC void player_do_motion(void)
1160 {
1161 struct player_phys *phys = &player.phys;
1162
1163 if( world.water.enabled )
1164 {
1165 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
1166 {
1167 audio_lock();
1168 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
1169 audio_player_set_position( &audio_player_extra, phys->rb.co );
1170 audio_player_set_vol( &audio_player_extra, 20.0f );
1171 audio_player_playclip( &audio_player_extra, &audio_splash );
1172 audio_unlock();
1173
1174 player_kill();
1175 }
1176 }
1177
1178 v3f prevco;
1179 v3_copy( phys->rb.co, prevco );
1180
1181 if( phys->on_board )
1182 {
1183 rb_ct manifold[72];
1184 int len = player_update_collision_manifold( manifold );
1185 int grind_col = player_update_grind_collision( &manifold[len] );
1186
1187 static int _grind_col_pre = 0;
1188
1189 if( grind_col )
1190 {
1191 phys->grind = 1;
1192 v3f up = { 0.0f, 1.0f, 0.0f };
1193 float angle = v3_dot( phys->rb.up, up );
1194
1195 if( fabsf(angle) < 0.99f )
1196 {
1197 v3f axis;
1198 v3_cross( phys->rb.up, up, axis );
1199
1200 v4f correction;
1201 q_axis_angle( correction, axis,
1202 VG_TIMESTEP_FIXED * 10.0f * acosf(angle) );
1203 q_mul( correction, phys->rb.q, phys->rb.q );
1204 }
1205
1206 float const DOWNFORCE = -k_downforce*1.2f*VG_TIMESTEP_FIXED;
1207 v3_muladds( phys->rb.v, manifold[len].n, DOWNFORCE, phys->rb.v );
1208 m3x3_identity( phys->vr );
1209 m3x3_identity( phys->vr_pstep );
1210
1211 if( !_grind_col_pre )
1212 {
1213 audio_lock();
1214 audio_player_set_flags( &audio_player_extra,
1215 AUDIO_FLAG_SPACIAL_3D );
1216 audio_player_set_position( &audio_player_extra, phys->rb.co );
1217 audio_player_set_vol( &audio_player_extra, 20.0f );
1218 audio_player_playclip( &audio_player_extra, &audio_board[5] );
1219 audio_unlock();
1220 }
1221 }
1222 else
1223 {
1224 phys->grind = 0;
1225 player_adhere_ground( manifold, len );
1226
1227 if( _grind_col_pre )
1228 {
1229 audio_lock();
1230 audio_player_set_flags( &audio_player_extra,
1231 AUDIO_FLAG_SPACIAL_3D );
1232 audio_player_set_position( &audio_player_extra, phys->rb.co );
1233 audio_player_set_vol( &audio_player_extra, 20.0f );
1234 audio_player_playclip( &audio_player_extra, &audio_board[6] );
1235 audio_unlock();
1236 }
1237 }
1238
1239 _grind_col_pre = grind_col;
1240
1241 rb_presolve_contacts( manifold, len+ VG_MAX(0,grind_col) );
1242 player_collision_response( manifold, len+ VG_MAX(0,grind_col) );
1243
1244 player_physics_control_passive();
1245
1246 if( grind_col )
1247 {
1248 phys->in_air = 0;
1249 player_physics_control_grind();
1250 }
1251 else
1252 {
1253 if( phys->in_air )
1254 player_physics_control_air();
1255 else
1256 player_physics_control();
1257 }
1258
1259 player_integrate();
1260 }
1261 else
1262 player_walk_physics();
1263
1264
1265 /* Real angular velocity integration */
1266 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f*0.5f, phys->rb.w );
1267 if( v3_length2( phys->rb.w ) > 0.0f )
1268 {
1269 v4f rotation;
1270 v3f axis;
1271 v3_copy( phys->rb.w, axis );
1272
1273 float mag = v3_length( axis );
1274 v3_divs( axis, mag, axis );
1275 q_axis_angle( rotation, axis, mag*k_rb_delta );
1276 q_mul( rotation, phys->rb.q, phys->rb.q );
1277 }
1278
1279 /* Faux angular velocity */
1280 v4f rotate;
1281
1282 float lerpq = phys->in_air? 0.04f: 0.3f;
1283 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
1284
1285 q_axis_angle( rotate, phys->rb.up, phys->siY );
1286 q_mul( rotate, phys->rb.q, phys->rb.q );
1287 phys->iY = 0.0f;
1288
1289 /*
1290 * Gate intersection, by tracing a line over the gate planes
1291 */
1292 for( int i=0; i<world.gate_count; i++ )
1293 {
1294 struct route_gate *rg = &world.gates[i];
1295 teleport_gate *gate = &rg->gate;
1296
1297 if( gate_intersect( gate, phys->rb.co, prevco ) )
1298 {
1299 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
1300 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
1301 m3x3_mulv( gate->transport, phys->vl, phys->vl );
1302 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
1303 m3x3_mulv( gate->transport, phys->m, phys->m );
1304 m3x3_mulv( gate->transport, phys->bob, phys->bob );
1305
1306 /* Pre-emptively edit the camera matrices so that the motion vectors
1307 * are correct */
1308 m4x3f transport_i;
1309 m4x4f transport_4;
1310 m4x3_invert_affine( gate->transport, transport_i );
1311 m4x3_expand( transport_i, transport_4 );
1312 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
1313 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
1314
1315 v4f transport_rotation;
1316 m3x3_q( gate->transport, transport_rotation );
1317 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
1318
1319 world_routes_activate_gate( i );
1320
1321 if( !phys->on_board )
1322 {
1323 v3f fwd_dir = {cosf(player.angles[0]),
1324 0.0f,
1325 sinf(player.angles[0])};
1326 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
1327
1328 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
1329 }
1330
1331 player.rewind_length = 0;
1332 player.rewind_total_length = 0.0f;
1333 player.rewind_incrementer = 10000;
1334 player_save_frame();
1335
1336 audio_lock();
1337 audio_play_oneshot( &audio_gate_pass, 1.0f );
1338 audio_unlock();
1339 break;
1340 }
1341 }
1342
1343 rb_update_transform( &phys->rb );
1344 }
1345
1346 VG_STATIC void player_freecam(void)
1347 {
1348 player_mouseview();
1349
1350 float movespeed = fc_speed * VG_TIMESTEP_FIXED;
1351 v3f lookdir = { 0.0f, 0.0f, -1.0f },
1352 sidedir = { 1.0f, 0.0f, 0.0f };
1353
1354 m3x3_mulv( main_camera.transform, lookdir, lookdir );
1355 m3x3_mulv( main_camera.transform, sidedir, sidedir );
1356
1357 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
1358
1359 v2f steer = { player.input_js1h->axis.value,
1360 player.input_js1v->axis.value };
1361
1362 v3_muladds( move_vel, sidedir, movespeed*steer[0], move_vel );
1363 v3_muladds( move_vel, lookdir, -movespeed*steer[1], move_vel );
1364
1365 v3_muls( move_vel, 0.7f, move_vel );
1366 v3_add( move_vel, player.camera_pos, player.camera_pos );
1367 }
1368
1369 VG_STATIC int kill_player( int argc, char const *argv[] )
1370 {
1371 player_kill();
1372 return 0;
1373 }
1374
1375 VG_STATIC int reset_player( int argc, char const *argv[] )
1376 {
1377 struct player_phys *phys = &player.phys;
1378 struct respawn_point *rp = NULL, *r;
1379
1380 if( argc == 1 )
1381 {
1382 for( int i=0; i<world.spawn_count; i++ )
1383 {
1384 r = &world.spawns[i];
1385 if( !strcmp( r->name, argv[0] ) )
1386 {
1387 rp = r;
1388 break;
1389 }
1390 }
1391
1392 if( !rp )
1393 vg_warn( "No spawn named '%s'\n", argv[0] );
1394 }
1395
1396 if( !rp )
1397 {
1398 float min_dist = INFINITY;
1399
1400 for( int i=0; i<world.spawn_count; i++ )
1401 {
1402 r = &world.spawns[i];
1403 float d = v3_dist2( r->co, phys->rb.co );
1404
1405 vg_info( "Dist %s : %f\n", r->name, d );
1406 if( d < min_dist )
1407 {
1408 min_dist = d;
1409 rp = r;
1410 }
1411 }
1412 }
1413
1414 if( !rp )
1415 {
1416 vg_error( "No spawn found\n" );
1417 vg_info( "Player position: %f %f %f\n", player.phys.rb.co[0],
1418 player.phys.rb.co[1],
1419 player.phys.rb.co[2] );
1420 vg_info( "Player velocity: %f %f %f\n", player.phys.rb.v[0],
1421 player.phys.rb.v[1],
1422 player.phys.rb.v[2] );
1423
1424 if( !world.spawn_count )
1425 return 0;
1426
1427 rp = &world.spawns[0];
1428 }
1429
1430 player.is_dead = 0;
1431
1432 m3x3f the_long_way;
1433 q_m3x3( rp->q, the_long_way );
1434
1435 v3f delta = {1.0f,0.0f,0.0f};
1436 m3x3_mulv( the_long_way, delta, delta );
1437
1438 if( !freecam )
1439 {
1440 player.angles[0] = atan2f( delta[0], -delta[2] );
1441 player.angles[1] = -asinf( delta[1] );
1442 }
1443
1444 v4_copy( rp->q, phys->rb.q );
1445 v3_copy( rp->co, phys->rb.co );
1446 v3_zero( phys->rb.v );
1447
1448 phys->vswitch = 1.0f;
1449 phys->slip_last = 0.0f;
1450 phys->in_air = 1;
1451 phys->on_board = 0;
1452 m3x3_identity( phys->vr );
1453
1454 player.mdl.shoes[0] = 1;
1455 player.mdl.shoes[1] = 1;
1456
1457 rb_update_transform( &phys->rb );
1458 player_save_frame();
1459 return 1;
1460 }
1461
1462 #endif /* PLAYER_PHYSICS_H */