3 #include "vg/vg_rigidbody_collision.h"
4 #include "scene_rigidbody.h"
10 static void player_walk_drop_in_vector( v3f vec
){
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
);
18 static float player_xyspeed2(void){
19 return v3_length2( (v3f
){localplayer
.rb
.v
[0], 0.0f
, localplayer
.rb
.v
[2]} );
22 static void player_walk_generic_to_skate( enum skate_activity init
, f32 yaw
){
23 localplayer
.subsystem
= k_player_subsystem_skate
;
27 if( player_xyspeed2() < 0.1f
* 0.1f
)
28 q_mulv( localplayer
.rb
.q
, (v3f
){0.0f
,0.0f
,1.6f
}, v
);
30 v3_copy( localplayer
.rb
.v
, v
);
32 player_skate
.state
.activity_prev
= k_skate_activity_ground
;
33 player_skate
.state
.activity
= init
;
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
);
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
);
46 v3_copy( v
, player_skate
.state
.cog_v
);
47 v3_copy( v
, localplayer
.rb
.v
);
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
);
55 if( init
== k_skate_activity_air
)
56 player__approximate_best_trajectory();
59 static void player_walk_drop_in_to_skate(void){
60 localplayer
.immobile
= 0;
61 localplayer
.subsystem
= k_player_subsystem_skate
;
63 player_skate
.state
.activity_prev
= k_skate_activity_ground
;
64 player_skate
.state
.activity
= k_skate_activity_ground
;
66 player__begin_holdout( (v3f
){0,0,0} );
67 player__skate_clear_mechanics();
68 player__skate_reset_animator();
71 player_walk_drop_in_vector( init_velocity
);
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
);
83 static void player_walk_drop_in_overhang_transform( f32 t
, v3f co
, v4f q
){
85 v3_cross( (v3f
){0,1,0}, player_walk
.state
.drop_in_normal
, axis
);
88 float a
= acosf( player_walk
.state
.drop_in_normal
[1] ) * t
;
89 q_axis_angle( q
, axis
, a
);
92 heading_angle
= player_walk
.state
.drop_in_angle
;
95 overhang
[0] = sinf( heading_angle
) * l
;
96 overhang
[1] = 0.28f
* l
;
97 overhang
[2] = cosf( heading_angle
) * l
;
99 q_mulv( q
, overhang
, overhang
);
100 v3_add( player_walk
.state
.drop_in_target
, overhang
, co
);
103 static int player_walk_scan_for_drop_in(void){
104 world_instance
*world
= world_current_instance();
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
);
111 int sample_count
= 0;
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
);
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
);
123 ray_hit
*ray
= &samples
[ sample_count
];
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
);
134 float min_a
= 0.70710678118654752f
;
135 ray_hit
*candidate
= NULL
;
137 if( sample_count
>= 2 ){
138 for( int i
=0; i
<sample_count
-1; i
++ ){
139 ray_hit
*s0
= &samples
[i
],
142 float a
= v3_dot( s0
->normal
, s1
->normal
);
144 if( (a
< min_a
) && (a
>= -0.1f
) && (s0
->normal
[1]>s1
->normal
[1]) ){
154 ray_hit
*s0
= candidate
,
157 vg_line( s0
->pos
, s1
->pos
, VG__WHITE
);
159 v3_copy( s0
->normal
, pa
);
160 v3_copy( s1
->normal
, pb
);
161 v3_cross( localplayer
.rb
.to_world
[1], dir
, pc
);
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
);
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
);
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] );
179 /* TODO: scan multiple of these? */
182 player_walk_drop_in_overhang_transform( 1.0f
, oco
, oq
);
184 v3f va
= {0.0f
,0.0f
,-k_board_length
- 0.3f
},
185 vb
= {0.0f
,0.0f
, k_board_length
+ 0.3f
};
187 q_mulv( oq
, va
, va
);
188 q_mulv( oq
, vb
, vb
);
189 v3_add( oco
, va
, va
);
190 v3_add( oco
, vb
, vb
);
193 v3_sub( vb
, va
, v0
);
197 ray
.dist
= k_board_length
*2.0f
+ 0.6f
;
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" );
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" );
214 player_walk_drop_in_vector( localplayer
.rb
.v
);
218 vg_error( "failed to find intersection of drop in\n" );
225 static bool player__preupdate_anim( struct skeleton_anim
*anim
, f32
*t
,
227 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
;
228 *t
+= (vg
.time_delta
* speed
) / length
;
230 if( *t
>= 1.0f
) return 1;
234 static void player_walk_pre_sit(void){
235 struct player_walk
*w
= &player_walk
;
238 joystick_state( k_srjoystick_steer
, steer
);
240 vg_slewf( &w
->state
.transition_t
, 1.0f
, vg
.time_delta
);
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
;
249 static void player_walk_pre_sit_up(void){
250 struct player_walk
*w
= &player_walk
;
252 if( w
->state
.transition_t
> 0.0f
)
253 vg_slewf( &w
->state
.transition_t
, 0.0f
, vg
.time_delta
);
255 w
->state
.activity
= k_walk_activity_ground
;
257 if( button_down(k_srbind_sit
) )
258 w
->state
.activity
= k_walk_activity_sit
;
263 static void player_walk_pre_ground(void){
264 struct player_walk
*w
= &player_walk
;
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
;
273 if( button_down( k_srbind_use
) ){
274 if( player_walk_scan_for_drop_in() ){
275 w
->state
.activity
= k_walk_activity_odrop_in
;
278 w
->state
.activity
= k_walk_activity_oregular
;
281 w
->state
.transition_t
= 0.0f
;
284 if( button_down( k_srbind_jump
) ){
285 w
->state
.jump_queued
= 1;
286 w
->state
.jump_input_time
= vg
.time
;
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
;
297 if( button_down( k_srbind_jump
) ){
298 w
->state
.jump_queued
= 1;
299 w
->state
.jump_input_time
= vg
.time
;
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
);
308 player_walk_drop_in_to_skate();
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
);
316 player_walk_generic_to_skate( k_skate_activity_air
,
317 player_walk
.animator
.board_yaw
);
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
);
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
);
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
);
339 w
->state
.activity
= k_walk_activity_ground
;
340 w
->animator
.board_yaw
+= 1.0f
;
344 static void player__walk_pre_update(void){
345 struct player_walk
*w
= &player_walk
;
347 if( localplayer
.immobile
) return;
348 else player_look( localplayer
.angles
, skaterift
.time_rate
);
350 enum walk_activity a
= w
->state
.activity
;
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();
362 static int player_walk_normal_standable( v3f n
){
363 return n
[1] > 0.70710678118f
;
366 static void player_accelerate( v3f v
, v3f movedir
, f32 speed
, f32 accel
){
367 float currentspeed
= v3_dot( v
, movedir
),
368 addspeed
= speed
- currentspeed
;
373 float accelspeed
= accel
* k_rb_delta
* speed
;
375 if( accelspeed
> addspeed
)
376 accelspeed
= addspeed
;
378 v3_muladds( v
, movedir
, accelspeed
, v
);
381 static void player_friction( v3f v
, f32 friction
){
382 float speed
= v3_length( v
),
384 control
= vg_maxf( speed
, k_stopspeed
);
389 drop
+= control
* friction
* k_rb_delta
;
391 float newspeed
= vg_maxf( 0.0f
, speed
- drop
);
394 v3_muls( v
, newspeed
, v
);
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
++ ){
401 if( ci
->type
== k_contact_type_disabled
||
402 ci
->type
== k_contact_type_edge
)
406 float d1
= v3_dot( ci
->co
, ci
->n
);
408 for( int j
=0; j
<len
; j
++ ){
413 if( cj
->type
== k_contact_type_disabled
)
416 struct world_surface
*si
= world_contact_surface( world
, ci
),
417 *sj
= world_contact_surface( world
, cj
);
419 if( (sj
->info
.flags
& k_material_flag_walking
) &&
420 !(si
->info
.flags
& k_material_flag_walking
)){
424 float d2
= v3_dot( cj
->co
, ci
->n
),
427 if( fabsf( d
) <= w
){
428 cj
->type
= k_contact_type_disabled
;
434 static void player_walk_update_generic(void){
435 struct player_walk
*w
= &player_walk
;
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
);
445 v3_copy( localplayer
.rb
.co
, w
->state
.prev_pos
);
446 v3_zero( localplayer
.rb
.w
);
448 world_instance
*world
= world_current_instance();
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
);
461 enum walk_activity prev_state
= w
->state
.activity
;
463 w
->collider
.h
= 2.0f
;
464 w
->collider
.r
= 0.3f
;
467 m3x3_copy( localplayer
.rb
.to_world
, mtx
);
468 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
470 vg_line_capsule( mtx
, w
->collider
.r
, w
->collider
.h
, VG__WHITE
);
475 float yaw
= localplayer
.angles
[0];
477 v3f forward_dir
= { -sinf(yaw
), 0.0f
, cosf(yaw
) };
478 v3f right_dir
= { forward_dir
[2], 0.0f
, -forward_dir
[0] };
481 * Collision detection
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
);
489 v3f surface_avg
= { 0.0f
, 0.0f
, 0.0f
};
491 w
->state
.activity
= k_walk_activity_air
;
492 w
->surface
= k_surface_prop_concrete
;
494 for( int i
=0; i
<len
; i
++ ){
495 rb_ct
*ct
= &manifold
[i
];
496 rb_debug_contact( ct
);
498 if( player_walk_normal_standable( ct
->n
) ){
499 w
->state
.activity
= k_walk_activity_ground
;
501 v3_add( surface_avg
, ct
->n
, surface_avg
);
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
;
508 rb_prepare_contact( ct
, k_rb_delta
);
514 float accel_speed
= 0.0f
, nominal_speed
= 0.0f
;
517 v3_muls( right_dir
, w
->state
.steer
[0], movedir
);
518 v3_muladds( movedir
, forward_dir
, w
->state
.steer
[1], movedir
);
520 if( w
->state
.activity
== k_walk_activity_ground
){
521 v3_normalize( surface_avg
);
524 v3_tangent_basis( surface_avg
, tx
, ty
);
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
);
532 accel_speed
= k_walk_accel
;
533 nominal_speed
= w
->state
.steer
[2];
536 if( w
->state
.jump_queued
){
537 w
->state
.jump_queued
= 0;
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
;
549 player_friction( localplayer
.rb
.v
, k_walk_friction
);
553 accel_speed
= k_walk_air_accel
;
554 nominal_speed
= k_airspeed
;
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
);
564 * Resolve velocity constraints
566 for( int j
=0; j
<5; j
++ ){
567 for( int i
=0; i
<len
; i
++ ){
568 rb_ct
*ct
= &manifold
[i
];
571 float vn
= -v3_dot( localplayer
.rb
.v
, ct
->n
);
573 float temp
= ct
->norm_impulse
;
574 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
575 vn
= ct
->norm_impulse
- temp
;
577 v3_muladds( localplayer
.rb
.v
, ct
->n
, vn
, localplayer
.rb
.v
);
582 if( w
->state
.activity
== k_walk_activity_ground
||
583 prev_state
== k_walk_activity_ground
){
584 float max_dist
= 0.4f
;
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 );
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
;
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
;
612 rb_depenetrate( manifold
, len
, dt
);
613 v3_add( dt
, localplayer
.rb
.co
, localplayer
.rb
.co
);
616 if( w
->state
.activity
== k_walk_activity_air
){
617 localplayer
.rb
.v
[1] += -k_gravity
*k_rb_delta
;
620 if( localplayer
.immobile
){
621 localplayer
.rb
.v
[0] = 0.0f
;
622 localplayer
.rb
.v
[2] = 0.0f
;
625 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.v
, k_rb_delta
,
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
);
632 * ---------------------------------------------------
637 lwr_offs
= { 0.0f
, w
->collider
.r
, 0.0f
};
639 v3_add( lwr_offs
, w
->state
.prev_pos
, lwr_prev
);
640 v3_add( lwr_offs
, localplayer
.rb
.co
, lwr_now
);
643 v3_sub( localplayer
.rb
.co
, w
->state
.prev_pos
, movedelta
);
645 float movedist
= v3_length( movedelta
);
647 if( movedist
> 0.3f
){
648 float t
, sr
= w
->collider
.r
-0.04f
;
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
);
660 u32 id
= world_intersect_gates(world
, localplayer
.rb
.co
, w
->state
.prev_pos
);
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
);
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
);
673 rb_update_matrices( &localplayer
.rb
);
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
;
681 w
->move_speed
= vg_minf( v2_length( (v2f
){ localplayer
.rb
.v
[0],
682 localplayer
.rb
.v
[2] } ),
686 static void player__walk_post_update(void){
687 struct player_walk
*w
= &player_walk
;
690 m3x3_copy( localplayer
.rb
.to_world
, mtx
);
691 v3_add( localplayer
.rb
.co
, (v3f
){0,1,0}, mtx
[3] );
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
);
697 /* Calculate header */
699 if( (player_xyspeed2() > 0.1f
*0.1f
) ){
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
) ){
707 f32 ta
= atan2f( localplayer
.rb
.v
[0], localplayer
.rb
.v
[2] );
709 q_axis_angle( qt
, (v3f
){0,1,0}, ta
);
710 q_nlerp( localplayer
.rb
.q
, qt
, vg
.time_delta
/r
, localplayer
.rb
.q
);
713 vg_line_point( w
->state
.drop_in_target
, 0.1f
, VG__GREEN
);
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
);
720 float a
= player_get_heading_yaw();
725 v3_add( localplayer
.rb
.co
, p1
, p1
);
726 vg_line( localplayer
.rb
.co
, p1
, VG__PINK
);
729 if( vg_fractf(w
->state
.walk_timer
) > 0.5f
)
734 if( (w
->state
.step_phase
!= walk_phase
) &&
735 (w
->state
.activity
== k_walk_activity_ground
) )
738 if( w
->surface
== k_surface_prop_concrete
){
740 &audio_footsteps
[vg_randu32(&vg
.rand
) %
741 vg_list_size(audio_footsteps
)],
742 localplayer
.rb
.co
, 40.0f
, 1.0f
745 else if( w
->surface
== k_surface_prop_grass
){
747 &audio_footsteps_grass
[ vg_randu32(&vg
.rand
) %
748 vg_list_size(audio_footsteps_grass
)],
749 localplayer
.rb
.co
, 40.0f
, 1.0f
752 else if( w
->surface
== k_surface_prop_wood
){
754 &audio_footsteps_wood
[ vg_randu32(&vg
.rand
) %
755 vg_list_size(audio_footsteps_wood
)],
756 localplayer
.rb
.co
, 40.0f
, 1.0f
762 w
->state
.step_phase
= walk_phase
;
765 static void player__walk_update(void){
766 struct player_walk
*w
= &player_walk
;
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();
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
;
782 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
,
783 time
= w
->state
.transition_t
;
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
);
790 q_axis_angle( localplayer
.rb
.q
, (v3f
){0,1,0}, walk_yaw
+ VG_PIf
);
792 /* the drop in bit */
795 player_walk_drop_in_overhang_transform( animator
->transition_t
,
798 q_mul( final_q
, localplayer
.rb
.q
, localplayer
.rb
.q
);
799 v3_lerp( localplayer
.rb
.co
, final_co
, animator
->transition_t
,
802 rb_update_matrices( &localplayer
.rb
);
804 v3_muladds( localplayer
.rb
.co
, localplayer
.rb
.to_world
[1],
805 -0.1f
*animator
->transition_t
, localplayer
.rb
.co
);
807 v3_copy( localplayer
.rb
.co
, animator
->root_co
);
808 v4_copy( localplayer
.rb
.q
, animator
->root_q
);
810 /* for the camera purposes only */
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
);
818 static void player_walk_animate_generic(void){
819 struct player_walk
*w
= &player_walk
;
820 struct player_walk_animator
*animator
= &w
->animator
;
823 rb_extrapolate( &localplayer
.rb
, animator
->root_co
, _null
);
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
);
831 if( fabsf(y
) > k_sit_yaw_limit
){
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
);
841 q_axis_angle( qrev
, (v3f
){0,1,0}, VG_TAUf
*0.5f
);
842 q_mul( localplayer
.rb
.q
, qrev
, animator
->root_q
);
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
;
850 animator
->activity
= w
->state
.activity
;
851 animator
->transition_t
= w
->state
.transition_t
;
854 f32 fly
= (w
->state
.activity
== k_walk_activity_air
)? 1.0f
: 0.0f
,
857 if( w
->state
.activity
== k_walk_activity_air
) rate
= 2.4f
;
860 animator
->fly
= vg_lerpf( animator
->fly
, fly
, rate
*vg
.time_delta
);
861 animator
->run
= vg_lerpf( animator
->run
, w
->move_speed
,
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
,
870 if( animator
->run
<= k_walkspeed
)
871 l
= (animator
->run
/ k_walkspeed
) * walk_norm
;
873 l
= vg_lerpf( walk_norm
, run_norm
,
874 (animator
->run
-k_walkspeed
) / (k_runspeed
-k_walkspeed
) );
876 w
->state
.walk_timer
+= l
* vg
.time_delta
;
879 w
->state
.walk_timer
= 0.0f
;
881 animator
->walk_timer
= w
->state
.walk_timer
;
883 player_walk_animate_generic();
884 if( w
->state
.activity
== k_walk_activity_odrop_in
){
885 player_walk_animate_drop_in();
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
;
893 else if( w
->state
.activity
== k_walk_activity_ipopoff
){
894 localplayer
.cam_velocity_influence
= 1.0f
-w
->animator
.transition_t
;
897 localplayer
.cam_velocity_influence
= 0.0f
;
899 if( w
->state
.activity
== k_walk_activity_sit
){
900 localplayer
.cam_dist
= 3.8f
;
903 localplayer
.cam_dist
= 1.8f
;
907 static void player_walk_pose_sit( struct player_walk_animator
*animator
,
909 mdl_keyframe bpose
[32];
911 struct player_walk
*w
= &player_walk
;
912 struct skeleton
*sk
= &localplayer
.skeleton
;
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
);
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
);
926 qh
= bpose
[localplayer
.id_chest
-1].q
;
927 q_axis_angle( qy
, (v3f
){0,1,0}, animator
->yaw
*0.5f
*t
);
931 skeleton_lerp_pose( sk
, pose
->keyframes
, bpose
,
932 vg_minf(1.0f
,t
*10.0f
), pose
->keyframes
);
935 enum walk_transition_type
{
936 k_walk_transition_in
,
937 k_walk_transition_out
,
938 k_walk_transition_outin
,
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
){
946 mdl_keyframe bpose
[32];
948 struct player_walk
*w
= &player_walk
;
949 struct skeleton
*sk
= &localplayer
.skeleton
;
951 f32 length
= (f32
)(anim
->length
-1) / anim
->rate
,
952 t
= animator
->transition_t
* length
,
955 if( type
== k_walk_transition_in
|| type
== k_walk_transition_outin
)
956 blend
= vg_minf( blend
, length
-t
);
958 if( type
== k_walk_transition_out
|| type
== k_walk_transition_outin
)
959 blend
= vg_minf( blend
, t
);
961 blend
= vg_smoothstepf( vg_minf(1,blend
/k_anim_transition
) );
963 skeleton_sample_anim_clamped( sk
, anim
, t
, bpose
);
965 mdl_keyframe
*kf_board
= &bpose
[localplayer
.id_board
-1];
966 f32 yaw
= animator
->board_yaw
* VG_TAUf
* 0.5f
;
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
);
974 for( i32 i
=0; i
<sk
->bone_count
-1; i
++ )
975 keyframe_lerp( apose
+i
, bpose
+i
, blend
*mask
[i
], pose
->keyframes
+i
);
978 skeleton_lerp_pose( sk
, apose
, bpose
, blend
, pose
->keyframes
);
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
;
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
;
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
;
996 mdl_keyframe apose
[32], bpose
[32];
997 if( animator
->run
<= k_walkspeed
){
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
);
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
);
1013 skeleton_sample_anim( sk
, w
->anim_jump
, vg
.time
*0.6f
, bpose
);
1014 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->fly
, apose
);
1016 mdl_keyframe
*kf_board
= &apose
[localplayer
.id_board
-1];
1017 f32 yaw
= animator
->board_yaw
;
1019 if( animator
->activity
== k_walk_activity_ipopoff
)
1020 if( animator
->transition_t
> 0.5f
)
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
);
1029 if( (animator
->activity
== k_walk_activity_sit
) ||
1030 (animator
->activity
== k_walk_activity_sit_up
) ){
1031 player_walk_pose_sit( animator
, pose
);
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
,
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
,
1043 else if( animator
->activity
== k_walk_activity_oregular
){
1044 player_walk_pose_transition(
1045 animator
, w
->anim_intro
, k_walk_transition_out
, apose
,
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
),
1053 for( u32 i
=0; i
<32; i
++ )
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
,
1066 player_walk_pose_transition(
1067 animator
, w
->anim_popoff
, k_walk_transition_in
, apose
,
1072 skeleton_copy_pose( sk
, apose
, pose
->keyframes
);
1076 static void player__walk_post_animate(void){
1080 struct player_walk
*w
= &player_walk
;
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",
1103 [w
->state
.activity
] );
1104 player__debugtext( 1, "surface: %s\n",
1105 (const char *[]){ "concrete",
1115 static void player__walk_bind(void){
1116 struct player_walk
*w
= &player_walk
;
1117 struct skeleton
*sk
= &localplayer
.skeleton
;
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" );
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
;
1135 w
->state
.activity
= k_walk_activity_ipopoff
;
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
);
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
;
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]) );
1157 rb_update_matrices( &localplayer
.rb
);
1160 static void player__walk_animator_exchange( bitpack_ctx
*ctx
, void *data
){
1161 struct player_walk_animator
*animator
= data
;
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
);
1169 for( int i
=0; i
<1; i
++ ){ /* without this you get a warning from gcc. lol */
1170 bitpack_bytes( ctx
, 8, &animator
->activity
);
1173 bitpack_qf32( ctx
, 8, 0.0f
, 1.0f
, &animator
->transition_t
);
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
,
1182 bitpack_qf32( ctx
, 16, -100.0f
, 100.0f
, &animator
->board_yaw
);
1185 static void player__walk_sfx_oneshot( u8 id
, v3f pos
, f32 volume
){
1188 if( id
== k_player_walk_soundeffect_splash
){
1189 audio_oneshot_3d( &audio_splash
, pos
, 40.0f
, 1.0f
);