add logs for fall off reason
[carveJwlIkooP6JGAAIwe30JlM.git] / player_walk.c
1 #pragma once
2
3 #include "vg/vg_rigidbody_collision.h"
4 #include "scene_rigidbody.h"
5
6 #include "player.h"
7 #include "input.h"
8 #include "audio.h"
9
10 static void player_walk_drop_in_vector( v3f vec ){
11 v3f axis, init_dir;
12 v3_cross( (v3f){0.0f,1.0f,0.0f}, player_walk.state.drop_in_normal, axis );
13 v3_cross( axis, player_walk.state.drop_in_normal, init_dir );
14 v3_normalize( init_dir );
15 v3_muls( init_dir, 4.25f, vec );
16 }
17
18 static float player_xyspeed2(void){
19 return v3_length2( (v3f){localplayer.rb.v[0], 0.0f, localplayer.rb.v[2]} );
20 }
21
22 static void player_walk_generic_to_skate( enum skate_activity init, f32 yaw ){
23 localplayer.subsystem = k_player_subsystem_skate;
24
25 v3f v;
26
27 if( player_xyspeed2() < 0.1f * 0.1f )
28 q_mulv( localplayer.rb.q, (v3f){0.0f,0.0f,1.6f}, v );
29 else
30 v3_copy( localplayer.rb.v, v );
31
32 player_skate.state.activity_prev = k_skate_activity_ground;
33 player_skate.state.activity = init;
34
35 v3f dir;
36 v3_copy( v, dir );
37 v3_normalize( dir );
38
39 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
40 atan2f(-dir[0],-dir[2]) );
41 q_normalize( localplayer.rb.q );
42
43 q_mulv( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, player_skate.state.cog );
44 v3_add( player_skate.state.cog, localplayer.rb.co, player_skate.state.cog );
45
46 v3_copy( v, player_skate.state.cog_v );
47 v3_copy( v, localplayer.rb.v );
48
49 player__begin_holdout( (v3f){0.0f,0.0f,0.0f} );
50 player__skate_reset_animator();
51 player__skate_clear_mechanics();
52 rb_update_matrices( &localplayer.rb );
53 v3_copy( (v3f){yaw,0.0f,0.0f}, player_skate.state.trick_euler );
54
55 if( init == k_skate_activity_air )
56 player__approximate_best_trajectory();
57 }
58
59 static void player_walk_drop_in_to_skate(void){
60 localplayer.immobile = 0;
61 localplayer.subsystem = k_player_subsystem_skate;
62
63 player_skate.state.activity_prev = k_skate_activity_ground;
64 player_skate.state.activity = k_skate_activity_ground;
65
66 player__begin_holdout( (v3f){0,0,0} );
67 player__skate_clear_mechanics();
68 player__skate_reset_animator();
69
70 v3f init_velocity;
71 player_walk_drop_in_vector( init_velocity );
72
73 rb_update_matrices( &localplayer.rb );
74 v3_muladds( localplayer.rb.co, localplayer.rb.to_world[1], 1.0f,
75 player_skate.state.cog );
76 v3_copy( init_velocity, player_skate.state.cog_v );
77 v3_copy( init_velocity, localplayer.rb.v );
78 v3_copy( init_velocity, localplayer.cam_control.cam_velocity_smooth );
79 v3_copy( (v3f){player_walk.animator.board_yaw+1.0f,0,0},
80 player_skate.state.trick_euler );
81 }
82
83 static void player_walk_drop_in_overhang_transform( f32 t, v3f co, v4f q ){
84 v3f axis;
85 v3_cross( (v3f){0,1,0}, player_walk.state.drop_in_normal, axis );
86 v3_normalize( axis );
87
88 float a = acosf( player_walk.state.drop_in_normal[1] ) * t;
89 q_axis_angle( q, axis, a );
90
91 float l = t * 0.5f,
92 heading_angle = player_walk.state.drop_in_angle;
93
94 v3f overhang;
95 overhang[0] = sinf( heading_angle ) * l;
96 overhang[1] = 0.28f * l;
97 overhang[2] = cosf( heading_angle ) * l;
98
99 q_mulv( q, overhang, overhang );
100 v3_add( player_walk.state.drop_in_target, overhang, co );
101 }
102
103 static int player_walk_scan_for_drop_in(void){
104 world_instance *world = world_current_instance();
105
106 v3f dir, center;
107 q_mulv( localplayer.rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
108 v3_muladds( localplayer.rb.co, localplayer.rb.to_world[1], -1.0f, center );
109
110 ray_hit samples[20];
111 int sample_count = 0;
112
113 for( int i=0; i<20; i ++ ){
114 float t = (float)i * (1.0f/19.0f),
115 s = sinf( t * VG_PIf * 0.25f ),
116 c = cosf( t * VG_PIf * 0.25f );
117
118 v3f ray_dir, pos;
119 v3_muls ( localplayer.rb.to_world[1], -c, ray_dir );
120 v3_muladds( ray_dir, dir, -s, ray_dir );
121 v3_muladds( center, ray_dir, -2.0f, pos );
122
123 ray_hit *ray = &samples[ sample_count ];
124 ray->dist = 2.0f;
125
126 if( ray_world( world, pos, ray_dir, ray, 0 ) ){
127 vg_line( pos, ray->pos, VG__RED );
128 vg_line_point( ray->pos, 0.025f, VG__BLACK );
129
130 sample_count ++;
131 }
132 }
133
134 float min_a = 0.70710678118654752f;
135 ray_hit *candidate = NULL;
136
137 if( sample_count >= 2 ){
138 for( int i=0; i<sample_count-1; i++ ){
139 ray_hit *s0 = &samples[i],
140 *s1 = &samples[i+1];
141
142 float a = v3_dot( s0->normal, s1->normal );
143
144 if( (a < min_a) && (a >= -0.1f) && (s0->normal[1]>s1->normal[1]) ){
145 min_a = a;
146 candidate = s0;
147 }
148 }
149 }
150
151 if( candidate ){
152 v4f pa, pb, pc;
153
154 ray_hit *s0 = candidate,
155 *s1 = candidate+1;
156
157 vg_line( s0->pos, s1->pos, VG__WHITE );
158
159 v3_copy( s0->normal, pa );
160 v3_copy( s1->normal, pb );
161 v3_cross( localplayer.rb.to_world[1], dir, pc );
162 v3_normalize( pc );
163
164 pa[3] = v3_dot( pa, s0->pos );
165 pb[3] = v3_dot( pb, s1->pos );
166 pc[3] = v3_dot( pc, localplayer.rb.co );
167
168 v3f edge;
169 if( plane_intersect3( pa, pb, pc, edge ) ){
170 v3_copy( edge, player_walk.state.drop_in_target );
171 v3_copy( s1->normal, player_walk.state.drop_in_normal );
172 v3_copy( localplayer.rb.co, player_walk.state.drop_in_start );
173
174 player_walk.state.drop_in_start_angle = player_get_heading_yaw();
175 player_walk.state.drop_in_angle =
176 atan2f( player_walk.state.drop_in_normal[0],
177 player_walk.state.drop_in_normal[2] );
178
179 /* TODO: scan multiple of these? */
180 v3f oco;
181 v4f oq;
182 player_walk_drop_in_overhang_transform( 1.0f, oco, oq );
183
184 v3f va = {0.0f,0.0f,-k_board_length - 0.3f},
185 vb = {0.0f,0.0f, k_board_length + 0.3f};
186
187 q_mulv( oq, va, va );
188 q_mulv( oq, vb, vb );
189 v3_add( oco, va, va );
190 v3_add( oco, vb, vb );
191
192 v3f v0;
193 v3_sub( vb, va, v0 );
194 v3_normalize( v0 );
195
196 ray_hit ray;
197 ray.dist = k_board_length*2.0f + 0.6f;
198
199 if( ray_world( world, va, v0, &ray, 0 ) ){
200 vg_line( va, vb, VG__RED );
201 vg_line_point( ray.pos, 0.1f, VG__RED );
202 vg_error( "invalidated\n" );
203 return 0;
204 }
205
206 v3_muls( v0, -1.0f, v0 );
207 if( ray_world( world, vb, v0, &ray, 0 ) ){
208 vg_line( va, vb, VG__RED );
209 vg_line_point( ray.pos, 0.1f, VG__RED );
210 vg_error( "invalidated\n" );
211 return 0;
212 }
213
214 player_walk_drop_in_vector( localplayer.rb.v );
215 return 1;
216 }
217 else{
218 vg_error( "failed to find intersection of drop in\n" );
219 }
220 }
221
222 return 0;
223 }
224
225 static bool player__preupdate_anim( struct skeleton_anim *anim, f32 *t,
226 f32 speed ){
227 f32 length = (f32)(anim->length-1) / anim->rate;
228 *t += (vg.time_delta * speed) / length;
229
230 if( *t >= 1.0f ) return 1;
231 else return 0;
232 }
233
234 static void player_walk_pre_sit(void){
235 struct player_walk *w = &player_walk;
236
237 v2f steer;
238 joystick_state( k_srjoystick_steer, steer );
239
240 vg_slewf( &w->state.transition_t, 1.0f, vg.time_delta );
241
242 if( button_down(k_srbind_sit) || (v2_length2(steer)>0.2f) ||
243 button_down(k_srbind_jump) ){
244 w->state.activity = k_walk_activity_sit_up;
245 }
246 return;
247 }
248
249 static void player_walk_pre_sit_up(void){
250 struct player_walk *w = &player_walk;
251
252 if( w->state.transition_t > 0.0f )
253 vg_slewf( &w->state.transition_t, 0.0f, vg.time_delta );
254 else
255 w->state.activity = k_walk_activity_ground;
256
257 if( button_down(k_srbind_sit) )
258 w->state.activity = k_walk_activity_sit;
259
260 return;
261 }
262
263 static void player_walk_pre_ground(void){
264 struct player_walk *w = &player_walk;
265
266 if( button_down(k_srbind_sit) ){
267 v3_zero( localplayer.rb.v );
268 w->state.activity = k_walk_activity_sit;
269 w->state.transition_t = 0.0f;
270 return;
271 }
272
273 if( button_down( k_srbind_use ) ){
274 if( player_walk_scan_for_drop_in() ){
275 w->state.activity = k_walk_activity_odrop_in;
276 }
277 else{
278 w->state.activity = k_walk_activity_oregular;
279 }
280
281 w->state.transition_t = 0.0f;
282 }
283
284 if( button_down( k_srbind_jump ) ){
285 w->state.jump_queued = 1;
286 w->state.jump_input_time = vg.time;
287 }
288 }
289
290 static void player_walk_pre_air(void){
291 struct player_walk *w = &player_walk;
292 if( button_down( k_srbind_use ) ){
293 w->state.activity = k_walk_activity_oair;
294 w->state.transition_t = 0.0f;
295 }
296
297 if( button_down( k_srbind_jump ) ){
298 w->state.jump_queued = 1;
299 w->state.jump_input_time = vg.time;
300 }
301 }
302
303 static void player_walk_pre_drop_in(void){
304 struct player_walk *w = &player_walk;
305 bool finished = player__preupdate_anim( w->anim_drop_in,
306 &w->state.transition_t, 1.0f );
307 if( finished )
308 player_walk_drop_in_to_skate();
309 }
310
311 static void player_walk_pre_caveman(void){
312 struct player_walk *w = &player_walk;
313 bool finished = player__preupdate_anim( w->anim_jump_to_air,
314 &w->state.transition_t, 1.0f );
315 if( finished ){
316 player_walk_generic_to_skate( k_skate_activity_air,
317 player_walk.animator.board_yaw );
318 }
319 }
320
321 static void player_walk_pre_running_start(void){
322 struct player_walk *w = &player_walk;
323 bool finished = player__preupdate_anim( w->anim_intro,
324 &w->state.transition_t, 1.0f );
325 if( finished ){
326 /* TODO: get the derivative of the last keyframes to calculate new
327 * velocity for player */
328 player_walk_generic_to_skate( k_skate_activity_ground,
329 player_walk.animator.board_yaw+1.0f );
330 }
331 }
332
333 static void player_walk_pre_popoff(void){
334 struct player_walk *w = &player_walk;
335 bool finished = player__preupdate_anim( w->anim_popoff,
336 &w->state.transition_t, 1.0f );
337
338 if( finished ){
339 w->state.activity = k_walk_activity_ground;
340 w->animator.board_yaw += 1.0f;
341 }
342 }
343
344 static void player__walk_pre_update(void){
345 struct player_walk *w = &player_walk;
346
347 if( localplayer.immobile ) return;
348 else player_look( localplayer.angles, skaterift.time_rate );
349
350 enum walk_activity a = w->state.activity;
351
352 if ( a == k_walk_activity_sit ) player_walk_pre_sit();
353 else if( a == k_walk_activity_sit_up ) player_walk_pre_sit_up();
354 else if( a == k_walk_activity_ground ) player_walk_pre_ground();
355 else if( a == k_walk_activity_air ) player_walk_pre_air();
356 else if( a == k_walk_activity_odrop_in ) player_walk_pre_drop_in();
357 else if( a == k_walk_activity_oair ) player_walk_pre_caveman();
358 else if( a == k_walk_activity_oregular ) player_walk_pre_running_start();
359 else if( a == k_walk_activity_ipopoff ) player_walk_pre_popoff();
360 }
361
362 static int player_walk_normal_standable( v3f n ){
363 return n[1] > 0.70710678118f;
364 }
365
366 static void player_accelerate( v3f v, v3f movedir, f32 speed, f32 accel ){
367 float currentspeed = v3_dot( v, movedir ),
368 addspeed = speed - currentspeed;
369
370 if( addspeed <= 0 )
371 return;
372
373 float accelspeed = accel * k_rb_delta * speed;
374
375 if( accelspeed > addspeed )
376 accelspeed = addspeed;
377
378 v3_muladds( v, movedir, accelspeed, v );
379 }
380
381 static void player_friction( v3f v, f32 friction ){
382 float speed = v3_length( v ),
383 drop = 0.0f,
384 control = vg_maxf( speed, k_stopspeed );
385
386 if( speed < 0.04f )
387 return;
388
389 drop += control * friction * k_rb_delta;
390
391 float newspeed = vg_maxf( 0.0f, speed - drop );
392 newspeed /= speed;
393
394 v3_muls( v, newspeed, v );
395 }
396
397 static void player_walk_custom_filter( world_instance *world,
398 rb_ct *man, int len, f32 w ){
399 for( int i=0; i<len; i++ ){
400 rb_ct *ci = &man[i];
401 if( ci->type == k_contact_type_disabled ||
402 ci->type == k_contact_type_edge )
403 continue;
404
405
406 float d1 = v3_dot( ci->co, ci->n );
407
408 for( int j=0; j<len; j++ ){
409 if( j == i )
410 continue;
411
412 rb_ct *cj = &man[j];
413 if( cj->type == k_contact_type_disabled )
414 continue;
415
416 struct world_surface *si = world_contact_surface( world, ci ),
417 *sj = world_contact_surface( world, cj );
418
419 if( (sj->info.flags & k_material_flag_walking) &&
420 !(si->info.flags & k_material_flag_walking)){
421 continue;
422 }
423
424 float d2 = v3_dot( cj->co, ci->n ),
425 d = d2-d1;
426
427 if( fabsf( d ) <= w ){
428 cj->type = k_contact_type_disabled;
429 }
430 }
431 }
432 }
433
434 static void player_walk_update_generic(void){
435 struct player_walk *w = &player_walk;
436
437 if( (w->state.activity != k_walk_activity_oregular) &&
438 (w->state.activity != k_walk_activity_oair) ){
439 joystick_state( k_srjoystick_steer, w->state.steer );
440 w->state.steer[2] = button_press(k_srbind_run)? k_runspeed: k_walkspeed;
441 if( v2_length2(w->state.steer)>1.0f )
442 v2_normalize(w->state.steer);
443 }
444
445 v3_copy( localplayer.rb.co, w->state.prev_pos );
446 v3_zero( localplayer.rb.w );
447
448 world_instance *world = world_current_instance();
449
450 if( world->water.enabled ){
451 if( localplayer.rb.co[1]+0.4f < world->water.height ){
452 player__networked_sfx( k_player_subsystem_walk, 32,
453 k_player_walk_soundeffect_splash,
454 localplayer.rb.co, 1.0f );
455 vg_info( "player fell of due to walking into walker\n" );
456 player__dead_transition( k_player_die_type_generic );
457 return;
458 }
459 }
460
461 enum walk_activity prev_state = w->state.activity;
462
463 w->collider.h = 2.0f;
464 w->collider.r = 0.3f;
465
466 m4x3f mtx;
467 m3x3_copy( localplayer.rb.to_world, mtx );
468 v3_add( localplayer.rb.co, (v3f){0,1,0}, mtx[3] );
469
470 vg_line_capsule( mtx, w->collider.r, w->collider.h, VG__WHITE );
471
472 rb_ct manifold[64];
473 int len;
474
475 float yaw = localplayer.angles[0];
476
477 v3f forward_dir = { -sinf(yaw), 0.0f, cosf(yaw) };
478 v3f right_dir = { forward_dir[2], 0.0f, -forward_dir[0] };
479
480 /*
481 * Collision detection
482 */
483
484 len = rb_capsule__scene( mtx, &w->collider, NULL,
485 world->geo_bh, manifold, 0 );
486 player_walk_custom_filter( world, manifold, len, 0.01f );
487 len = rb_manifold_apply_filtered( manifold, len );
488
489 v3f surface_avg = { 0.0f, 0.0f, 0.0f };
490
491 w->state.activity = k_walk_activity_air;
492 w->surface = k_surface_prop_concrete;
493
494 for( int i=0; i<len; i++ ){
495 rb_ct *ct = &manifold[i];
496 rb_debug_contact( ct );
497
498 if( player_walk_normal_standable( ct->n ) ){
499 w->state.activity = k_walk_activity_ground;
500
501 v3_add( surface_avg, ct->n, surface_avg );
502
503 struct world_surface *surf = world_contact_surface( world, ct );
504 if( surf->info.surface_prop > w->surface )
505 w->surface = surf->info.surface_prop;
506 }
507
508 rb_prepare_contact( ct, k_rb_delta );
509 }
510
511 /*
512 * Move & Friction
513 */
514 float accel_speed = 0.0f, nominal_speed = 0.0f;
515 v3f movedir;
516
517 v3_muls( right_dir, w->state.steer[0], movedir );
518 v3_muladds( movedir, forward_dir, w->state.steer[1], movedir );
519
520 if( w->state.activity == k_walk_activity_ground ){
521 v3_normalize( surface_avg );
522
523 v3f tx, ty;
524 v3_tangent_basis( surface_avg, tx, ty );
525
526 if( v2_length2(w->state.steer) > 0.001f ){
527 /* clip movement to the surface */
528 float d = v3_dot(surface_avg,movedir);
529 v3_muladds( movedir, surface_avg, -d, movedir );
530 }
531
532 accel_speed = k_walk_accel;
533 nominal_speed = w->state.steer[2];
534
535 /* jump */
536 if( w->state.jump_queued ){
537 w->state.jump_queued = 0;
538
539 f32 t = vg.time - w->state.jump_input_time;
540 if( t < PLAYER_JUMP_EPSILON ){
541 localplayer.rb.v[1] = 5.0f;
542 w->state.activity = k_walk_activity_air;
543 prev_state = k_walk_activity_air;
544 accel_speed = k_walk_air_accel;
545 nominal_speed = k_airspeed;
546 }
547 }
548 else{
549 player_friction( localplayer.rb.v, k_walk_friction );
550 }
551 }
552 else{
553 accel_speed = k_walk_air_accel;
554 nominal_speed = k_airspeed;
555 }
556
557 if( v2_length2( w->state.steer ) > 0.001f ){
558 player_accelerate( localplayer.rb.v, movedir,
559 nominal_speed, accel_speed );
560 v3_normalize( movedir );
561 }
562
563 /*
564 * Resolve velocity constraints
565 */
566 for( int j=0; j<5; j++ ){
567 for( int i=0; i<len; i++ ){
568 rb_ct *ct = &manifold[i];
569
570 /*normal */
571 float vn = -v3_dot( localplayer.rb.v, ct->n );
572
573 float temp = ct->norm_impulse;
574 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
575 vn = ct->norm_impulse - temp;
576
577 v3_muladds( localplayer.rb.v, ct->n, vn, localplayer.rb.v );
578 }
579 }
580
581 /* stepping */
582 if( w->state.activity == k_walk_activity_ground||
583 prev_state == k_walk_activity_ground ){
584 float max_dist = 0.4f;
585
586 v3f pa, pb;
587 v3_copy( localplayer.rb.co, pa );
588 pa[1] += w->collider.r + max_dist;
589 v3_add( pa, (v3f){0, -max_dist * 2.0f, 0}, pb );
590 vg_line( pa, pb, 0xff000000 );
591
592 v3f n;
593 float t;
594 if( spherecast_world( world, pa, pb,
595 w->collider.r, &t, n, 0 ) != -1 ){
596 if( player_walk_normal_standable(n) ){
597 v3_lerp( pa, pb, t, localplayer.rb.co );
598 localplayer.rb.co[1] += -w->collider.r - k_penetration_slop;
599 w->state.activity = k_walk_activity_ground;
600
601 float d = -v3_dot(n,localplayer.rb.v);
602 v3_muladds( localplayer.rb.v, n, d, localplayer.rb.v );
603 localplayer.rb.v[1] += -k_gravity * k_rb_delta;
604 }
605 }
606 }
607
608 /*
609 * Depenetrate
610 */
611 v3f dt;
612 rb_depenetrate( manifold, len, dt );
613 v3_add( dt, localplayer.rb.co, localplayer.rb.co );
614
615 /* integrate */
616 if( w->state.activity == k_walk_activity_air ){
617 localplayer.rb.v[1] += -k_gravity*k_rb_delta;
618 }
619
620 if( localplayer.immobile ){
621 localplayer.rb.v[0] = 0.0f;
622 localplayer.rb.v[2] = 0.0f;
623 }
624
625 v3_muladds( localplayer.rb.co, localplayer.rb.v, k_rb_delta,
626 localplayer.rb.co );
627 v3_add( localplayer.rb.co, (v3f){0,1,0}, mtx[3] );
628 vg_line_capsule( mtx, w->collider.r, w->collider.h, VG__GREEN );
629
630 /*
631 * CCD routine
632 * ---------------------------------------------------
633 *
634 */
635 v3f lwr_prev,
636 lwr_now,
637 lwr_offs = { 0.0f, w->collider.r, 0.0f };
638
639 v3_add( lwr_offs, w->state.prev_pos, lwr_prev );
640 v3_add( lwr_offs, localplayer.rb.co, lwr_now );
641
642 v3f movedelta;
643 v3_sub( localplayer.rb.co, w->state.prev_pos, movedelta );
644
645 float movedist = v3_length( movedelta );
646
647 if( movedist > 0.3f ){
648 float t, sr = w->collider.r-0.04f;
649 v3f n;
650
651 if( spherecast_world( world, lwr_prev, lwr_now, sr, &t, n, 0 ) != -1 ){
652 v3_lerp( lwr_prev, lwr_now, vg_maxf(0.01f,t), localplayer.rb.co );
653 localplayer.rb.co[1] -= w->collider.r;
654 rb_update_matrices( &localplayer.rb );
655 v3_add( localplayer.rb.co, (v3f){0,1,0}, mtx[3] );
656 vg_line_capsule( mtx, w->collider.r, w->collider.h, VG__RED);
657 }
658 }
659
660 u32 id = world_intersect_gates(world, localplayer.rb.co, w->state.prev_pos);
661 if( id ){
662 ent_gate *gate = mdl_arritm( &world->ent_gate, mdl_entity_id_id(id) );
663 m4x3_mulv( gate->transport, localplayer.rb.co, localplayer.rb.co );
664 m3x3_mulv( gate->transport, localplayer.rb.v, localplayer.rb.v );
665
666 v4f transport_rotation;
667 m3x3_q( gate->transport, transport_rotation );
668 q_mul( transport_rotation, localplayer.rb.q, localplayer.rb.q );
669 q_normalize( localplayer.rb.q );
670 rb_update_matrices( &localplayer.rb );
671 player__pass_gate( id );
672 }
673 rb_update_matrices( &localplayer.rb );
674
675 if( (prev_state == k_walk_activity_oregular) ||
676 (prev_state == k_walk_activity_oair) ||
677 (prev_state == k_walk_activity_ipopoff) ){
678 w->state.activity = prev_state;
679 }
680
681 w->move_speed = vg_minf( v2_length( (v2f){ localplayer.rb.v[0],
682 localplayer.rb.v[2] } ),
683 k_runspeed );
684 }
685
686 static void player__walk_post_update(void){
687 struct player_walk *w = &player_walk;
688
689 m4x3f mtx;
690 m3x3_copy( localplayer.rb.to_world, mtx );
691 v3_add( localplayer.rb.co, (v3f){0,1,0}, mtx[3] );
692
693 float substep = vg.time_fixed_extrapolate;
694 v3_muladds( mtx[3], localplayer.rb.v, k_rb_delta*substep, mtx[3] );
695 vg_line_capsule( mtx, w->collider.r, w->collider.h, VG__YELOW );
696
697 /* Calculate header */
698 v3f v;
699 if( (player_xyspeed2() > 0.1f*0.1f) ){
700 f32 r = 0.3f;
701 if( (w->state.activity == k_walk_activity_ground) ||
702 (w->state.activity == k_walk_activity_ipopoff) ||
703 (w->state.activity == k_walk_activity_oregular) ){
704 r = 0.07f;
705 }
706
707 f32 ta = atan2f( localplayer.rb.v[0], localplayer.rb.v[2] );
708 v4f qt;
709 q_axis_angle( qt, (v3f){0,1,0}, ta );
710 q_nlerp( localplayer.rb.q, qt, vg.time_delta/r, localplayer.rb.q );
711 }
712
713 vg_line_point( w->state.drop_in_target, 0.1f, VG__GREEN );
714 v3f p1;
715 v3_muladds( w->state.drop_in_target, w->state.drop_in_normal, 0.3f, p1 );
716 vg_line( w->state.drop_in_target, p1, VG__GREEN );
717 v3_muladds( w->state.drop_in_target, localplayer.rb.to_world[1], 0.3f, p1 );
718 vg_line( w->state.drop_in_target, p1, VG__GREEN );
719
720 float a = player_get_heading_yaw();
721 p1[0] = sinf( a );
722 p1[1] = 0.0f;
723 p1[2] = cosf( a );
724
725 v3_add( localplayer.rb.co, p1, p1 );
726 vg_line( localplayer.rb.co, p1, VG__PINK );
727
728 int walk_phase = 0;
729 if( vg_fractf(w->state.walk_timer) > 0.5f )
730 walk_phase = 1;
731 else
732 walk_phase = 0;
733
734 if( (w->state.step_phase != walk_phase) &&
735 (w->state.activity == k_walk_activity_ground ) )
736 {
737 audio_lock();
738 if( w->surface == k_surface_prop_concrete ){
739 audio_oneshot_3d(
740 &audio_footsteps[vg_randu32(&vg.rand) %
741 vg_list_size(audio_footsteps)],
742 localplayer.rb.co, 40.0f, 1.0f
743 );
744 }
745 else if( w->surface == k_surface_prop_grass ){
746 audio_oneshot_3d(
747 &audio_footsteps_grass[ vg_randu32(&vg.rand) %
748 vg_list_size(audio_footsteps_grass)],
749 localplayer.rb.co, 40.0f, 1.0f
750 );
751 }
752 else if( w->surface == k_surface_prop_wood ){
753 audio_oneshot_3d(
754 &audio_footsteps_wood[ vg_randu32(&vg.rand) %
755 vg_list_size(audio_footsteps_wood)],
756 localplayer.rb.co, 40.0f, 1.0f
757 );
758 }
759 audio_unlock();
760 }
761
762 w->state.step_phase = walk_phase;
763 }
764
765 static void player__walk_update(void){
766 struct player_walk *w = &player_walk;
767
768 if( (w->state.activity == k_walk_activity_air) ||
769 (w->state.activity == k_walk_activity_ground) ||
770 (w->state.activity == k_walk_activity_oair) ||
771 (w->state.activity == k_walk_activity_oregular) ||
772 (w->state.activity == k_walk_activity_ipopoff) ){
773 player_walk_update_generic();
774 }
775 }
776
777 static void player_walk_animate_drop_in(void){
778 struct player_walk *w = &player_walk;
779 struct player_walk_animator *animator = &w->animator;
780 struct skeleton_anim *anim = w->anim_drop_in;
781
782 f32 length = (f32)(anim->length-1) / anim->rate,
783 time = w->state.transition_t;
784
785 f32 walk_yaw = vg_alerpf( w->state.drop_in_start_angle,
786 w->state.drop_in_angle, animator->transition_t );
787 v3_lerp( w->state.drop_in_start, w->state.drop_in_target,
788 animator->transition_t, localplayer.rb.co );
789
790 q_axis_angle( localplayer.rb.q, (v3f){0,1,0}, walk_yaw + VG_PIf );
791
792 /* the drop in bit */
793 v3f final_co;
794 v4f final_q;
795 player_walk_drop_in_overhang_transform( animator->transition_t,
796 final_co, final_q );
797
798 q_mul( final_q, localplayer.rb.q, localplayer.rb.q );
799 v3_lerp( localplayer.rb.co, final_co, animator->transition_t,
800 localplayer.rb.co );
801
802 rb_update_matrices( &localplayer.rb );
803
804 v3_muladds( localplayer.rb.co, localplayer.rb.to_world[1],
805 -0.1f*animator->transition_t, localplayer.rb.co );
806
807 v3_copy( localplayer.rb.co, animator->root_co );
808 v4_copy( localplayer.rb.q, animator->root_q );
809
810 /* for the camera purposes only */
811 v3f init_velocity;
812 player_walk_drop_in_vector( init_velocity );
813 v3_muls( init_velocity, animator->transition_t, localplayer.rb.v );
814 v3_copy( localplayer.rb.v,
815 localplayer.cam_control.cam_velocity_smooth );
816 }
817
818 static void player_walk_animate_generic(void){
819 struct player_walk *w = &player_walk;
820 struct player_walk_animator *animator = &w->animator;
821
822 v4f _null;
823 rb_extrapolate( &localplayer.rb, animator->root_co, _null );
824
825 f32 walk_yaw = player_get_heading_yaw(),
826 head_yaw = localplayer.angles[0] + VG_PIf,
827 y = vg_angle_diff( head_yaw, -walk_yaw ),
828 p = vg_clampf( localplayer.angles[1],
829 -k_sit_pitch_limit, k_sit_pitch_limit );
830
831 if( fabsf(y) > k_sit_yaw_limit ){
832 y = 0.0f;
833 p = 0.0f;
834 }
835
836 animator->yaw = vg_lerpf( animator->yaw, y, vg.time_delta*2.0f );
837 animator->pitch = vg_lerpf( animator->pitch, p, vg.time_delta*2.8f );
838 q_axis_angle( animator->root_q, (v3f){0,1,0}, walk_yaw + VG_PIf );
839
840 v4f qrev;
841 q_axis_angle( qrev, (v3f){0,1,0}, VG_TAUf*0.5f );
842 q_mul( localplayer.rb.q, qrev, animator->root_q );
843 }
844
845 static void player__walk_animate(void){
846 struct player_walk *w = &player_walk;
847 player_pose *pose = &localplayer.pose;
848 struct player_walk_animator *animator = &w->animator;
849
850 animator->activity = w->state.activity;
851 animator->transition_t = w->state.transition_t;
852
853 {
854 f32 fly = (w->state.activity == k_walk_activity_air)? 1.0f: 0.0f,
855 rate;
856
857 if( w->state.activity == k_walk_activity_air ) rate = 2.4f;
858 else rate = 9.0f;
859
860 animator->fly = vg_lerpf( animator->fly, fly, rate*vg.time_delta );
861 animator->run = vg_lerpf( animator->run, w->move_speed,
862 8.0f*vg.time_delta);
863 }
864
865 if( animator->run > 0.025f ){
866 f32 walk_norm = 30.0f/(float)w->anim_walk->length,
867 run_norm = 30.0f/(float)w->anim_run->length,
868 l;
869
870 if( animator->run <= k_walkspeed )
871 l = (animator->run / k_walkspeed) * walk_norm;
872 else {
873 l = vg_lerpf( walk_norm, run_norm,
874 (animator->run-k_walkspeed) / (k_runspeed-k_walkspeed) );
875 }
876 w->state.walk_timer += l * vg.time_delta;
877 }
878 else
879 w->state.walk_timer = 0.0f;
880
881 animator->walk_timer = w->state.walk_timer;
882
883 player_walk_animate_generic();
884 if( w->state.activity == k_walk_activity_odrop_in ){
885 player_walk_animate_drop_in();
886 }
887
888 if( (w->state.activity == k_walk_activity_odrop_in) ||
889 (w->state.activity == k_walk_activity_oregular) ||
890 (w->state.activity == k_walk_activity_oair) ){
891 localplayer.cam_velocity_influence = w->animator.transition_t;
892 }
893 else if( w->state.activity == k_walk_activity_ipopoff ){
894 localplayer.cam_velocity_influence = 1.0f-w->animator.transition_t;
895 }
896 else
897 localplayer.cam_velocity_influence = 0.0f;
898
899 if( w->state.activity == k_walk_activity_sit ){
900 localplayer.cam_dist = 3.8f;
901 }
902 else {
903 localplayer.cam_dist = 1.8f;
904 }
905 }
906
907 static void player_walk_pose_sit( struct player_walk_animator *animator,
908 player_pose *pose ){
909 mdl_keyframe bpose[32];
910
911 struct player_walk *w = &player_walk;
912 struct skeleton *sk = &localplayer.skeleton;
913
914 f32 t = animator->transition_t,
915 st = t * ((f32)(w->anim_sit->length-1)/30.0f);
916 skeleton_sample_anim( sk, w->anim_sit, st, bpose );
917
918 v4f qy,qp;
919 f32 *qh = bpose[localplayer.id_head-1].q;
920 q_axis_angle( qy, (v3f){0,1,0}, animator->yaw*0.5f*t );
921 q_axis_angle( qp, (v3f){0,0,1}, animator->pitch*t );
922 q_mul( qy, qh, qh );
923 q_mul( qh, qp, qh );
924 q_normalize( qh );
925
926 qh = bpose[localplayer.id_chest-1].q;
927 q_axis_angle( qy, (v3f){0,1,0}, animator->yaw*0.5f*t );
928 q_mul( qy, qh, qh );
929 q_normalize( qh );
930
931 skeleton_lerp_pose( sk, pose->keyframes, bpose,
932 vg_minf(1.0f,t*10.0f), pose->keyframes );
933 }
934
935 enum walk_transition_type {
936 k_walk_transition_in,
937 k_walk_transition_out,
938 k_walk_transition_outin,
939 };
940
941 static void player_walk_pose_transition(
942 struct player_walk_animator *animator, struct skeleton_anim *anim,
943 enum walk_transition_type type,
944 mdl_keyframe apose[32], f32 *mask, player_pose *pose ){
945
946 mdl_keyframe bpose[32];
947
948 struct player_walk *w = &player_walk;
949 struct skeleton *sk = &localplayer.skeleton;
950
951 f32 length = (f32)(anim->length-1) / anim->rate,
952 t = animator->transition_t * length,
953 blend = 1.0f;
954
955 if( type == k_walk_transition_in || type == k_walk_transition_outin )
956 blend = vg_minf( blend, length-t );
957
958 if( type == k_walk_transition_out || type == k_walk_transition_outin )
959 blend = vg_minf( blend, t );
960
961 blend = vg_smoothstepf( vg_minf(1,blend/k_anim_transition) );
962
963 skeleton_sample_anim_clamped( sk, anim, t, bpose );
964
965 mdl_keyframe *kf_board = &bpose[localplayer.id_board-1];
966 f32 yaw = animator->board_yaw * VG_TAUf * 0.5f;
967
968 v4f qyaw;
969 q_axis_angle( qyaw, (v3f){0,1,0}, yaw );
970 q_mul( kf_board->q, qyaw, kf_board->q );
971 q_normalize( kf_board->q );
972
973 if( mask ){
974 for( i32 i=0; i<sk->bone_count-1; i ++ )
975 keyframe_lerp( apose+i, bpose+i, blend*mask[i], pose->keyframes+i );
976 }
977 else
978 skeleton_lerp_pose( sk, apose, bpose, blend, pose->keyframes );
979 }
980
981 static void player__walk_pose( void *_animator, player_pose *pose ){
982 struct player_walk *w = &player_walk;
983 struct player_walk_animator *animator = _animator;
984 struct skeleton *sk = &localplayer.skeleton;
985
986 v3_copy( animator->root_co, pose->root_co );
987 v4_copy( animator->root_q, pose->root_q );
988 pose->board.lean = 0.0f;
989 pose->type = k_player_pose_type_ik;
990
991 float walk_norm = (float)w->anim_walk->length/30.0f,
992 run_norm = (float)w->anim_run->length/30.0f,
993 t = animator->walk_timer;
994
995 /* walk/run */
996 mdl_keyframe apose[32], bpose[32];
997 if( animator->run <= k_walkspeed ){
998 /* walk / idle */
999 f32 l = vg_minf( 1, (animator->run/k_walkspeed)*6.0f );
1000 skeleton_sample_anim( sk, w->anim_idle, vg.time*0.1f, apose );
1001 skeleton_sample_anim( sk, w->anim_walk, t*walk_norm, bpose );
1002 skeleton_lerp_pose( sk, apose, bpose, l, apose );
1003 }
1004 else {
1005 /* walk / run */
1006 f32 l = (animator->run-k_walkspeed) / (k_runspeed-k_walkspeed);
1007 skeleton_sample_anim( sk, w->anim_walk, t*walk_norm, apose );
1008 skeleton_sample_anim( sk, w->anim_run, t*run_norm, bpose );
1009 skeleton_lerp_pose( sk, apose, bpose, l, apose );
1010 }
1011
1012 /* air */
1013 skeleton_sample_anim( sk, w->anim_jump, vg.time*0.6f, bpose );
1014 skeleton_lerp_pose( sk, apose, bpose, animator->fly, apose );
1015
1016 mdl_keyframe *kf_board = &apose[localplayer.id_board-1];
1017 f32 yaw = animator->board_yaw;
1018
1019 if( animator->activity == k_walk_activity_ipopoff )
1020 if( animator->transition_t > 0.5f )
1021 yaw += 1.0f;
1022
1023 v4f qyaw;
1024 q_axis_angle( qyaw, (v3f){0,1,0}, yaw * VG_TAUf * 0.5f );
1025 q_mul( kf_board->q, qyaw, kf_board->q );
1026 q_normalize( kf_board->q );
1027
1028 /* sit */
1029 if( (animator->activity == k_walk_activity_sit) ||
1030 (animator->activity == k_walk_activity_sit_up) ){
1031 player_walk_pose_sit( animator, pose );
1032 }
1033 else if( animator->activity == k_walk_activity_odrop_in ){
1034 player_walk_pose_transition(
1035 animator, w->anim_drop_in, k_walk_transition_out, apose,
1036 NULL, pose );
1037 }
1038 else if( animator->activity == k_walk_activity_oair ){
1039 player_walk_pose_transition(
1040 animator, w->anim_jump_to_air, k_walk_transition_out, apose,
1041 NULL, pose );
1042 }
1043 else if( animator->activity == k_walk_activity_oregular ){
1044 player_walk_pose_transition(
1045 animator, w->anim_intro, k_walk_transition_out, apose,
1046 NULL, pose );
1047 }
1048 else if( animator->activity == k_walk_activity_ipopoff ){
1049 if( animator->run > 0.2f ){
1050 f32 t = 1.0f-vg_minf( animator->run-0.2f, 1.0f ),
1051 mask[ 32 ];
1052
1053 for( u32 i=0; i<32; i ++ )
1054 mask[i] = 1.0f;
1055
1056 mask[ localplayer.id_ik_foot_l-1 ] = t;
1057 mask[ localplayer.id_ik_foot_r-1 ] = t;
1058 mask[ localplayer.id_ik_knee_l-1 ] = t;
1059 mask[ localplayer.id_ik_knee_r-1 ] = t;
1060 mask[ localplayer.id_hip-1 ] = t;
1061 player_walk_pose_transition(
1062 animator, w->anim_popoff, k_walk_transition_in, apose,
1063 mask, pose );
1064 }
1065 else{
1066 player_walk_pose_transition(
1067 animator, w->anim_popoff, k_walk_transition_in, apose,
1068 NULL, pose );
1069 }
1070 }
1071 else {
1072 skeleton_copy_pose( sk, apose, pose->keyframes );
1073 }
1074 }
1075
1076 static void player__walk_post_animate(void){
1077 /*
1078 * Camera
1079 */
1080 struct player_walk *w = &player_walk;
1081
1082 }
1083
1084 static void player__walk_im_gui(void){
1085 struct player_walk *w = &player_walk;
1086 player__debugtext( 1, "V: %5.2f %5.2f %5.2f (%5.2fm/s)",
1087 localplayer.rb.v[0], localplayer.rb.v[1], localplayer.rb.v[2],
1088 v3_length(localplayer.rb.v) );
1089 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",localplayer.rb.co[0],
1090 localplayer.rb.co[1],
1091 localplayer.rb.co[2] );
1092 player__debugtext( 1, "transition: %5.2f ", w->state.transition_t );
1093 player__debugtext( 1, "activity: %s\n",
1094 (const char *[]){ "air",
1095 "ground",
1096 "sit",
1097 "sit_up",
1098 "inone",
1099 "ipopoff",
1100 "oair",
1101 "odrop_in",
1102 "oregular" }
1103 [w->state.activity] );
1104 player__debugtext( 1, "surface: %s\n",
1105 (const char *[]){ "concrete",
1106 "wood",
1107 "grass",
1108 "tiles",
1109 "metal",
1110 "snow",
1111 "sand" }
1112 [w->surface] );
1113 }
1114
1115 static void player__walk_bind(void){
1116 struct player_walk *w = &player_walk;
1117 struct skeleton *sk = &localplayer.skeleton;
1118
1119 w->anim_idle = skeleton_get_anim( sk, "idle_cycle+y" );
1120 w->anim_walk = skeleton_get_anim( sk, "walk+y" );
1121 w->anim_run = skeleton_get_anim( sk, "run+y" );
1122 w->anim_jump = skeleton_get_anim( sk, "jump+y" );
1123 w->anim_jump_to_air = skeleton_get_anim( sk, "jump_to_air" );
1124 w->anim_drop_in = skeleton_get_anim( sk, "drop_in" );
1125 w->anim_intro = skeleton_get_anim( sk, "into_skate" );
1126 w->anim_sit = skeleton_get_anim( sk, "sit" );
1127 w->anim_popoff = skeleton_get_anim( sk, "pop_off_short" );
1128 }
1129
1130 static void player__walk_transition( bool grounded, f32 board_yaw ){
1131 struct player_walk *w = &player_walk;
1132 w->state.activity = k_walk_activity_air;
1133
1134 if( grounded ){
1135 w->state.activity = k_walk_activity_ipopoff;
1136 }
1137
1138 w->state.transition_t = 0.0f;
1139 w->state.jump_queued = 0;
1140 w->state.jump_input_time = 0.0;
1141 w->state.walk_timer = 0.0f;
1142 w->state.step_phase = 0;
1143 w->animator.board_yaw = fmodf( board_yaw, 2.0f );
1144 rb_update_matrices( &localplayer.rb );
1145 }
1146
1147 static void player__walk_reset(void){
1148 struct player_walk *w = &player_walk;
1149 w->state.activity = k_walk_activity_air;
1150 w->state.transition_t = 0.0f;
1151
1152 v3f fwd = { 0.0f, 0.0f, 1.0f };
1153 q_mulv( localplayer.rb.q, fwd, fwd );
1154 q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
1155 atan2f(fwd[0], fwd[2]) );
1156
1157 rb_update_matrices( &localplayer.rb );
1158 }
1159
1160 static void player__walk_animator_exchange( bitpack_ctx *ctx, void *data ){
1161 struct player_walk_animator *animator = data;
1162
1163 bitpack_qv3f( ctx, 24, -1024.0f, 1024.0f, animator->root_co );
1164 bitpack_qquat( ctx, animator->root_q );
1165 bitpack_qf32( ctx, 8, 0.0f, 1.0f, &animator->fly );
1166 bitpack_qf32( ctx, 8, 0.0f, k_runspeed, &animator->run );
1167 bitpack_qf32( ctx, 16, 0.0f, 120.0f, &animator->walk_timer );
1168
1169 for( int i=0; i<1; i++ ){ /* without this you get a warning from gcc. lol */
1170 bitpack_bytes( ctx, 8, &animator->activity );
1171 }
1172
1173 bitpack_qf32( ctx, 8, 0.0f, 1.0f, &animator->transition_t );
1174
1175 if( (animator->activity == k_walk_activity_sit) ||
1176 (animator->activity == k_walk_activity_sit_up) ){
1177 bitpack_qf32( ctx, 8, -k_sit_yaw_limit, k_sit_yaw_limit, &animator->yaw );
1178 bitpack_qf32( ctx, 8, -k_sit_pitch_limit, k_sit_pitch_limit,
1179 &animator->pitch );
1180 }
1181
1182 bitpack_qf32( ctx, 16, -100.0f, 100.0f, &animator->board_yaw );
1183 }
1184
1185 static void player__walk_sfx_oneshot( u8 id, v3f pos, f32 volume ){
1186 audio_lock();
1187
1188 if( id == k_player_walk_soundeffect_splash ){
1189 audio_oneshot_3d( &audio_splash, pos, 40.0f, 1.0f );
1190 }
1191
1192 audio_unlock();
1193 }