Fix major overstep with last commit
[carveJwlIkooP6JGAAIwe30JlM.git] / player_physics.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
4 *
5 * Player physics and control submodule
6 * contains main physics models, input, and player control.
7 *
8 * -----------------------------------------------------------------------------
9 */
10
11 #ifndef PLAYER_PHYSICS_H
12 #define PLAYER_PHYSICS_H
13
14 #include "player.h"
15
16 static void apply_gravity( v3f vel, float const timestep )
17 {
18 v3f gravity = { 0.0f, -9.6f, 0.0f };
19 v3_muladds( vel, gravity, timestep, vel );
20 }
21
22 /*
23 * Called when launching into the air to predict and adjust trajectories
24 */
25 static void player_start_air(void)
26 {
27 struct player_phys *phys = &player.phys;
28
29 if( phys->in_air )
30 return;
31
32 phys->in_air = 1;
33
34 float pstep = ktimestep*10.0f;
35 float best_velocity_delta = -9999.9f;
36 float k_bias = 0.96f;
37
38 v3f axis;
39 v3_cross( phys->rb.up, phys->rb.v, axis );
40 v3_normalize( axis );
41 player.land_log_count = 0;
42
43 m3x3_identity( phys->vr );
44
45 for( int m=-3;m<=12; m++ )
46 {
47 float vmod = ((float)m / 15.0f)*0.09f;
48
49 v3f pco, pco1, pv;
50 v3_copy( phys->rb.co, pco );
51 v3_muls( phys->rb.v, k_bias, pv );
52
53 /*
54 * Try different 'rotations' of the velocity to find the best possible
55 * landing normal. This conserves magnitude at the expense of slightly
56 * unrealistic results
57 */
58
59 m3x3f vr;
60 v4f vr_q;
61
62 q_axis_angle( vr_q, axis, vmod );
63 q_m3x3( vr_q, vr );
64
65 m3x3_mulv( vr, pv, pv );
66 v3_muladds( pco, pv, pstep, pco );
67
68 for( int i=0; i<50; i++ )
69 {
70 v3_copy( pco, pco1 );
71 apply_gravity( pv, pstep );
72
73 m3x3_mulv( vr, pv, pv );
74 v3_muladds( pco, pv, pstep, pco );
75
76 ray_hit contact;
77 v3f vdir;
78
79 v3_sub( pco, pco1, vdir );
80 contact.dist = v3_length( vdir );
81 v3_divs( vdir, contact.dist, vdir);
82
83 if( ray_world( pco1, vdir, &contact ))
84 {
85 float land_delta = v3_dot( pv, contact.normal );
86 u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
87
88 /* Bias prediction towords ramps */
89 if( ray_hit_is_ramp( &contact ) )
90 {
91 land_delta *= 0.1f;
92 scolour |= 0x0000a000;
93 }
94
95 if( (land_delta < 0.0f) && (land_delta > best_velocity_delta) )
96 {
97 best_velocity_delta = land_delta;
98
99 v3_copy( contact.pos, player.land_target );
100
101 m3x3_copy( vr, phys->vr_pstep );
102 q_axis_angle( vr_q, axis, vmod*0.1f );
103 q_m3x3( vr_q, phys->vr );
104 }
105
106 v3_copy( contact.pos,
107 player.land_target_log[player.land_log_count] );
108 player.land_target_colours[player.land_log_count] =
109 0xff000000 | scolour;
110
111 player.land_log_count ++;
112
113 break;
114 }
115 }
116 }
117 }
118
119 /*
120 * Main friction interface model
121 */
122 static void player_physics_control(void)
123 {
124 struct player_phys *phys = &player.phys;
125
126 /*
127 * Computing localized friction forces for controlling the character
128 * Friction across X is significantly more than Z
129 */
130
131 v3f vel;
132 m3x3_mulv( phys->rb.to_local, phys->rb.v, vel );
133 float slip = 0.0f;
134
135 if( fabsf(vel[2]) > 0.01f )
136 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
137
138 if( fabsf( slip ) > 1.2f )
139 slip = vg_signf( slip ) * 1.2f;
140 phys->slip = slip;
141 phys->reverse = -vg_signf(vel[2]);
142
143 float substep = ktimestep * 0.2f;
144 float fwd_resistance = (vg_get_button( "break" )? 5.0f: 0.02f) * -substep;
145
146 for( int i=0; i<5; i++ )
147 {
148 vel[2] = stable_force( vel[2], vg_signf( vel[2] ) * fwd_resistance );
149 vel[0] = stable_force( vel[0],
150 vg_signf( vel[0] ) * -k_friction_lat*substep );
151 }
152
153 static double start_push = 0.0;
154 if( vg_get_button_down( "push" ) )
155 start_push = vg_time;
156
157 if( vg_get_button( "jump" ) )
158 {
159 phys->jump += ktimestep * k_jump_charge_speed;
160
161 if( !phys->jump_charge )
162 phys->jump_dir = phys->reverse > 0.0f? 1: 0;
163
164 phys->jump_charge = 1;
165 }
166
167 if( !vg_get_button("break") && vg_get_button( "push" ) )
168 {
169 player.pushing = 1.0f;
170 player.push_time = vg_time-start_push;
171
172 float cycle_time = player.push_time*k_push_cycle_rate,
173 amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*ktimestep,
174 current = v3_length( vel ),
175 new_vel = vg_minf( current + amt, k_max_push_speed );
176
177 new_vel -= vg_minf(current, k_max_push_speed);
178 vel[2] -= new_vel * phys->reverse;
179 }
180
181 /* Pumping */
182 static float previous = 0.0f;
183 float delta = previous - phys->grab,
184 pump = delta * k_pump_force*ktimestep;
185 previous = phys->grab;
186
187 v3f p1;
188 v3_muladds( phys->rb.co, phys->rb.up, pump, p1 );
189 vg_line( phys->rb.co, p1, 0xff0000ff );
190
191 vel[1] += pump;
192
193
194 m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
195
196 float steer = vg_get_axis( "horizontal" );
197 phys->iY -= vg_signf(steer)*powf(steer,2.0f) * k_steer_ground * ktimestep;
198 }
199
200 /*
201 * Air control, no real physics
202 */
203 static void player_physics_control_air(void)
204 {
205 struct player_phys *phys = &player.phys;
206
207 m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
208 vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
209
210 ray_hit hit;
211
212 /*
213 * Prediction
214 */
215 float pstep = ktimestep*10.0f;
216
217 v3f pco, pco1, pv;
218 v3_copy( phys->rb.co, pco );
219 v3_copy( phys->rb.v, pv );
220
221 float time_to_impact = 0.0f;
222 float limiter = 1.0f;
223
224 for( int i=0; i<50; i++ )
225 {
226 v3_copy( pco, pco1 );
227 m3x3_mulv( phys->vr_pstep, pv, pv );
228 apply_gravity( pv, pstep );
229 v3_muladds( pco, pv, pstep, pco );
230
231 ray_hit contact;
232 v3f vdir;
233
234 v3_sub( pco, pco1, vdir );
235 contact.dist = v3_length( vdir );
236 v3_divs( vdir, contact.dist, vdir);
237
238 float orig_dist = contact.dist;
239 if( ray_world( pco1, vdir, &contact ))
240 {
241 float angle = v3_dot( phys->rb.up, contact.normal );
242 v3f axis;
243 v3_cross( phys->rb.up, contact.normal, axis );
244
245 time_to_impact += (contact.dist/orig_dist)*pstep;
246 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
247 limiter = 1.0f-limiter;
248 limiter *= limiter;
249 limiter = 1.0f-limiter;
250
251 if( angle < 0.99f )
252 {
253 v4f correction;
254 q_axis_angle( correction, axis, acosf(angle)*0.05f*(1.0f-limiter) );
255 q_mul( correction, phys->rb.q, phys->rb.q );
256 }
257
258 vg_line_cross( contact.pos, 0xffff0000, 0.25f );
259 break;
260 }
261 time_to_impact += pstep;
262 }
263
264 phys->iY -= vg_get_axis( "horizontal" ) * k_steer_air * ktimestep;
265 {
266 float iX = vg_get_axis( "vertical" ) *
267 phys->reverse * k_steer_air * limiter * ktimestep;
268
269 static float siX = 0.0f;
270 siX = vg_lerpf( siX, iX, k_steer_air_lerp );
271
272 v4f rotate;
273 q_axis_angle( rotate, phys->rb.right, siX );
274 q_mul( rotate, phys->rb.q, phys->rb.q );
275 }
276
277 v2f target = {0.0f,0.0f};
278 v2_muladds( target, (v2f){ vg_get_axis("h1"), vg_get_axis("v1") },
279 phys->grab, target );
280 }
281
282 /*
283 * Entire Walking physics model
284 * TODO: sleep when under certain velotiy
285 */
286 static void player_walk_physics(void)
287 {
288 struct player_phys *phys = &player.phys;
289 rigidbody *rbf = &player.collide_front,
290 *rbb = &player.collide_back;
291
292 m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
293 m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
294
295 float h0 = 0.3f,
296 h1 = 0.9f;
297
298 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h0,0.0f}, rbf->co );
299 v3_copy( rbf->co, rbf->to_world[3] );
300 m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h1,0.0f}, rbb->co );
301 v3_copy( rbb->co, rbb->to_world[3] );
302
303 m4x3_invert_affine( rbf->to_world, rbf->to_local );
304 m4x3_invert_affine( rbb->to_world, rbb->to_local );
305
306 rb_update_bounds( rbf );
307 rb_update_bounds( rbb );
308
309 rb_debug( rbf, 0xff0000ff );
310 rb_debug( rbb, 0xff0000ff );
311
312 rb_ct manifold[64];
313 int len = 0;
314
315 len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
316 len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
317
318 rb_presolve_contacts( manifold, len );
319
320 for( int j=0; j<5; j++ )
321 {
322 for( int i=0; i<len; i++ )
323 {
324 struct contact *ct = &manifold[i];
325
326 /*normal */
327 float vn = -v3_dot( phys->rb.v, ct->n );
328 vn += ct->bias;
329
330 float temp = ct->norm_impulse;
331 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
332 vn = ct->norm_impulse - temp;
333
334 v3f impulse;
335 v3_muls( ct->n, vn, impulse );
336
337 v3_add( impulse, phys->rb.v, phys->rb.v );
338
339 /* friction */
340 for( int j=0; j<2; j++ )
341 {
342 float f = k_friction * ct->norm_impulse,
343 vt = v3_dot( phys->rb.v, ct->t[j] ),
344 lambda = -vt;
345
346 float temp = ct->tangent_impulse[j];
347 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
348 lambda = ct->tangent_impulse[j] - temp;
349
350 v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
351 }
352 }
353 }
354
355 phys->in_air = len==0?1:0;
356
357 v3_zero( phys->rb.w );
358 q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
359
360 v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
361
362 v3f p1;
363 v3_muladds( phys->rb.co, forward_dir, 2.0f, p1 );
364 vg_line( phys->rb.co, p1, 0xff0000ff );
365
366 float move_dead = 0.1f,
367 move = vg_get_axis("grabr")*0.5f + 0.5f - move_dead;
368
369 if( move > 0.0f )
370 {
371 float move_norm = move * (1.0f/(1.0f-move_dead)),
372 speed = vg_lerpf( 0.1f*k_runspeed, k_runspeed, move_norm ),
373 amt = k_walk_accel * ktimestep,
374 zvel = v3_dot( phys->rb.v, forward_dir ),
375 new_vel = vg_minf( zvel + amt, speed ),
376 diff = new_vel - vg_minf( zvel, speed );
377
378 v3_muladds( phys->rb.v, forward_dir, diff, phys->rb.v );
379
380 /* TODO move */
381 float walk_norm = 30.0f/(float)player.mdl.anim_walk->length,
382 run_norm = 30.0f/(float)player.mdl.anim_run->length ;
383
384 player.walk_timer += ktimestep * vg_lerpf( walk_norm,run_norm,move_norm );
385 }
386 else
387 {
388 player.walk_timer = 0.0f;
389 }
390
391 phys->rb.v[0] *= 1.0f - (ktimestep*k_walk_friction);
392 phys->rb.v[2] *= 1.0f - (ktimestep*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.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 v3_muladds( phys->rb.v, phys->rb.up,
526 -k_downforce*ktimestep, 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 * ktimestep;
568 }
569 phys->jump_charge = 0;
570 phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
571 }
572
573 static void player_save_frame(void)
574 {
575 player.phys_gate_frame = player.phys;
576 }
577
578 static void player_restore_frame(void)
579 {
580 player.phys = player.phys_gate_frame;
581 rb_update_transform( &player.phys.rb );
582 }
583
584 static void player_do_motion(void)
585 {
586 struct player_phys *phys = &player.phys;
587
588 float horizontal = vg_get_axis("horizontal"),
589 vertical = vg_get_axis("vertical");
590
591 if( (phys->rb.co[1] < 0.0f) && !player.is_dead )
592 {
593 audio_lock();
594 audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
595 audio_player_set_position( &audio_player_extra, phys->rb.co );
596 audio_player_set_vol( &audio_player_extra, 20.0f );
597 audio_player_playclip( &audio_player_extra, &audio_splash );
598 audio_unlock();
599
600 player_kill();
601 }
602
603 if( phys->on_board )
604 player_physics();
605 else
606 player_walk_physics();
607
608 /* Integrate velocity */
609 v3f prevco;
610 v3_copy( phys->rb.co, prevco );
611
612 apply_gravity( phys->rb.v, ktimestep );
613 v3_muladds( phys->rb.co, phys->rb.v, ktimestep, phys->rb.co );
614
615 /* Real angular velocity integration */
616 v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, phys->rb.w );
617 if( v3_length2( phys->rb.w ) > 0.0f )
618 {
619 v4f rotation;
620 v3f axis;
621 v3_copy( phys->rb.w, axis );
622
623 float mag = v3_length( axis );
624 v3_divs( axis, mag, axis );
625 q_axis_angle( rotation, axis, mag*k_rb_delta );
626 q_mul( rotation, phys->rb.q, phys->rb.q );
627 }
628
629 /* Faux angular velocity */
630 v4f rotate;
631
632 float lerpq = phys->in_air? 0.04f: 0.3f;
633 phys->siY = vg_lerpf( phys->siY, phys->iY, lerpq );
634
635 q_axis_angle( rotate, phys->rb.up, phys->siY );
636 q_mul( rotate, phys->rb.q, phys->rb.q );
637 phys->iY = 0.0f;
638
639 /*
640 * Gate intersection, by tracing a line over the gate planes
641 */
642 for( int i=0; i<world.routes.gate_count; i++ )
643 {
644 struct route_gate *rg = &world.routes.gates[i];
645 teleport_gate *gate = &rg->gate;
646
647 if( gate_intersect( gate, phys->rb.co, prevco ) )
648 {
649 m4x3_mulv( gate->transport, phys->rb.co, phys->rb.co );
650 m3x3_mulv( gate->transport, phys->rb.v, phys->rb.v );
651 m3x3_mulv( gate->transport, phys->vl, phys->vl );
652 m3x3_mulv( gate->transport, phys->v_last, phys->v_last );
653 m3x3_mulv( gate->transport, phys->m, phys->m );
654 m3x3_mulv( gate->transport, phys->bob, phys->bob );
655
656 v4f transport_rotation;
657 m3x3_q( gate->transport, transport_rotation );
658 q_mul( transport_rotation, phys->rb.q, phys->rb.q );
659
660 world_routes_activate_gate( i );
661
662 if( !phys->on_board )
663 {
664 v3f fwd_dir = {cosf(player.angles[0]),
665 0.0f,
666 sinf(player.angles[0])};
667 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
668
669 player.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
670 }
671
672 player_save_frame();
673
674 audio_lock();
675 audio_play_oneshot( &audio_gate_pass, 1.0f );
676 audio_unlock();
677 break;
678 }
679 }
680
681 rb_update_transform( &phys->rb );
682 }
683
684 /*
685 * Free camera movement
686 */
687 static void player_mouseview(void)
688 {
689 if( gui_want_mouse() )
690 return;
691
692 static v2f mouse_last,
693 view_vel = { 0.0f, 0.0f };
694
695 if( vg_get_button_down( "primary" ) )
696 v2_copy( vg_mouse, mouse_last );
697
698 else if( vg_get_button( "primary" ) )
699 {
700 v2f delta;
701 v2_sub( vg_mouse, mouse_last, delta );
702 v2_copy( vg_mouse, mouse_last );
703
704 v2_muladds( view_vel, delta, 0.001f, view_vel );
705 }
706
707 v2_muladds( view_vel,
708 (v2f){ vg_get_axis("h1"), vg_get_axis("v1") },
709 0.05f, view_vel );
710 v2_muls( view_vel, 0.93f, view_vel );
711 v2_add( view_vel, player.angles, player.angles );
712 player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
713 }
714
715 static void player_freecam(void)
716 {
717 player_mouseview();
718
719 float movespeed = fc_speed;
720 v3f lookdir = { 0.0f, 0.0f, -1.0f },
721 sidedir = { 1.0f, 0.0f, 0.0f };
722
723 m3x3_mulv( player.camera, lookdir, lookdir );
724 m3x3_mulv( player.camera, sidedir, sidedir );
725
726 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
727 if( vg_get_button( "forward" ) )
728 v3_muladds( move_vel, lookdir, ktimestep * movespeed, move_vel );
729 if( vg_get_button( "back" ) )
730 v3_muladds( move_vel, lookdir, ktimestep *-movespeed, move_vel );
731 if( vg_get_button( "left" ) )
732 v3_muladds( move_vel, sidedir, ktimestep *-movespeed, move_vel );
733 if( vg_get_button( "right" ) )
734 v3_muladds( move_vel, sidedir, ktimestep * movespeed, move_vel );
735
736 v3_muls( move_vel, 0.7f, move_vel );
737 v3_add( move_vel, player.camera_pos, player.camera_pos );
738 }
739
740 static void player_camera_update(void)
741 {
742 /* Update camera matrices */
743 v4f qyaw, qpitch, qcam;
744 q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -player.angles[0] );
745 q_axis_angle( qpitch, (v3f){ 1.0f, 0.0f, 0.0f }, -player.angles[1] );
746
747 q_mul( qyaw, qpitch, qcam );
748 q_m3x3( qcam, player.camera );
749
750 v3_copy( player.camera_pos, player.camera[3] );
751 m4x3_invert_affine( player.camera, player.camera_inverse );
752 }
753
754 static int reset_player( int argc, char const *argv[] )
755 {
756 struct player_phys *phys = &player.phys;
757 struct respawn_point *rp = NULL, *r;
758
759 if( argc == 1 )
760 {
761 for( int i=0; i<world.spawn_count; i++ )
762 {
763 r = &world.spawns[i];
764 if( !strcmp( r->name, argv[0] ) )
765 {
766 rp = r;
767 break;
768 }
769 }
770
771 if( !rp )
772 vg_warn( "No spawn named '%s'\n", argv[0] );
773 }
774
775 if( !rp )
776 {
777 float min_dist = INFINITY;
778
779 for( int i=0; i<world.spawn_count; i++ )
780 {
781 r = &world.spawns[i];
782 float d = v3_dist2( r->co, phys->rb.co );
783
784 vg_info( "Dist %s : %f\n", r->name, d );
785 if( d < min_dist )
786 {
787 min_dist = d;
788 rp = r;
789 }
790 }
791 }
792
793 if( !rp )
794 {
795 vg_error( "No spawn found\n" );
796 if( !world.spawn_count )
797 return 0;
798
799 rp = &world.spawns[0];
800 }
801
802 player.is_dead = 0;
803
804 v4_copy( rp->q, phys->rb.q );
805 v3_copy( rp->co, phys->rb.co );
806 v3_zero( phys->rb.v );
807
808 phys->vswitch = 1.0f;
809 phys->slip_last = 0.0f;
810 phys->in_air = 1;
811 phys->on_board = 0;
812 m3x3_identity( phys->vr );
813
814 player.mdl.shoes[0] = 1;
815 player.mdl.shoes[1] = 1;
816
817 rb_update_transform( &phys->rb );
818 player_save_frame();
819 return 1;
820 }
821
822 #endif /* PLAYER_PHYSICS_H */