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