rewinds
[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 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 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 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 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 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 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 phys->in_air = 0;
469 }
470
471 for( int j=0; j<5; j++ )
472 {
473 for( int i=0; i<len; i++ )
474 {
475 struct contact *ct = &manifold[i];
476
477 v3f dv, delta;
478 v3_sub( ct->co, phys->rb.co, delta );
479 v3_cross( phys->rb.w, delta, dv );
480 v3_add( phys->rb.v, dv, dv );
481
482 float vn = -v3_dot( dv, ct->n );
483 vn += ct->bias;
484
485 float temp = ct->norm_impulse;
486 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
487 vn = ct->norm_impulse - temp;
488
489 v3f impulse;
490 v3_muls( ct->n, vn, impulse );
491
492 if( fabsf(v3_dot( impulse, phys->rb.forward )) > 10.0f ||
493 fabsf(v3_dot( impulse, phys->rb.up )) > 50.0f )
494 {
495 player_kill();
496 return;
497 }
498
499 v3_add( impulse, phys->rb.v, phys->rb.v );
500 v3_cross( delta, impulse, impulse );
501
502 /*
503 * W Impulses are limited to the Y and X axises, we don't really want
504 * roll angular velocities being included.
505 *
506 * Can also tweak the resistance of each axis here by scaling the wx,wy
507 * components.
508 */
509
510 float wy = v3_dot( phys->rb.up, impulse ),
511 wx = v3_dot( phys->rb.right, impulse )*1.5f;
512
513 v3_muladds( phys->rb.w, phys->rb.up, wy, phys->rb.w );
514 v3_muladds( phys->rb.w, phys->rb.right, wx, phys->rb.w );
515 }
516 }
517
518 float grabt = vg_get_axis( "grab" )*0.5f+0.5f;
519 phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
520 player.phys.pushing = 0.0f;
521
522 if( !phys->in_air )
523 {
524 v3f axis;
525 float angle = v3_dot( phys->rb.up, surface_avg );
526 v3_cross( phys->rb.up, surface_avg, axis );
527
528 //float cz = v3_dot( player.rb.forward, axis );
529 //v3_muls( player.rb.forward, cz, axis );
530
531 if( angle < 0.999f )
532 {
533 v4f correction;
534 q_axis_angle( correction, axis, acosf(angle)*18.0f*VG_TIMESTEP_FIXED );
535 q_mul( correction, phys->rb.q, phys->rb.q );
536 }
537
538 float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
539 v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v );
540
541 player_physics_control();
542
543 if( !phys->jump_charge && phys->jump > 0.2f )
544 {
545 v3f jumpdir;
546
547 /* Launch more up if alignment is up else improve velocity */
548 float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
549 mod = 0.5f,
550 dir = mod + aup*(1.0f-mod);
551
552 v3_copy( phys->rb.v, jumpdir );
553 v3_normalize( jumpdir );
554 v3_muls( jumpdir, 1.0f-dir, jumpdir );
555 v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
556 v3_normalize( jumpdir );
557
558 float force = k_jump_force*phys->jump;
559 v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
560 phys->jump = 0.0f;
561
562 player.jump_time = vg.time;
563
564 /* TODO: Move to audio file */
565 audio_lock();
566 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
567 audio_player_set_position( &audio_player_extra, phys->rb.co );
568 audio_player_set_vol( &audio_player_extra, 20.0f );
569 audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%4] );
570 audio_unlock();
571 }
572 }
573 else
574 {
575 player_physics_control_air();
576 }
577
578 if( !phys->jump_charge )
579 {
580 phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
581 }
582
583 phys->jump_charge = 0;
584 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
585 }
586
587 static void player_save_frame(void)
588 {
589 player.phys_gate_frame = player.phys;
590 }
591
592 static void player_restore_frame(void)
593 {
594 player.phys = player.phys_gate_frame;
595 rb_update_transform( &player.phys.rb );
596 }
597
598 static void player_do_motion(void)
599 {
600 struct player_phys *phys = &player.phys;
601
602 float horizontal = vg_get_axis("horizontal"),
603 vertical = vg_get_axis("vertical");
604
605 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
606 {
607 audio_lock();
608 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
609 audio_player_set_position( &audio_player_extra, phys->rb.co );
610 audio_player_set_vol( &audio_player_extra, 20.0f );
611 audio_player_playclip( &audio_player_extra, &audio_splash );
612 audio_unlock();
613
614 player_kill();
615 }
616
617 if( phys->on_board )
618 player_physics();
619 else
620 player_walk_physics();
621
622 /* Integrate velocity */
623 v3f prevco;
624 v3_copy( phys->rb.co, prevco );
625
626 apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED );
627 v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co );
628
629 /* Real angular velocity integration */
630 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, phys->rb.w );
631 if( v3_length2( phys->rb.w ) > 0.0f )
632 {
633 v4f rotation;
634 v3f axis;
635 v3_copy( phys->rb.w, axis );
636
637 float mag = v3_length( axis );
638 v3_divs( axis, mag, axis );
639 q_axis_angle( rotation, axis, mag*k_rb_delta );
640 q_mul( rotation, phys->rb.q, phys->rb.q );
641 }
642
643 /* Faux angular velocity */
644 v4f rotate;
645
646 float lerpq = phys->in_air? 0.04f: 0.3f;
647 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
648
649 q_axis_angle( rotate, phys->rb.up, phys->siY );
650 q_mul( rotate, phys->rb.q, phys->rb.q );
651 phys->iY = 0.0f;
652
653 /*
654 * Gate intersection, by tracing a line over the gate planes
655 */
656 for( int i=0; i<world.routes.gate_count; i++ )
657 {
658 struct route_gate *rg = &world.routes.gates[i];
659 teleport_gate *gate = &rg->gate;
660
661 if( gate_intersect( gate, phys->rb.co, prevco ) )
662 {
663 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
664 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
665 m3x3_mulv( gate->transport, phys->vl, phys->vl );
666 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
667 m3x3_mulv( gate->transport, phys->m, phys->m );
668 m3x3_mulv( gate->transport, phys->bob, phys->bob );
669
670 v4f transport_rotation;
671 m3x3_q( gate->transport, transport_rotation );
672 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
673
674 world_routes_activate_gate( i );
675
676 if( !phys->on_board )
677 {
678 v3f fwd_dir = {cosf(player.angles[0]),
679 0.0f,
680 sinf(player.angles[0])};
681 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
682
683 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
684 }
685
686 player.rewind_length = 0;
687 player.rewind_incrementer = 10000;
688 player_save_frame();
689
690 audio_lock();
691 audio_play_oneshot( &audio_gate_pass, 1.0f );
692 audio_unlock();
693 break;
694 }
695 }
696
697 rb_update_transform( &phys->rb );
698 }
699
700 /*
701 * Free camera movement
702 */
703 static void player_mouseview(void)
704 {
705 if( gui_want_mouse() )
706 return;
707
708 static v2f mouse_last,
709 view_vel = { 0.0f, 0.0f };
710
711 if( vg_get_button_down( "primary" ) )
712 v2_copy( vg.mouse, mouse_last );
713
714 else if( vg_get_button( "primary" ) )
715 {
716 v2f delta;
717 v2_sub( vg.mouse, mouse_last, delta );
718 v2_copy( vg.mouse, mouse_last );
719
720 v2_muladds( view_vel, delta, 0.06f*vg.time_delta, view_vel );
721 }
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( camera_mtx, lookdir, lookdir );
737 m3x3_mulv( camera_mtx, 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 int reset_player( int argc, char const *argv[] )
754 {
755 struct player_phys *phys = &player.phys;
756 struct respawn_point *rp = NULL, *r;
757
758 if( argc == 1 )
759 {
760 for( int i=0; i<world.spawn_count; i++ )
761 {
762 r = &world.spawns[i];
763 if( !strcmp( r->name, argv[0] ) )
764 {
765 rp = r;
766 break;
767 }
768 }
769
770 if( !rp )
771 vg_warn( "No spawn named '%s'\n", argv[0] );
772 }
773
774 if( !rp )
775 {
776 float min_dist = INFINITY;
777
778 for( int i=0; i<world.spawn_count; i++ )
779 {
780 r = &world.spawns[i];
781 float d = v3_dist2( r->co, phys->rb.co );
782
783 vg_info( "Dist %s : %f\n", r->name, d );
784 if( d < min_dist )
785 {
786 min_dist = d;
787 rp = r;
788 }
789 }
790 }
791
792 if( !rp )
793 {
794 vg_error( "No spawn found\n" );
795 if( !world.spawn_count )
796 return 0;
797
798 rp = &world.spawns[0];
799 }
800
801 player.is_dead = 0;
802
803 v4_copy( rp->q, phys->rb.q );
804 v3_copy( rp->co, phys->rb.co );
805 v3_zero( phys->rb.v );
806
807 phys->vswitch = 1.0f;
808 phys->slip_last = 0.0f;
809 phys->in_air = 1;
810 phys->on_board = 0;
811 m3x3_identity( phys->vr );
812
813 player.mdl.shoes[0] = 1;
814 player.mdl.shoes[1] = 1;
815
816 rb_update_transform( &phys->rb );
817 player_save_frame();
818 return 1;
819 }
820
821 #endif /* PLAYER_PHYSICS_H */