b3cd9d5832699d675a617c2e3e679212a3bf3f1a
[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 if( !phys->in_air )
357 {
358 float const DOWNFORCE = -k_walk_downforce*VG_TIMESTEP_FIXED;
359 v3_muladds( phys->rb.v, (v3f){0.0f,-1.0f,0.0f}, DOWNFORCE, phys->rb.v );
360
361 if( vg_get_button("jump") )
362 {
363 phys->rb.v[1] = 5.0f;
364 }
365 }
366
367 v3_zero( phys->rb.w );
368 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
369
370 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
371
372 v3f p1;
373 v3_muladds( phys->rb.co, forward_dir, 2.0f, p1 );
374 vg_line( phys->rb.co, p1, 0xff0000ff );
375
376 float move_dead = 0.1f,
377 move = vg_get_axis("grabl")*0.5f + 0.5f - move_dead;
378
379 if( move > 0.0f )
380 {
381 float move_norm = move * (1.0f/(1.0f-move_dead)),
382 speed = vg_lerpf( 0.1f*k_runspeed, k_runspeed, move_norm ),
383 amt = k_walk_accel * VG_TIMESTEP_FIXED,
384 zvel = v3_dot( phys->rb.v, forward_dir ),
385 new_vel = vg_minf( zvel + amt, speed ),
386 diff = new_vel - vg_minf( zvel, speed );
387
388 if( !phys->in_air )
389 {
390 v3_muladds( phys->rb.v, forward_dir, diff, phys->rb.v );
391 }
392
393 /* TODO move */
394 float walk_norm = 30.0f/(float)player.mdl.anim_walk->length,
395 run_norm = 30.0f/(float)player.mdl.anim_run->length,
396 walk_adv = vg_lerpf( walk_norm,run_norm,move_norm );
397
398 player.walk_timer += walk_adv * VG_TIMESTEP_FIXED;
399 }
400 else
401 {
402 player.walk_timer = 0.0f;
403 }
404
405 if( !phys->in_air )
406 {
407 phys->rb.v[0] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction);
408 phys->rb.v[2] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction);
409 }
410 }
411
412 /*
413 * Physics collision detection, and control
414 */
415 static void player_physics(void)
416 {
417 struct player_phys *phys = &player.phys;
418 /*
419 * Update collision fronts
420 */
421
422 rigidbody *rbf = &player.collide_front,
423 *rbb = &player.collide_back;
424
425 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
426 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
427
428 player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
429 float h = player.air_blend*0.2f;
430
431 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
432 v3_copy( rbf->co, rbf->to_world[3] );
433 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
434 v3_copy( rbb->co, rbb->to_world[3] );
435
436 m4x3_invert_affine( rbf->to_world, rbf->to_local );
437 m4x3_invert_affine( rbb->to_world, rbb->to_local );
438
439 rb_update_bounds( rbf );
440 rb_update_bounds( rbb );
441
442 rb_debug( rbf, 0xff00ffff );
443 rb_debug( rbb, 0xffffff00 );
444
445 rb_ct manifold[64];
446 int len = 0;
447
448 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
449 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
450
451 rb_presolve_contacts( manifold, len );
452 v3f surface_avg = {0.0f, 0.0f, 0.0f};
453
454 if( !len )
455 {
456 player_start_air();
457 }
458 else
459 {
460 for( int i=0; i<len; i++ )
461 {
462 v3_add( manifold[i].n, surface_avg, surface_avg );
463 }
464
465 v3_normalize( surface_avg );
466
467 if( v3_dot( phys->rb.v, surface_avg ) > 0.5f )
468 {
469 player_start_air();
470 }
471 else
472 phys->in_air = 0;
473 }
474
475 for( int j=0; j<5; j++ )
476 {
477 for( int i=0; i<len; i++ )
478 {
479 struct contact *ct = &manifold[i];
480
481 v3f dv, delta;
482 v3_sub( ct->co, phys->rb.co, delta );
483 v3_cross( phys->rb.w, delta, dv );
484 v3_add( phys->rb.v, dv, dv );
485
486 float vn = -v3_dot( dv, ct->n );
487 vn += ct->bias;
488
489 float temp = ct->norm_impulse;
490 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
491 vn = ct->norm_impulse - temp;
492
493 v3f impulse;
494 v3_muls( ct->n, vn, impulse );
495
496 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
497 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
498 {
499 player_kill();
500 return;
501 }
502
503 v3_add( impulse, phys->rb.v, phys->rb.v );
504 v3_cross( delta, impulse, impulse );
505
506 /*
507 * W Impulses are limited to the Y and X axises, we don't really want
508 * roll angular velocities being included.
509 *
510 * Can also tweak the resistance of each axis here by scaling the wx,wy
511 * components.
512 */
513
514 float wy = v3_dot( phys->rb.up, impulse ),
515 wx = v3_dot( phys->rb.right, impulse )*1.5f;
516
517 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
518 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
519 }
520 }
521
522 float grabt = vg_get_axis( "grabr" )*0.5f+0.5f;
523 phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
524 player.phys.pushing = 0.0f;
525
526 if( !phys->in_air )
527 {
528 v3f axis;
529 float angle = v3_dot( phys->rb.up, surface_avg );
530 v3_cross( phys->rb.up, surface_avg, axis );
531
532 //float cz = v3_dot( player.rb.forward, axis );
533 //v3_muls( player.rb.forward, cz, axis );
534
535 if( angle < 0.999f )
536 {
537 v4f correction;
538 q_axis_angle( correction, axis, acosf(angle)*0.3f );
539 q_mul( correction, phys->rb.q, phys->rb.q );
540 }
541
542 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
543 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
544
545 player_physics_control();
546
547 if( !phys->jump_charge && phys->jump > 0.2f )
548 {
549 v3f jumpdir;
550
551 /* Launch more up if alignment is up else improve velocity */
552 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
553 mod = 0.5f,
554 dir = mod + aup*(1.0f-mod);
555
556 v3_copy( phys->rb.v, jumpdir );
557 v3_normalize( jumpdir );
558 v3_muls( jumpdir, 1.0f-dir, jumpdir );
559 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
560 v3_normalize( jumpdir );
561
562 float force = k_jump_force*phys->jump;
563 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
564 phys->jump = 0.0f;
565
566 player.jump_time = vg.time;
567
568 /* TODO: Move to audio file */
569 audio_lock();
570 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
571 audio_player_set_position( &audio_player_extra, phys->rb.co );
572 audio_player_set_vol( &audio_player_extra, 20.0f );
573 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%4] );
574 audio_unlock();
575 }
576 }
577 else
578 {
579 player_physics_control_air();
580 }
581
582 if( !phys->jump_charge )
583 {
584 phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
585 }
586
587 phys->jump_charge = 0;
588 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
589 }
590
591 static void player_save_frame(void)
592 {
593 player.phys_gate_frame = player.phys;
594 }
595
596 static void player_restore_frame(void)
597 {
598 player.phys = player.phys_gate_frame;
599 rb_update_transform( &player.phys.rb );
600 }
601
602 static void player_do_motion(void)
603 {
604 struct player_phys *phys = &player.phys;
605
606 float horizontal = vg_get_axis("horizontal"),
607 vertical = vg_get_axis("vertical");
608
609 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
610 {
611 audio_lock();
612 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
613 audio_player_set_position( &audio_player_extra, phys->rb.co );
614 audio_player_set_vol( &audio_player_extra, 20.0f );
615 audio_player_playclip( &audio_player_extra, &audio_splash );
616 audio_unlock();
617
618 player_kill();
619 }
620
621 if( phys->on_board )
622 player_physics();
623 else
624 player_walk_physics();
625
626 /* Integrate velocity */
627 v3f prevco;
628 v3_copy( phys->rb.co, prevco );
629
630 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
631 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
632
633 /* Real angular velocity integration */
634 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, phys->rb.w );
635 if( v3_length2( phys->rb.w ) > 0.0f )
636 {
637 v4f rotation;
638 v3f axis;
639 v3_copy( phys->rb.w, axis );
640
641 float mag = v3_length( axis );
642 v3_divs( axis, mag, axis );
643 q_axis_angle( rotation, axis, mag*k_rb_delta );
644 q_mul( rotation, phys->rb.q, phys->rb.q );
645 }
646
647 /* Faux angular velocity */
648 v4f rotate;
649
650 float lerpq = phys->in_air? 0.04f: 0.3f;
651 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
652
653 q_axis_angle( rotate, phys->rb.up, phys->siY );
654 q_mul( rotate, phys->rb.q, phys->rb.q );
655 phys->iY = 0.0f;
656
657 /*
658 * Gate intersection, by tracing a line over the gate planes
659 */
660 for( int i=0; i<world.routes.gate_count; i++ )
661 {
662 struct route_gate *rg = &world.routes.gates[i];
663 teleport_gate *gate = &rg->gate;
664
665 if( gate_intersect( gate, phys->rb.co, prevco ) )
666 {
667 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
668 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
669 m3x3_mulv( gate->transport, phys->vl, phys->vl );
670 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
671 m3x3_mulv( gate->transport, phys->m, phys->m );
672 m3x3_mulv( gate->transport, phys->bob, phys->bob );
673
674 v4f transport_rotation;
675 m3x3_q( gate->transport, transport_rotation );
676 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
677
678 world_routes_activate_gate( i );
679
680 if( !phys->on_board )
681 {
682 v3f fwd_dir = {cosf(player.angles[0]),
683 0.0f,
684 sinf(player.angles[0])};
685 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
686
687 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
688 }
689
690 player_save_frame();
691
692 audio_lock();
693 audio_play_oneshot( &audio_gate_pass, 1.0f );
694 audio_unlock();
695 break;
696 }
697 }
698
699 rb_update_transform( &phys->rb );
700 }
701
702 /*
703 * Free camera movement
704 */
705 static void player_mouseview(void)
706 {
707 if( gui_want_mouse() )
708 return;
709
710 static v2f mouse_last,
711 view_vel = { 0.0f, 0.0f };
712
713 if( vg_get_button_down( "primary" ) )
714 v2_copy( vg.mouse, mouse_last );
715
716 else if( vg_get_button( "primary" ) )
717 {
718 v2f delta;
719 v2_sub( vg.mouse, mouse_last, delta );
720 v2_copy( vg.mouse, mouse_last );
721
722 v2_muladds( view_vel, delta, 0.06f*vg.time_delta, view_vel );
723 }
724
725 v2_muladds( view_vel, (v2f){ vg_get_axis("h1"), vg_get_axis("v1") },
726 3.0f * vg.time_delta, view_vel );
727
728 v2_muls( view_vel, 1.0f-4.2f*vg.time_delta, view_vel );
729 v2_add( view_vel, player.angles, player.angles );
730 player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
731 }
732
733 static void player_freecam(void)
734 {
735 player_mouseview();
736
737 float movespeed = fc_speed;
738 v3f lookdir = { 0.0f, 0.0f, -1.0f },
739 sidedir = { 1.0f, 0.0f, 0.0f };
740
741 m3x3_mulv( player.camera, lookdir, lookdir );
742 m3x3_mulv( player.camera, sidedir, sidedir );
743
744 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
745 if( vg_get_button( "forward" ) )
746 v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED * movespeed, move_vel );
747 if( vg_get_button( "back" ) )
748 v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED *-movespeed, move_vel );
749 if( vg_get_button( "left" ) )
750 v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED *-movespeed, move_vel );
751 if( vg_get_button( "right" ) )
752 v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED * movespeed, move_vel );
753
754 v3_muls( move_vel, 0.7f, move_vel );
755 v3_add( move_vel, player.camera_pos, player.camera_pos );
756 }
757
758 static void player_camera_update(void)
759 {
760 /* Update camera matrices */
761 v4f qyaw, qpitch, qcam;
762 q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -player.angles[0] );
763 q_axis_angle( qpitch, (v3f){ 1.0f, 0.0f, 0.0f }, -player.angles[1] );
764
765 q_mul( qyaw, qpitch, qcam );
766 q_m3x3( qcam, player.camera );
767
768 v3_copy( player.camera_pos, player.camera[3] );
769 m4x3_invert_affine( player.camera, player.camera_inverse );
770 }
771
772 static int reset_player( int argc, char const *argv[] )
773 {
774 struct player_phys *phys = &player.phys;
775 struct respawn_point *rp = NULL, *r;
776
777 if( argc == 1 )
778 {
779 for( int i=0; i<world.spawn_count; i++ )
780 {
781 r = &world.spawns[i];
782 if( !strcmp( r->name, argv[0] ) )
783 {
784 rp = r;
785 break;
786 }
787 }
788
789 if( !rp )
790 vg_warn( "No spawn named '%s'\n", argv[0] );
791 }
792
793 if( !rp )
794 {
795 float min_dist = INFINITY;
796
797 for( int i=0; i<world.spawn_count; i++ )
798 {
799 r = &world.spawns[i];
800 float d = v3_dist2( r->co, phys->rb.co );
801
802 vg_info( "Dist %s : %f\n", r->name, d );
803 if( d < min_dist )
804 {
805 min_dist = d;
806 rp = r;
807 }
808 }
809 }
810
811 if( !rp )
812 {
813 vg_error( "No spawn found\n" );
814 if( !world.spawn_count )
815 return 0;
816
817 rp = &world.spawns[0];
818 }
819
820 player.is_dead = 0;
821
822 v4_copy( rp->q, phys->rb.q );
823 v3_copy( rp->co, phys->rb.co );
824 v3_zero( phys->rb.v );
825
826 phys->vswitch = 1.0f;
827 phys->slip_last = 0.0f;
828 phys->in_air = 1;
829 phys->on_board = 0;
830 m3x3_identity( phys->vr );
831
832 player.mdl.shoes[0] = 1;
833 player.mdl.shoes[1] = 1;
834
835 rb_update_transform( &phys->rb );
836 player_save_frame();
837 return 1;
838 }
839
840 #endif /* PLAYER_PHYSICS_H */