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