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