6 #include "vg/vg_perlin.h"
8 #include "ent_skateshop.h"
11 VG_STATIC
void player__skate_bind( player_instance
*player
)
13 struct player_skate
*s
= &player
->_skate
;
14 struct player_avatar
*av
= player
->playeravatar
;
15 struct skeleton
*sk
= &av
->sk
;
17 rb_update_transform( &player
->rb
);
18 s
->anim_grind
= skeleton_get_anim( sk
, "pose_grind" );
19 s
->anim_grind_jump
= skeleton_get_anim( sk
, "pose_grind_jump" );
20 s
->anim_stand
= skeleton_get_anim( sk
, "pose_stand" );
21 s
->anim_highg
= skeleton_get_anim( sk
, "pose_highg" );
22 s
->anim_air
= skeleton_get_anim( sk
, "pose_air" );
23 s
->anim_slide
= skeleton_get_anim( sk
, "pose_slide" );
24 s
->anim_push
= skeleton_get_anim( sk
, "push" );
25 s
->anim_push_reverse
= skeleton_get_anim( sk
, "push_reverse" );
26 s
->anim_ollie
= skeleton_get_anim( sk
, "ollie" );
27 s
->anim_ollie_reverse
= skeleton_get_anim( sk
, "ollie_reverse" );
28 s
->anim_grabs
= skeleton_get_anim( sk
, "grabs" );
31 VG_STATIC
void player__skate_kill_audio( player_instance
*player
)
33 struct player_skate
*s
= &player
->_skate
;
37 s
->aud_main
= audio_channel_fadeout( s
->aud_main
, 0.1f
);
39 s
->aud_air
= audio_channel_fadeout( s
->aud_air
, 0.1f
);
41 s
->aud_slide
= audio_channel_fadeout( s
->aud_slide
, 0.1f
);
46 * Collision detection routines
52 * Does collision detection on a sphere vs world, and applies some smoothing
53 * filters to the manifold afterwards
55 VG_STATIC
int skate_collide_smooth( player_instance
*player
,
56 m4x3f mtx
, rb_sphere
*sphere
,
59 world_instance
*world
= world_current_instance();
62 len
= rb_sphere__scene( mtx
, sphere
, NULL
, &world
->rb_geo
.inf
.scene
, man
,
63 k_material_flag_walking
);
65 for( int i
=0; i
<len
; i
++ ){
66 man
[i
].rba
= &player
->rb
;
70 rb_manifold_filter_coplanar( man
, len
, 0.03f
);
73 rb_manifold_filter_backface( man
, len
);
74 rb_manifold_filter_joint_edges( man
, len
, 0.03f
);
75 rb_manifold_filter_pairs( man
, len
, 0.03f
);
77 int new_len
= rb_manifold_apply_filtered( man
, len
);
91 VG_STATIC
int skate_grind_scansq( player_instance
*player
,
92 v3f pos
, v3f dir
, float r
,
93 struct grind_info
*inf
)
95 world_instance
*world
= world_current_instance();
98 v3_copy( dir
, plane
);
99 v3_normalize( plane
);
100 plane
[3] = v3_dot( plane
, pos
);
103 v3_add( pos
, (v3f
){ r
, r
, r
}, box
[1] );
104 v3_sub( pos
, (v3f
){ r
, r
, r
}, box
[0] );
113 int sample_count
= 0;
119 v3_cross( plane
, player
->basis
[1], support_axis
);
120 v3_normalize( support_axis
);
123 bh_iter_init_box( 0, &it
, box
);
126 while( bh_next( world
->geo_bh
, &it
, &idx
) ){
127 u32
*ptri
= &world
->scene_geo
.arrindices
[ idx
*3 ];
130 struct world_surface
*surf
= world_tri_index_surface(world
,ptri
[0]);
131 if( !(surf
->info
.flags
& k_material_flag_grindable
) )
134 for( int j
=0; j
<3; j
++ )
135 v3_copy( world
->scene_geo
.arrvertices
[ptri
[j
]].co
, tri
[j
] );
137 for( int j
=0; j
<3; j
++ ){
141 struct grind_sample
*sample
= &samples
[ sample_count
];
144 if( plane_segment( plane
, tri
[i0
], tri
[i1
], co
) ){
146 v3_sub( co
, pos
, d
);
147 if( v3_length2( d
) > r
*r
)
151 v3_sub( tri
[1], tri
[0], va
);
152 v3_sub( tri
[2], tri
[0], vb
);
153 v3_cross( va
, vb
, normal
);
155 sample
->normal
[0] = v3_dot( support_axis
, normal
);
156 sample
->normal
[1] = v3_dot( player
->basis
[1], normal
);
157 sample
->co
[0] = v3_dot( support_axis
, d
);
158 sample
->co
[1] = v3_dot( player
->basis
[1], d
);
160 v3_copy( normal
, sample
->normal3
); /* normalize later
161 if we want to us it */
163 v3_muls( tri
[0], 1.0f
/3.0f
, sample
->centroid
);
164 v3_muladds( sample
->centroid
, tri
[1], 1.0f
/3.0f
, sample
->centroid
);
165 v3_muladds( sample
->centroid
, tri
[2], 1.0f
/3.0f
, sample
->centroid
);
167 v2_normalize( sample
->normal
);
170 if( sample_count
== vg_list_size( samples
) )
171 goto too_many_samples
;
178 if( sample_count
< 2 )
186 v2_fill( min_co
, INFINITY
);
187 v2_fill( max_co
, -INFINITY
);
189 v3_zero( average_direction
);
190 v3_zero( average_normal
);
192 int passed_samples
= 0;
194 for( int i
=0; i
<sample_count
-1; i
++ ){
195 struct grind_sample
*si
, *sj
;
199 for( int j
=i
+1; j
<sample_count
; j
++ ){
205 /* non overlapping */
206 if( v2_dist2( si
->co
, sj
->co
) >= (0.01f
*0.01f
) )
209 /* not sharp angle */
210 if( v2_dot( si
->normal
, sj
->normal
) >= 0.7f
)
215 v3_sub( sj
->centroid
, si
->centroid
, v0
);
216 if( v3_dot( v0
, si
->normal3
) >= 0.0f
||
217 v3_dot( v0
, sj
->normal3
) <= 0.0f
)
220 v2_minv( sj
->co
, min_co
, min_co
);
221 v2_maxv( sj
->co
, max_co
, max_co
);
224 v3_copy( si
->normal3
, n0
);
225 v3_copy( sj
->normal3
, n1
);
226 v3_cross( n0
, n1
, dir
);
229 /* make sure the directions all face a common hemisphere */
230 v3_muls( dir
, vg_signf(v3_dot(dir
,plane
)), dir
);
231 v3_add( average_direction
, dir
, average_direction
);
233 float yi
= v3_dot( player
->basis
[1], si
->normal3
),
234 yj
= v3_dot( player
->basis
[1], sj
->normal3
);
236 if( yi
> yj
) v3_add( si
->normal3
, average_normal
, average_normal
);
237 else v3_add( sj
->normal3
, average_normal
, average_normal
);
243 if( !passed_samples
)
246 if( (v3_length2( average_direction
) <= 0.001f
) ||
247 (v3_length2( average_normal
) <= 0.001f
) )
250 float div
= 1.0f
/(float)passed_samples
;
251 v3_normalize( average_direction
);
252 v3_normalize( average_normal
);
255 v2_add( min_co
, max_co
, average_coord
);
256 v2_muls( average_coord
, 0.5f
, average_coord
);
258 v3_muls( support_axis
, average_coord
[0], inf
->co
);
259 inf
->co
[1] += average_coord
[1];
260 v3_add( pos
, inf
->co
, inf
->co
);
261 v3_copy( average_normal
, inf
->n
);
262 v3_copy( average_direction
, inf
->dir
);
264 vg_line_point( inf
->co
, 0.02f
, VG__GREEN
);
265 vg_line_arrow( inf
->co
, average_direction
, 0.3f
, VG__GREEN
);
266 vg_line_arrow( inf
->co
, inf
->n
, 0.2f
, VG__CYAN
);
268 return passed_samples
;
271 VG_STATIC
void reset_jump_info( jump_info
*inf
)
274 inf
->land_dist
= 0.0f
;
276 inf
->type
= k_prediction_unset
;
277 v3_zero( inf
->apex
);
280 VG_STATIC
int create_jumps_to_hit_target( player_instance
*player
,
282 v3f target
, float max_angle_delta
,
285 struct player_skate
*s
= &player
->_skate
;
287 /* calculate the exact 2 solutions to jump onto that grind spot */
290 v3_sub( target
, player
->rb
.co
, v0
);
291 m3x3_mulv( player
->invbasis
, v0
, v0
);
299 m3x3_mulv( player
->invbasis
, player
->rb
.v
, v_local
);
301 v2f d
= { v3_dot( ax
, v0
), v0
[1] },
302 v
= { v3_dot( ax
, v_local
), v_local
[1] };
304 float a
= atan2f( v
[1], v
[0] ),
306 root
= m
*m
*m
*m
- gravity
*(gravity
*d
[0]*d
[0] + 2.0f
*d
[1]*m
*m
);
311 root
= sqrtf( root
);
312 float a0
= atanf( (m
*m
+ root
) / (gravity
* d
[0]) ),
313 a1
= atanf( (m
*m
- root
) / (gravity
* d
[0]) );
315 if( fabsf(a0
-a
) < max_angle_delta
){
316 jump_info
*inf
= &jumps
[ valid_count
++ ];
317 reset_jump_info( inf
);
319 v3_muls( ax
, cosf( a0
) * m
, inf
->v
);
320 inf
->v
[1] += sinf( a0
) * m
;
321 m3x3_mulv( player
->basis
, inf
->v
, inf
->v
);
322 inf
->land_dist
= d
[0] / (cosf(a0
)*m
);
323 inf
->gravity
= gravity
;
325 v3_copy( target
, inf
->log
[inf
->log_length
++] );
328 if( fabsf(a1
-a
) < max_angle_delta
){
329 jump_info
*inf
= &jumps
[ valid_count
++ ];
330 reset_jump_info( inf
);
332 v3_muls( ax
, cosf( a1
) * m
, inf
->v
);
333 inf
->v
[1] += sinf( a1
) * m
;
334 m3x3_mulv( player
->basis
, inf
->v
, inf
->v
);
335 inf
->land_dist
= d
[0] / (cosf(a1
)*m
);
336 inf
->gravity
= gravity
;
338 v3_copy( target
, inf
->log
[inf
->log_length
++] );
346 void player__approximate_best_trajectory( player_instance
*player
)
348 world_instance
*world0
= world_current_instance();
350 struct player_skate
*s
= &player
->_skate
;
351 float k_trace_delta
= k_rb_delta
* 10.0f
;
353 s
->state
.air_start
= vg
.time
;
354 v3_copy( player
->rb
.v
, s
->state
.air_init_v
);
355 v3_copy( player
->rb
.co
, s
->state
.air_init_co
);
357 s
->possible_jump_count
= 0;
360 v3_cross( player
->rb
.v
, player
->rb
.to_world
[1], axis
);
361 v3_normalize( axis
);
363 /* at high slopes, Y component is low */
364 float upness
= v3_dot( player
->rb
.to_world
[1], player
->basis
[1] ),
365 angle_begin
= -(1.0f
-fabsf( upness
)),
368 struct grind_info grind
;
369 int grind_located
= 0;
370 float grind_located_gravity
= k_gravity
;
373 v3f launch_v_bounds
[2];
375 for( int i
=0; i
<2; i
++ ){
376 v3_copy( player
->rb
.v
, launch_v_bounds
[i
] );
377 float ang
= (float[]){ angle_begin
, angle_end
}[ i
];
381 q_axis_angle( qbias
, axis
, ang
);
382 q_mulv( qbias
, launch_v_bounds
[i
], launch_v_bounds
[i
] );
385 for( int m
=0;m
<=30; m
++ ){
386 jump_info
*inf
= &s
->possible_jumps
[ s
->possible_jump_count
++ ];
387 reset_jump_info( inf
);
389 v3f launch_co
, launch_v
, co0
, co1
;
390 v3_copy( player
->rb
.co
, launch_co
);
391 v3_copy( player
->rb
.v
, launch_v
);
392 v3_copy( launch_co
, co0
);
393 world_instance
*trace_world
= world0
;
395 float vt
= (float)m
* (1.0f
/30.0f
),
396 ang
= vg_lerpf( angle_begin
, angle_end
, vt
) * 0.15f
;
399 q_axis_angle( qbias
, axis
, ang
);
400 q_mulv( qbias
, launch_v
, launch_v
);
402 float yaw_sketch
= 1.0f
-fabsf(upness
);
404 float yaw_bias
= ((float)(m
%3) - 1.0f
) * 0.08f
* yaw_sketch
;
405 q_axis_angle( qbias
, player
->rb
.to_world
[1], yaw_bias
);
406 q_mulv( qbias
, launch_v
, launch_v
);
408 float gravity_bias
= vg_lerpf( 0.85f
, 1.4f
, vt
),
409 gravity
= k_gravity
* gravity_bias
;
410 inf
->gravity
= gravity
;
411 v3_copy( launch_v
, inf
->v
);
414 m3x3_copy( player
->basis
, basis
);
416 for( int i
=1; i
<=50; i
++ ){
417 float t
= (float)i
* k_trace_delta
;
419 v3_muls( launch_v
, t
, co1
);
420 v3_muladds( co1
, basis
[1], -0.5f
* gravity
* t
*t
, co1
);
421 v3_add( launch_co
, co1
, co1
);
423 float launch_vy
= v3_dot( launch_v
,basis
[1] );
425 int search_for_grind
= 1;
426 if( grind_located
) search_for_grind
= 0;
427 if( launch_vy
- gravity
*t
> 0.0f
) search_for_grind
= 0;
431 v3f closest
={0.0f
,0.0f
,0.0f
};
432 if( search_for_grind
){
433 if( bh_closest_point(trace_world
->geo_bh
,co1
,closest
,1.0f
) != -1 ){
434 float min_dist
= 0.75f
;
435 min_dist
*= min_dist
;
437 if( v3_dist2( closest
, launch_co
) < min_dist
)
438 search_for_grind
= 0;
442 for( int j
=0; j
<2; j
++ ){
443 v3_muls( launch_v_bounds
[j
], t
, bound
[j
] );
444 v3_muladds( bound
[j
], basis
[1], -0.5f
*gravity
*t
*t
, bound
[j
] );
445 v3_add( launch_co
, bound
[j
], bound
[j
] );
448 float limh
= vg_minf( 2.0f
, t
),
449 minh
= vg_minf( bound
[0][1], bound
[1][1] )-limh
,
450 maxh
= vg_maxf( bound
[0][1], bound
[1][1] )+limh
;
452 if( (closest
[1] < minh
) || (closest
[1] > maxh
) ){
453 search_for_grind
= 0;
457 search_for_grind
= 0;
460 if( search_for_grind
){
462 v3_copy( launch_v
, ve
);
463 v3_muladds( ve
, basis
[1], -gravity
* t
, ve
);
465 if( skate_grind_scansq( player
, closest
, ve
, 0.5f
, &grind
) ){
466 /* check alignment */
467 v2f v0
= { v3_dot( ve
, basis
[0] ),
468 v3_dot( ve
, basis
[2] ) },
469 v1
= { v3_dot( grind
.dir
, basis
[0] ),
470 v3_dot( grind
.dir
, basis
[2] ) };
475 float a
= v2_dot( v0
, v1
);
477 float a_min
= cosf( VG_PIf
* 0.185f
);
478 if( s
->state
.grind_cooldown
)
479 a_min
= cosf( VG_PIf
* 0.05f
);
482 if( (fabsf(v3_dot( ve
, grind
.dir
))>=k_grind_axel_min_vel
) &&
484 (fabsf(grind
.dir
[1]) < 0.70710678118654752f
))
487 grind_located_gravity
= inf
->gravity
;
492 if( trace_world
->rendering_gate
){
493 ent_gate
*gate
= trace_world
->rendering_gate
;
494 if( gate_intersect( gate
, co1
, co0
) ){
495 m4x3_mulv( gate
->transport
, co0
, co0
);
496 m4x3_mulv( gate
->transport
, co1
, co1
);
497 m3x3_mulv( gate
->transport
, launch_v
, launch_v
);
498 m4x3_mulv( gate
->transport
, launch_co
, launch_co
);
499 m3x3_mul( gate
->transport
, basis
, basis
);
501 if( gate
->flags
& k_ent_gate_nonlocal
){
502 trace_world
= &world_static
.instances
[ gate
->target
];
510 float scan_radius
= k_board_radius
;
511 scan_radius
*= vg_clampf( t
, 0.02f
, 1.0f
);
513 int idx
= spherecast_world( trace_world
, co0
, co1
, scan_radius
, &t1
, n
,
514 k_material_flag_walking
);
517 v3_lerp( co0
, co1
, t1
, co
);
518 v3_copy( co
, inf
->log
[ inf
->log_length
++ ] );
520 v3_copy( n
, inf
->n
);
521 u32
*tri
= &trace_world
->scene_geo
.arrindices
[ idx
*3 ];
522 struct world_surface
*surf
=
523 world_tri_index_surface( trace_world
, tri
[0] );
525 inf
->type
= k_prediction_land
;
528 v3_copy( launch_v
, ve
);
529 v3_muladds( ve
, player
->basis
[1], -gravity
* t
, ve
);
531 inf
->score
= -v3_dot( ve
, inf
->n
);
532 inf
->land_dist
= t
+ k_trace_delta
* t1
;
534 /* Bias prediction towords ramps */
535 if( !(surf
->info
.flags
& k_material_flag_skate_target
) )
538 if( surf
->info
.flags
& k_material_flag_boundary
)
539 s
->possible_jump_count
--;
545 v3_copy( co1
, inf
->log
[ inf
->log_length
++ ] );
550 if( inf
->type
== k_prediction_unset
)
551 s
->possible_jump_count
--;
555 jump_info grind_jumps
[2];
558 create_jumps_to_hit_target( player
, grind_jumps
, grind
.co
,
559 0.175f
*VG_PIf
, grind_located_gravity
);
561 /* knock out original landing points in the 1m area */
562 for( u32 j
=0; j
<s
->possible_jump_count
; j
++ ){
563 jump_info
*jump
= &s
->possible_jumps
[ j
];
564 float dist
= v3_dist2( jump
->log
[jump
->log_length
-1], grind
.co
);
565 float descale
= 1.0f
-vg_minf(1.0f
,dist
);
566 jump
->score
+= descale
*3.0f
;
569 for( int i
=0; i
<valid_count
; i
++ ){
570 jump_info
*jump
= &grind_jumps
[i
];
571 jump
->type
= k_prediction_grind
;
573 v3f launch_v
, launch_co
, co0
, co1
;
575 v3_copy( jump
->v
, launch_v
);
576 v3_copy( player
->rb
.co
, launch_co
);
579 m3x3_copy( player
->basis
, basis
);
581 float t
= 0.05f
* jump
->land_dist
;
582 v3_muls( launch_v
, t
, co0
);
583 v3_muladds( co0
, basis
[1], -0.5f
* jump
->gravity
* t
*t
, co0
);
584 v3_add( launch_co
, co0
, co0
);
586 /* rough scan to make sure we dont collide with anything */
587 for( int j
=1; j
<=16; j
++ ){
588 t
= (float)j
*(1.0f
/16.0f
);
591 t
*= jump
->land_dist
;
593 v3_muls( launch_v
, t
, co1
);
594 v3_muladds( co1
, basis
[1], -0.5f
* jump
->gravity
* t
*t
, co1
);
595 v3_add( launch_co
, co1
, co1
);
600 int idx
= spherecast_world( world0
, co0
,co1
,
601 k_board_radius
*0.1f
, &t1
, n
,
602 k_material_flag_walking
);
604 goto invalidated_grind
;
610 v3_copy( grind
.n
, jump
->n
);
612 /* determine score */
614 v3_copy( jump
->v
, ve
);
615 v3_muladds( ve
, player
->basis
[1], -jump
->gravity
*jump
->land_dist
, ve
);
616 jump
->score
= -v3_dot( ve
, grind
.n
) * 0.9f
;
618 s
->possible_jumps
[ s
->possible_jump_count
++ ] = *jump
;
626 float score_min
= INFINITY
,
627 score_max
= -INFINITY
;
629 jump_info
*best
= NULL
;
631 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
632 jump_info
*jump
= &s
->possible_jumps
[i
];
634 if( jump
->score
< score_min
)
637 score_min
= vg_minf( score_min
, jump
->score
);
638 score_max
= vg_maxf( score_max
, jump
->score
);
641 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
642 jump_info
*jump
= &s
->possible_jumps
[i
];
643 float s
= jump
->score
;
646 s
/= (score_max
-score_min
);
650 jump
->colour
= s
* 255.0f
;
654 else if( jump
->type
== k_prediction_land
)
657 jump
->colour
|= 0xff000000;
661 v3_copy( best
->n
, s
->state
.land_normal
);
662 v3_copy( best
->v
, player
->rb
.v
);
663 s
->state
.land_dist
= best
->land_dist
;
665 s
->state
.gravity_bias
= best
->gravity
;
667 if( best
->type
== k_prediction_grind
){
668 s
->state
.activity
= k_skate_activity_air_to_grind
;
672 joystick_state( k_srjoystick_steer
, steer
);
673 v2_normalize_clamp( steer
);
675 if( (fabsf(steer
[1]) > 0.5f
) && (s
->state
.land_dist
>= 1.5f
) ){
676 s
->state
.flip_rate
= (1.0f
/s
->state
.land_dist
) * vg_signf(steer
[1]) *
678 s
->state
.flip_time
= 0.0f
;
679 v3_copy( player
->rb
.to_world
[0], s
->state
.flip_axis
);
682 s
->state
.flip_rate
= 0.0f
;
683 v3_zero( s
->state
.flip_axis
);
687 v3_copy( player
->basis
[1], s
->state
.land_normal
);
693 * Varius physics models
694 * ------------------------------------------------
698 * Air control, no real physics
700 VG_STATIC
void skate_apply_air_model( player_instance
*player
)
702 struct player_skate
*s
= &player
->_skate
;
704 if( s
->state
.activity_prev
> k_skate_activity_air_to_grind
)
705 player__approximate_best_trajectory( player
);
707 float angle
= v3_dot( player
->rb
.to_world
[1], s
->state
.land_normal
);
708 angle
= vg_clampf( angle
, -1.0f
, 1.0f
);
710 v3_cross( player
->rb
.to_world
[1], s
->state
.land_normal
, axis
);
713 q_axis_angle( correction
, axis
,
714 acosf(angle
)*2.0f
*VG_TIMESTEP_FIXED
);
715 q_mul( correction
, player
->rb
.q
, player
->rb
.q
);
718 VG_STATIC
enum trick_type
player_skate_trick_input( player_instance
*player
);
719 VG_STATIC
void skate_apply_trick_model( player_instance
*player
)
721 struct player_skate
*s
= &player
->_skate
;
724 v3f strength
= { 3.7f
, 3.6f
, 8.0f
};
726 v3_muls( s
->state
.trick_residualv
, -4.0f
, Fd
);
727 v3_muls( s
->state
.trick_residuald
, -10.0f
, Fs
);
729 v3_mul( strength
, F
, F
);
731 v3_muladds( s
->state
.trick_residualv
, F
, k_rb_delta
,
732 s
->state
.trick_residualv
);
733 v3_muladds( s
->state
.trick_residuald
, s
->state
.trick_residualv
,
734 k_rb_delta
, s
->state
.trick_residuald
);
736 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
737 if( v3_length2( s
->state
.trick_vel
) < 0.0001f
)
740 int carry_on
= player_skate_trick_input( player
);
742 /* we assume velocities share a common divisor, in which case the
743 * interval is the minimum value (if not zero) */
745 float min_rate
= 99999.0f
;
747 for( int i
=0; i
<3; i
++ ){
748 float v
= s
->state
.trick_vel
[i
];
749 if( (v
> 0.0f
) && (v
< min_rate
) )
753 float interval
= 1.0f
/ min_rate
,
754 current
= floorf( s
->state
.trick_time
/ interval
),
755 next_end
= (current
+1.0f
) * interval
;
758 /* integrate trick velocities */
759 v3_muladds( s
->state
.trick_euler
, s
->state
.trick_vel
, k_rb_delta
,
760 s
->state
.trick_euler
);
762 if( !carry_on
&& (s
->state
.trick_time
+ k_rb_delta
>= next_end
) ){
763 s
->state
.trick_time
= 0.0f
;
764 s
->state
.trick_euler
[0] = roundf( s
->state
.trick_euler
[0] );
765 s
->state
.trick_euler
[1] = roundf( s
->state
.trick_euler
[1] );
766 s
->state
.trick_euler
[2] = roundf( s
->state
.trick_euler
[2] );
767 v3_copy( s
->state
.trick_vel
, s
->state
.trick_residualv
);
768 v3_zero( s
->state
.trick_vel
);
771 s
->state
.trick_time
+= k_rb_delta
;
774 if( (v3_length2(s
->state
.trick_vel
) >= 0.0001f
) &&
775 s
->state
.trick_time
> 0.2f
)
777 player__skate_kill_audio( player
);
778 player__dead_transition( player
);
781 s
->state
.trick_euler
[0] = roundf( s
->state
.trick_euler
[0] );
782 s
->state
.trick_euler
[1] = roundf( s
->state
.trick_euler
[1] );
783 s
->state
.trick_euler
[2] = roundf( s
->state
.trick_euler
[2] );
784 s
->state
.trick_time
= 0.0f
;
785 v3_zero( s
->state
.trick_vel
);
789 VG_STATIC
void skate_apply_grab_model( player_instance
*player
)
791 struct player_skate
*s
= &player
->_skate
;
793 float grabt
= axis_state( k_sraxis_grab
);
796 v2_muladds( s
->state
.grab_mouse_delta
, vg
.mouse_delta
, 0.02f
,
797 s
->state
.grab_mouse_delta
);
799 v2_normalize_clamp( s
->state
.grab_mouse_delta
);
802 v2_zero( s
->state
.grab_mouse_delta
);
804 s
->state
.grabbing
= vg_lerpf( s
->state
.grabbing
, grabt
, 8.4f
*k_rb_delta
);
807 VG_STATIC
void skate_apply_steering_model( player_instance
*player
)
809 struct player_skate
*s
= &player
->_skate
;
812 joystick_state( k_srjoystick_steer
, jsteer
);
815 float steer
= jsteer
[0],
816 grab
= axis_state( k_sraxis_grab
);
818 steer
= vg_signf( steer
) * steer
*steer
* k_steer_ground
;
821 v3_muls( player
->rb
.to_world
[1], -vg_signf( steer
), steer_axis
);
826 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
827 rate
= 6.0f
* fabsf(steer
);
831 /* rotate slower when grabbing on ground */
832 steer
*= (1.0f
-(s
->state
.jump_charge
+grab
)*0.4f
);
834 if( s
->state
.activity
== k_skate_activity_grind_5050
){
839 else if( s
->state
.activity
>= k_skate_activity_grind_any
){
840 rate
*= fabsf(steer
);
842 float a
= 0.8f
* -steer
* k_rb_delta
;
845 q_axis_angle( q
, player
->rb
.to_world
[1], a
);
846 q_mulv( q
, s
->grind_vec
, s
->grind_vec
);
848 v3_normalize( s
->grind_vec
);
851 else if( s
->state
.manual_direction
){
857 top
*= 1.0f
+v3_length( s
->state
.throw_v
)*k_mmthrow_steer
;
861 float current
= v3_dot( player
->rb
.to_world
[1], player
->rb
.w
),
862 addspeed
= (steer
* -top
) - current
,
863 maxaccel
= rate
* k_rb_delta
,
864 accel
= vg_clampf( addspeed
, -maxaccel
, maxaccel
);
866 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[1], accel
, player
->rb
.w
);
870 * Computes friction and surface interface model
872 VG_STATIC
void skate_apply_friction_model( player_instance
*player
)
874 struct player_skate
*s
= &player
->_skate
;
877 * Computing localized friction forces for controlling the character
878 * Friction across X is significantly more than Z
882 m3x3_mulv( player
->rb
.to_local
, player
->rb
.v
, vel
);
885 if( fabsf(vel
[2]) > 0.01f
)
886 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
888 if( fabsf( slip
) > 1.2f
)
889 slip
= vg_signf( slip
) * 1.2f
;
891 s
->state
.slip
= slip
;
892 s
->state
.reverse
= -vg_signf(vel
[2]);
894 vel
[0] += vg_cfrictf( vel
[0], k_friction_lat
* k_rb_delta
);
895 vel
[2] += vg_cfrictf( vel
[2], k_friction_resistance
* k_rb_delta
);
897 /* Pushing additive force */
899 if( !button_press( k_srbind_jump
) ){
900 if( button_press( k_srbind_push
) || (vg
.time
-s
->state
.start_push
<0.75) )
902 if( (vg
.time
- s
->state
.cur_push
) > 0.25 )
903 s
->state
.start_push
= vg
.time
;
905 s
->state
.cur_push
= vg
.time
;
907 double push_time
= vg
.time
- s
->state
.start_push
;
909 float cycle_time
= push_time
*k_push_cycle_rate
,
910 accel
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
),
911 amt
= accel
* VG_TIMESTEP_FIXED
,
912 current
= v3_length( vel
),
913 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
),
914 delta
= new_vel
- vg_minf( current
, k_max_push_speed
);
916 vel
[2] += delta
* -s
->state
.reverse
;
920 /* Send back to velocity */
921 m3x3_mulv( player
->rb
.to_world
, vel
, player
->rb
.v
);
924 VG_STATIC
void skate_apply_jump_model( player_instance
*player
)
926 struct player_skate
*s
= &player
->_skate
;
927 int charging_jump_prev
= s
->state
.charging_jump
;
928 s
->state
.charging_jump
= button_press( k_srbind_jump
);
930 /* Cannot charge this in air */
931 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
932 s
->state
.charging_jump
= 0;
936 if( s
->state
.charging_jump
){
937 s
->state
.jump_charge
+= k_rb_delta
* k_jump_charge_speed
;
939 if( !charging_jump_prev
)
940 s
->state
.jump_dir
= s
->state
.reverse
>0.0f
? 1: 0;
943 s
->state
.jump_charge
-= k_jump_charge_speed
* k_rb_delta
;
946 s
->state
.jump_charge
= vg_clampf( s
->state
.jump_charge
, 0.0f
, 1.0f
);
948 /* player let go after charging past 0.2: trigger jump */
949 if( (!s
->state
.charging_jump
) && (s
->state
.jump_charge
> 0.2f
) ){
952 /* Launch more up if alignment is up else improve velocity */
953 float aup
= v3_dot( player
->basis
[1], player
->rb
.to_world
[1] ),
955 dir
= mod
+ fabsf(aup
)*(1.0f
-mod
);
957 if( s
->state
.activity
== k_skate_activity_ground
){
958 v3_copy( player
->rb
.v
, jumpdir
);
959 v3_normalize( jumpdir
);
960 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
961 v3_muladds( jumpdir
, player
->rb
.to_world
[1], dir
, jumpdir
);
962 v3_normalize( jumpdir
);
964 v3_copy( s
->state
.up_dir
, jumpdir
);
965 s
->state
.grind_cooldown
= 30;
966 s
->state
.activity
= k_skate_activity_ground
;
969 joystick_state( k_srjoystick_steer
, steer
);
971 float tilt
= steer
[0] * 0.3f
;
972 tilt
*= vg_signf(v3_dot( player
->rb
.v
, s
->grind_dir
));
975 q_axis_angle( qtilt
, s
->grind_dir
, tilt
);
976 q_mulv( qtilt
, jumpdir
, jumpdir
);
978 s
->state
.surface_cooldown
= 10;
980 float force
= k_jump_force
*s
->state
.jump_charge
;
981 v3_muladds( player
->rb
.v
, jumpdir
, force
, player
->rb
.v
);
982 s
->state
.jump_charge
= 0.0f
;
983 s
->state
.jump_time
= vg
.time
;
986 audio_oneshot_3d( &audio_jumps
[vg_randu32()%2], player
->rb
.co
,40.0f
,1.0f
);
991 VG_STATIC
void skate_apply_pump_model( player_instance
*player
)
993 struct player_skate
*s
= &player
->_skate
;
995 if( s
->state
.activity
!= k_skate_activity_ground
){
996 v3_zero( s
->state
.throw_v
);
1000 /* Throw / collect routine
1002 if( axis_state( k_sraxis_grab
) > 0.5f
){
1003 if( s
->state
.activity
== k_skate_activity_ground
){
1005 v3_muls( player
->rb
.to_world
[1], k_mmthrow_scale
, s
->state
.throw_v
);
1010 float doty
= v3_dot( player
->rb
.to_world
[1], s
->state
.throw_v
);
1013 v3_muladds( s
->state
.throw_v
, player
->rb
.to_world
[1], -doty
, Fl
);
1015 if( s
->state
.activity
== k_skate_activity_ground
){
1016 if( v3_length2(player
->rb
.v
)<(20.0f
*20.0f
) )
1017 v3_muladds( player
->rb
.v
, Fl
, k_mmcollect_lat
, player
->rb
.v
);
1018 v3_muladds( s
->state
.throw_v
, Fl
, -k_mmcollect_lat
, s
->state
.throw_v
);
1021 v3_muls( player
->rb
.to_world
[1], -doty
, Fv
);
1022 v3_muladds( player
->rb
.v
, Fv
, k_mmcollect_vert
, player
->rb
.v
);
1023 v3_muladds( s
->state
.throw_v
, Fv
, k_mmcollect_vert
, s
->state
.throw_v
);
1027 if( v3_length2( s
->state
.throw_v
) > 0.0001f
){
1029 v3_copy( s
->state
.throw_v
, dir
);
1030 v3_normalize( dir
);
1032 float max
= v3_dot( dir
, s
->state
.throw_v
),
1033 amt
= vg_minf( k_mmdecay
* k_rb_delta
, max
);
1034 v3_muladds( s
->state
.throw_v
, dir
, -amt
, s
->state
.throw_v
);
1038 VG_STATIC
void skate_apply_cog_model( player_instance
*player
)
1040 struct player_skate
*s
= &player
->_skate
;
1042 v3f ideal_cog
, ideal_diff
, ideal_dir
;
1043 v3_copy( s
->state
.up_dir
, ideal_dir
);
1044 v3_normalize( ideal_dir
);
1046 float grab
= axis_state( k_sraxis_grab
);
1047 v3_muladds( player
->rb
.co
, ideal_dir
, 1.0f
-grab
, ideal_cog
);
1048 v3_sub( ideal_cog
, s
->state
.cog
, ideal_diff
);
1050 /* Apply velocities */
1052 v3_sub( player
->rb
.v
, s
->state
.cog_v
, rv
);
1055 v3_muls( ideal_diff
, -k_cog_spring
* k_rb_rate
, F
);
1056 v3_muladds( F
, rv
, -k_cog_damp
* k_rb_rate
, F
);
1058 float ra
= k_cog_mass_ratio
,
1059 rb
= 1.0f
-k_cog_mass_ratio
;
1061 /* Apply forces & intergrate */
1062 v3_muladds( s
->state
.cog_v
, F
, -rb
, s
->state
.cog_v
);
1063 v3_muladds( s
->state
.cog_v
, player
->basis
[1], -9.8f
* k_rb_delta
,
1066 v3_muladds( s
->state
.cog
, s
->state
.cog_v
, k_rb_delta
, s
->state
.cog
);
1070 VG_STATIC
void skate_integrate( player_instance
*player
)
1072 struct player_skate
*s
= &player
->_skate
;
1074 float decay_rate_x
= 1.0f
- (k_rb_delta
* 3.0f
),
1075 decay_rate_z
= decay_rate_x
,
1076 decay_rate_y
= 1.0f
;
1078 if( s
->state
.activity
>= k_skate_activity_grind_any
){
1080 decay_rate
= 1.0f
-vg_lerpf( 3.0f
, 20.0f
, s
->grind_strength
) * k_rb_delta
;
1081 decay_rate_y
= decay_rate
;
1083 decay_rate_x
= 1.0f
-(16.0f
*k_rb_delta
);
1084 decay_rate_y
= 1.0f
-(10.0f
*k_rb_delta
);
1085 decay_rate_z
= 1.0f
-(40.0f
*k_rb_delta
);
1088 float wx
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[0] ) * decay_rate_x
,
1089 wy
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[1] ) * decay_rate_y
,
1090 wz
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[2] ) * decay_rate_z
;
1092 v3_muls( player
->rb
.to_world
[0], wx
, player
->rb
.w
);
1093 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[1], wy
, player
->rb
.w
);
1094 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[2], wz
, player
->rb
.w
);
1096 s
->state
.flip_time
+= s
->state
.flip_rate
* k_rb_delta
;
1097 rb_update_transform( &player
->rb
);
1100 VG_STATIC
enum trick_type
player_skate_trick_input( player_instance
*player
){
1101 return (button_press( k_srbind_trick0
) ) |
1102 (button_press( k_srbind_trick1
) << 1) |
1103 (button_press( k_srbind_trick2
) << 1) |
1104 (button_press( k_srbind_trick2
) );
1107 VG_STATIC
void player__skate_pre_update( player_instance
*player
){
1108 struct player_skate
*s
= &player
->_skate
;
1110 if( button_down( k_srbind_use
) ){
1111 player
->subsystem
= k_player_subsystem_walk
;
1114 v3_copy( player
->cam
.angles
, player
->angles
);
1115 player
->angles
[2] = 0.0f
;
1117 player__begin_holdout( player
);
1118 player__skate_kill_audio( player
);
1119 player__walk_transition( player
);
1123 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
1124 enum trick_type trick
= k_trick_type_none
;
1125 if( (trick
= player_skate_trick_input( player
)) ){
1126 if( (vg
.time
- s
->state
.jump_time
) < 0.1f
){
1127 v3_zero( s
->state
.trick_vel
);
1128 s
->state
.trick_time
= 0.0f
;
1130 if( trick
== k_trick_type_kickflip
){
1131 s
->state
.trick_vel
[0] = 3.0f
;
1133 else if( trick
== k_trick_type_shuvit
){
1134 s
->state
.trick_vel
[2] = 3.0f
;
1136 else if( trick
== k_trick_type_treflip
){
1137 s
->state
.trick_vel
[0] = 2.0f
;
1138 s
->state
.trick_vel
[2] = 2.0f
;
1140 s
->state
.trick_type
= trick
;
1145 s
->state
.trick_type
= k_trick_type_none
;
1148 VG_STATIC
void player__skate_post_update( player_instance
*player
){
1149 struct player_skate
*s
= &player
->_skate
;
1151 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
1152 jump_info
*jump
= &s
->possible_jumps
[i
];
1154 if( jump
->log_length
== 0 ){
1155 vg_fatal_error( "assert: jump->log_length == 0\n" );
1158 for( int j
=0; j
<jump
->log_length
- 1; j
++ ){
1159 float brightness
= jump
->score
*jump
->score
*jump
->score
;
1161 v3_lerp( jump
->log
[j
], jump
->log
[j
+1], brightness
, p1
);
1162 vg_line( jump
->log
[j
], p1
, jump
->colour
);
1165 vg_line_cross( jump
->log
[jump
->log_length
-1], jump
->colour
, 0.25f
);
1168 v3_add( jump
->log
[jump
->log_length
-1], jump
->n
, p1
);
1169 vg_line( jump
->log
[jump
->log_length
-1], p1
, 0xffffffff );
1171 vg_line_point( jump
->apex
, 0.02f
, 0xffffffff );
1176 float air
= s
->state
.activity
<= k_skate_activity_air_to_grind
? 1.0f
: 0.0f
,
1177 speed
= v3_length( player
->rb
.v
),
1178 attn
= vg_minf( 1.0f
, speed
*0.1f
),
1179 slide
= vg_clampf( fabsf(s
->state
.slip
), 0.0f
, 1.0f
);
1181 if( s
->state
.activity
>= k_skate_activity_grind_any
){
1185 f32 gate
= skaterift
.time_rate
,
1186 vol_main
= sqrtf( (1.0f
-air
)*attn
*(1.0f
-slide
) * 0.4f
) * gate
,
1187 vol_air
= sqrtf( air
*attn
* 0.5f
) * gate
,
1188 vol_slide
= sqrtf( (1.0f
-air
)*attn
*slide
* 0.25f
) * gate
;
1190 const u32 flags
= AUDIO_FLAG_SPACIAL_3D
|AUDIO_FLAG_LOOP
;
1193 s
->aud_air
= audio_get_first_idle_channel();
1195 audio_channel_init( s
->aud_air
, &audio_board
[1], flags
);
1198 if( !s
->aud_slide
){
1199 s
->aud_slide
= audio_get_first_idle_channel();
1201 audio_channel_init( s
->aud_slide
, &audio_board
[2], flags
);
1205 /* brrrrrrrrrrrt sound for tiles and stuff
1206 * --------------------------------------------------------*/
1207 float sidechain_amt
= 0.0f
,
1208 hz
= vg_maxf( speed
* 2.0f
, 2.0f
);
1210 if( (s
->surface
== k_surface_prop_tiles
) &&
1211 (s
->state
.activity
< k_skate_activity_grind_any
) )
1212 sidechain_amt
= 1.0f
;
1214 sidechain_amt
= 0.0f
;
1216 audio_set_lfo_frequency( 0, hz
);
1217 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar
,
1218 vg_lerpf( 250.0f
, 80.0f
, attn
) );
1220 if( s
->sample_change_cooldown
> 0.0f
){
1221 s
->sample_change_cooldown
-= vg
.time_frame_delta
;
1224 int sample_type
= k_skate_sample_concrete
;
1226 if( s
->state
.activity
== k_skate_activity_grind_5050
){
1227 if( s
->surface
== k_surface_prop_metal
)
1228 sample_type
= k_skate_sample_metal_scrape_generic
;
1230 sample_type
= k_skate_sample_concrete_scrape_metal
;
1232 else if( (s
->state
.activity
== k_skate_activity_grind_back50
) ||
1233 (s
->state
.activity
== k_skate_activity_grind_front50
) )
1235 if( s
->surface
== k_surface_prop_metal
){
1236 sample_type
= k_skate_sample_metal_scrape_generic
;
1239 float a
= v3_dot( player
->rb
.to_world
[2], s
->grind_dir
);
1240 if( fabsf(a
) > 0.70710678118654752f
)
1241 sample_type
= k_skate_sample_concrete_scrape_wood
;
1243 sample_type
= k_skate_sample_concrete_scrape_metal
;
1246 else if( s
->state
.activity
== k_skate_activity_grind_boardslide
){
1247 if( s
->surface
== k_surface_prop_metal
)
1248 sample_type
= k_skate_sample_metal_scrape_generic
;
1250 sample_type
= k_skate_sample_concrete_scrape_wood
;
1253 audio_clip
*relevant_samples
[] = {
1261 if( (s
->main_sample_type
!= sample_type
) || (!s
->aud_main
) ){
1263 audio_channel_crossfade( s
->aud_main
, relevant_samples
[sample_type
],
1265 s
->sample_change_cooldown
= 0.1f
;
1266 s
->main_sample_type
= sample_type
;
1271 s
->aud_main
->colour
= 0x00103efe;
1272 audio_channel_set_spacial( s
->aud_main
, player
->rb
.co
, 40.0f
);
1273 //audio_channel_slope_volume( s->aud_main, 0.05f, vol_main );
1274 audio_channel_edit_volume( s
->aud_main
, vol_main
, 1 );
1275 audio_channel_sidechain_lfo( s
->aud_main
, 0, sidechain_amt
);
1277 float rate
= 1.0f
+ (attn
-0.5f
)*0.2f
;
1278 audio_channel_set_sampling_rate( s
->aud_main
, rate
);
1282 s
->aud_slide
->colour
= 0x00103efe;
1283 audio_channel_set_spacial( s
->aud_slide
, player
->rb
.co
, 40.0f
);
1284 //audio_channel_slope_volume( s->aud_slide, 0.05f, vol_slide );
1285 audio_channel_edit_volume( s
->aud_slide
, vol_slide
, 1 );
1286 audio_channel_sidechain_lfo( s
->aud_slide
, 0, sidechain_amt
);
1290 s
->aud_air
->colour
= 0x00103efe;
1291 audio_channel_set_spacial( s
->aud_air
, player
->rb
.co
, 40.0f
);
1292 //audio_channel_slope_volume( s->aud_air, 0.05f, vol_air );
1293 audio_channel_edit_volume( s
->aud_air
, vol_air
, 1 );
1300 * truck alignment model at ra(local)
1301 * returns 1 if valid surface:
1302 * surface_normal will be filled out with an averaged normal vector
1303 * axel_dir will be the direction from left to right wheels
1305 * returns 0 if no good surface found
1308 int skate_compute_surface_alignment( player_instance
*player
,
1310 v3f surface_normal
, v3f axel_dir
){
1311 struct player_skate
*s
= &player
->_skate
;
1312 world_instance
*world
= world_current_instance();
1314 v3f truck
, left
, right
;
1315 m4x3_mulv( player
->rb
.to_world
, ra
, truck
);
1317 v3_muladds( truck
, player
->rb
.to_world
[0], -k_board_width
, left
);
1318 v3_muladds( truck
, player
->rb
.to_world
[0], k_board_width
, right
);
1319 vg_line( left
, right
, colour
);
1321 float k_max_truck_flex
= VG_PIf
* 0.25f
;
1323 ray_hit ray_l
, ray_r
;
1326 v3_muls( player
->rb
.to_world
[1], -1.0f
, dir
);
1328 int res_l
= 0, res_r
= 0;
1330 for( int i
=0; i
<8; i
++ ){
1331 float t
= 1.0f
- (float)i
* (1.0f
/8.0f
);
1332 v3_muladds( truck
, player
->rb
.to_world
[0], -k_board_radius
*t
, left
);
1333 v3_muladds( left
, player
->rb
.to_world
[1], k_board_radius
, left
);
1334 ray_l
.dist
= 2.1f
* k_board_radius
;
1336 res_l
= ray_world( world
, left
, dir
, &ray_l
, k_material_flag_walking
);
1342 for( int i
=0; i
<8; i
++ ){
1343 float t
= 1.0f
- (float)i
* (1.0f
/8.0f
);
1344 v3_muladds( truck
, player
->rb
.to_world
[0], k_board_radius
*t
, right
);
1345 v3_muladds( right
, player
->rb
.to_world
[1], k_board_radius
, right
);
1346 ray_r
.dist
= 2.1f
* k_board_radius
;
1348 res_r
= ray_world( world
, right
, dir
, &ray_r
, k_material_flag_walking
);
1356 v3f tangent_average
;
1357 v3_muladds( truck
, player
->rb
.to_world
[1], -k_board_radius
, midpoint
);
1358 v3_zero( tangent_average
);
1360 if( res_l
|| res_r
){
1362 v3_copy( midpoint
, p0
);
1363 v3_copy( midpoint
, p1
);
1366 v3_copy( ray_l
.pos
, p0
);
1367 v3_cross( ray_l
.normal
, player
->rb
.to_world
[0], t
);
1368 v3_add( t
, tangent_average
, tangent_average
);
1371 v3_copy( ray_r
.pos
, p1
);
1372 v3_cross( ray_r
.normal
, player
->rb
.to_world
[0], t
);
1373 v3_add( t
, tangent_average
, tangent_average
);
1376 v3_sub( p1
, p0
, v0
);
1380 /* fallback: use the closes point to the trucks */
1382 int idx
= bh_closest_point( world
->geo_bh
, midpoint
, closest
, 0.1f
);
1385 u32
*tri
= &world
->scene_geo
.arrindices
[ idx
* 3 ];
1388 for( int j
=0; j
<3; j
++ )
1389 v3_copy( world
->scene_geo
.arrvertices
[ tri
[j
] ].co
, verts
[j
] );
1391 v3f vert0
, vert1
, n
;
1392 v3_sub( verts
[1], verts
[0], vert0
);
1393 v3_sub( verts
[2], verts
[0], vert1
);
1394 v3_cross( vert0
, vert1
, n
);
1397 if( v3_dot( n
, player
->rb
.to_world
[1] ) < 0.3f
)
1400 v3_cross( n
, player
->rb
.to_world
[2], v0
);
1401 v3_muladds( v0
, player
->rb
.to_world
[2],
1402 -v3_dot( player
->rb
.to_world
[2], v0
), v0
);
1406 v3_cross( n
, player
->rb
.to_world
[0], t
);
1407 v3_add( t
, tangent_average
, tangent_average
);
1413 v3_muladds( truck
, v0
, k_board_width
, right
);
1414 v3_muladds( truck
, v0
, -k_board_width
, left
);
1416 vg_line( left
, right
, VG__WHITE
);
1418 v3_normalize( tangent_average
);
1419 v3_cross( v0
, tangent_average
, surface_normal
);
1420 v3_copy( v0
, axel_dir
);
1425 VG_STATIC
void skate_weight_distribute( player_instance
*player
){
1426 struct player_skate
*s
= &player
->_skate
;
1427 v3_zero( s
->weight_distribution
);
1429 int reverse_dir
= v3_dot( player
->rb
.to_world
[2], player
->rb
.v
) < 0.0f
?1:-1;
1432 joystick_state( k_srjoystick_steer
, steer
);
1434 if( s
->state
.manual_direction
== 0 ){
1435 if( (steer
[1] > 0.7f
) && (s
->state
.activity
== k_skate_activity_ground
) &&
1436 (s
->state
.jump_charge
<= 0.01f
) )
1437 s
->state
.manual_direction
= reverse_dir
;
1440 if( steer
[1] < 0.1f
){
1441 s
->state
.manual_direction
= 0;
1444 if( reverse_dir
!= s
->state
.manual_direction
){
1450 if( s
->state
.manual_direction
){
1451 float amt
= vg_minf( steer
[1] * 8.0f
, 1.0f
);
1452 s
->weight_distribution
[2] = k_board_length
* amt
*
1453 (float)s
->state
.manual_direction
;
1456 if( s
->state
.manual_direction
){
1459 m3x3_mulv( player
->rb
.to_world
, s
->weight_distribution
, plane_z
);
1460 v3_negate( plane_z
, plane_z
);
1462 v3_muladds( plane_z
, s
->surface_picture
,
1463 -v3_dot( plane_z
, s
->surface_picture
), plane_z
);
1464 v3_normalize( plane_z
);
1466 v3_muladds( plane_z
, s
->surface_picture
, 0.3f
, plane_z
);
1467 v3_normalize( plane_z
);
1470 v3_muladds( player
->rb
.co
, plane_z
, 1.5f
, p1
);
1471 vg_line( player
->rb
.co
, p1
, VG__GREEN
);
1474 v3_muls( player
->rb
.to_world
[2], -(float)s
->state
.manual_direction
,
1477 rb_effect_spring_target_vector( &player
->rb
, refdir
, plane_z
,
1478 k_manul_spring
, k_manul_dampener
,
1483 VG_STATIC
void skate_adjust_up_direction( player_instance
*player
){
1484 struct player_skate
*s
= &player
->_skate
;
1486 if( s
->state
.activity
== k_skate_activity_ground
){
1488 v3_copy( s
->surface_picture
, target
);
1490 target
[1] += 2.0f
* s
->surface_picture
[1];
1491 v3_normalize( target
);
1493 v3_lerp( s
->state
.up_dir
, target
,
1494 8.0f
* s
->substep_delta
, s
->state
.up_dir
);
1496 else if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
1497 v3_lerp( s
->state
.up_dir
, player
->rb
.to_world
[1],
1498 8.0f
* s
->substep_delta
, s
->state
.up_dir
);
1501 v3_lerp( s
->state
.up_dir
, player
->basis
[1],
1502 12.0f
* s
->substep_delta
, s
->state
.up_dir
);
1506 VG_STATIC
int skate_point_visible( v3f origin
, v3f target
){
1508 v3_sub( target
, origin
, dir
);
1511 ray
.dist
= v3_length( dir
);
1512 v3_muls( dir
, 1.0f
/ray
.dist
, dir
);
1515 if( ray_world( world_current_instance(), origin
, dir
, &ray
,
1516 k_material_flag_walking
) )
1522 VG_STATIC
void skate_grind_orient( struct grind_info
*inf
, m3x3f mtx
){
1523 v3_copy( inf
->dir
, mtx
[0] );
1524 v3_copy( inf
->n
, mtx
[1] );
1525 v3_cross( mtx
[0], mtx
[1], mtx
[2] );
1528 VG_STATIC
void skate_grind_friction( player_instance
*player
,
1529 struct grind_info
*inf
, float strength
){
1531 v3_muladds( player
->rb
.to_world
[2], inf
->n
,
1532 -v3_dot( player
->rb
.to_world
[2], inf
->n
), v2
);
1534 float a
= 1.0f
-fabsf( v3_dot( v2
, inf
->dir
) ),
1535 dir
= vg_signf( v3_dot( player
->rb
.v
, inf
->dir
) ),
1536 F
= a
* -dir
* k_grind_max_friction
;
1538 v3_muladds( player
->rb
.v
, inf
->dir
, F
*k_rb_delta
*strength
, player
->rb
.v
);
1541 VG_STATIC
void skate_grind_decay( player_instance
*player
,
1542 struct grind_info
*inf
, float strength
){
1544 skate_grind_orient( inf
, mtx
);
1545 m3x3_transpose( mtx
, mtx_inv
);
1548 m3x3_mulv( mtx_inv
, player
->rb
.v
, v_grind
);
1550 float decay
= 1.0f
- ( k_rb_delta
* k_grind_decayxy
* strength
);
1551 v3_mul( v_grind
, (v3f
){ 1.0f
, decay
, decay
}, v_grind
);
1552 m3x3_mulv( mtx
, v_grind
, player
->rb
.v
);
1555 VG_STATIC
void skate_grind_truck_apply( player_instance
*player
,
1556 float sign
, struct grind_info
*inf
,
1558 struct player_skate
*s
= &player
->_skate
;
1561 v3f ra
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1563 m3x3_mulv( player
->rb
.to_world
, ra
, raw
);
1564 v3_add( player
->rb
.co
, raw
, wsp
);
1566 v3_copy( ra
, s
->weight_distribution
);
1569 v3_sub( inf
->co
, wsp
, delta
);
1572 v3_muladds( player
->rb
.v
, delta
, k_spring_force
*strength
*k_rb_delta
,
1575 skate_grind_decay( player
, inf
, strength
);
1576 skate_grind_friction( player
, inf
, strength
);
1578 /* yeah yeah yeah yeah */
1579 v3f raw_nplane
, axis
;
1580 v3_muladds( raw
, inf
->n
, -v3_dot( inf
->n
, raw
), raw_nplane
);
1581 v3_cross( raw_nplane
, inf
->n
, axis
);
1582 v3_normalize( axis
);
1586 skate_grind_orient( inf
, mtx
);
1587 v3f target_fwd
, fwd
, up
, target_up
;
1588 m3x3_mulv( mtx
, s
->grind_vec
, target_fwd
);
1589 v3_copy( raw_nplane
, fwd
);
1590 v3_copy( player
->rb
.to_world
[1], up
);
1591 v3_copy( inf
->n
, target_up
);
1593 v3_muladds( target_fwd
, inf
->n
, -v3_dot(inf
->n
,target_fwd
), target_fwd
);
1594 v3_muladds( fwd
, inf
->n
, -v3_dot(inf
->n
,fwd
), fwd
);
1596 v3_normalize( target_fwd
);
1597 v3_normalize( fwd
);
1600 joystick_state( k_srjoystick_steer
, steer
);
1602 float way
= steer
[1] * vg_signf( v3_dot( raw_nplane
, player
->rb
.v
) );
1605 q_axis_angle( q
, axis
, VG_PIf
*0.125f
* way
);
1606 q_mulv( q
, target_up
, target_up
);
1607 q_mulv( q
, target_fwd
, target_fwd
);
1609 rb_effect_spring_target_vector( &player
->rb
, up
, target_up
,
1614 rb_effect_spring_target_vector( &player
->rb
, fwd
, target_fwd
,
1615 k_grind_spring
*strength
,
1616 k_grind_dampener
*strength
,
1619 vg_line_arrow( player
->rb
.co
, target_up
, 1.0f
, VG__GREEN
);
1620 vg_line_arrow( player
->rb
.co
, fwd
, 0.8f
, VG__RED
);
1621 vg_line_arrow( player
->rb
.co
, target_fwd
, 1.0f
, VG__YELOW
);
1623 s
->grind_strength
= strength
;
1626 struct grind_limit
*limit
= &s
->limits
[ s
->limit_count
++ ];
1627 m4x3_mulv( player
->rb
.to_local
, wsp
, limit
->ra
);
1628 m3x3_mulv( player
->rb
.to_local
, inf
->n
, limit
->n
);
1631 v3_copy( inf
->dir
, s
->grind_dir
);
1634 VG_STATIC
void skate_5050_apply( player_instance
*player
,
1635 struct grind_info
*inf_front
,
1636 struct grind_info
*inf_back
)
1638 struct player_skate
*s
= &player
->_skate
;
1639 struct grind_info inf_avg
;
1641 v3_sub( inf_front
->co
, inf_back
->co
, inf_avg
.dir
);
1642 v3_muladds( inf_back
->co
, inf_avg
.dir
, 0.5f
, inf_avg
.co
);
1643 v3_normalize( inf_avg
.dir
);
1646 v3_muls( inf_avg
.dir
, vg_signf(v3_dot(inf_avg
.dir
,player
->rb
.v
)),
1649 v3f axis_front
, axis_back
, axis
;
1650 v3_cross( inf_front
->dir
, inf_front
->n
, axis_front
);
1651 v3_cross( inf_back
->dir
, inf_back
->n
, axis_back
);
1652 v3_add( axis_front
, axis_back
, axis
);
1653 v3_normalize( axis
);
1655 v3_cross( axis
, inf_avg
.dir
, inf_avg
.n
);
1656 skate_grind_decay( player
, &inf_avg
, 1.0f
);
1659 joystick_state( k_srjoystick_steer
, steer
);
1661 float way
= steer
[1] *
1662 vg_signf( v3_dot( player
->rb
.to_world
[2], player
->rb
.v
) );
1665 v3_copy( player
->rb
.to_world
[1], up
);
1666 v3_copy( inf_avg
.n
, target_up
);
1667 q_axis_angle( q
, player
->rb
.to_world
[0], VG_PIf
*0.25f
* -way
);
1668 q_mulv( q
, target_up
, target_up
);
1670 v3_zero( s
->weight_distribution
);
1671 s
->weight_distribution
[2] = k_board_length
* -way
;
1673 rb_effect_spring_target_vector( &player
->rb
, up
, target_up
,
1677 vg_line_arrow( player
->rb
.co
, up
, 1.0f
, VG__GREEN
);
1678 vg_line_arrow( player
->rb
.co
, target_up
, 1.0f
, VG__GREEN
);
1680 v3f fwd_nplane
, dir_nplane
;
1681 v3_muladds( player
->rb
.to_world
[2], inf_avg
.n
,
1682 -v3_dot( player
->rb
.to_world
[2], inf_avg
.n
), fwd_nplane
);
1685 v3_muls( inf_avg
.dir
, v3_dot( fwd_nplane
, inf_avg
.dir
), dir
);
1686 v3_muladds( dir
, inf_avg
.n
, -v3_dot( dir
, inf_avg
.n
), dir_nplane
);
1688 v3_normalize( fwd_nplane
);
1689 v3_normalize( dir_nplane
);
1691 rb_effect_spring_target_vector( &player
->rb
, fwd_nplane
, dir_nplane
,
1695 vg_line_arrow( player
->rb
.co
, fwd_nplane
, 0.8f
, VG__RED
);
1696 vg_line_arrow( player
->rb
.co
, dir_nplane
, 0.8f
, VG__RED
);
1698 v3f pos_front
= { 0.0f
, -k_board_radius
, -1.0f
* k_board_length
},
1699 pos_back
= { 0.0f
, -k_board_radius
, 1.0f
* k_board_length
},
1700 delta_front
, delta_back
, delta_total
;
1702 m4x3_mulv( player
->rb
.to_world
, pos_front
, pos_front
);
1703 m4x3_mulv( player
->rb
.to_world
, pos_back
, pos_back
);
1705 v3_sub( inf_front
->co
, pos_front
, delta_front
);
1706 v3_sub( inf_back
->co
, pos_back
, delta_back
);
1707 v3_add( delta_front
, delta_back
, delta_total
);
1709 v3_muladds( player
->rb
.v
, delta_total
, 50.0f
* k_rb_delta
, player
->rb
.v
);
1712 struct grind_limit
*limit
= &s
->limits
[ s
->limit_count
++ ];
1713 v3_zero( limit
->ra
);
1714 m3x3_mulv( player
->rb
.to_local
, inf_avg
.n
, limit
->n
);
1717 v3_copy( inf_avg
.dir
, s
->grind_dir
);
1720 VG_STATIC
int skate_grind_truck_renew( player_instance
*player
, float sign
,
1721 struct grind_info
*inf
)
1723 struct player_skate
*s
= &player
->_skate
;
1725 v3f wheel_co
= { 0.0f
, 0.0f
, sign
* k_board_length
},
1726 grind_co
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1728 m4x3_mulv( player
->rb
.to_world
, wheel_co
, wheel_co
);
1729 m4x3_mulv( player
->rb
.to_world
, grind_co
, grind_co
);
1731 /* Exit condition: lost grind tracking */
1732 if( !skate_grind_scansq( player
, grind_co
, player
->rb
.v
, 0.3f
, inf
) )
1735 /* Exit condition: cant see grind target directly */
1736 if( !skate_point_visible( wheel_co
, inf
->co
) )
1739 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1740 float dv
= fabsf(v3_dot( player
->rb
.v
, inf
->dir
)),
1741 minv
= k_grind_axel_min_vel
*0.8f
;
1746 if( fabsf(v3_dot( inf
->dir
, s
->grind_dir
)) < k_grind_max_edge_angle
)
1749 v3_copy( inf
->dir
, s
->grind_dir
);
1753 VG_STATIC
int skate_grind_truck_entry( player_instance
*player
, float sign
,
1754 struct grind_info
*inf
)
1756 struct player_skate
*s
= &player
->_skate
;
1759 v3f ra
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1762 m3x3_mulv( player
->rb
.to_world
, ra
, raw
);
1763 v3_add( player
->rb
.co
, raw
, wsp
);
1765 if( skate_grind_scansq( player
, wsp
, player
->rb
.v
, 0.3, inf
) )
1767 if( fabsf(v3_dot( player
->rb
.v
, inf
->dir
)) < k_grind_axel_min_vel
)
1770 /* velocity should be at least 60% aligned */
1772 v3_cross( inf
->n
, inf
->dir
, axis
);
1773 v3_muladds( player
->rb
.v
, inf
->n
, -v3_dot( player
->rb
.v
, inf
->n
), pv
);
1775 if( v3_length2( pv
) < 0.0001f
)
1779 if( fabsf(v3_dot( pv
, inf
->dir
)) < k_grind_axel_max_angle
)
1782 if( v3_dot( player
->rb
.v
, inf
->n
) > 0.5f
)
1786 /* check for vertical alignment */
1787 if( v3_dot( player
->rb
.to_world
[1], inf
->n
) < k_grind_axel_max_vangle
)
1791 v3f local_co
, local_dir
, local_n
;
1792 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1793 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1794 m3x3_mulv( player
->rb
.to_local
, inf
->n
, local_n
);
1796 v2f delta
= { local_co
[0], local_co
[2] - k_board_length
*sign
};
1798 float truck_height
= -(k_board_radius
+0.03f
);
1801 v3_cross( player
->rb
.w
, raw
, rv
);
1802 v3_add( player
->rb
.v
, rv
, rv
);
1804 if( (local_co
[1] >= truck_height
) &&
1805 (v2_length2( delta
) <= k_board_radius
*k_board_radius
) )
1814 VG_STATIC
void skate_boardslide_apply( player_instance
*player
,
1815 struct grind_info
*inf
)
1817 struct player_skate
*s
= &player
->_skate
;
1819 v3f local_co
, local_dir
, local_n
;
1820 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1821 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1822 m3x3_mulv( player
->rb
.to_local
, inf
->n
, local_n
);
1825 v3_muladds( local_co
, local_dir
, local_co
[0]/-local_dir
[0],
1827 v3_copy( intersection
, s
->weight_distribution
);
1829 skate_grind_decay( player
, inf
, 0.0125f
);
1830 skate_grind_friction( player
, inf
, 0.25f
);
1832 /* direction alignment */
1834 v3_cross( local_dir
, local_n
, perp
);
1835 v3_muls( local_dir
, vg_signf(local_dir
[0]), dir
);
1836 v3_muls( perp
, vg_signf(perp
[2]), perp
);
1838 m3x3_mulv( player
->rb
.to_world
, dir
, dir
);
1839 m3x3_mulv( player
->rb
.to_world
, perp
, perp
);
1842 q_axis_angle( qbalance
, dir
, local_co
[0]*k_grind_balance
);
1843 q_mulv( qbalance
, perp
, perp
);
1845 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[0],
1847 k_grind_spring
, k_grind_dampener
,
1850 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[2],
1852 k_grind_spring
, k_grind_dampener
,
1855 vg_line_arrow( player
->rb
.co
, dir
, 0.5f
, VG__GREEN
);
1856 vg_line_arrow( player
->rb
.co
, perp
, 0.5f
, VG__BLUE
);
1858 v3_copy( inf
->dir
, s
->grind_dir
);
1861 VG_STATIC
int skate_boardslide_entry( player_instance
*player
,
1862 struct grind_info
*inf
)
1864 struct player_skate
*s
= &player
->_skate
;
1866 if( skate_grind_scansq( player
, player
->rb
.co
,
1867 player
->rb
.to_world
[0], k_board_length
,
1870 v3f local_co
, local_dir
;
1871 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1872 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1874 if( (fabsf(local_co
[2]) <= k_board_length
) && /* within wood area */
1875 (local_co
[1] >= 0.0f
) && /* at deck level */
1876 (fabsf(local_dir
[0]) >= 0.25f
) ) /* perpendicular to us */
1878 if( fabsf(v3_dot( player
->rb
.v
, inf
->dir
)) < k_grind_axel_min_vel
)
1888 VG_STATIC
int skate_boardslide_renew( player_instance
*player
,
1889 struct grind_info
*inf
)
1891 struct player_skate
*s
= &player
->_skate
;
1893 if( !skate_grind_scansq( player
, player
->rb
.co
,
1894 player
->rb
.to_world
[0], k_board_length
,
1898 /* Exit condition: cant see grind target directly */
1900 v3_muladds( player
->rb
.co
, player
->rb
.to_world
[1], 0.2f
, vis
);
1901 if( !skate_point_visible( vis
, inf
->co
) )
1904 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1905 float dv
= fabsf(v3_dot( player
->rb
.v
, inf
->dir
)),
1906 minv
= k_grind_axel_min_vel
*0.8f
;
1911 if( fabsf(v3_dot( inf
->dir
, s
->grind_dir
)) < k_grind_max_edge_angle
)
1917 VG_STATIC
void skate_store_grind_vec( player_instance
*player
,
1918 struct grind_info
*inf
)
1920 struct player_skate
*s
= &player
->_skate
;
1923 skate_grind_orient( inf
, mtx
);
1924 m3x3_transpose( mtx
, mtx
);
1927 v3_sub( inf
->co
, player
->rb
.co
, raw
);
1929 m3x3_mulv( mtx
, raw
, s
->grind_vec
);
1930 v3_normalize( s
->grind_vec
);
1931 v3_copy( inf
->dir
, s
->grind_dir
);
1934 VG_STATIC
enum skate_activity
skate_availible_grind( player_instance
*player
)
1936 struct player_skate
*s
= &player
->_skate
;
1938 if( s
->state
.grind_cooldown
> 100 ){
1939 vg_fatal_error( "wth!\n" );
1942 /* debounces this state manager a little bit */
1943 if( s
->state
.grind_cooldown
){
1944 s
->state
.grind_cooldown
--;
1945 return k_skate_activity_undefined
;
1948 struct grind_info inf_back50
,
1960 joystick_state( k_srjoystick_steer
, steer
);
1962 if( s
->state
.activity
== k_skate_activity_grind_5050
||
1963 s
->state
.activity
== k_skate_activity_grind_back50
||
1964 s
->state
.activity
== k_skate_activity_grind_front50
)
1966 float tilt
= steer
[1];
1968 if( fabsf(tilt
) >= 0.25f
){
1969 v3f raw
= {0.0f
,0.0f
,tilt
};
1970 m3x3_mulv( player
->rb
.to_world
, raw
, raw
);
1972 float way
= tilt
* vg_signf( v3_dot( raw
, player
->rb
.v
) );
1974 if( way
< 0.0f
) allow_front
= 0;
1975 else allow_back
= 0;
1979 if( s
->state
.activity
== k_skate_activity_grind_boardslide
){
1980 res_slide
= skate_boardslide_renew( player
, &inf_slide
);
1982 else if( s
->state
.activity
== k_skate_activity_grind_back50
){
1983 res_back50
= skate_grind_truck_renew( player
, 1.0f
, &inf_back50
);
1986 res_front50
= skate_grind_truck_entry( player
, -1.0f
, &inf_front50
);
1988 else if( s
->state
.activity
== k_skate_activity_grind_front50
){
1989 res_front50
= skate_grind_truck_renew( player
, -1.0f
, &inf_front50
);
1992 res_back50
= skate_grind_truck_entry( player
, 1.0f
, &inf_back50
);
1994 else if( s
->state
.activity
== k_skate_activity_grind_5050
){
1996 res_front50
= skate_grind_truck_renew( player
, -1.0f
, &inf_front50
);
1998 res_back50
= skate_grind_truck_renew( player
, 1.0f
, &inf_back50
);
2001 res_slide
= skate_boardslide_entry( player
, &inf_slide
);
2004 res_back50
= skate_grind_truck_entry( player
, 1.0f
, &inf_back50
);
2007 res_front50
= skate_grind_truck_entry( player
, -1.0f
, &inf_front50
);
2009 if( res_back50
!= res_front50
){
2010 int wants_to_do_that
= fabsf(steer
[1]) >= 0.25f
;
2012 res_back50
&= wants_to_do_that
;
2013 res_front50
&= wants_to_do_that
;
2017 const enum skate_activity table
[] =
2018 { /* slide | back | front */
2019 k_skate_activity_undefined
, /* 0 0 0 */
2020 k_skate_activity_grind_front50
, /* 0 0 1 */
2021 k_skate_activity_grind_back50
, /* 0 1 0 */
2022 k_skate_activity_grind_5050
, /* 0 1 1 */
2024 /* slide has priority always */
2025 k_skate_activity_grind_boardslide
, /* 1 0 0 */
2026 k_skate_activity_grind_boardslide
, /* 1 0 1 */
2027 k_skate_activity_grind_boardslide
, /* 1 1 0 */
2028 k_skate_activity_grind_boardslide
, /* 1 1 1 */
2030 , new_activity
= table
[ res_slide
<< 2 | res_back50
<< 1 | res_front50
];
2032 if( new_activity
== k_skate_activity_undefined
){
2033 if( s
->state
.activity
>= k_skate_activity_grind_any
){
2034 s
->state
.grind_cooldown
= 15;
2035 s
->state
.surface_cooldown
= 10;
2038 else if( new_activity
== k_skate_activity_grind_boardslide
){
2039 skate_boardslide_apply( player
, &inf_slide
);
2041 else if( new_activity
== k_skate_activity_grind_back50
){
2042 if( s
->state
.activity
!= k_skate_activity_grind_back50
)
2043 skate_store_grind_vec( player
, &inf_back50
);
2045 skate_grind_truck_apply( player
, 1.0f
, &inf_back50
, 1.0f
);
2047 else if( new_activity
== k_skate_activity_grind_front50
){
2048 if( s
->state
.activity
!= k_skate_activity_grind_front50
)
2049 skate_store_grind_vec( player
, &inf_front50
);
2051 skate_grind_truck_apply( player
, -1.0f
, &inf_front50
, 1.0f
);
2053 else if( new_activity
== k_skate_activity_grind_5050
)
2054 skate_5050_apply( player
, &inf_front50
, &inf_back50
);
2056 return new_activity
;
2059 VG_STATIC
void player__skate_update( player_instance
*player
)
2061 struct player_skate
*s
= &player
->_skate
;
2062 world_instance
*world
= world_current_instance();
2064 if( world
->water
.enabled
){
2065 if( player
->rb
.co
[1]+0.25f
< world
->water
.height
){
2066 audio_oneshot_3d( &audio_splash
, player
->rb
.co
, 40.0f
, 1.0f
);
2067 player__skate_kill_audio( player
);
2068 player__dead_transition( player
);
2073 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
2074 s
->state
.activity_prev
= s
->state
.activity
;
2076 v3_zero( normal_total
);
2078 struct board_collider
2085 enum board_collider_state
2087 k_collider_state_default
,
2088 k_collider_state_disabled
,
2089 k_collider_state_colliding
2096 { 0.0f
, 0.0f
, -k_board_length
},
2097 .radius
= k_board_radius
,
2101 { 0.0f
, 0.0f
, k_board_length
},
2102 .radius
= k_board_radius
,
2109 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
2110 float min_dist
= 0.6f
;
2111 for( int i
=0; i
<2; i
++ ){
2113 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, wpos
);
2115 if( bh_closest_point( world
->geo_bh
, wpos
, closest
, min_dist
) != -1 ){
2116 min_dist
= vg_minf( min_dist
, v3_dist( closest
, wpos
) );
2120 float vy
= v3_dot( player
->basis
[1], player
->rb
.v
);
2121 vy
= vg_maxf( 0.0f
, vy
);
2123 slap
= vg_clampf( (min_dist
/0.5f
) + vy
, 0.0f
, 1.0f
)*0.3f
;
2125 s
->state
.slap
= vg_lerpf( s
->state
.slap
, slap
, 10.0f
*k_rb_delta
);
2127 wheels
[0].pos
[1] = s
->state
.slap
;
2128 wheels
[1].pos
[1] = s
->state
.slap
;
2131 const int k_wheel_count
= 2;
2133 s
->substep
= k_rb_delta
;
2134 s
->substep_delta
= s
->substep
;
2137 int substep_count
= 0;
2139 v3_zero( s
->surface_picture
);
2141 int prev_contacts
[2];
2143 for( int i
=0; i
<k_wheel_count
; i
++ ){
2144 wheels
[i
].state
= k_collider_state_default
;
2145 prev_contacts
[i
] = s
->wheel_contacts
[i
];
2148 /* check if we can enter or continue grind */
2149 enum skate_activity grindable_activity
= skate_availible_grind( player
);
2150 if( grindable_activity
!= k_skate_activity_undefined
){
2151 s
->state
.activity
= grindable_activity
;
2155 int contact_count
= 0;
2156 for( int i
=0; i
<2; i
++ ){
2158 v3_copy( player
->rb
.to_world
[0], axel
);
2160 if( skate_compute_surface_alignment( player
, wheels
[i
].pos
,
2161 wheels
[i
].colour
, normal
, axel
) )
2163 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[0],
2165 k_surface_spring
, k_surface_dampener
,
2168 v3_add( normal
, s
->surface_picture
, s
->surface_picture
);
2170 s
->wheel_contacts
[i
] = 1;
2173 s
->wheel_contacts
[i
] = 0;
2176 m3x3_mulv( player
->rb
.to_local
, axel
, s
->truckv0
[i
] );
2179 if( s
->state
.surface_cooldown
){
2180 s
->state
.surface_cooldown
--;
2184 if( (prev_contacts
[0]+prev_contacts
[1] == 1) && (contact_count
== 2) ){
2186 for( int i
=0; i
<2; i
++ ){
2187 if( !prev_contacts
[i
] ){
2189 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, co
);
2190 audio_oneshot_3d( &audio_taps
[vg_randu32()%4], co
, 40.0f
, 0.75f
);
2196 if( contact_count
){
2197 s
->state
.activity
= k_skate_activity_ground
;
2198 s
->state
.gravity_bias
= k_gravity
;
2199 v3_normalize( s
->surface_picture
);
2201 skate_apply_friction_model( player
);
2202 skate_weight_distribute( player
);
2205 if( s
->state
.activity
> k_skate_activity_air_to_grind
)
2206 s
->state
.activity
= k_skate_activity_air
;
2208 v3_zero( s
->weight_distribution
);
2209 skate_apply_air_model( player
);
2214 if( s
->state
.activity
== k_skate_activity_grind_back50
)
2215 wheels
[1].state
= k_collider_state_disabled
;
2216 if( s
->state
.activity
== k_skate_activity_grind_front50
)
2217 wheels
[0].state
= k_collider_state_disabled
;
2218 if( s
->state
.activity
== k_skate_activity_grind_5050
){
2219 wheels
[0].state
= k_collider_state_disabled
;
2220 wheels
[1].state
= k_collider_state_disabled
;
2223 /* all activities */
2224 skate_apply_steering_model( player
);
2225 skate_adjust_up_direction( player
);
2226 skate_apply_cog_model( player
);
2227 skate_apply_jump_model( player
);
2228 skate_apply_grab_model( player
);
2229 skate_apply_trick_model( player
);
2230 skate_apply_pump_model( player
);
2235 * Phase 0: Continous collision detection
2236 * --------------------------------------------------------------------------
2239 v3f head_wp0
, head_wp1
, start_co
;
2240 m4x3_mulv( player
->rb
.to_world
, s
->state
.head_position
, head_wp0
);
2241 v3_copy( player
->rb
.co
, start_co
);
2243 /* calculate transform one step into future */
2246 v3_muladds( player
->rb
.co
, player
->rb
.v
, s
->substep
, future_co
);
2248 if( v3_length2( player
->rb
.w
) > 0.0f
){
2251 v3_copy( player
->rb
.w
, axis
);
2253 float mag
= v3_length( axis
);
2254 v3_divs( axis
, mag
, axis
);
2255 q_axis_angle( rotation
, axis
, mag
*s
->substep
);
2256 q_mul( rotation
, player
->rb
.q
, future_q
);
2257 q_normalize( future_q
);
2260 v4_copy( player
->rb
.q
, future_q
);
2262 v3f future_cg
, current_cg
, cg_offset
;
2263 q_mulv( player
->rb
.q
, s
->weight_distribution
, current_cg
);
2264 q_mulv( future_q
, s
->weight_distribution
, future_cg
);
2265 v3_sub( future_cg
, current_cg
, cg_offset
);
2267 /* calculate the minimum time we can move */
2268 float max_time
= s
->substep
;
2270 for( int i
=0; i
<k_wheel_count
; i
++ ){
2271 if( wheels
[i
].state
== k_collider_state_disabled
)
2274 v3f current
, future
, r_cg
;
2276 q_mulv( future_q
, wheels
[i
].pos
, future
);
2277 v3_add( future
, future_co
, future
);
2278 v3_add( cg_offset
, future
, future
);
2280 q_mulv( player
->rb
.q
, wheels
[i
].pos
, current
);
2281 v3_add( current
, player
->rb
.co
, current
);
2286 float cast_radius
= wheels
[i
].radius
- k_penetration_slop
* 2.0f
;
2287 if( spherecast_world( world
, current
, future
, cast_radius
, &t
, n
,
2288 k_material_flag_walking
) != -1)
2289 max_time
= vg_minf( max_time
, t
* s
->substep
);
2292 /* clamp to a fraction of delta, to prevent locking */
2293 float rate_lock
= substep_count
;
2294 rate_lock
*= k_rb_delta
* 0.1f
;
2295 rate_lock
*= rate_lock
;
2297 max_time
= vg_maxf( max_time
, rate_lock
);
2298 s
->substep_delta
= max_time
;
2301 v3_muladds( player
->rb
.co
, player
->rb
.v
, s
->substep_delta
, player
->rb
.co
);
2302 if( v3_length2( player
->rb
.w
) > 0.0f
){
2305 v3_copy( player
->rb
.w
, axis
);
2307 float mag
= v3_length( axis
);
2308 v3_divs( axis
, mag
, axis
);
2309 q_axis_angle( rotation
, axis
, mag
*s
->substep_delta
);
2310 q_mul( rotation
, player
->rb
.q
, player
->rb
.q
);
2311 q_normalize( player
->rb
.q
);
2313 q_mulv( player
->rb
.q
, s
->weight_distribution
, future_cg
);
2314 v3_sub( current_cg
, future_cg
, cg_offset
);
2315 v3_add( player
->rb
.co
, cg_offset
, player
->rb
.co
);
2318 rb_update_transform( &player
->rb
);
2319 v3_muladds( player
->rb
.v
, player
->basis
[1],
2320 -s
->state
.gravity_bias
* s
->substep_delta
, player
->rb
.v
);
2322 s
->substep
-= s
->substep_delta
;
2324 rb_ct manifold
[128];
2325 int manifold_len
= 0;
2327 * Phase -1: head detection
2328 * --------------------------------------------------------------------------
2330 m4x3_mulv( player
->rb
.to_world
, s
->state
.head_position
, head_wp1
);
2334 if( (v3_dist2( head_wp0
, head_wp1
) > 0.001f
) &&
2335 (spherecast_world( world
, head_wp0
, head_wp1
, 0.2f
, &t
, n
,
2336 k_material_flag_walking
) != -1) )
2338 v3_lerp( start_co
, player
->rb
.co
, t
, player
->rb
.co
);
2339 rb_update_transform( &player
->rb
);
2341 player__skate_kill_audio( player
);
2342 player__dead_transition( player
);
2347 * Phase 1: Regular collision detection
2348 * --------------------------------------------------------------------------
2351 for( int i
=0; i
<k_wheel_count
; i
++ ){
2352 if( wheels
[i
].state
== k_collider_state_disabled
)
2356 m3x3_identity( mtx
);
2357 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, mtx
[3] );
2359 rb_sphere collider
= { .radius
= wheels
[i
].radius
};
2361 rb_ct
*man
= &manifold
[ manifold_len
];
2363 int l
= skate_collide_smooth( player
, mtx
, &collider
, man
);
2365 wheels
[i
].state
= k_collider_state_colliding
;
2370 float grind_radius
= k_board_radius
* 0.75f
;
2371 rb_capsule capsule
= { .height
= (k_board_length
+0.2f
)*2.0f
,
2372 .radius
=grind_radius
};
2374 v3_muls( player
->rb
.to_world
[0], 1.0f
, mtx
[0] );
2375 v3_muls( player
->rb
.to_world
[2], -1.0f
, mtx
[1] );
2376 v3_muls( player
->rb
.to_world
[1], 1.0f
, mtx
[2] );
2377 v3_muladds( player
->rb
.to_world
[3], player
->rb
.to_world
[1],
2378 grind_radius
+ k_board_radius
*0.25f
+s
->state
.slap
, mtx
[3] );
2380 rb_ct
*cman
= &manifold
[manifold_len
];
2382 int l
= rb_capsule__scene( mtx
, &capsule
, NULL
, &world
->rb_geo
.inf
.scene
,
2383 cman
, k_material_flag_walking
);
2386 for( int i
=0; i
<l
; i
++ )
2387 cman
[l
].type
= k_contact_type_edge
;
2388 rb_manifold_filter_joint_edges( cman
, l
, 0.03f
);
2389 l
= rb_manifold_apply_filtered( cman
, l
);
2394 vg_line_capsule( mtx
, capsule
.radius
, capsule
.height
, VG__WHITE
);
2397 if( s
->state
.activity
>= k_skate_activity_grind_any
){
2398 for( int i
=0; i
<s
->limit_count
; i
++ ){
2399 struct grind_limit
*limit
= &s
->limits
[i
];
2400 rb_ct
*ct
= &manifold
[ manifold_len
++ ];
2401 m4x3_mulv( player
->rb
.to_world
, limit
->ra
, ct
->co
);
2402 m3x3_mulv( player
->rb
.to_world
, limit
->n
, ct
->n
);
2404 ct
->type
= k_contact_type_default
;
2410 * --------------------------------------------------------------------------
2415 m4x3_mulv( player
->rb
.to_world
, s
->weight_distribution
, world_cog
);
2416 vg_line_point( world_cog
, 0.02f
, VG__BLACK
);
2418 for( int i
=0; i
<manifold_len
; i
++ ){
2419 rb_prepare_contact( &manifold
[i
], s
->substep_delta
);
2420 rb_debug_contact( &manifold
[i
] );
2423 /* yes, we are currently rebuilding mass matrices every frame. too bad! */
2424 v3f extent
= { k_board_width
, 0.1f
, k_board_length
};
2425 float ex2
= k_board_interia
*extent
[0]*extent
[0],
2426 ey2
= k_board_interia
*extent
[1]*extent
[1],
2427 ez2
= k_board_interia
*extent
[2]*extent
[2];
2429 float mass
= 2.0f
* (extent
[0]*extent
[1]*extent
[2]);
2430 float inv_mass
= 1.0f
/mass
;
2433 I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
2434 I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
2435 I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
2438 m3x3_identity( iI
);
2445 m3x3_mul( iI
, player
->rb
.to_local
, iIw
);
2446 m3x3_mul( player
->rb
.to_world
, iIw
, iIw
);
2448 for( int j
=0; j
<10; j
++ ){
2449 for( int i
=0; i
<manifold_len
; i
++ ){
2451 * regular dance; calculate velocity & total mass, apply impulse.
2454 struct contact
*ct
= &manifold
[i
];
2457 v3_sub( ct
->co
, world_cog
, delta
);
2458 v3_cross( player
->rb
.w
, delta
, rv
);
2459 v3_add( player
->rb
.v
, rv
, rv
);
2462 v3_cross( delta
, ct
->n
, raCn
);
2465 m3x3_mulv( iIw
, raCn
, raCnI
);
2467 float normal_mass
= 1.0f
/ (inv_mass
+ v3_dot(raCn
,raCnI
)),
2468 vn
= v3_dot( rv
, ct
->n
),
2469 lambda
= normal_mass
* ( -vn
);
2471 float temp
= ct
->norm_impulse
;
2472 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
2473 lambda
= ct
->norm_impulse
- temp
;
2476 v3_muls( ct
->n
, lambda
, impulse
);
2478 v3_muladds( normal_total
, impulse
, inv_mass
, normal_total
);
2479 v3_muladds( player
->rb
.v
, impulse
, inv_mass
, player
->rb
.v
);
2480 v3_cross( delta
, impulse
, impulse
);
2481 m3x3_mulv( iIw
, impulse
, impulse
);
2482 v3_add( impulse
, player
->rb
.w
, player
->rb
.w
);
2484 v3_cross( player
->rb
.w
, delta
, rv
);
2485 v3_add( player
->rb
.v
, rv
, rv
);
2486 vn
= v3_dot( rv
, ct
->n
);
2491 rb_depenetrate( manifold
, manifold_len
, dt
);
2492 v3_add( dt
, player
->rb
.co
, player
->rb
.co
);
2493 rb_update_transform( &player
->rb
);
2497 if( s
->substep
>= 0.0001f
)
2498 goto begin_collision
; /* again! */
2501 * End of collision and dynamics routine
2502 * --------------------------------------------------------------------------
2505 f32 nforce
= v3_length(normal_total
);
2506 if( nforce
> 4.0f
){
2507 if( nforce
> 17.6f
){
2508 v3_muladds( player
->rb
.v
, normal_total
, -1.0f
, player
->rb
.v
);
2509 player__dead_transition(player
);
2510 player__skate_kill_audio(player
);
2514 f32 amt
= k_cam_punch
;
2515 if( player
->cam_control
.camera_mode
== k_cam_firstperson
){
2519 v3_muladds( player
->cam_land_punch_v
, normal_total
, amt
,
2520 player
->cam_land_punch_v
);
2523 s
->surface
= k_surface_prop_concrete
;
2525 for( int i
=0; i
<manifold_len
; i
++ ){
2526 rb_ct
*ct
= &manifold
[i
];
2527 struct world_surface
*surf
= world_contact_surface( world
, ct
);
2529 if( surf
->info
.surface_prop
> s
->surface
)
2530 s
->surface
= surf
->info
.surface_prop
;
2533 for( int i
=0; i
<k_wheel_count
; i
++ ){
2535 m3x3_copy( player
->rb
.to_world
, mtx
);
2536 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, mtx
[3] );
2537 vg_line_sphere( mtx
, wheels
[i
].radius
,
2538 (u32
[]){ VG__WHITE
, VG__BLACK
,
2539 wheels
[i
].colour
}[ wheels
[i
].state
]);
2542 skate_integrate( player
);
2543 vg_line_point( s
->state
.cog
, 0.02f
, VG__WHITE
);
2546 world_intersect_gates(world
, player
->rb
.co
, s
->state
.prev_pos
);
2549 m4x3_mulv( gate
->transport
, player
->rb
.co
, player
->rb
.co
);
2550 m3x3_mulv( gate
->transport
, player
->rb
.v
, player
->rb
.v
);
2551 m4x3_mulv( gate
->transport
, s
->state
.cog
, s
->state
.cog
);
2552 m3x3_mulv( gate
->transport
, s
->state
.cog_v
, s
->state
.cog_v
);
2553 m3x3_mulv( gate
->transport
, s
->state
.throw_v
, s
->state
.throw_v
);
2554 m3x3_mulv( gate
->transport
, s
->state
.head_position
,
2555 s
->state
.head_position
);
2556 m3x3_mulv( gate
->transport
, s
->state
.up_dir
, s
->state
.up_dir
);
2558 v4f transport_rotation
;
2559 m3x3_q( gate
->transport
, transport_rotation
);
2560 q_mul( transport_rotation
, player
->rb
.q
, player
->rb
.q
);
2561 q_mul( transport_rotation
, s
->state
.smoothed_rotation
,
2562 s
->state
.smoothed_rotation
);
2563 rb_update_transform( &player
->rb
);
2564 player__pass_gate( player
, gate
);
2567 /* FIXME: Rate limit */
2568 static int stick_frames
= 0;
2570 if( s
->state
.activity
>= k_skate_activity_ground
)
2575 if( stick_frames
> 5 ) stick_frames
= 5;
2577 if( stick_frames
== 4 ){
2580 if( s
->state
.activity
== k_skate_activity_ground
){
2581 if( (fabsf(s
->state
.slip
) > 0.75f
) ){
2582 audio_oneshot_3d( &audio_lands
[vg_randu32()%2+3], player
->rb
.co
,
2586 audio_oneshot_3d( &audio_lands
[vg_randu32()%3], player
->rb
.co
,
2590 else if( s
->surface
== k_surface_prop_metal
){
2591 audio_oneshot_3d( &audio_board
[3], player
->rb
.co
, 40.0f
, 1.0f
);
2594 audio_oneshot_3d( &audio_board
[8], player
->rb
.co
, 40.0f
, 1.0f
);
2598 } else if( stick_frames
== 0 ){
2603 VG_STATIC
void player__skate_im_gui( player_instance
*player
){
2604 struct player_skate
*s
= &player
->_skate
;
2605 player__debugtext( 1, "V: %5.2f %5.2f %5.2f",player
->rb
.v
[0],
2608 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",player
->rb
.co
[0],
2611 player__debugtext( 1, "W: %5.2f %5.2f %5.2f",player
->rb
.w
[0],
2615 const char *activity_txt
[] =
2620 "undefined (INVALID)",
2621 "grind_any (INVALID)",
2623 "grind_metallic (INVALID)",
2629 player__debugtext( 1, "activity: %s", activity_txt
[s
->state
.activity
] );
2631 player__debugtext( 1, "steer_s: %5.2f %5.2f [%.2f %.2f]",
2632 s
->state
.steerx_s
, s
->state
.steery_s
,
2633 k_steer_ground
, k_steer_air
);
2635 player__debugtext( 1, "flip: %.4f %.4f", s
->state
.flip_rate
,
2636 s
->state
.flip_time
);
2637 player__debugtext( 1, "trickv: %.2f %.2f %.2f",
2638 s
->state
.trick_vel
[0],
2639 s
->state
.trick_vel
[1],
2640 s
->state
.trick_vel
[2] );
2641 player__debugtext( 1, "tricke: %.2f %.2f %.2f",
2642 s
->state
.trick_euler
[0],
2643 s
->state
.trick_euler
[1],
2644 s
->state
.trick_euler
[2] );
2647 VG_STATIC
void player__skate_animate( player_instance
*player
){
2648 struct player_skate
*s
= &player
->_skate
;
2649 struct player_skate_state
*state
= &player
->_skate
.state
;
2650 struct player_skate_animator
*animator
= &s
->animator
;
2653 float kheight
= 2.0f
,
2656 v3_zero( animator
->offset
);
2658 v3f cog_local
, cog_ideal
;
2659 m4x3_mulv( player
->rb
.to_local
, s
->state
.cog
, cog_local
);
2661 v3_copy( s
->state
.up_dir
, cog_ideal
);
2662 v3_normalize( cog_ideal
);
2663 m3x3_mulv( player
->rb
.to_local
, cog_ideal
, cog_ideal
);
2665 v3_sub( cog_ideal
, cog_local
, animator
->offset
);
2667 v3_muls( animator
->offset
, 4.0f
, animator
->offset
);
2668 animator
->offset
[1] *= -1.0f
;
2670 float curspeed
= v3_length( player
->rb
.v
),
2671 kickspeed
= vg_clampf( curspeed
*(1.0f
/40.0f
), 0.0f
, 1.0f
),
2672 kicks
= (vg_randf64()-0.5f
)*2.0f
*kickspeed
,
2673 sign
= vg_signf( kicks
);
2675 animator
->wobble
[0] = vg_lerpf( animator
->wobble
[0], kicks
*kicks
*sign
,
2676 6.0f
*vg
.time_delta
);
2677 animator
->wobble
[1] = vg_lerpf( animator
->wobble
[1], animator
->wobble
[0],
2678 2.4f
*vg
.time_delta
);
2680 animator
->offset
[0] *= 0.26f
;
2681 animator
->offset
[0] += animator
->wobble
[1]*3.0f
;
2683 animator
->offset
[1] *= -0.3f
;
2684 animator
->offset
[2] *= 0.01f
;
2686 animator
->offset
[0]=vg_clampf(animator
->offset
[0],-0.8f
,0.8f
)*
2687 (1.0f
-fabsf(animator
->slide
)*0.9f
);
2688 animator
->offset
[1]=vg_clampf(animator
->offset
[1],-0.5f
,0.0f
);
2690 v3_muls( animator
->offset
, 0.3f
, player
->cam_control
.tpv_offset_extra
);
2692 /* localized vectors */
2693 m4x3_mulv( player
->rb
.to_local
, s
->state
.cog
, animator
->local_cog
);
2696 * Animation blending
2697 * ===========================================
2702 float desired
= 0.0f
;
2703 if( s
->state
.activity
== k_skate_activity_ground
)
2704 desired
= vg_clampf( fabsf( s
->state
.slip
), 0.0f
, 1.0f
);
2706 animator
->slide
= vg_lerpf( animator
->slide
, desired
, 2.4f
*vg
.time_delta
);
2709 /* movement information */
2710 int iair
= s
->state
.activity
<= k_skate_activity_air_to_grind
;
2712 float dirz
= s
->state
.reverse
> 0.0f
? 0.0f
: 1.0f
,
2713 dirx
= s
->state
.slip
< 0.0f
? 0.0f
: 1.0f
,
2714 fly
= iair
? 1.0f
: 0.0f
,
2715 wdist
= s
->weight_distribution
[2] / k_board_length
;
2717 if( s
->state
.activity
>= k_skate_activity_grind_any
)
2720 animator
->z
= vg_lerpf( animator
->z
, dirz
, 2.4f
*vg
.time_delta
);
2721 animator
->x
= vg_lerpf( animator
->x
, dirx
, 0.6f
*vg
.time_delta
);
2722 animator
->fly
= vg_lerpf( animator
->fly
, fly
, 3.4f
*vg
.time_delta
);
2723 animator
->weight
= vg_lerpf( animator
->weight
, wdist
, 9.0f
*vg
.time_delta
);
2725 float stand
= 1.0f
- vg_clampf( curspeed
* 0.03f
, 0.0f
, 1.0f
);
2726 animator
->stand
= vg_lerpf( animator
->stand
, stand
, 6.0f
*vg
.time_delta
);
2727 animator
->reverse
= s
->state
.reverse
;
2729 if( fabsf(s
->state
.slip
) > 0.3f
){
2730 f32 slide_dir
= vg_signf(v3_dot(player
->rb
.v
,player
->rb
.to_world
[0]));
2731 s
->state
.delayed_slip_dir
= slide_dir
;
2735 f32 grind
=s
->state
.activity
>= k_skate_activity_grind_any
? 1.0f
: 0.0f
;
2736 animator
->grind
= vg_lerpf( animator
->grind
, grind
, 5.0f
*vg
.time_delta
);
2738 f32 grind_frame
= 0.5f
;
2740 if( s
->state
.activity
== k_skate_activity_grind_front50
)
2742 else if( s
->state
.activity
== k_skate_activity_grind_back50
)
2745 animator
->grind_balance
= vg_lerpf( animator
->grind_balance
, grind_frame
,
2746 5.0f
*vg
.time_delta
);
2749 animator
->push_time
= vg
.time
- s
->state
.start_push
;
2750 animator
->push
= vg_lerpf( animator
->push
,
2751 (vg
.time
- s
->state
.cur_push
) < 0.125,
2752 6.0f
*vg
.time_delta
);
2755 animator
->jump_charge
= s
->state
.jump_charge
;
2756 animator
->jump
= vg_lerpf( animator
->jump
, animator
->jump_charge
,
2757 8.4f
*vg
.time_delta
);
2760 animator
->jump_dir
= s
->state
.jump_dir
;
2761 f32 jump_start_frame
= 14.0f
/30.0f
;
2762 animator
->jump_time
= animator
->jump_charge
* jump_start_frame
;
2763 f32 jump_frame
= (vg
.time
- s
->state
.jump_time
) + jump_start_frame
;
2764 if( jump_frame
>= jump_start_frame
&& jump_frame
<= (40.0f
/30.0f
) )
2765 animator
->jump_time
= jump_frame
;
2768 float jump_t
= vg
.time
-s
->state
.jump_time
;
2771 float extra
= h
*exp(1.0-h
) * (s
->state
.jump_dir
?1.0f
:-1.0f
);
2772 extra
*= s
->state
.slap
* 4.0f
;
2774 v3_add( s
->state
.trick_euler
, s
->state
.trick_residuald
,
2775 animator
->board_euler
);
2776 v3_muls( animator
->board_euler
, VG_TAUf
, animator
->board_euler
);
2778 animator
->board_euler
[0] *= 0.5f
;
2779 animator
->board_euler
[1] += extra
;
2780 animator
->trick_type
= s
->state
.trick_type
;
2783 f32 lean1
, lean2
= animator
->steer
[0] * animator
->reverse
* -0.36f
,
2786 lean1
= animator
->slide
* animator
->delayed_slip_dir
;
2787 if( fabsf(lean1
)>fabsf(lean2
) ) lean
= lean1
;
2790 if( ((int)roundf(animator
->board_euler
[0]/VG_PIf
)) % 2 ) lean
= -lean
;
2791 lean
= vg_clampf( lean
, -1.0f
, 1.0f
);
2792 animator
->board_lean
=
2793 vg_lerpf(animator
->board_lean
, lean
, vg
.time_delta
*18.0f
);
2795 /* feet placement */
2796 struct player_board
*board
=
2797 addon_cache_item_if_loaded( k_addon_type_board
,
2798 player
->board_view_slot
);
2800 if( animator
->weight
> 0.0f
){
2801 animator
->foot_offset
[0] =
2802 board
->truck_positions
[k_board_truck_back
][2]+0.3f
;
2805 animator
->foot_offset
[1] =
2806 board
->truck_positions
[k_board_truck_front
][2]-0.3f
;
2810 f32 slapm
= vg_maxf( 1.0f
-v3_length2( s
->state
.trick_vel
), 0.0f
);
2811 animator
->slap
= s
->state
.slap
;
2812 animator
->subslap
= vg_lerpf( animator
->subslap
, slapm
,
2813 vg
.time_delta
*10.0f
);
2815 f32 l
= ((s
->state
.activity
< k_skate_activity_ground
) &&
2816 v3_length2(s
->state
.trick_vel
) > 0.1f
)? 1: 0;
2817 animator
->trick_foot
= vg_lerpf( animator
->trick_foot
, l
,
2818 8.4f
*vg
.time_delta
);
2822 joystick_state( k_srjoystick_grab
, grab_input
);
2823 v2_add( s
->state
.grab_mouse_delta
, grab_input
, grab_input
);
2825 if( v2_length2( grab_input
) <= 0.001f
) grab_input
[0] = -1.0f
;
2826 else v2_normalize_clamp( grab_input
);
2827 v2_lerp( animator
->grab
, grab_input
, 2.4f
*vg
.time_delta
, animator
->grab
);
2828 animator
->grabbing
= s
->state
.grabbing
;
2831 joystick_state( k_srjoystick_steer
, animator
->steer
);
2833 animator
->airdir
= vg_lerpf( animator
->airdir
, -animator
->steer
[0],
2834 2.4f
*vg
.time_delta
);
2838 if( (s
->state
.activity
<= k_skate_activity_air_to_grind
) &&
2839 (fabsf(s
->state
.flip_rate
) > 0.01f
) ){
2840 float substep
= vg
.time_fixed_extrapolate
;
2841 float t
= s
->state
.flip_time
+s
->state
.flip_rate
*substep
*k_rb_delta
;
2842 sign
= vg_signf( t
);
2844 t
= 1.0f
- vg_minf( 1.0f
, fabsf( t
* 1.1f
) );
2845 t
= sign
* (1.0f
-t
*t
);
2847 f32 angle
= vg_clampf( t
, -1.0f
, 1.0f
) * VG_TAUf
,
2848 distm
= s
->state
.land_dist
* fabsf(s
->state
.flip_rate
) * 3.0f
,
2849 blend
= vg_clampf( 1.0f
-distm
, 0.0f
, 1.0f
);
2850 angle
= vg_lerpf( angle
, vg_signf(s
->state
.flip_rate
)*VG_TAUf
, blend
);
2851 q_axis_angle( animator
->qflip
, s
->state
.flip_axis
, angle
);
2854 q_identity( animator
->qflip
);
2856 /* counter-rotation */
2857 if( v3_length2( s
->state
.up_dir
) > 0.001f
){
2858 v4_lerp( s
->state
.smoothed_rotation
, player
->rb
.q
,
2859 2.0f
*vg
.time_frame_delta
,
2860 s
->state
.smoothed_rotation
);
2861 q_normalize( s
->state
.smoothed_rotation
);
2863 v3f yaw_smooth
= {1.0f
,0.0f
,0.0f
};
2864 q_mulv( s
->state
.smoothed_rotation
, yaw_smooth
, yaw_smooth
);
2865 m3x3_mulv( player
->rb
.to_local
, yaw_smooth
, yaw_smooth
);
2866 yaw_smooth
[1] = 0.0f
;
2867 v3_normalize( yaw_smooth
);
2869 f32 yaw_counter_rotate
= yaw_smooth
[0];
2870 yaw_counter_rotate
= vg_maxf( 0.7f
, yaw_counter_rotate
);
2871 yaw_counter_rotate
= acosf( yaw_counter_rotate
);
2872 yaw_counter_rotate
*= 1.0f
-animator
->fly
;
2875 m3x3_mulv( player
->rb
.to_local
, s
->state
.up_dir
, ndir
);
2876 v3_normalize( ndir
);
2878 v3f up
= { 0.0f
, 1.0f
, 0.0f
};
2879 float a
= v3_dot( ndir
, up
);
2880 a
= acosf( vg_clampf( a
, -1.0f
, 1.0f
) );
2883 v4f qcounteryaw
, qfixup
;
2885 v3_cross( up
, ndir
, axis
);
2886 q_axis_angle( qfixup
, axis
, a
*2.0f
);
2888 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, yaw_smooth
, axis
);
2889 q_axis_angle( qcounteryaw
, axis
, yaw_counter_rotate
);
2891 q_mul( qcounteryaw
, qfixup
, animator
->qfixuptotal
);
2892 q_normalize( animator
->qfixuptotal
);
2895 m3x3_mulv( player
->rb
.to_world
, up
, p1
);
2896 m3x3_mulv( player
->rb
.to_world
, ndir
, p2
);
2898 vg_line_arrow( player
->rb
.co
, p1
, 0.5f
, VG__PINK
);
2899 vg_line_arrow( player
->rb
.co
, p2
, 0.5f
, VG__PINK
);
2901 else q_identity( animator
->qfixuptotal
);
2902 rb_extrapolate( &player
->rb
, animator
->root_co
, animator
->root_q
);
2905 VG_STATIC
void player__skate_pose( player_instance
*player
, player_pose
*pose
){
2906 struct player_avatar
*av
= player
->playeravatar
;
2907 struct skeleton
*sk
= &av
->sk
;
2908 struct player_skate
*s
= &player
->_skate
;
2909 struct player_skate_animator
*animator
= &s
->animator
;
2910 pose
->type
= k_player_pose_type_ik
;
2911 v3_copy( animator
->root_co
, pose
->root_co
);
2912 v4_copy( animator
->root_q
, pose
->root_q
);
2916 q_mulv( pose
->root_q
, (v3f
){0.0f
,1.0f
,0.0f
}, ext_up
);
2917 v3_copy( pose
->root_co
, ext_co
);
2918 v3_muladds( pose
->root_co
, ext_up
, -0.1f
, pose
->root_co
);
2920 /* apply flip rotation at midpoint */
2921 q_mul( animator
->qflip
, pose
->root_q
, pose
->root_q
);
2922 q_normalize( pose
->root_q
);
2924 v3f rotation_point
, rco
;
2925 v3_muladds( ext_co
, ext_up
, 0.5f
, rotation_point
);
2926 v3_sub( pose
->root_co
, rotation_point
, rco
);
2928 q_mulv( animator
->qflip
, rco
, rco
);
2929 v3_add( rco
, rotation_point
, pose
->root_co
);
2932 * ---------------------------------------------------------------------- */
2934 mdl_keyframe apose
[32], bpose
[32];
2935 mdl_keyframe ground_pose
[32];
2938 f32 dir_frame
= animator
->z
* (15.0f
/30.0f
),
2939 stand_blend
= animator
->offset
[1]*-2.0f
;
2941 pose
->board
.lean
= animator
->board_lean
;
2943 stand_blend
= vg_clampf( 1.0f
-animator
->local_cog
[1], 0, 1 );
2945 skeleton_sample_anim( sk
, s
->anim_stand
, dir_frame
, apose
);
2946 skeleton_sample_anim( sk
, s
->anim_highg
, dir_frame
, bpose
);
2947 skeleton_lerp_pose( sk
, apose
, bpose
, stand_blend
, apose
);
2950 skeleton_sample_anim( sk
, s
->anim_slide
, animator
->x
* 0.5f
, bpose
);
2951 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->slide
, apose
);
2953 if( animator
->reverse
> 0.0f
)
2954 skeleton_sample_anim( sk
, s
->anim_push
, animator
->push_time
, bpose
);
2956 skeleton_sample_anim( sk
, s
->anim_push_reverse
, animator
->push_time
,
2959 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->push
, apose
);
2961 struct skeleton_anim
*jump_anim
= animator
->jump_dir
?
2963 s
->anim_ollie_reverse
;
2965 f32 setup_blend
= vg_minf( animator
->jump
, 1.0f
);
2966 skeleton_sample_anim_clamped( sk
, jump_anim
, animator
->jump_time
, bpose
);
2967 skeleton_lerp_pose( sk
, apose
, bpose
, setup_blend
, ground_pose
);
2970 mdl_keyframe air_pose
[32];
2972 float air_frame
= (animator
->airdir
*0.5f
+0.5f
) * (15.0f
/30.0f
);
2973 skeleton_sample_anim( sk
, s
->anim_air
, air_frame
, apose
);
2975 float ang
= atan2f( animator
->grab
[0], animator
->grab
[1] ),
2976 ang_unit
= (ang
+VG_PIf
) * (1.0f
/VG_TAUf
),
2977 grab_frame
= ang_unit
* (15.0f
/30.0f
);
2979 skeleton_sample_anim( sk
, s
->anim_grabs
, grab_frame
, bpose
);
2980 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->grabbing
, air_pose
);
2983 skeleton_lerp_pose( sk
, ground_pose
, air_pose
, animator
->fly
,
2986 mdl_keyframe
*kf_board
= &pose
->keyframes
[av
->id_board
-1],
2987 *kf_foot_l
= &pose
->keyframes
[av
->id_ik_foot_l
-1],
2988 *kf_foot_r
= &pose
->keyframes
[av
->id_ik_foot_r
-1],
2989 *kf_knee_l
= &pose
->keyframes
[av
->id_ik_knee_l
-1],
2990 *kf_knee_r
= &pose
->keyframes
[av
->id_ik_knee_r
-1],
2991 *kf_hip
= &pose
->keyframes
[av
->id_hip
-1],
2992 *kf_wheels
[] = { &pose
->keyframes
[av
->id_wheel_r
-1],
2993 &pose
->keyframes
[av
->id_wheel_l
-1] };
2996 mdl_keyframe grind_pose
[32];
2998 f32 frame
= animator
->grind_balance
* 0.5f
;
3000 skeleton_sample_anim( sk
, s
->anim_grind
, frame
, apose
);
3001 skeleton_sample_anim( sk
, s
->anim_grind_jump
, frame
, bpose
);
3002 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->jump
, grind_pose
);
3004 skeleton_lerp_pose( sk
, pose
->keyframes
, grind_pose
,
3005 animator
->grind
, pose
->keyframes
);
3006 float add_grab_mod
= 1.0f
- animator
->fly
;
3008 /* additive effects */
3009 u32 apply_to
[] = { av
->id_hip
,
3013 av
->id_ik_elbow_r
};
3015 float apply_rates
[] = { 1.0f
,
3021 for( int i
=0; i
<vg_list_size(apply_to
); i
++ ){
3022 pose
->keyframes
[apply_to
[i
]-1].co
[0] += animator
->offset
[0]*add_grab_mod
;
3023 pose
->keyframes
[apply_to
[i
]-1].co
[2] += animator
->offset
[2]*add_grab_mod
;
3026 /* angle 'correction' */
3028 v3_add( av
->sk
.bones
[av
->id_hip
].co
, kf_hip
->co
, origin
);
3030 for( int i
=0; i
<vg_list_size(apply_to
); i
++ ){
3031 mdl_keyframe
*kf
= &pose
->keyframes
[apply_to
[i
]-1];
3032 keyframe_rotate_around( kf
, origin
, av
->sk
.bones
[apply_to
[i
]].co
,
3033 animator
->qfixuptotal
);
3036 /* trick rotation */
3037 v4f qtrick
, qyaw
, qpitch
, qroll
;
3038 q_axis_angle( qyaw
, (v3f
){0.0f
,1.0f
,0.0f
}, animator
->board_euler
[0] );
3039 q_axis_angle( qpitch
, (v3f
){1.0f
,0.0f
,0.0f
}, animator
->board_euler
[1] );
3040 q_axis_angle( qroll
, (v3f
){0.0f
,0.0f
,1.0f
}, animator
->board_euler
[2] );
3042 q_mul( qyaw
, qroll
, qtrick
);
3043 q_mul( qpitch
, qtrick
, qtrick
);
3044 q_mul( kf_board
->q
, qtrick
, kf_board
->q
);
3045 q_normalize( kf_board
->q
);
3047 kf_foot_l
->co
[2] = vg_lerpf( kf_foot_l
->co
[2], animator
->foot_offset
[0],
3048 0.5f
* animator
->weight
);
3049 kf_foot_r
->co
[2] = vg_lerpf( kf_foot_r
->co
[2], animator
->foot_offset
[1],
3050 -0.5f
* animator
->weight
);
3052 kf_foot_l
->co
[1] += animator
->slap
;
3053 kf_foot_r
->co
[1] += animator
->slap
;
3054 kf_knee_l
->co
[1] += animator
->slap
;
3055 kf_knee_r
->co
[1] += animator
->slap
;
3056 kf_board
->co
[1] += animator
->slap
* animator
->subslap
;
3057 kf_hip
->co
[1] += animator
->slap
* 0.25f
;
3059 if( animator
->trick_type
== k_trick_type_kickflip
){
3060 kf_foot_l
->co
[0] += animator
->trick_foot
* 0.2f
;
3061 kf_foot_l
->co
[1] -= animator
->trick_foot
* 0.18f
;
3063 else if( animator
->trick_type
== k_trick_type_shuvit
){
3064 kf_foot_l
->co
[0] += animator
->trick_foot
* 0.1f
;
3065 kf_foot_r
->co
[0] -= animator
->trick_foot
* 0.15f
;
3066 kf_foot_l
->co
[1] -= animator
->trick_foot
* 0.18f
;
3067 kf_foot_r
->co
[1] -= animator
->trick_foot
* 0.18f
;
3069 else if( animator
->trick_type
== k_trick_type_treflip
){
3070 kf_foot_l
->co
[0] += animator
->trick_foot
* 0.2f
;
3071 kf_foot_r
->co
[0] -= animator
->trick_foot
* 0.15f
;
3072 kf_foot_l
->co
[1] -= animator
->trick_foot
* 0.18f
;
3073 kf_foot_r
->co
[1] -= animator
->trick_foot
* 0.18f
;
3077 * animation wishlist:
3078 * boardslide/grind jump animations
3079 * when tricking the slap should not appply or less apply
3080 * not animations however DONT target grinds that are vertically down.
3083 /* truck rotation */
3084 for( int i
=0; i
<2; i
++ ){
3085 float a
= vg_minf( s
->truckv0
[i
][0], 1.0f
);
3086 a
= -acosf( a
) * vg_signf( s
->truckv0
[i
][1] );
3089 q_axis_angle( q
, (v3f
){0.0f
,0.0f
,1.0f
}, a
);
3090 q_mul( q
, kf_wheels
[i
]->q
, kf_wheels
[i
]->q
);
3091 q_normalize( kf_wheels
[i
]->q
);
3097 *kf_head
= &pose
->keyframes
[av
->id_head
-1],
3098 *kf_elbow_l
= &pose
->keyframes
[av
->id_ik_elbow_l
-1],
3099 *kf_elbow_r
= &pose
->keyframes
[av
->id_ik_elbow_r
-1],
3100 *kf_hand_l
= &pose
->keyframes
[av
->id_ik_hand_l
-1],
3101 *kf_hand_r
= &pose
->keyframes
[av
->id_ik_hand_r
-1];
3103 float warble
= perlin1d( vg
.time
, 2.0f
, 2, 300 );
3104 warble
*= vg_maxf(animator
->grind
, fabsf(animator
->weight
)) * 0.3f
;
3107 q_axis_angle( qrot
, (v3f
){0.8f
,0.7f
,0.6f
}, warble
);
3109 v3f origin
= {0.0f
,0.2f
,0.0f
};
3110 keyframe_rotate_around( kf_hand_l
, origin
,
3111 av
->sk
.bones
[av
->id_ik_hand_l
].co
, qrot
);
3112 keyframe_rotate_around( kf_hand_r
, origin
,
3113 av
->sk
.bones
[av
->id_ik_hand_r
].co
, qrot
);
3114 keyframe_rotate_around( kf_hip
, origin
,
3115 av
->sk
.bones
[av
->id_hip
].co
, qrot
);
3116 keyframe_rotate_around( kf_elbow_r
, origin
,
3117 av
->sk
.bones
[av
->id_ik_elbow_r
].co
, qrot
);
3118 keyframe_rotate_around( kf_elbow_l
, origin
,
3119 av
->sk
.bones
[av
->id_ik_elbow_l
].co
, qrot
);
3121 q_inv( qrot
, qrot
);
3122 q_mul( qrot
, kf_head
->q
, kf_head
->q
);
3123 q_normalize( kf_head
->q
);
3128 VG_STATIC
void player__skate_post_animate( player_instance
*player
)
3130 struct player_skate
*s
= &player
->_skate
;
3131 struct player_avatar
*av
= player
->playeravatar
;
3133 player
->cam_velocity_influence
= 1.0f
;
3135 v3f head
= { 0.0f
, 1.8f
, 0.0f
};
3136 m4x3_mulv( av
->sk
.final_mtx
[ av
->id_head
], head
, s
->state
.head_position
);
3137 m4x3_mulv( player
->rb
.to_local
, s
->state
.head_position
,
3138 s
->state
.head_position
);
3141 VG_STATIC
void player__skate_reset_animator( player_instance
*player
){
3142 struct player_skate
*s
= &player
->_skate
;
3143 struct player_skate_state
*state
= &s
->state
;
3145 memset( &s
->animator
, 0, sizeof(s
->animator
) );
3147 if( s
->state
.activity
<= k_skate_activity_air_to_grind
)
3148 s
->animator
.fly
= 1.0f
;
3150 s
->animator
.fly
= 0.0f
;
3153 VG_STATIC
void player__skate_clear_mechanics( player_instance
*player
)
3155 struct player_skate
*s
= &player
->_skate
;
3156 s
->state
.jump_charge
= 0.0f
;
3157 s
->state
.charging_jump
= 0;
3158 s
->state
.jump_dir
= 0;
3159 v3_zero( s
->state
.flip_axis
);
3160 s
->state
.flip_time
= 0.0f
;
3161 s
->state
.flip_rate
= 0.0f
;
3162 s
->state
.reverse
= 0.0f
;
3163 s
->state
.slip
= 0.0f
;
3164 s
->state
.grabbing
= 0.0f
;
3165 v2_zero( s
->state
.grab_mouse_delta
);
3166 s
->state
.slap
= 0.0f
;
3167 s
->state
.jump_time
= 0.0;
3168 s
->state
.start_push
= 0.0;
3169 s
->state
.cur_push
= 0.0;
3170 s
->state
.air_start
= 0.0;
3172 v3_zero( s
->state
.air_init_v
);
3173 v3_zero( s
->state
.air_init_co
);
3175 s
->state
.gravity_bias
= k_gravity
;
3176 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
3177 v4_copy( player
->rb
.q
, s
->state
.smoothed_rotation
);
3178 v3_zero( s
->state
.throw_v
);
3179 v3_zero( s
->state
.trick_vel
);
3180 v3_zero( s
->state
.trick_euler
);
3181 v3_zero( s
->state
.cog_v
);
3182 s
->state
.grind_cooldown
= 0;
3183 s
->state
.surface_cooldown
= 0;
3184 v3_muladds( player
->rb
.co
, player
->rb
.to_world
[1], 1.0f
, s
->state
.cog
);
3185 v3_copy( player
->rb
.to_world
[1], s
->state
.up_dir
);
3186 v3_copy( player
->rb
.to_world
[1], s
->surface_picture
);
3187 v3_zero( s
->weight_distribution
);
3188 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
3191 VG_STATIC
void player__skate_reset( player_instance
*player
,
3194 struct player_skate
*s
= &player
->_skate
;
3195 v3_zero( player
->rb
.v
);
3196 v4_copy( rp
->transform
.q
, player
->rb
.q
);
3198 s
->state
.activity
= k_skate_activity_air
;
3199 s
->state
.activity_prev
= k_skate_activity_air
;
3201 player__skate_clear_mechanics( player
);
3202 player__skate_reset_animator( player
);
3204 v3_zero( s
->state
.head_position
);
3205 s
->state
.head_position
[1] = 1.8f
;
3208 #endif /* PLAYER_SKATE_C */