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