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