1 #include "vg/vg_rigidbody_collision.h"
4 #include "player_walk.h"
5 #include "player_skate.h"
6 #include "player_dead.h"
10 #include "scene_rigidbody.h"
12 struct player_walk player_walk
;
13 struct player_subsystem_interface player_subsystem_walk
=
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 .sfx_oneshot
= player__walk_sfx_oneshot
,
27 .animator_data
= &player_walk
.animator
,
28 .animator_size
= sizeof(player_walk
.animator
),
33 static void player_walk_drop_in_vector( v3f vec
){
35 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, player_walk
.state
.drop_in_normal
, axis
);
36 v3_cross( axis
, player_walk
.state
.drop_in_normal
, init_dir
);
37 v3_normalize( init_dir
);
38 v3_muls( init_dir
, 4.25f
, vec
);
41 static float player_xyspeed2(void){
42 return v3_length2( (v3f
){localplayer
.rb
.v
[0], 0.0f
, localplayer
.rb
.v
[2]} );
45 static void player_walk_generic_to_skate( enum skate_activity init
, f32 yaw
){
46 localplayer
.subsystem
= k_player_subsystem_skate
;
50 if( player_xyspeed2() < 0.1f
* 0.1f
)
51 q_mulv( localplayer
.rb
.q
, (v3f
){0.0f
,0.0f
,1.6f
}, v
);
53 v3_copy( localplayer
.rb
.v
, v
);
55 player_skate
.state
.activity_prev
= k_skate_activity_ground
;
56 player_skate
.state
.activity
= init
;
62 q_axis_angle( localplayer
.rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
},
63 atan2f(-dir
[0],-dir
[2]) );
64 q_normalize( localplayer
.rb
.q
);
66 q_mulv( localplayer
.rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, player_skate
.state
.cog
);
67 v3_add( player_skate
.state
.cog
, localplayer
.rb
.co
, player_skate
.state
.cog
);
69 v3_copy( v
, player_skate
.state
.cog_v
);
70 v3_copy( v
, localplayer
.rb
.v
);
72 player__begin_holdout( (v3f
){0.0f
,0.0f
,0.0f
} );
73 player__skate_reset_animator();
74 player__skate_clear_mechanics();
75 rb_update_matrices( &localplayer
.rb
);
76 v3_copy( (v3f
){yaw
,0.0f
,0.0f
}, player_skate
.state
.trick_euler
);
78 if( init
== k_skate_activity_air
)
79 player__approximate_best_trajectory();
82 static void player_walk_drop_in_to_skate(void){
83 localplayer
.immobile
= 0;
84 localplayer
.subsystem
= k_player_subsystem_skate
;
86 player_skate
.state
.activity_prev
= k_skate_activity_ground
;
87 player_skate
.state
.activity
= k_skate_activity_ground
;
89 player__begin_holdout( (v3f
){0,0,0} );
90 player__skate_clear_mechanics();
91 player__skate_reset_animator();
94 player_walk_drop_in_vector( init_velocity
);
96 rb_update_matrices( &localplayer
.rb
);
97 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.to_world
[1], 1.0f
,
98 player_skate
.state
.cog
);
99 v3_copy( init_velocity
, player_skate
.state
.cog_v
);
100 v3_copy( init_velocity
, localplayer
.rb
.v
);
101 v3_copy( init_velocity
, localplayer
.cam_control
.cam_velocity_smooth
);
102 v3_copy( (v3f
){player_walk
.animator
.board_yaw
+1.0f
,0,0},
103 player_skate
.state
.trick_euler
);
106 static void player_walk_drop_in_overhang_transform( f32 t
, v3f co
, v4f q
){
108 v3_cross( (v3f
){0,1,0}, player_walk
.state
.drop_in_normal
, axis
);
109 v3_normalize( axis
);
111 float a
= acosf( player_walk
.state
.drop_in_normal
[1] ) * t
;
112 q_axis_angle( q
, axis
, a
);
115 heading_angle
= player_walk
.state
.drop_in_angle
;
118 overhang
[0] = sinf( heading_angle
) * l
;
119 overhang
[1] = 0.28f
* l
;
120 overhang
[2] = cosf( heading_angle
) * l
;
122 q_mulv( q
, overhang
, overhang
);
123 v3_add( player_walk
.state
.drop_in_target
, overhang
, co
);
126 static int player_walk_scan_for_drop_in(void){
127 world_instance
*world
= world_current_instance();
130 q_mulv( localplayer
.rb
.q
, (v3f
){0.0f
,0.0f
,1.0f
}, dir
);
131 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.to_world
[1], -1.0f
, center
);
134 int sample_count
= 0;
136 for( int i
=0; i
<20; i
++ ){
137 float t
= (float)i
* (1.0f
/19.0f
),
138 s
= sinf( t
* VG_PIf
* 0.25f
),
139 c
= cosf( t
* VG_PIf
* 0.25f
);
142 v3_muls ( localplayer
.rb
.to_world
[1], -c
, ray_dir
);
143 v3_muladds( ray_dir
, dir
, -s
, ray_dir
);
144 v3_muladds( center
, ray_dir
, -2.0f
, pos
);
146 ray_hit
*ray
= &samples
[ sample_count
];
149 if( ray_world( world
, pos
, ray_dir
, ray
, 0 ) ){
150 vg_line( pos
, ray
->pos
, VG__RED
);
151 vg_line_point( ray
->pos
, 0.025f
, VG__BLACK
);
157 float min_a
= 0.70710678118654752f
;
158 ray_hit
*candidate
= NULL
;
160 if( sample_count
>= 2 ){
161 for( int i
=0; i
<sample_count
-1; i
++ ){
162 ray_hit
*s0
= &samples
[i
],
165 float a
= v3_dot( s0
->normal
, s1
->normal
);
167 if( (a
< min_a
) && (a
>= -0.1f
) && (s0
->normal
[1]>s1
->normal
[1]) ){
177 ray_hit
*s0
= candidate
,
180 vg_line( s0
->pos
, s1
->pos
, VG__WHITE
);
182 v3_copy( s0
->normal
, pa
);
183 v3_copy( s1
->normal
, pb
);
184 v3_cross( localplayer
.rb
.to_world
[1], dir
, pc
);
187 pa
[3] = v3_dot( pa
, s0
->pos
);
188 pb
[3] = v3_dot( pb
, s1
->pos
);
189 pc
[3] = v3_dot( pc
, localplayer
.rb
.co
);
192 if( plane_intersect3( pa
, pb
, pc
, edge
) ){
193 v3_copy( edge
, player_walk
.state
.drop_in_target
);
194 v3_copy( s1
->normal
, player_walk
.state
.drop_in_normal
);
195 v3_copy( localplayer
.rb
.co
, player_walk
.state
.drop_in_start
);
197 player_walk
.state
.drop_in_start_angle
= player_get_heading_yaw();
198 player_walk
.state
.drop_in_angle
=
199 atan2f( player_walk
.state
.drop_in_normal
[0],
200 player_walk
.state
.drop_in_normal
[2] );
202 /* TODO: scan multiple of these? */
205 player_walk_drop_in_overhang_transform( 1.0f
, oco
, oq
);
207 v3f va
= {0.0f
,0.0f
,-k_board_length
- 0.3f
},
208 vb
= {0.0f
,0.0f
, k_board_length
+ 0.3f
};
210 q_mulv( oq
, va
, va
);
211 q_mulv( oq
, vb
, vb
);
212 v3_add( oco
, va
, va
);
213 v3_add( oco
, vb
, vb
);
216 v3_sub( vb
, va
, v0
);
220 ray
.dist
= k_board_length
*2.0f
+ 0.6f
;
222 if( ray_world( world
, va
, v0
, &ray
, 0 ) ){
223 vg_line( va
, vb
, VG__RED
);
224 vg_line_point( ray
.pos
, 0.1f
, VG__RED
);
225 vg_error( "invalidated\n" );
229 v3_muls( v0
, -1.0f
, v0
);
230 if( ray_world( world
, vb
, v0
, &ray
, 0 ) ){
231 vg_line( va
, vb
, VG__RED
);
232 vg_line_point( ray
.pos
, 0.1f
, VG__RED
);
233 vg_error( "invalidated\n" );
237 player_walk_drop_in_vector( localplayer
.rb
.v
);
241 vg_error( "failed to find intersection of drop in\n" );
248 static bool player__preupdate_anim( struct skeleton_anim
*anim
, f32
*t
,
250 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
;
251 *t
+= (vg
.time_delta
* speed
) / length
;
253 if( *t
>= 1.0f
) return 1;
257 static void player_walk_pre_sit(void){
258 struct player_walk
*w
= &player_walk
;
261 joystick_state( k_srjoystick_steer
, steer
);
263 vg_slewf( &w
->state
.transition_t
, 1.0f
, vg
.time_delta
);
265 if( button_down(k_srbind_sit
) || (v2_length2(steer
)>0.2f
) ||
266 button_down(k_srbind_jump
) ){
267 w
->state
.activity
= k_walk_activity_sit_up
;
272 static void player_walk_pre_sit_up(void){
273 struct player_walk
*w
= &player_walk
;
275 if( w
->state
.transition_t
> 0.0f
)
276 vg_slewf( &w
->state
.transition_t
, 0.0f
, vg
.time_delta
);
278 w
->state
.activity
= k_walk_activity_ground
;
280 if( button_down(k_srbind_sit
) )
281 w
->state
.activity
= k_walk_activity_sit
;
286 static void player_walk_pre_ground(void){
287 struct player_walk
*w
= &player_walk
;
289 if( button_down(k_srbind_sit
) ){
290 v3_zero( localplayer
.rb
.v
);
291 w
->state
.activity
= k_walk_activity_sit
;
292 w
->state
.transition_t
= 0.0f
;
296 if( button_down( k_srbind_use
) ){
297 if( player_walk_scan_for_drop_in() ){
298 w
->state
.activity
= k_walk_activity_odrop_in
;
301 w
->state
.activity
= k_walk_activity_oregular
;
304 w
->state
.transition_t
= 0.0f
;
307 if( button_down( k_srbind_jump
) ){
308 w
->state
.jump_queued
= 1;
309 w
->state
.jump_input_time
= vg
.time
;
313 static void player_walk_pre_air(void){
314 struct player_walk
*w
= &player_walk
;
315 if( button_down( k_srbind_use
) ){
316 w
->state
.activity
= k_walk_activity_oair
;
317 w
->state
.transition_t
= 0.0f
;
320 if( button_down( k_srbind_jump
) ){
321 w
->state
.jump_queued
= 1;
322 w
->state
.jump_input_time
= vg
.time
;
326 static void player_walk_pre_drop_in(void){
327 struct player_walk
*w
= &player_walk
;
328 bool finished
= player__preupdate_anim( w
->anim_drop_in
,
329 &w
->state
.transition_t
, 1.0f
);
331 player_walk_drop_in_to_skate();
334 static void player_walk_pre_caveman(void){
335 struct player_walk
*w
= &player_walk
;
336 bool finished
= player__preupdate_anim( w
->anim_jump_to_air
,
337 &w
->state
.transition_t
, 1.0f
);
339 player_walk_generic_to_skate( k_skate_activity_air
,
340 player_walk
.animator
.board_yaw
);
344 static void player_walk_pre_running_start(void){
345 struct player_walk
*w
= &player_walk
;
346 bool finished
= player__preupdate_anim( w
->anim_intro
,
347 &w
->state
.transition_t
, 1.0f
);
349 /* TODO: get the derivative of the last keyframes to calculate new
350 * velocity for player */
351 player_walk_generic_to_skate( k_skate_activity_ground
,
352 player_walk
.animator
.board_yaw
+1.0f
);
356 static void player_walk_pre_popoff(void){
357 struct player_walk
*w
= &player_walk
;
358 bool finished
= player__preupdate_anim( w
->anim_popoff
,
359 &w
->state
.transition_t
, 1.0f
);
362 w
->state
.activity
= k_walk_activity_ground
;
363 w
->animator
.board_yaw
+= 1.0f
;
367 void player__walk_pre_update(void){
368 struct player_walk
*w
= &player_walk
;
370 if( localplayer
.immobile
) return;
371 else player_look( localplayer
.angles
, skaterift
.time_rate
);
373 enum walk_activity a
= w
->state
.activity
;
375 if ( a
== k_walk_activity_sit
) player_walk_pre_sit();
376 else if( a
== k_walk_activity_sit_up
) player_walk_pre_sit_up();
377 else if( a
== k_walk_activity_ground
) player_walk_pre_ground();
378 else if( a
== k_walk_activity_air
) player_walk_pre_air();
379 else if( a
== k_walk_activity_odrop_in
) player_walk_pre_drop_in();
380 else if( a
== k_walk_activity_oair
) player_walk_pre_caveman();
381 else if( a
== k_walk_activity_oregular
) player_walk_pre_running_start();
382 else if( a
== k_walk_activity_ipopoff
) player_walk_pre_popoff();
385 static int player_walk_normal_standable( v3f n
){
386 return n
[1] > 0.70710678118f
;
389 static void player_accelerate( v3f v
, v3f movedir
, f32 speed
, f32 accel
){
390 float currentspeed
= v3_dot( v
, movedir
),
391 addspeed
= speed
- currentspeed
;
396 float accelspeed
= accel
* vg
.time_fixed_delta
* speed
;
398 if( accelspeed
> addspeed
)
399 accelspeed
= addspeed
;
401 v3_muladds( v
, movedir
, accelspeed
, v
);
404 static void player_friction( v3f v
, f32 friction
){
405 float speed
= v3_length( v
),
407 control
= vg_maxf( speed
, k_stopspeed
);
412 drop
+= control
* friction
* vg
.time_fixed_delta
;
414 float newspeed
= vg_maxf( 0.0f
, speed
- drop
);
417 v3_muls( v
, newspeed
, v
);
420 static void player_walk_custom_filter( world_instance
*world
,
421 rb_ct
*man
, int len
, f32 w
){
422 for( int i
=0; i
<len
; i
++ ){
424 if( ci
->type
== k_contact_type_disabled
||
425 ci
->type
== k_contact_type_edge
)
429 float d1
= v3_dot( ci
->co
, ci
->n
);
431 for( int j
=0; j
<len
; j
++ ){
436 if( cj
->type
== k_contact_type_disabled
)
439 struct world_surface
*si
= world_contact_surface( world
, ci
),
440 *sj
= world_contact_surface( world
, cj
);
442 if( (sj
->info
.flags
& k_material_flag_walking
) &&
443 !(si
->info
.flags
& k_material_flag_walking
)){
447 float d2
= v3_dot( cj
->co
, ci
->n
),
450 if( fabsf( d
) <= w
){
451 cj
->type
= k_contact_type_disabled
;
457 static void player_walk_update_generic(void){
458 struct player_walk
*w
= &player_walk
;
460 if( (w
->state
.activity
!= k_walk_activity_oregular
) &&
461 (w
->state
.activity
!= k_walk_activity_oair
) ){
462 joystick_state( k_srjoystick_steer
, w
->state
.steer
);
463 w
->state
.steer
[2] = button_press(k_srbind_run
)? k_runspeed
: k_walkspeed
;
464 if( v2_length2(w
->state
.steer
)>1.0f
)
465 v2_normalize(w
->state
.steer
);
468 v3_copy( localplayer
.rb
.co
, w
->state
.prev_pos
);
469 v3_zero( localplayer
.rb
.w
);
471 world_instance
*world
= world_current_instance();
472 if( !world_water_player_safe( world
, 0.4f
) ) return;
474 enum walk_activity prev_state
= w
->state
.activity
;
476 w
->collider
.h
= 2.0f
;
477 w
->collider
.r
= 0.3f
;
480 m3x3_copy( localplayer
.rb
.to_world
, mtx
);
481 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
483 vg_line_capsule( mtx
, w
->collider
.r
, w
->collider
.h
, VG__WHITE
);
488 float yaw
= localplayer
.angles
[0];
490 v3f forward_dir
= { -sinf(yaw
), 0.0f
, cosf(yaw
) };
491 v3f right_dir
= { forward_dir
[2], 0.0f
, -forward_dir
[0] };
494 * Collision detection
497 len
= rb_capsule__scene( mtx
, &w
->collider
, NULL
,
498 world
->geo_bh
, manifold
, 0 );
499 player_walk_custom_filter( world
, manifold
, len
, 0.01f
);
500 len
= rb_manifold_apply_filtered( manifold
, len
);
502 v3f surface_avg
= { 0.0f
, 0.0f
, 0.0f
};
504 w
->state
.activity
= k_walk_activity_air
;
505 w
->surface
= k_surface_prop_concrete
;
507 for( int i
=0; i
<len
; i
++ ){
508 rb_ct
*ct
= &manifold
[i
];
509 rb_debug_contact( ct
);
511 if( player_walk_normal_standable( ct
->n
) ){
512 w
->state
.activity
= k_walk_activity_ground
;
514 v3_add( surface_avg
, ct
->n
, surface_avg
);
516 struct world_surface
*surf
= world_contact_surface( world
, ct
);
517 if( surf
->info
.surface_prop
> w
->surface
)
518 w
->surface
= surf
->info
.surface_prop
;
521 rb_prepare_contact( ct
, vg
.time_fixed_delta
);
527 float accel_speed
= 0.0f
, nominal_speed
= 0.0f
;
530 v3_muls( right_dir
, w
->state
.steer
[0], movedir
);
531 v3_muladds( movedir
, forward_dir
, w
->state
.steer
[1], movedir
);
533 if( w
->state
.activity
== k_walk_activity_ground
){
534 v3_normalize( surface_avg
);
537 v3_tangent_basis( surface_avg
, tx
, ty
);
539 if( v2_length2(w
->state
.steer
) > 0.001f
){
540 /* clip movement to the surface */
541 float d
= v3_dot(surface_avg
,movedir
);
542 v3_muladds( movedir
, surface_avg
, -d
, movedir
);
545 accel_speed
= k_walk_accel
;
546 nominal_speed
= w
->state
.steer
[2];
549 if( w
->state
.jump_queued
){
550 w
->state
.jump_queued
= 0;
552 f32 t
= vg
.time
- w
->state
.jump_input_time
;
553 if( t
< PLAYER_JUMP_EPSILON
){
554 localplayer
.rb
.v
[1] = 5.0f
;
555 w
->state
.activity
= k_walk_activity_air
;
556 prev_state
= k_walk_activity_air
;
557 accel_speed
= k_walk_air_accel
;
558 nominal_speed
= k_airspeed
;
562 player_friction( localplayer
.rb
.v
, k_walk_friction
);
566 accel_speed
= k_walk_air_accel
;
567 nominal_speed
= k_airspeed
;
570 if( v2_length2( w
->state
.steer
) > 0.001f
){
571 player_accelerate( localplayer
.rb
.v
, movedir
,
572 nominal_speed
, accel_speed
);
573 v3_normalize( movedir
);
577 * Resolve velocity constraints
579 for( int j
=0; j
<5; j
++ ){
580 for( int i
=0; i
<len
; i
++ ){
581 rb_ct
*ct
= &manifold
[i
];
584 float vn
= -v3_dot( localplayer
.rb
.v
, ct
->n
);
586 float temp
= ct
->norm_impulse
;
587 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
588 vn
= ct
->norm_impulse
- temp
;
590 v3_muladds( localplayer
.rb
.v
, ct
->n
, vn
, localplayer
.rb
.v
);
595 if( w
->state
.activity
== k_walk_activity_ground
||
596 prev_state
== k_walk_activity_ground
){
597 float max_dist
= 0.4f
;
600 v3_copy( localplayer
.rb
.co
, pa
);
601 pa
[1] += w
->collider
.r
+ max_dist
;
602 v3_add( pa
, (v3f
){0, -max_dist
* 2.0f
, 0}, pb
);
603 vg_line( pa
, pb
, 0xff000000 );
607 if( spherecast_world( world
, pa
, pb
,
608 w
->collider
.r
, &t
, n
, 0 ) != -1 ){
609 if( player_walk_normal_standable(n
) ){
610 v3_lerp( pa
, pb
, t
, localplayer
.rb
.co
);
611 localplayer
.rb
.co
[1] += -w
->collider
.r
- k_penetration_slop
;
612 w
->state
.activity
= k_walk_activity_ground
;
614 float d
= -v3_dot(n
,localplayer
.rb
.v
);
615 v3_muladds( localplayer
.rb
.v
, n
, d
, localplayer
.rb
.v
);
616 localplayer
.rb
.v
[1] += -k_gravity
* vg
.time_fixed_delta
;
625 rb_depenetrate( manifold
, len
, dt
);
626 v3_add( dt
, localplayer
.rb
.co
, localplayer
.rb
.co
);
629 if( w
->state
.activity
== k_walk_activity_air
){
630 localplayer
.rb
.v
[1] += -k_gravity
*vg
.time_fixed_delta
;
633 if( localplayer
.immobile
){
634 localplayer
.rb
.v
[0] = 0.0f
;
635 localplayer
.rb
.v
[2] = 0.0f
;
638 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.v
, vg
.time_fixed_delta
,
640 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
641 vg_line_capsule( mtx
, w
->collider
.r
, w
->collider
.h
, VG__GREEN
);
645 * ---------------------------------------------------
650 lwr_offs
= { 0.0f
, w
->collider
.r
, 0.0f
};
652 v3_add( lwr_offs
, w
->state
.prev_pos
, lwr_prev
);
653 v3_add( lwr_offs
, localplayer
.rb
.co
, lwr_now
);
656 v3_sub( localplayer
.rb
.co
, w
->state
.prev_pos
, movedelta
);
658 float movedist
= v3_length( movedelta
);
660 if( movedist
> 0.3f
){
661 float t
, sr
= w
->collider
.r
-0.04f
;
664 if( spherecast_world( world
, lwr_prev
, lwr_now
, sr
, &t
, n
, 0 ) != -1 ){
665 v3_lerp( lwr_prev
, lwr_now
, vg_maxf(0.01f
,t
), localplayer
.rb
.co
);
666 localplayer
.rb
.co
[1] -= w
->collider
.r
;
667 rb_update_matrices( &localplayer
.rb
);
668 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
669 vg_line_capsule( mtx
, w
->collider
.r
, w
->collider
.h
, VG__RED
);
673 u32 id
= world_intersect_gates(world
, localplayer
.rb
.co
, w
->state
.prev_pos
);
675 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, mdl_entity_id_id(id
) );
676 m4x3_mulv( gate
->transport
, localplayer
.rb
.co
, localplayer
.rb
.co
);
677 m3x3_mulv( gate
->transport
, localplayer
.rb
.v
, localplayer
.rb
.v
);
679 v4f transport_rotation
;
680 m3x3_q( gate
->transport
, transport_rotation
);
681 q_mul( transport_rotation
, localplayer
.rb
.q
, localplayer
.rb
.q
);
682 q_normalize( localplayer
.rb
.q
);
683 rb_update_matrices( &localplayer
.rb
);
684 player__pass_gate( id
);
686 rb_update_matrices( &localplayer
.rb
);
688 if( (prev_state
== k_walk_activity_oregular
) ||
689 (prev_state
== k_walk_activity_oair
) ||
690 (prev_state
== k_walk_activity_ipopoff
) ){
691 w
->state
.activity
= prev_state
;
694 w
->move_speed
= vg_minf( v2_length( (v2f
){ localplayer
.rb
.v
[0],
695 localplayer
.rb
.v
[2] } ),
699 void player__walk_post_update(void){
700 struct player_walk
*w
= &player_walk
;
703 m3x3_copy( localplayer
.rb
.to_world
, mtx
);
704 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
706 float substep
= vg
.time_fixed_extrapolate
;
707 v3_muladds( mtx
[3], localplayer
.rb
.v
, vg
.time_fixed_delta
*substep
, mtx
[3] );
708 vg_line_capsule( mtx
, w
->collider
.r
, w
->collider
.h
, VG__YELOW
);
710 /* Calculate header */
712 if( (player_xyspeed2() > 0.1f
*0.1f
) ){
714 if( (w
->state
.activity
== k_walk_activity_ground
) ||
715 (w
->state
.activity
== k_walk_activity_ipopoff
) ||
716 (w
->state
.activity
== k_walk_activity_oregular
) ){
720 f32 ta
= atan2f( localplayer
.rb
.v
[0], localplayer
.rb
.v
[2] );
722 q_axis_angle( qt
, (v3f
){0,1,0}, ta
);
723 q_nlerp( localplayer
.rb
.q
, qt
, vg
.time_delta
/r
, localplayer
.rb
.q
);
726 vg_line_point( w
->state
.drop_in_target
, 0.1f
, VG__GREEN
);
728 v3_muladds( w
->state
.drop_in_target
, w
->state
.drop_in_normal
, 0.3f
, p1
);
729 vg_line( w
->state
.drop_in_target
, p1
, VG__GREEN
);
730 v3_muladds( w
->state
.drop_in_target
, localplayer
.rb
.to_world
[1], 0.3f
, p1
);
731 vg_line( w
->state
.drop_in_target
, p1
, VG__GREEN
);
733 float a
= player_get_heading_yaw();
738 v3_add( localplayer
.rb
.co
, p1
, p1
);
739 vg_line( localplayer
.rb
.co
, p1
, VG__PINK
);
742 if( vg_fractf(w
->state
.walk_timer
) > 0.5f
)
747 if( (w
->state
.step_phase
!= walk_phase
) &&
748 (w
->state
.activity
== k_walk_activity_ground
) )
751 if( w
->surface
== k_surface_prop_concrete
){
753 &audio_footsteps
[vg_randu32(&vg
.rand
) % 4],
754 localplayer
.rb
.co
, 40.0f
, 1.0f
757 else if( w
->surface
== k_surface_prop_grass
){
759 &audio_footsteps_grass
[ vg_randu32(&vg
.rand
) % 6 ],
760 localplayer
.rb
.co
, 40.0f
, 1.0f
763 else if( w
->surface
== k_surface_prop_wood
){
765 &audio_footsteps_wood
[ vg_randu32(&vg
.rand
) % 6 ],
766 localplayer
.rb
.co
, 40.0f
, 1.0f
772 w
->state
.step_phase
= walk_phase
;
775 void player__walk_update(void){
776 struct player_walk
*w
= &player_walk
;
778 if( (w
->state
.activity
== k_walk_activity_air
) ||
779 (w
->state
.activity
== k_walk_activity_ground
) ||
780 (w
->state
.activity
== k_walk_activity_oair
) ||
781 (w
->state
.activity
== k_walk_activity_oregular
) ||
782 (w
->state
.activity
== k_walk_activity_ipopoff
) ){
783 player_walk_update_generic();
787 static void player_walk_animate_drop_in(void){
788 struct player_walk
*w
= &player_walk
;
789 struct player_walk_animator
*animator
= &w
->animator
;
790 struct skeleton_anim
*anim
= w
->anim_drop_in
;
792 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
,
793 time
= w
->state
.transition_t
;
795 f32 walk_yaw
= vg_alerpf( w
->state
.drop_in_start_angle
,
796 w
->state
.drop_in_angle
, animator
->transition_t
);
797 v3_lerp( w
->state
.drop_in_start
, w
->state
.drop_in_target
,
798 animator
->transition_t
, localplayer
.rb
.co
);
800 q_axis_angle( localplayer
.rb
.q
, (v3f
){0,1,0}, walk_yaw
+ VG_PIf
);
802 /* the drop in bit */
805 player_walk_drop_in_overhang_transform( animator
->transition_t
,
808 q_mul( final_q
, localplayer
.rb
.q
, localplayer
.rb
.q
);
809 v3_lerp( localplayer
.rb
.co
, final_co
, animator
->transition_t
,
812 rb_update_matrices( &localplayer
.rb
);
814 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.to_world
[1],
815 -0.1f
*animator
->transition_t
, localplayer
.rb
.co
);
817 v3_copy( localplayer
.rb
.co
, animator
->root_co
);
818 v4_copy( localplayer
.rb
.q
, animator
->root_q
);
820 /* for the camera purposes only */
822 player_walk_drop_in_vector( init_velocity
);
823 v3_muls( init_velocity
, animator
->transition_t
, localplayer
.rb
.v
);
824 v3_copy( localplayer
.rb
.v
,
825 localplayer
.cam_control
.cam_velocity_smooth
);
828 static void player_walk_animate_generic(void){
829 struct player_walk
*w
= &player_walk
;
830 struct player_walk_animator
*animator
= &w
->animator
;
833 rb_extrapolate( &localplayer
.rb
, animator
->root_co
, _null
);
835 f32 walk_yaw
= player_get_heading_yaw(),
836 head_yaw
= localplayer
.angles
[0] + VG_PIf
,
837 y
= vg_angle_diff( head_yaw
, -walk_yaw
),
838 p
= vg_clampf( localplayer
.angles
[1],
839 -k_sit_pitch_limit
, k_sit_pitch_limit
);
841 if( fabsf(y
) > k_sit_yaw_limit
){
846 animator
->yaw
= vg_lerpf( animator
->yaw
, y
, vg
.time_delta
*2.0f
);
847 animator
->pitch
= vg_lerpf( animator
->pitch
, p
, vg
.time_delta
*2.8f
);
848 q_axis_angle( animator
->root_q
, (v3f
){0,1,0}, walk_yaw
+ VG_PIf
);
851 q_axis_angle( qrev
, (v3f
){0,1,0}, VG_TAUf
*0.5f
);
852 q_mul( localplayer
.rb
.q
, qrev
, animator
->root_q
);
855 void player__walk_animate(void){
856 struct player_walk
*w
= &player_walk
;
857 player_pose
*pose
= &localplayer
.pose
;
858 struct player_walk_animator
*animator
= &w
->animator
;
860 animator
->activity
= w
->state
.activity
;
861 animator
->transition_t
= w
->state
.transition_t
;
864 f32 fly
= (w
->state
.activity
== k_walk_activity_air
)? 1.0f
: 0.0f
,
867 if( w
->state
.activity
== k_walk_activity_air
) rate
= 2.4f
;
870 animator
->fly
= vg_lerpf( animator
->fly
, fly
, rate
*vg
.time_delta
);
871 animator
->run
= vg_lerpf( animator
->run
, w
->move_speed
,
875 if( animator
->run
> 0.025f
){
876 f32 walk_norm
= 30.0f
/(float)w
->anim_walk
->length
,
877 run_norm
= 30.0f
/(float)w
->anim_run
->length
,
880 if( animator
->run
<= k_walkspeed
)
881 l
= (animator
->run
/ k_walkspeed
) * walk_norm
;
883 l
= vg_lerpf( walk_norm
, run_norm
,
884 (animator
->run
-k_walkspeed
) / (k_runspeed
-k_walkspeed
) );
886 w
->state
.walk_timer
+= l
* vg
.time_delta
;
889 w
->state
.walk_timer
= 0.0f
;
891 animator
->walk_timer
= w
->state
.walk_timer
;
893 player_walk_animate_generic();
894 if( w
->state
.activity
== k_walk_activity_odrop_in
){
895 player_walk_animate_drop_in();
898 if( (w
->state
.activity
== k_walk_activity_odrop_in
) ||
899 (w
->state
.activity
== k_walk_activity_oregular
) ||
900 (w
->state
.activity
== k_walk_activity_oair
) ){
901 localplayer
.cam_velocity_influence
= w
->animator
.transition_t
;
903 else if( w
->state
.activity
== k_walk_activity_ipopoff
){
904 localplayer
.cam_velocity_influence
= 1.0f
-w
->animator
.transition_t
;
907 localplayer
.cam_velocity_influence
= 0.0f
;
909 if( w
->state
.activity
== k_walk_activity_sit
){
910 localplayer
.cam_dist
= 3.8f
;
913 localplayer
.cam_dist
= 1.8f
;
917 static void player_walk_pose_sit( struct player_walk_animator
*animator
,
919 mdl_keyframe bpose
[32];
921 struct player_walk
*w
= &player_walk
;
922 struct skeleton
*sk
= &localplayer
.skeleton
;
924 f32 t
= animator
->transition_t
,
925 st
= t
* ((f32
)(w
->anim_sit
->length
-1)/30.0f
);
926 skeleton_sample_anim( sk
, w
->anim_sit
, st
, bpose
);
929 f32
*qh
= bpose
[localplayer
.id_head
-1].q
;
930 q_axis_angle( qy
, (v3f
){0,1,0}, animator
->yaw
*0.5f
*t
);
931 q_axis_angle( qp
, (v3f
){0,0,1}, animator
->pitch
*t
);
936 qh
= bpose
[localplayer
.id_chest
-1].q
;
937 q_axis_angle( qy
, (v3f
){0,1,0}, animator
->yaw
*0.5f
*t
);
941 skeleton_lerp_pose( sk
, pose
->keyframes
, bpose
,
942 vg_minf(1.0f
,t
*10.0f
), pose
->keyframes
);
945 enum walk_transition_type
{
946 k_walk_transition_in
,
947 k_walk_transition_out
,
948 k_walk_transition_outin
,
951 static void player_walk_pose_transition(
952 struct player_walk_animator
*animator
, struct skeleton_anim
*anim
,
953 enum walk_transition_type type
,
954 mdl_keyframe apose
[32], f32
*mask
, player_pose
*pose
){
956 mdl_keyframe bpose
[32];
958 struct player_walk
*w
= &player_walk
;
959 struct skeleton
*sk
= &localplayer
.skeleton
;
961 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
,
962 t
= animator
->transition_t
* length
,
965 if( type
== k_walk_transition_in
|| type
== k_walk_transition_outin
)
966 blend
= vg_minf( blend
, length
-t
);
968 if( type
== k_walk_transition_out
|| type
== k_walk_transition_outin
)
969 blend
= vg_minf( blend
, t
);
971 blend
= vg_smoothstepf( vg_minf(1,blend
/k_anim_transition
) );
973 skeleton_sample_anim_clamped( sk
, anim
, t
, bpose
);
975 mdl_keyframe
*kf_board
= &bpose
[localplayer
.id_board
-1];
976 f32 yaw
= animator
->board_yaw
* VG_TAUf
* 0.5f
;
979 q_axis_angle( qyaw
, (v3f
){0,1,0}, yaw
);
980 q_mul( kf_board
->q
, qyaw
, kf_board
->q
);
981 q_normalize( kf_board
->q
);
984 for( i32 i
=0; i
<sk
->bone_count
-1; i
++ )
985 keyframe_lerp( apose
+i
, bpose
+i
, blend
*mask
[i
], pose
->keyframes
+i
);
988 skeleton_lerp_pose( sk
, apose
, bpose
, blend
, pose
->keyframes
);
991 void player__walk_pose( void *_animator
, player_pose
*pose
){
992 struct player_walk
*w
= &player_walk
;
993 struct player_walk_animator
*animator
= _animator
;
994 struct skeleton
*sk
= &localplayer
.skeleton
;
996 v3_copy( animator
->root_co
, pose
->root_co
);
997 v4_copy( animator
->root_q
, pose
->root_q
);
998 pose
->board
.lean
= 0.0f
;
999 pose
->type
= k_player_pose_type_ik
;
1001 float walk_norm
= (float)w
->anim_walk
->length
/30.0f
,
1002 run_norm
= (float)w
->anim_run
->length
/30.0f
,
1003 t
= animator
->walk_timer
;
1006 mdl_keyframe apose
[32], bpose
[32];
1007 if( animator
->run
<= k_walkspeed
){
1009 f32 l
= vg_minf( 1, (animator
->run
/k_walkspeed
)*6.0f
);
1010 skeleton_sample_anim( sk
, w
->anim_idle
, vg
.time
*0.1f
, apose
);
1011 skeleton_sample_anim( sk
, w
->anim_walk
, t
*walk_norm
, bpose
);
1012 skeleton_lerp_pose( sk
, apose
, bpose
, l
, apose
);
1016 f32 l
= (animator
->run
-k_walkspeed
) / (k_runspeed
-k_walkspeed
);
1017 skeleton_sample_anim( sk
, w
->anim_walk
, t
*walk_norm
, apose
);
1018 skeleton_sample_anim( sk
, w
->anim_run
, t
*run_norm
, bpose
);
1019 skeleton_lerp_pose( sk
, apose
, bpose
, l
, apose
);
1023 skeleton_sample_anim( sk
, w
->anim_jump
, vg
.time
*0.6f
, bpose
);
1024 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->fly
, apose
);
1026 mdl_keyframe
*kf_board
= &apose
[localplayer
.id_board
-1];
1027 f32 yaw
= animator
->board_yaw
;
1029 if( animator
->activity
== k_walk_activity_ipopoff
)
1030 if( animator
->transition_t
> 0.5f
)
1034 q_axis_angle( qyaw
, (v3f
){0,1,0}, yaw
* VG_TAUf
* 0.5f
);
1035 q_mul( kf_board
->q
, qyaw
, kf_board
->q
);
1036 q_normalize( kf_board
->q
);
1039 if( (animator
->activity
== k_walk_activity_sit
) ||
1040 (animator
->activity
== k_walk_activity_sit_up
) ){
1041 player_walk_pose_sit( animator
, pose
);
1043 else if( animator
->activity
== k_walk_activity_odrop_in
){
1044 player_walk_pose_transition(
1045 animator
, w
->anim_drop_in
, k_walk_transition_out
, apose
,
1048 else if( animator
->activity
== k_walk_activity_oair
){
1049 player_walk_pose_transition(
1050 animator
, w
->anim_jump_to_air
, k_walk_transition_out
, apose
,
1053 else if( animator
->activity
== k_walk_activity_oregular
){
1054 player_walk_pose_transition(
1055 animator
, w
->anim_intro
, k_walk_transition_out
, apose
,
1058 else if( animator
->activity
== k_walk_activity_ipopoff
){
1059 if( animator
->run
> 0.2f
){
1060 f32 t
= 1.0f
-vg_minf( animator
->run
-0.2f
, 1.0f
),
1063 for( u32 i
=0; i
<32; i
++ )
1066 mask
[ localplayer
.id_ik_foot_l
-1 ] = t
;
1067 mask
[ localplayer
.id_ik_foot_r
-1 ] = t
;
1068 mask
[ localplayer
.id_ik_knee_l
-1 ] = t
;
1069 mask
[ localplayer
.id_ik_knee_r
-1 ] = t
;
1070 mask
[ localplayer
.id_hip
-1 ] = t
;
1071 player_walk_pose_transition(
1072 animator
, w
->anim_popoff
, k_walk_transition_in
, apose
,
1076 player_walk_pose_transition(
1077 animator
, w
->anim_popoff
, k_walk_transition_in
, apose
,
1082 skeleton_copy_pose( sk
, apose
, pose
->keyframes
);
1086 void player__walk_post_animate(void){
1090 struct player_walk
*w
= &player_walk
;
1094 void player__walk_im_gui(void){
1095 struct player_walk
*w
= &player_walk
;
1096 player__debugtext( 1, "V: %5.2f %5.2f %5.2f (%5.2fm/s)",
1097 localplayer
.rb
.v
[0], localplayer
.rb
.v
[1], localplayer
.rb
.v
[2],
1098 v3_length(localplayer
.rb
.v
) );
1099 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",localplayer
.rb
.co
[0],
1100 localplayer
.rb
.co
[1],
1101 localplayer
.rb
.co
[2] );
1102 player__debugtext( 1, "transition: %5.2f ", w
->state
.transition_t
);
1103 player__debugtext( 1, "activity: %s\n",
1104 (const char *[]){ "air",
1113 [w
->state
.activity
] );
1114 player__debugtext( 1, "surface: %s\n",
1115 (const char *[]){ "concrete",
1125 void player__walk_bind(void){
1126 struct player_walk
*w
= &player_walk
;
1127 struct skeleton
*sk
= &localplayer
.skeleton
;
1129 w
->anim_idle
= skeleton_get_anim( sk
, "idle_cycle+y" );
1130 w
->anim_walk
= skeleton_get_anim( sk
, "walk+y" );
1131 w
->anim_run
= skeleton_get_anim( sk
, "run+y" );
1132 w
->anim_jump
= skeleton_get_anim( sk
, "jump+y" );
1133 w
->anim_jump_to_air
= skeleton_get_anim( sk
, "jump_to_air" );
1134 w
->anim_drop_in
= skeleton_get_anim( sk
, "drop_in" );
1135 w
->anim_intro
= skeleton_get_anim( sk
, "into_skate" );
1136 w
->anim_sit
= skeleton_get_anim( sk
, "sit" );
1137 w
->anim_popoff
= skeleton_get_anim( sk
, "pop_off_short" );
1140 void player__walk_transition( bool grounded
, f32 board_yaw
){
1141 struct player_walk
*w
= &player_walk
;
1142 w
->state
.activity
= k_walk_activity_air
;
1145 w
->state
.activity
= k_walk_activity_ipopoff
;
1148 w
->state
.transition_t
= 0.0f
;
1149 w
->state
.jump_queued
= 0;
1150 w
->state
.jump_input_time
= 0.0;
1151 w
->state
.walk_timer
= 0.0f
;
1152 w
->state
.step_phase
= 0;
1153 w
->animator
.board_yaw
= fmodf( board_yaw
, 2.0f
);
1154 rb_update_matrices( &localplayer
.rb
);
1157 void player__walk_reset(void)
1159 struct player_walk
*w
= &player_walk
;
1160 w
->state
.activity
= k_walk_activity_air
;
1161 w
->state
.transition_t
= 0.0f
;
1163 v3f fwd
= { 0.0f
, 0.0f
, 1.0f
};
1164 q_mulv( localplayer
.rb
.q
, fwd
, fwd
);
1165 q_axis_angle( localplayer
.rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
},
1166 atan2f(fwd
[0], fwd
[2]) );
1168 rb_update_matrices( &localplayer
.rb
);
1171 void player__walk_animator_exchange( bitpack_ctx
*ctx
, void *data
){
1172 struct player_walk_animator
*animator
= data
;
1174 bitpack_qv3f( ctx
, 24, -1024.0f
, 1024.0f
, animator
->root_co
);
1175 bitpack_qquat( ctx
, animator
->root_q
);
1176 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &animator
->fly
);
1177 bitpack_qf32( ctx
, 8, 0.0f
, k_runspeed
, &animator
->run
);
1178 bitpack_qf32( ctx
, 16, 0.0f
, 120.0f
, &animator
->walk_timer
);
1180 for( int i
=0; i
<1; i
++ ){ /* without this you get a warning from gcc. lol */
1181 bitpack_bytes( ctx
, 8, &animator
->activity
);
1184 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &animator
->transition_t
);
1186 if( (animator
->activity
== k_walk_activity_sit
) ||
1187 (animator
->activity
== k_walk_activity_sit_up
) ){
1188 bitpack_qf32( ctx
, 8, -k_sit_yaw_limit
, k_sit_yaw_limit
, &animator
->yaw
);
1189 bitpack_qf32( ctx
, 8, -k_sit_pitch_limit
, k_sit_pitch_limit
,
1193 bitpack_qf32( ctx
, 16, -100.0f
, 100.0f
, &animator
->board_yaw
);
1196 void player__walk_sfx_oneshot( u8 id
, v3f pos
, f32 volume
)
1200 if( id
== k_player_walk_soundeffect_splash
){
1201 audio_oneshot_3d( &audio_splash
, pos
, 40.0f
, 1.0f
);