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
);
64 for( int i
=0; i
<len
; i
++ )
66 man
[i
].rba
= &player
->rb
;
70 rb_manifold_filter_coplanar( man
, len
, 0.03f
);
74 rb_manifold_filter_backface( man
, len
);
75 rb_manifold_filter_joint_edges( man
, len
, 0.03f
);
76 rb_manifold_filter_pairs( man
, len
, 0.03f
);
78 int new_len
= rb_manifold_apply_filtered( man
, len
);
92 VG_STATIC
int skate_grind_scansq( player_instance
*player
,
93 v3f pos
, v3f dir
, float r
,
94 struct grind_info
*inf
)
96 world_instance
*world
= world_current_instance();
99 v3_copy( dir
, plane
);
100 v3_normalize( plane
);
101 plane
[3] = v3_dot( plane
, pos
);
104 v3_add( pos
, (v3f
){ r
, r
, r
}, box
[1] );
105 v3_sub( pos
, (v3f
){ r
, r
, r
}, box
[0] );
114 int sample_count
= 0;
120 v3_cross( plane
, player
->basis
[1], support_axis
);
121 v3_normalize( support_axis
);
124 bh_iter_init_box( 0, &it
, box
);
127 while( bh_next( world
->geo_bh
, &it
, &idx
) ){
128 u32
*ptri
= &world
->scene_geo
.arrindices
[ idx
*3 ];
131 struct world_surface
*surf
= world_tri_index_surface(world
,ptri
[0]);
132 if( !(surf
->info
.flags
& k_material_flag_grindable
) )
135 for( int j
=0; j
<3; j
++ )
136 v3_copy( world
->scene_geo
.arrvertices
[ptri
[j
]].co
, tri
[j
] );
138 for( int j
=0; j
<3; j
++ ){
142 struct grind_sample
*sample
= &samples
[ sample_count
];
145 if( plane_segment( plane
, tri
[i0
], tri
[i1
], co
) ){
147 v3_sub( co
, pos
, d
);
148 if( v3_length2( d
) > r
*r
)
152 v3_sub( tri
[1], tri
[0], va
);
153 v3_sub( tri
[2], tri
[0], vb
);
154 v3_cross( va
, vb
, normal
);
156 sample
->normal
[0] = v3_dot( support_axis
, normal
);
157 sample
->normal
[1] = v3_dot( player
->basis
[1], normal
);
158 sample
->co
[0] = v3_dot( support_axis
, d
);
159 sample
->co
[1] = v3_dot( player
->basis
[1], d
);
161 v3_copy( normal
, sample
->normal3
); /* normalize later
162 if we want to us it */
164 v3_muls( tri
[0], 1.0f
/3.0f
, sample
->centroid
);
165 v3_muladds( sample
->centroid
, tri
[1], 1.0f
/3.0f
, sample
->centroid
);
166 v3_muladds( sample
->centroid
, tri
[2], 1.0f
/3.0f
, sample
->centroid
);
168 v2_normalize( sample
->normal
);
171 if( sample_count
== vg_list_size( samples
) )
172 goto too_many_samples
;
179 if( sample_count
< 2 )
187 v2_fill( min_co
, INFINITY
);
188 v2_fill( max_co
, -INFINITY
);
190 v3_zero( average_direction
);
191 v3_zero( average_normal
);
193 int passed_samples
= 0;
195 for( int i
=0; i
<sample_count
-1; i
++ ){
196 struct grind_sample
*si
, *sj
;
200 for( int j
=i
+1; j
<sample_count
; j
++ ){
206 /* non overlapping */
207 if( v2_dist2( si
->co
, sj
->co
) >= (0.01f
*0.01f
) )
210 /* not sharp angle */
211 if( v2_dot( si
->normal
, sj
->normal
) >= 0.7f
)
216 v3_sub( sj
->centroid
, si
->centroid
, v0
);
217 if( v3_dot( v0
, si
->normal3
) >= 0.0f
||
218 v3_dot( v0
, sj
->normal3
) <= 0.0f
)
221 v2_minv( sj
->co
, min_co
, min_co
);
222 v2_maxv( sj
->co
, max_co
, max_co
);
225 v3_copy( si
->normal3
, n0
);
226 v3_copy( sj
->normal3
, n1
);
227 v3_cross( n0
, n1
, dir
);
230 /* make sure the directions all face a common hemisphere */
231 v3_muls( dir
, vg_signf(v3_dot(dir
,plane
)), dir
);
232 v3_add( average_direction
, dir
, average_direction
);
234 float yi
= v3_dot( player
->basis
[1], si
->normal3
),
235 yj
= v3_dot( player
->basis
[1], sj
->normal3
);
237 if( yi
> yj
) v3_add( si
->normal3
, average_normal
, average_normal
);
238 else v3_add( sj
->normal3
, average_normal
, average_normal
);
244 if( !passed_samples
)
247 if( (v3_length2( average_direction
) <= 0.001f
) ||
248 (v3_length2( average_normal
) <= 0.001f
) )
251 float div
= 1.0f
/(float)passed_samples
;
252 v3_normalize( average_direction
);
253 v3_normalize( average_normal
);
256 v2_add( min_co
, max_co
, average_coord
);
257 v2_muls( average_coord
, 0.5f
, average_coord
);
259 v3_muls( support_axis
, average_coord
[0], inf
->co
);
260 inf
->co
[1] += average_coord
[1];
261 v3_add( pos
, inf
->co
, inf
->co
);
262 v3_copy( average_normal
, inf
->n
);
263 v3_copy( average_direction
, inf
->dir
);
265 vg_line_point( inf
->co
, 0.02f
, VG__GREEN
);
266 vg_line_arrow( inf
->co
, average_direction
, 0.3f
, VG__GREEN
);
267 vg_line_arrow( inf
->co
, inf
->n
, 0.2f
, VG__CYAN
);
269 return passed_samples
;
272 VG_STATIC
void reset_jump_info( jump_info
*inf
)
275 inf
->land_dist
= 0.0f
;
277 inf
->type
= k_prediction_unset
;
278 v3_zero( inf
->apex
);
281 VG_STATIC
int create_jumps_to_hit_target( player_instance
*player
,
283 v3f target
, float max_angle_delta
,
286 struct player_skate
*s
= &player
->_skate
;
288 /* calculate the exact 2 solutions to jump onto that grind spot */
291 v3_sub( target
, player
->rb
.co
, v0
);
292 m3x3_mulv( player
->invbasis
, v0
, v0
);
300 m3x3_mulv( player
->invbasis
, player
->rb
.v
, v_local
);
302 v2f d
= { v3_dot( ax
, v0
), v0
[1] },
303 v
= { v3_dot( ax
, v_local
), v_local
[1] };
305 float a
= atan2f( v
[1], v
[0] ),
307 root
= m
*m
*m
*m
- gravity
*(gravity
*d
[0]*d
[0] + 2.0f
*d
[1]*m
*m
);
312 root
= sqrtf( root
);
313 float a0
= atanf( (m
*m
+ root
) / (gravity
* d
[0]) ),
314 a1
= atanf( (m
*m
- root
) / (gravity
* d
[0]) );
316 if( fabsf(a0
-a
) < max_angle_delta
){
317 jump_info
*inf
= &jumps
[ valid_count
++ ];
318 reset_jump_info( inf
);
320 v3_muls( ax
, cosf( a0
) * m
, inf
->v
);
321 inf
->v
[1] += sinf( a0
) * m
;
322 m3x3_mulv( player
->basis
, inf
->v
, inf
->v
);
323 inf
->land_dist
= d
[0] / (cosf(a0
)*m
);
324 inf
->gravity
= gravity
;
326 v3_copy( target
, inf
->log
[inf
->log_length
++] );
329 if( fabsf(a1
-a
) < max_angle_delta
){
330 jump_info
*inf
= &jumps
[ valid_count
++ ];
331 reset_jump_info( inf
);
333 v3_muls( ax
, cosf( a1
) * m
, inf
->v
);
334 inf
->v
[1] += sinf( a1
) * m
;
335 m3x3_mulv( player
->basis
, inf
->v
, inf
->v
);
336 inf
->land_dist
= d
[0] / (cosf(a1
)*m
);
337 inf
->gravity
= gravity
;
339 v3_copy( target
, inf
->log
[inf
->log_length
++] );
347 void player__approximate_best_trajectory( player_instance
*player
)
349 world_instance
*world0
= world_current_instance();
351 struct player_skate
*s
= &player
->_skate
;
352 float k_trace_delta
= k_rb_delta
* 10.0f
;
354 s
->state
.air_start
= vg
.time
;
355 v3_copy( player
->rb
.v
, s
->state
.air_init_v
);
356 v3_copy( player
->rb
.co
, s
->state
.air_init_co
);
358 s
->possible_jump_count
= 0;
361 v3_cross( player
->rb
.v
, player
->rb
.to_world
[1], axis
);
362 v3_normalize( axis
);
364 /* at high slopes, Y component is low */
365 float upness
= v3_dot( player
->rb
.to_world
[1], player
->basis
[1] ),
366 angle_begin
= -(1.0f
-fabsf( upness
)),
369 struct grind_info grind
;
370 int grind_located
= 0;
371 float grind_located_gravity
= k_gravity
;
374 v3f launch_v_bounds
[2];
376 for( int i
=0; i
<2; i
++ ){
377 v3_copy( player
->rb
.v
, launch_v_bounds
[i
] );
378 float ang
= (float[]){ angle_begin
, angle_end
}[ i
];
382 q_axis_angle( qbias
, axis
, ang
);
383 q_mulv( qbias
, launch_v_bounds
[i
], launch_v_bounds
[i
] );
386 for( int m
=0;m
<=30; m
++ ){
387 jump_info
*inf
= &s
->possible_jumps
[ s
->possible_jump_count
++ ];
388 reset_jump_info( inf
);
390 v3f launch_co
, launch_v
, co0
, co1
;
391 v3_copy( player
->rb
.co
, launch_co
);
392 v3_copy( player
->rb
.v
, launch_v
);
393 v3_copy( launch_co
, co0
);
394 world_instance
*trace_world
= world0
;
396 float vt
= (float)m
* (1.0f
/30.0f
),
397 ang
= vg_lerpf( angle_begin
, angle_end
, vt
) * 0.15f
;
400 q_axis_angle( qbias
, axis
, ang
);
401 q_mulv( qbias
, launch_v
, launch_v
);
403 float yaw_sketch
= 1.0f
-fabsf(upness
);
405 float yaw_bias
= ((float)(m
%3) - 1.0f
) * 0.08f
* yaw_sketch
;
406 q_axis_angle( qbias
, player
->rb
.to_world
[1], yaw_bias
);
407 q_mulv( qbias
, launch_v
, launch_v
);
409 float gravity_bias
= vg_lerpf( 0.85f
, 1.4f
, vt
),
410 gravity
= k_gravity
* gravity_bias
;
411 inf
->gravity
= gravity
;
412 v3_copy( launch_v
, inf
->v
);
415 m3x3_copy( player
->basis
, basis
);
417 for( int i
=1; i
<=50; i
++ ){
418 float t
= (float)i
* k_trace_delta
;
420 v3_muls( launch_v
, t
, co1
);
421 v3_muladds( co1
, basis
[1], -0.5f
* gravity
* t
*t
, co1
);
422 v3_add( launch_co
, co1
, co1
);
424 float launch_vy
= v3_dot( launch_v
,basis
[1] );
426 int search_for_grind
= 1;
427 if( grind_located
) search_for_grind
= 0;
428 if( launch_vy
- gravity
*t
> 0.0f
) search_for_grind
= 0;
432 v3f closest
={0.0f
,0.0f
,0.0f
};
433 if( search_for_grind
){
434 if( bh_closest_point(trace_world
->geo_bh
,co1
,closest
,1.0f
) != -1 ){
435 float min_dist
= 0.75f
;
436 min_dist
*= min_dist
;
438 if( v3_dist2( closest
, launch_co
) < min_dist
)
439 search_for_grind
= 0;
443 for( int j
=0; j
<2; j
++ ){
444 v3_muls( launch_v_bounds
[j
], t
, bound
[j
] );
445 v3_muladds( bound
[j
], basis
[1], -0.5f
*gravity
*t
*t
, bound
[j
] );
446 v3_add( launch_co
, bound
[j
], bound
[j
] );
449 float limh
= vg_minf( 2.0f
, t
),
450 minh
= vg_minf( bound
[0][1], bound
[1][1] )-limh
,
451 maxh
= vg_maxf( bound
[0][1], bound
[1][1] )+limh
;
453 if( (closest
[1] < minh
) || (closest
[1] > maxh
) ){
454 search_for_grind
= 0;
458 search_for_grind
= 0;
461 if( search_for_grind
){
463 v3_copy( launch_v
, ve
);
464 v3_muladds( ve
, basis
[1], -gravity
* t
, ve
);
466 if( skate_grind_scansq( player
, closest
, ve
, 0.5f
, &grind
) ){
467 /* check alignment */
468 v2f v0
= { v3_dot( ve
, basis
[0] ),
469 v3_dot( ve
, basis
[2] ) },
470 v1
= { v3_dot( grind
.dir
, basis
[0] ),
471 v3_dot( grind
.dir
, basis
[2] ) };
476 float a
= v2_dot( v0
, v1
);
478 float a_min
= cosf( VG_PIf
* 0.185f
);
479 if( s
->state
.grind_cooldown
)
480 a_min
= cosf( VG_PIf
* 0.05f
);
483 if( (fabsf(v3_dot( ve
, grind
.dir
))>=k_grind_axel_min_vel
) &&
485 (fabsf(grind
.dir
[1]) < 0.70710678118654752f
))
488 grind_located_gravity
= inf
->gravity
;
493 if( trace_world
->rendering_gate
){
494 ent_gate
*gate
= trace_world
->rendering_gate
;
495 if( gate_intersect( gate
, co1
, co0
) ){
496 m4x3_mulv( gate
->transport
, co0
, co0
);
497 m4x3_mulv( gate
->transport
, co1
, co1
);
498 m3x3_mulv( gate
->transport
, launch_v
, launch_v
);
499 m4x3_mulv( gate
->transport
, launch_co
, launch_co
);
500 m3x3_mul( gate
->transport
, basis
, basis
);
502 if( gate
->type
== k_gate_type_nonlocel
){
503 trace_world
= &world_static
.worlds
[ gate
->target
];
511 float scan_radius
= k_board_radius
;
512 scan_radius
*= vg_clampf( t
, 0.02f
, 1.0f
);
514 int idx
= spherecast_world(trace_world
, co0
, co1
, scan_radius
, &t1
, n
);
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
);
603 goto invalidated_grind
;
609 v3_copy( grind
.n
, jump
->n
);
611 /* determine score */
613 v3_copy( jump
->v
, ve
);
614 v3_muladds( ve
, player
->basis
[1], -jump
->gravity
*jump
->land_dist
, ve
);
615 jump
->score
= -v3_dot( ve
, grind
.n
) * 0.9f
;
617 s
->possible_jumps
[ s
->possible_jump_count
++ ] = *jump
;
625 float score_min
= INFINITY
,
626 score_max
= -INFINITY
;
628 jump_info
*best
= NULL
;
630 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
631 jump_info
*jump
= &s
->possible_jumps
[i
];
633 if( jump
->score
< score_min
)
636 score_min
= vg_minf( score_min
, jump
->score
);
637 score_max
= vg_maxf( score_max
, jump
->score
);
640 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
641 jump_info
*jump
= &s
->possible_jumps
[i
];
642 float s
= jump
->score
;
645 s
/= (score_max
-score_min
);
649 jump
->colour
= s
* 255.0f
;
653 else if( jump
->type
== k_prediction_land
)
656 jump
->colour
|= 0xff000000;
660 v3_copy( best
->n
, s
->state
.land_normal
);
661 v3_copy( best
->v
, player
->rb
.v
);
662 s
->state
.land_dist
= best
->land_dist
;
664 s
->state
.gravity_bias
= best
->gravity
;
666 if( best
->type
== k_prediction_grind
){
667 s
->state
.activity
= k_skate_activity_air_to_grind
;
671 joystick_state( k_srjoystick_steer
, steer
);
672 v2_normalize_clamp( steer
);
674 if( (fabsf(steer
[1]) > 0.5f
) && (s
->state
.land_dist
>= 1.5f
) ){
675 s
->state
.flip_rate
= (1.0f
/s
->state
.land_dist
) * vg_signf(steer
[1]) *
677 s
->state
.flip_time
= 0.0f
;
678 v3_copy( player
->rb
.to_world
[0], s
->state
.flip_axis
);
681 s
->state
.flip_rate
= 0.0f
;
682 v3_zero( s
->state
.flip_axis
);
686 v3_copy( player
->basis
[1], s
->state
.land_normal
);
692 * Varius physics models
693 * ------------------------------------------------
697 * Air control, no real physics
699 VG_STATIC
void skate_apply_air_model( player_instance
*player
)
701 struct player_skate
*s
= &player
->_skate
;
703 if( s
->state
.activity_prev
> k_skate_activity_air_to_grind
)
704 player__approximate_best_trajectory( player
);
706 float angle
= v3_dot( player
->rb
.to_world
[1], s
->state
.land_normal
);
707 angle
= vg_clampf( angle
, -1.0f
, 1.0f
);
709 v3_cross( player
->rb
.to_world
[1], s
->state
.land_normal
, axis
);
712 q_axis_angle( correction
, axis
,
713 acosf(angle
)*2.0f
*VG_TIMESTEP_FIXED
);
714 q_mul( correction
, player
->rb
.q
, player
->rb
.q
);
717 VG_STATIC
enum trick_type
player_skate_trick_input( player_instance
*player
);
718 VG_STATIC
void skate_apply_trick_model( player_instance
*player
)
720 struct player_skate
*s
= &player
->_skate
;
723 v3f strength
= { 3.7f
, 3.6f
, 8.0f
};
725 v3_muls( s
->state
.trick_residualv
, -4.0f
, Fd
);
726 v3_muls( s
->state
.trick_residuald
, -10.0f
, Fs
);
728 v3_mul( strength
, F
, F
);
730 v3_muladds( s
->state
.trick_residualv
, F
, k_rb_delta
,
731 s
->state
.trick_residualv
);
732 v3_muladds( s
->state
.trick_residuald
, s
->state
.trick_residualv
,
733 k_rb_delta
, s
->state
.trick_residuald
);
735 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
736 if( v3_length2( s
->state
.trick_vel
) < 0.0001f
)
739 int carry_on
= player_skate_trick_input( player
);
741 /* we assume velocities share a common divisor, in which case the
742 * interval is the minimum value (if not zero) */
744 float min_rate
= 99999.0f
;
746 for( int i
=0; i
<3; i
++ ){
747 float v
= s
->state
.trick_vel
[i
];
748 if( (v
> 0.0f
) && (v
< min_rate
) )
752 float interval
= 1.0f
/ min_rate
,
753 current
= floorf( s
->state
.trick_time
/ interval
),
754 next_end
= (current
+1.0f
) * interval
;
757 /* integrate trick velocities */
758 v3_muladds( s
->state
.trick_euler
, s
->state
.trick_vel
, k_rb_delta
,
759 s
->state
.trick_euler
);
761 if( !carry_on
&& (s
->state
.trick_time
+ k_rb_delta
>= next_end
) ){
762 s
->state
.trick_time
= 0.0f
;
763 s
->state
.trick_euler
[0] = roundf( s
->state
.trick_euler
[0] );
764 s
->state
.trick_euler
[1] = roundf( s
->state
.trick_euler
[1] );
765 s
->state
.trick_euler
[2] = roundf( s
->state
.trick_euler
[2] );
766 v3_copy( s
->state
.trick_vel
, s
->state
.trick_residualv
);
767 v3_zero( s
->state
.trick_vel
);
770 s
->state
.trick_time
+= k_rb_delta
;
773 if( (v3_length2(s
->state
.trick_vel
) >= 0.0001f
) &&
774 s
->state
.trick_time
> 0.2f
)
776 player__skate_kill_audio( player
);
777 player__dead_transition( player
);
780 s
->state
.trick_euler
[0] = roundf( s
->state
.trick_euler
[0] );
781 s
->state
.trick_euler
[1] = roundf( s
->state
.trick_euler
[1] );
782 s
->state
.trick_euler
[2] = roundf( s
->state
.trick_euler
[2] );
783 s
->state
.trick_time
= 0.0f
;
784 v3_zero( s
->state
.trick_vel
);
788 VG_STATIC
void skate_apply_grab_model( player_instance
*player
)
790 struct player_skate
*s
= &player
->_skate
;
792 float grabt
= axis_state( k_sraxis_grab
);
795 v2_muladds( s
->state
.grab_mouse_delta
, vg
.mouse_delta
, 0.02f
,
796 s
->state
.grab_mouse_delta
);
798 v2_normalize_clamp( s
->state
.grab_mouse_delta
);
801 v2_zero( s
->state
.grab_mouse_delta
);
803 s
->state
.grabbing
= vg_lerpf( s
->state
.grabbing
, grabt
, 8.4f
*k_rb_delta
);
806 VG_STATIC
void skate_apply_steering_model( player_instance
*player
)
808 struct player_skate
*s
= &player
->_skate
;
811 joystick_state( k_srjoystick_steer
, jsteer
);
814 float steer
= jsteer
[0],
815 grab
= axis_state( k_sraxis_grab
);
817 steer
= vg_signf( steer
) * steer
*steer
* k_steer_ground
;
820 v3_muls( player
->rb
.to_world
[1], -vg_signf( steer
), steer_axis
);
825 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
826 rate
= 6.0f
* fabsf(steer
);
830 /* rotate slower when grabbing on ground */
831 steer
*= (1.0f
-(s
->state
.jump_charge
+grab
)*0.4f
);
833 if( s
->state
.activity
== k_skate_activity_grind_5050
){
838 else if( s
->state
.activity
>= k_skate_activity_grind_any
){
839 rate
*= fabsf(steer
);
841 float a
= 0.8f
* -steer
* k_rb_delta
;
844 q_axis_angle( q
, player
->rb
.to_world
[1], a
);
845 q_mulv( q
, s
->grind_vec
, s
->grind_vec
);
847 v3_normalize( s
->grind_vec
);
850 else if( s
->state
.manual_direction
){
856 top
*= 1.0f
+v3_length( s
->state
.throw_v
)*k_mmthrow_steer
;
860 float current
= v3_dot( player
->rb
.to_world
[1], player
->rb
.w
),
861 addspeed
= (steer
* -top
) - current
,
862 maxaccel
= rate
* k_rb_delta
,
863 accel
= vg_clampf( addspeed
, -maxaccel
, maxaccel
);
865 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[1], accel
, player
->rb
.w
);
869 * Computes friction and surface interface model
871 VG_STATIC
void skate_apply_friction_model( player_instance
*player
)
873 struct player_skate
*s
= &player
->_skate
;
876 * Computing localized friction forces for controlling the character
877 * Friction across X is significantly more than Z
881 m3x3_mulv( player
->rb
.to_local
, player
->rb
.v
, vel
);
884 if( fabsf(vel
[2]) > 0.01f
)
885 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
887 if( fabsf( slip
) > 1.2f
)
888 slip
= vg_signf( slip
) * 1.2f
;
890 s
->state
.slip
= slip
;
891 s
->state
.reverse
= -vg_signf(vel
[2]);
893 vel
[0] += vg_cfrictf( vel
[0], k_friction_lat
* k_rb_delta
);
894 vel
[2] += vg_cfrictf( vel
[2], k_friction_resistance
* k_rb_delta
);
896 /* Pushing additive force */
898 if( !button_press( k_srbind_jump
) ){
899 if( button_press( k_srbind_push
) || (vg
.time
-s
->state
.start_push
<0.75) )
901 if( (vg
.time
- s
->state
.cur_push
) > 0.25 )
902 s
->state
.start_push
= vg
.time
;
904 s
->state
.cur_push
= vg
.time
;
906 double push_time
= vg
.time
- s
->state
.start_push
;
908 float cycle_time
= push_time
*k_push_cycle_rate
,
909 accel
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
),
910 amt
= accel
* VG_TIMESTEP_FIXED
,
911 current
= v3_length( vel
),
912 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
),
913 delta
= new_vel
- vg_minf( current
, k_max_push_speed
);
915 vel
[2] += delta
* -s
->state
.reverse
;
919 /* Send back to velocity */
920 m3x3_mulv( player
->rb
.to_world
, vel
, player
->rb
.v
);
923 VG_STATIC
void skate_apply_jump_model( player_instance
*player
)
925 struct player_skate
*s
= &player
->_skate
;
926 int charging_jump_prev
= s
->state
.charging_jump
;
927 s
->state
.charging_jump
= button_press( k_srbind_jump
);
929 /* Cannot charge this in air */
930 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
931 s
->state
.charging_jump
= 0;
935 if( s
->state
.charging_jump
){
936 s
->state
.jump_charge
+= k_rb_delta
* k_jump_charge_speed
;
938 if( !charging_jump_prev
)
939 s
->state
.jump_dir
= s
->state
.reverse
>0.0f
? 1: 0;
942 s
->state
.jump_charge
-= k_jump_charge_speed
* k_rb_delta
;
945 s
->state
.jump_charge
= vg_clampf( s
->state
.jump_charge
, 0.0f
, 1.0f
);
947 /* player let go after charging past 0.2: trigger jump */
948 if( (!s
->state
.charging_jump
) && (s
->state
.jump_charge
> 0.2f
) ){
951 /* Launch more up if alignment is up else improve velocity */
952 float aup
= v3_dot( player
->basis
[1], player
->rb
.to_world
[1] ),
954 dir
= mod
+ fabsf(aup
)*(1.0f
-mod
);
956 if( s
->state
.activity
== k_skate_activity_ground
){
957 v3_copy( player
->rb
.v
, jumpdir
);
958 v3_normalize( jumpdir
);
959 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
960 v3_muladds( jumpdir
, player
->rb
.to_world
[1], dir
, jumpdir
);
961 v3_normalize( jumpdir
);
963 v3_copy( s
->state
.up_dir
, jumpdir
);
964 s
->state
.grind_cooldown
= 30;
965 s
->state
.activity
= k_skate_activity_ground
;
968 joystick_state( k_srjoystick_steer
, steer
);
970 float tilt
= steer
[0] * 0.3f
;
971 tilt
*= vg_signf(v3_dot( player
->rb
.v
, s
->grind_dir
));
974 q_axis_angle( qtilt
, s
->grind_dir
, tilt
);
975 q_mulv( qtilt
, jumpdir
, jumpdir
);
977 s
->state
.surface_cooldown
= 10;
979 float force
= k_jump_force
*s
->state
.jump_charge
;
980 v3_muladds( player
->rb
.v
, jumpdir
, force
, player
->rb
.v
);
981 s
->state
.jump_charge
= 0.0f
;
982 s
->state
.jump_time
= vg
.time
;
985 audio_oneshot_3d( &audio_jumps
[vg_randu32()%2], player
->rb
.co
,40.0f
,1.0f
);
990 VG_STATIC
void skate_apply_pump_model( player_instance
*player
)
992 struct player_skate
*s
= &player
->_skate
;
994 if( s
->state
.activity
!= k_skate_activity_ground
){
995 v3_zero( s
->state
.throw_v
);
999 /* Throw / collect routine
1001 if( axis_state( k_sraxis_grab
) > 0.5f
){
1002 if( s
->state
.activity
== k_skate_activity_ground
){
1004 v3_muls( player
->rb
.to_world
[1], k_mmthrow_scale
, s
->state
.throw_v
);
1009 float doty
= v3_dot( player
->rb
.to_world
[1], s
->state
.throw_v
);
1012 v3_muladds( s
->state
.throw_v
, player
->rb
.to_world
[1], -doty
, Fl
);
1014 if( s
->state
.activity
== k_skate_activity_ground
){
1015 if( v3_length2(player
->rb
.v
)<(20.0f
*20.0f
) )
1016 v3_muladds( player
->rb
.v
, Fl
, k_mmcollect_lat
, player
->rb
.v
);
1017 v3_muladds( s
->state
.throw_v
, Fl
, -k_mmcollect_lat
, s
->state
.throw_v
);
1020 v3_muls( player
->rb
.to_world
[1], -doty
, Fv
);
1021 v3_muladds( player
->rb
.v
, Fv
, k_mmcollect_vert
, player
->rb
.v
);
1022 v3_muladds( s
->state
.throw_v
, Fv
, k_mmcollect_vert
, s
->state
.throw_v
);
1026 if( v3_length2( s
->state
.throw_v
) > 0.0001f
){
1028 v3_copy( s
->state
.throw_v
, dir
);
1029 v3_normalize( dir
);
1031 float max
= v3_dot( dir
, s
->state
.throw_v
),
1032 amt
= vg_minf( k_mmdecay
* k_rb_delta
, max
);
1033 v3_muladds( s
->state
.throw_v
, dir
, -amt
, s
->state
.throw_v
);
1037 VG_STATIC
void skate_apply_cog_model( player_instance
*player
)
1039 struct player_skate
*s
= &player
->_skate
;
1041 v3f ideal_cog
, ideal_diff
, ideal_dir
;
1042 v3_copy( s
->state
.up_dir
, ideal_dir
);
1043 v3_normalize( ideal_dir
);
1045 float grab
= axis_state( k_sraxis_grab
);
1046 v3_muladds( player
->rb
.co
, ideal_dir
, 1.0f
-grab
, ideal_cog
);
1047 v3_sub( ideal_cog
, s
->state
.cog
, ideal_diff
);
1049 /* Apply velocities */
1051 v3_sub( player
->rb
.v
, s
->state
.cog_v
, rv
);
1054 v3_muls( ideal_diff
, -k_cog_spring
* k_rb_rate
, F
);
1055 v3_muladds( F
, rv
, -k_cog_damp
* k_rb_rate
, F
);
1057 float ra
= k_cog_mass_ratio
,
1058 rb
= 1.0f
-k_cog_mass_ratio
;
1060 /* Apply forces & intergrate */
1061 v3_muladds( s
->state
.cog_v
, F
, -rb
, s
->state
.cog_v
);
1062 v3_muladds( s
->state
.cog_v
, player
->basis
[1], -9.8f
* k_rb_delta
,
1065 v3_muladds( s
->state
.cog
, s
->state
.cog_v
, k_rb_delta
, s
->state
.cog
);
1069 VG_STATIC
void skate_integrate( player_instance
*player
)
1071 struct player_skate
*s
= &player
->_skate
;
1073 float decay_rate_x
= 1.0f
- (k_rb_delta
* 3.0f
),
1074 decay_rate_z
= decay_rate_x
,
1075 decay_rate_y
= 1.0f
;
1077 if( s
->state
.activity
>= k_skate_activity_grind_any
){
1079 decay_rate
= 1.0f
-vg_lerpf( 3.0f
, 20.0f
, s
->grind_strength
) * k_rb_delta
;
1080 decay_rate_y
= decay_rate
;
1082 decay_rate_x
= 1.0f
-(16.0f
*k_rb_delta
);
1083 decay_rate_y
= 1.0f
-(10.0f
*k_rb_delta
);
1084 decay_rate_z
= 1.0f
-(40.0f
*k_rb_delta
);
1087 float wx
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[0] ) * decay_rate_x
,
1088 wy
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[1] ) * decay_rate_y
,
1089 wz
= v3_dot( player
->rb
.w
, player
->rb
.to_world
[2] ) * decay_rate_z
;
1091 v3_muls( player
->rb
.to_world
[0], wx
, player
->rb
.w
);
1092 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[1], wy
, player
->rb
.w
);
1093 v3_muladds( player
->rb
.w
, player
->rb
.to_world
[2], wz
, player
->rb
.w
);
1095 s
->state
.flip_time
+= s
->state
.flip_rate
* k_rb_delta
;
1096 rb_update_transform( &player
->rb
);
1099 VG_STATIC
enum trick_type
player_skate_trick_input( player_instance
*player
){
1100 return (button_press( k_srbind_trick0
) ) |
1101 (button_press( k_srbind_trick1
) << 1) |
1102 (button_press( k_srbind_trick2
) << 1) |
1103 (button_press( k_srbind_trick2
) );
1106 VG_STATIC
void player__skate_pre_update( player_instance
*player
){
1107 struct player_skate
*s
= &player
->_skate
;
1109 if( button_down( k_srbind_use
) ){
1110 player
->subsystem
= k_player_subsystem_walk
;
1113 v3_copy( player
->cam
.angles
, player
->angles
);
1114 player
->angles
[2] = 0.0f
;
1116 player__begin_holdout( player
);
1117 player__skate_kill_audio( player
);
1118 player__walk_transition( player
);
1122 enum trick_type trick
= k_trick_type_none
;
1123 if( (s
->state
.activity
<= k_skate_activity_air_to_grind
) &&
1124 (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 VG_STATIC
void player__skate_post_update( player_instance
*player
){
1146 struct player_skate
*s
= &player
->_skate
;
1148 for( int i
=0; i
<s
->possible_jump_count
; i
++ ){
1149 jump_info
*jump
= &s
->possible_jumps
[i
];
1151 if( jump
->log_length
== 0 ){
1152 vg_fatal_error( "assert: jump->log_length == 0\n" );
1155 for( int j
=0; j
<jump
->log_length
- 1; j
++ ){
1156 float brightness
= jump
->score
*jump
->score
*jump
->score
;
1158 v3_lerp( jump
->log
[j
], jump
->log
[j
+1], brightness
, p1
);
1159 vg_line( jump
->log
[j
], p1
, jump
->colour
);
1162 vg_line_cross( jump
->log
[jump
->log_length
-1], jump
->colour
, 0.25f
);
1165 v3_add( jump
->log
[jump
->log_length
-1], jump
->n
, p1
);
1166 vg_line( jump
->log
[jump
->log_length
-1], p1
, 0xffffffff );
1168 vg_line_point( jump
->apex
, 0.02f
, 0xffffffff );
1173 float air
= s
->state
.activity
<= k_skate_activity_air_to_grind
? 1.0f
: 0.0f
,
1174 speed
= v3_length( player
->rb
.v
),
1175 attn
= vg_minf( 1.0f
, speed
*0.1f
),
1176 slide
= vg_clampf( fabsf(s
->state
.slip
), 0.0f
, 1.0f
);
1178 if( s
->state
.activity
>= k_skate_activity_grind_any
){
1182 f32 gate
= skaterift
.time_rate
,
1183 vol_main
= sqrtf( (1.0f
-air
)*attn
*(1.0f
-slide
) * 0.4f
) * gate
,
1184 vol_air
= sqrtf( air
*attn
* 0.5f
) * gate
,
1185 vol_slide
= sqrtf( (1.0f
-air
)*attn
*slide
* 0.25f
) * gate
;
1187 const u32 flags
= AUDIO_FLAG_SPACIAL_3D
|AUDIO_FLAG_LOOP
;
1190 s
->aud_air
= audio_get_first_idle_channel();
1192 audio_channel_init( s
->aud_air
, &audio_board
[1], flags
);
1195 if( !s
->aud_slide
){
1196 s
->aud_slide
= audio_get_first_idle_channel();
1198 audio_channel_init( s
->aud_slide
, &audio_board
[2], flags
);
1202 /* brrrrrrrrrrrt sound for tiles and stuff
1203 * --------------------------------------------------------*/
1204 float sidechain_amt
= 0.0f
,
1205 hz
= vg_maxf( speed
* 2.0f
, 2.0f
);
1207 if( (s
->surface
== k_surface_prop_tiles
) &&
1208 (s
->state
.activity
< k_skate_activity_grind_any
) )
1209 sidechain_amt
= 1.0f
;
1211 sidechain_amt
= 0.0f
;
1213 audio_set_lfo_frequency( 0, hz
);
1214 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar
,
1215 vg_lerpf( 250.0f
, 80.0f
, attn
) );
1217 if( s
->sample_change_cooldown
> 0.0f
){
1218 s
->sample_change_cooldown
-= vg
.time_frame_delta
;
1221 int sample_type
= k_skate_sample_concrete
;
1223 if( s
->state
.activity
== k_skate_activity_grind_5050
){
1224 if( s
->surface
== k_surface_prop_metal
)
1225 sample_type
= k_skate_sample_metal_scrape_generic
;
1227 sample_type
= k_skate_sample_concrete_scrape_metal
;
1229 else if( (s
->state
.activity
== k_skate_activity_grind_back50
) ||
1230 (s
->state
.activity
== k_skate_activity_grind_front50
) )
1232 if( s
->surface
== k_surface_prop_metal
){
1233 sample_type
= k_skate_sample_metal_scrape_generic
;
1236 float a
= v3_dot( player
->rb
.to_world
[2], s
->grind_dir
);
1237 if( fabsf(a
) > 0.70710678118654752f
)
1238 sample_type
= k_skate_sample_concrete_scrape_wood
;
1240 sample_type
= k_skate_sample_concrete_scrape_metal
;
1243 else if( s
->state
.activity
== k_skate_activity_grind_boardslide
){
1244 if( s
->surface
== k_surface_prop_metal
)
1245 sample_type
= k_skate_sample_metal_scrape_generic
;
1247 sample_type
= k_skate_sample_concrete_scrape_wood
;
1250 audio_clip
*relevant_samples
[] = {
1258 if( (s
->main_sample_type
!= sample_type
) || (!s
->aud_main
) ){
1260 audio_channel_crossfade( s
->aud_main
, relevant_samples
[sample_type
],
1262 s
->sample_change_cooldown
= 0.1f
;
1263 s
->main_sample_type
= sample_type
;
1268 s
->aud_main
->colour
= 0x00103efe;
1269 audio_channel_set_spacial( s
->aud_main
, player
->rb
.co
, 40.0f
);
1270 //audio_channel_slope_volume( s->aud_main, 0.05f, vol_main );
1271 audio_channel_edit_volume( s
->aud_main
, vol_main
, 1 );
1272 audio_channel_sidechain_lfo( s
->aud_main
, 0, sidechain_amt
);
1274 float rate
= 1.0f
+ (attn
-0.5f
)*0.2f
;
1275 audio_channel_set_sampling_rate( s
->aud_main
, rate
);
1279 s
->aud_slide
->colour
= 0x00103efe;
1280 audio_channel_set_spacial( s
->aud_slide
, player
->rb
.co
, 40.0f
);
1281 //audio_channel_slope_volume( s->aud_slide, 0.05f, vol_slide );
1282 audio_channel_edit_volume( s
->aud_slide
, vol_slide
, 1 );
1283 audio_channel_sidechain_lfo( s
->aud_slide
, 0, sidechain_amt
);
1287 s
->aud_air
->colour
= 0x00103efe;
1288 audio_channel_set_spacial( s
->aud_air
, player
->rb
.co
, 40.0f
);
1289 //audio_channel_slope_volume( s->aud_air, 0.05f, vol_air );
1290 audio_channel_edit_volume( s
->aud_air
, vol_air
, 1 );
1297 * truck alignment model at ra(local)
1298 * returns 1 if valid surface:
1299 * surface_normal will be filled out with an averaged normal vector
1300 * axel_dir will be the direction from left to right wheels
1302 * returns 0 if no good surface found
1305 int skate_compute_surface_alignment( player_instance
*player
,
1307 v3f surface_normal
, v3f axel_dir
){
1308 struct player_skate
*s
= &player
->_skate
;
1309 world_instance
*world
= world_current_instance();
1311 v3f truck
, left
, right
;
1312 m4x3_mulv( player
->rb
.to_world
, ra
, truck
);
1314 v3_muladds( truck
, player
->rb
.to_world
[0], -k_board_width
, left
);
1315 v3_muladds( truck
, player
->rb
.to_world
[0], k_board_width
, right
);
1316 vg_line( left
, right
, colour
);
1318 float k_max_truck_flex
= VG_PIf
* 0.25f
;
1320 ray_hit ray_l
, ray_r
;
1323 v3_muls( player
->rb
.to_world
[1], -1.0f
, dir
);
1325 int res_l
= 0, res_r
= 0;
1327 for( int i
=0; i
<8; i
++ ){
1328 float t
= 1.0f
- (float)i
* (1.0f
/8.0f
);
1329 v3_muladds( truck
, player
->rb
.to_world
[0], -k_board_radius
*t
, left
);
1330 v3_muladds( left
, player
->rb
.to_world
[1], k_board_radius
, left
);
1331 ray_l
.dist
= 2.1f
* k_board_radius
;
1333 res_l
= ray_world( world
, left
, dir
, &ray_l
);
1339 for( int i
=0; i
<8; i
++ ){
1340 float t
= 1.0f
- (float)i
* (1.0f
/8.0f
);
1341 v3_muladds( truck
, player
->rb
.to_world
[0], k_board_radius
*t
, right
);
1342 v3_muladds( right
, player
->rb
.to_world
[1], k_board_radius
, right
);
1343 ray_r
.dist
= 2.1f
* k_board_radius
;
1345 res_r
= ray_world( world
, right
, dir
, &ray_r
);
1353 v3f tangent_average
;
1354 v3_muladds( truck
, player
->rb
.to_world
[1], -k_board_radius
, midpoint
);
1355 v3_zero( tangent_average
);
1357 if( res_l
|| res_r
){
1359 v3_copy( midpoint
, p0
);
1360 v3_copy( midpoint
, p1
);
1363 v3_copy( ray_l
.pos
, p0
);
1364 v3_cross( ray_l
.normal
, player
->rb
.to_world
[0], t
);
1365 v3_add( t
, tangent_average
, tangent_average
);
1368 v3_copy( ray_r
.pos
, p1
);
1369 v3_cross( ray_r
.normal
, player
->rb
.to_world
[0], t
);
1370 v3_add( t
, tangent_average
, tangent_average
);
1373 v3_sub( p1
, p0
, v0
);
1377 /* fallback: use the closes point to the trucks */
1379 int idx
= bh_closest_point( world
->geo_bh
, midpoint
, closest
, 0.1f
);
1382 u32
*tri
= &world
->scene_geo
.arrindices
[ idx
* 3 ];
1385 for( int j
=0; j
<3; j
++ )
1386 v3_copy( world
->scene_geo
.arrvertices
[ tri
[j
] ].co
, verts
[j
] );
1388 v3f vert0
, vert1
, n
;
1389 v3_sub( verts
[1], verts
[0], vert0
);
1390 v3_sub( verts
[2], verts
[0], vert1
);
1391 v3_cross( vert0
, vert1
, n
);
1394 if( v3_dot( n
, player
->rb
.to_world
[1] ) < 0.3f
)
1397 v3_cross( n
, player
->rb
.to_world
[2], v0
);
1398 v3_muladds( v0
, player
->rb
.to_world
[2],
1399 -v3_dot( player
->rb
.to_world
[2], v0
), v0
);
1403 v3_cross( n
, player
->rb
.to_world
[0], t
);
1404 v3_add( t
, tangent_average
, tangent_average
);
1410 v3_muladds( truck
, v0
, k_board_width
, right
);
1411 v3_muladds( truck
, v0
, -k_board_width
, left
);
1413 vg_line( left
, right
, VG__WHITE
);
1415 v3_normalize( tangent_average
);
1416 v3_cross( v0
, tangent_average
, surface_normal
);
1417 v3_copy( v0
, axel_dir
);
1422 VG_STATIC
void skate_weight_distribute( player_instance
*player
){
1423 struct player_skate
*s
= &player
->_skate
;
1424 v3_zero( s
->weight_distribution
);
1426 int reverse_dir
= v3_dot( player
->rb
.to_world
[2], player
->rb
.v
) < 0.0f
?1:-1;
1429 joystick_state( k_srjoystick_steer
, steer
);
1431 if( s
->state
.manual_direction
== 0 ){
1432 if( (steer
[1] > 0.7f
) && (s
->state
.activity
== k_skate_activity_ground
) &&
1433 (s
->state
.jump_charge
<= 0.01f
) )
1434 s
->state
.manual_direction
= reverse_dir
;
1437 if( steer
[1] < 0.1f
){
1438 s
->state
.manual_direction
= 0;
1441 if( reverse_dir
!= s
->state
.manual_direction
){
1447 if( s
->state
.manual_direction
){
1448 float amt
= vg_minf( steer
[1] * 8.0f
, 1.0f
);
1449 s
->weight_distribution
[2] = k_board_length
* amt
*
1450 (float)s
->state
.manual_direction
;
1453 if( s
->state
.manual_direction
){
1456 m3x3_mulv( player
->rb
.to_world
, s
->weight_distribution
, plane_z
);
1457 v3_negate( plane_z
, plane_z
);
1459 v3_muladds( plane_z
, s
->surface_picture
,
1460 -v3_dot( plane_z
, s
->surface_picture
), plane_z
);
1461 v3_normalize( plane_z
);
1463 v3_muladds( plane_z
, s
->surface_picture
, 0.3f
, plane_z
);
1464 v3_normalize( plane_z
);
1467 v3_muladds( player
->rb
.co
, plane_z
, 1.5f
, p1
);
1468 vg_line( player
->rb
.co
, p1
, VG__GREEN
);
1471 v3_muls( player
->rb
.to_world
[2], -(float)s
->state
.manual_direction
,
1474 rb_effect_spring_target_vector( &player
->rb
, refdir
, plane_z
,
1475 k_manul_spring
, k_manul_dampener
,
1480 VG_STATIC
void skate_adjust_up_direction( player_instance
*player
){
1481 struct player_skate
*s
= &player
->_skate
;
1483 if( s
->state
.activity
== k_skate_activity_ground
){
1485 v3_copy( s
->surface_picture
, target
);
1487 target
[1] += 2.0f
* s
->surface_picture
[1];
1488 v3_normalize( target
);
1490 v3_lerp( s
->state
.up_dir
, target
,
1491 8.0f
* s
->substep_delta
, s
->state
.up_dir
);
1493 else if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
1494 v3_lerp( s
->state
.up_dir
, player
->rb
.to_world
[1],
1495 8.0f
* s
->substep_delta
, s
->state
.up_dir
);
1498 v3_lerp( s
->state
.up_dir
, player
->basis
[1],
1499 12.0f
* s
->substep_delta
, s
->state
.up_dir
);
1503 VG_STATIC
int skate_point_visible( v3f origin
, v3f target
){
1505 v3_sub( target
, origin
, dir
);
1508 ray
.dist
= v3_length( dir
);
1509 v3_muls( dir
, 1.0f
/ray
.dist
, dir
);
1512 if( ray_world( world_current_instance(), origin
, dir
, &ray
) )
1518 VG_STATIC
void skate_grind_orient( struct grind_info
*inf
, m3x3f mtx
){
1519 v3_copy( inf
->dir
, mtx
[0] );
1520 v3_copy( inf
->n
, mtx
[1] );
1521 v3_cross( mtx
[0], mtx
[1], mtx
[2] );
1524 VG_STATIC
void skate_grind_friction( player_instance
*player
,
1525 struct grind_info
*inf
, float strength
){
1527 v3_muladds( player
->rb
.to_world
[2], inf
->n
,
1528 -v3_dot( player
->rb
.to_world
[2], inf
->n
), v2
);
1530 float a
= 1.0f
-fabsf( v3_dot( v2
, inf
->dir
) ),
1531 dir
= vg_signf( v3_dot( player
->rb
.v
, inf
->dir
) ),
1532 F
= a
* -dir
* k_grind_max_friction
;
1534 v3_muladds( player
->rb
.v
, inf
->dir
, F
*k_rb_delta
*strength
, player
->rb
.v
);
1537 VG_STATIC
void skate_grind_decay( player_instance
*player
,
1538 struct grind_info
*inf
, float strength
){
1540 skate_grind_orient( inf
, mtx
);
1541 m3x3_transpose( mtx
, mtx_inv
);
1544 m3x3_mulv( mtx_inv
, player
->rb
.v
, v_grind
);
1546 float decay
= 1.0f
- ( k_rb_delta
* k_grind_decayxy
* strength
);
1547 v3_mul( v_grind
, (v3f
){ 1.0f
, decay
, decay
}, v_grind
);
1548 m3x3_mulv( mtx
, v_grind
, player
->rb
.v
);
1551 VG_STATIC
void skate_grind_truck_apply( player_instance
*player
,
1552 float sign
, struct grind_info
*inf
,
1554 struct player_skate
*s
= &player
->_skate
;
1557 v3f ra
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1559 m3x3_mulv( player
->rb
.to_world
, ra
, raw
);
1560 v3_add( player
->rb
.co
, raw
, wsp
);
1562 v3_copy( ra
, s
->weight_distribution
);
1565 v3_sub( inf
->co
, wsp
, delta
);
1568 v3_muladds( player
->rb
.v
, delta
, k_spring_force
*strength
*k_rb_delta
,
1571 skate_grind_decay( player
, inf
, strength
);
1572 skate_grind_friction( player
, inf
, strength
);
1574 /* yeah yeah yeah yeah */
1575 v3f raw_nplane
, axis
;
1576 v3_muladds( raw
, inf
->n
, -v3_dot( inf
->n
, raw
), raw_nplane
);
1577 v3_cross( raw_nplane
, inf
->n
, axis
);
1578 v3_normalize( axis
);
1582 skate_grind_orient( inf
, mtx
);
1583 v3f target_fwd
, fwd
, up
, target_up
;
1584 m3x3_mulv( mtx
, s
->grind_vec
, target_fwd
);
1585 v3_copy( raw_nplane
, fwd
);
1586 v3_copy( player
->rb
.to_world
[1], up
);
1587 v3_copy( inf
->n
, target_up
);
1589 v3_muladds( target_fwd
, inf
->n
, -v3_dot(inf
->n
,target_fwd
), target_fwd
);
1590 v3_muladds( fwd
, inf
->n
, -v3_dot(inf
->n
,fwd
), fwd
);
1592 v3_normalize( target_fwd
);
1593 v3_normalize( fwd
);
1596 joystick_state( k_srjoystick_steer
, steer
);
1598 float way
= steer
[1] * vg_signf( v3_dot( raw_nplane
, player
->rb
.v
) );
1601 q_axis_angle( q
, axis
, VG_PIf
*0.125f
* way
);
1602 q_mulv( q
, target_up
, target_up
);
1603 q_mulv( q
, target_fwd
, target_fwd
);
1605 rb_effect_spring_target_vector( &player
->rb
, up
, target_up
,
1610 rb_effect_spring_target_vector( &player
->rb
, fwd
, target_fwd
,
1611 k_grind_spring
*strength
,
1612 k_grind_dampener
*strength
,
1615 vg_line_arrow( player
->rb
.co
, target_up
, 1.0f
, VG__GREEN
);
1616 vg_line_arrow( player
->rb
.co
, fwd
, 0.8f
, VG__RED
);
1617 vg_line_arrow( player
->rb
.co
, target_fwd
, 1.0f
, VG__YELOW
);
1619 s
->grind_strength
= strength
;
1622 struct grind_limit
*limit
= &s
->limits
[ s
->limit_count
++ ];
1623 m4x3_mulv( player
->rb
.to_local
, wsp
, limit
->ra
);
1624 m3x3_mulv( player
->rb
.to_local
, inf
->n
, limit
->n
);
1627 v3_copy( inf
->dir
, s
->grind_dir
);
1630 VG_STATIC
void skate_5050_apply( player_instance
*player
,
1631 struct grind_info
*inf_front
,
1632 struct grind_info
*inf_back
)
1634 struct player_skate
*s
= &player
->_skate
;
1635 struct grind_info inf_avg
;
1637 v3_sub( inf_front
->co
, inf_back
->co
, inf_avg
.dir
);
1638 v3_muladds( inf_back
->co
, inf_avg
.dir
, 0.5f
, inf_avg
.co
);
1639 v3_normalize( inf_avg
.dir
);
1642 v3_muls( inf_avg
.dir
, vg_signf(v3_dot(inf_avg
.dir
,player
->rb
.v
)),
1645 v3f axis_front
, axis_back
, axis
;
1646 v3_cross( inf_front
->dir
, inf_front
->n
, axis_front
);
1647 v3_cross( inf_back
->dir
, inf_back
->n
, axis_back
);
1648 v3_add( axis_front
, axis_back
, axis
);
1649 v3_normalize( axis
);
1651 v3_cross( axis
, inf_avg
.dir
, inf_avg
.n
);
1652 skate_grind_decay( player
, &inf_avg
, 1.0f
);
1655 joystick_state( k_srjoystick_steer
, steer
);
1657 float way
= steer
[1] *
1658 vg_signf( v3_dot( player
->rb
.to_world
[2], player
->rb
.v
) );
1661 v3_copy( player
->rb
.to_world
[1], up
);
1662 v3_copy( inf_avg
.n
, target_up
);
1663 q_axis_angle( q
, player
->rb
.to_world
[0], VG_PIf
*0.25f
* -way
);
1664 q_mulv( q
, target_up
, target_up
);
1666 v3_zero( s
->weight_distribution
);
1667 s
->weight_distribution
[2] = k_board_length
* -way
;
1669 rb_effect_spring_target_vector( &player
->rb
, up
, target_up
,
1673 vg_line_arrow( player
->rb
.co
, up
, 1.0f
, VG__GREEN
);
1674 vg_line_arrow( player
->rb
.co
, target_up
, 1.0f
, VG__GREEN
);
1676 v3f fwd_nplane
, dir_nplane
;
1677 v3_muladds( player
->rb
.to_world
[2], inf_avg
.n
,
1678 -v3_dot( player
->rb
.to_world
[2], inf_avg
.n
), fwd_nplane
);
1681 v3_muls( inf_avg
.dir
, v3_dot( fwd_nplane
, inf_avg
.dir
), dir
);
1682 v3_muladds( dir
, inf_avg
.n
, -v3_dot( dir
, inf_avg
.n
), dir_nplane
);
1684 v3_normalize( fwd_nplane
);
1685 v3_normalize( dir_nplane
);
1687 rb_effect_spring_target_vector( &player
->rb
, fwd_nplane
, dir_nplane
,
1691 vg_line_arrow( player
->rb
.co
, fwd_nplane
, 0.8f
, VG__RED
);
1692 vg_line_arrow( player
->rb
.co
, dir_nplane
, 0.8f
, VG__RED
);
1694 v3f pos_front
= { 0.0f
, -k_board_radius
, -1.0f
* k_board_length
},
1695 pos_back
= { 0.0f
, -k_board_radius
, 1.0f
* k_board_length
},
1696 delta_front
, delta_back
, delta_total
;
1698 m4x3_mulv( player
->rb
.to_world
, pos_front
, pos_front
);
1699 m4x3_mulv( player
->rb
.to_world
, pos_back
, pos_back
);
1701 v3_sub( inf_front
->co
, pos_front
, delta_front
);
1702 v3_sub( inf_back
->co
, pos_back
, delta_back
);
1703 v3_add( delta_front
, delta_back
, delta_total
);
1705 v3_muladds( player
->rb
.v
, delta_total
, 50.0f
* k_rb_delta
, player
->rb
.v
);
1708 struct grind_limit
*limit
= &s
->limits
[ s
->limit_count
++ ];
1709 v3_zero( limit
->ra
);
1710 m3x3_mulv( player
->rb
.to_local
, inf_avg
.n
, limit
->n
);
1713 v3_copy( inf_avg
.dir
, s
->grind_dir
);
1716 VG_STATIC
int skate_grind_truck_renew( player_instance
*player
, float sign
,
1717 struct grind_info
*inf
)
1719 struct player_skate
*s
= &player
->_skate
;
1721 v3f wheel_co
= { 0.0f
, 0.0f
, sign
* k_board_length
},
1722 grind_co
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1724 m4x3_mulv( player
->rb
.to_world
, wheel_co
, wheel_co
);
1725 m4x3_mulv( player
->rb
.to_world
, grind_co
, grind_co
);
1727 /* Exit condition: lost grind tracking */
1728 if( !skate_grind_scansq( player
, grind_co
, player
->rb
.v
, 0.3f
, inf
) )
1731 /* Exit condition: cant see grind target directly */
1732 if( !skate_point_visible( wheel_co
, inf
->co
) )
1735 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1736 float dv
= fabsf(v3_dot( player
->rb
.v
, inf
->dir
)),
1737 minv
= k_grind_axel_min_vel
*0.8f
;
1742 if( fabsf(v3_dot( inf
->dir
, s
->grind_dir
)) < k_grind_max_edge_angle
)
1745 v3_copy( inf
->dir
, s
->grind_dir
);
1749 VG_STATIC
int skate_grind_truck_entry( player_instance
*player
, float sign
,
1750 struct grind_info
*inf
)
1752 struct player_skate
*s
= &player
->_skate
;
1755 v3f ra
= { 0.0f
, -k_board_radius
, sign
* k_board_length
};
1758 m3x3_mulv( player
->rb
.to_world
, ra
, raw
);
1759 v3_add( player
->rb
.co
, raw
, wsp
);
1761 if( skate_grind_scansq( player
, wsp
, player
->rb
.v
, 0.3, inf
) )
1763 if( fabsf(v3_dot( player
->rb
.v
, inf
->dir
)) < k_grind_axel_min_vel
)
1766 /* velocity should be at least 60% aligned */
1768 v3_cross( inf
->n
, inf
->dir
, axis
);
1769 v3_muladds( player
->rb
.v
, inf
->n
, -v3_dot( player
->rb
.v
, inf
->n
), pv
);
1771 if( v3_length2( pv
) < 0.0001f
)
1775 if( fabsf(v3_dot( pv
, inf
->dir
)) < k_grind_axel_max_angle
)
1778 if( v3_dot( player
->rb
.v
, inf
->n
) > 0.5f
)
1782 /* check for vertical alignment */
1783 if( v3_dot( player
->rb
.to_world
[1], inf
->n
) < k_grind_axel_max_vangle
)
1787 v3f local_co
, local_dir
, local_n
;
1788 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1789 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1790 m3x3_mulv( player
->rb
.to_local
, inf
->n
, local_n
);
1792 v2f delta
= { local_co
[0], local_co
[2] - k_board_length
*sign
};
1794 float truck_height
= -(k_board_radius
+0.03f
);
1797 v3_cross( player
->rb
.w
, raw
, rv
);
1798 v3_add( player
->rb
.v
, rv
, rv
);
1800 if( (local_co
[1] >= truck_height
) &&
1801 (v2_length2( delta
) <= k_board_radius
*k_board_radius
) )
1810 VG_STATIC
void skate_boardslide_apply( player_instance
*player
,
1811 struct grind_info
*inf
)
1813 struct player_skate
*s
= &player
->_skate
;
1815 v3f local_co
, local_dir
, local_n
;
1816 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1817 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1818 m3x3_mulv( player
->rb
.to_local
, inf
->n
, local_n
);
1821 v3_muladds( local_co
, local_dir
, local_co
[0]/-local_dir
[0],
1823 v3_copy( intersection
, s
->weight_distribution
);
1825 skate_grind_decay( player
, inf
, 0.0125f
);
1826 skate_grind_friction( player
, inf
, 0.25f
);
1828 /* direction alignment */
1830 v3_cross( local_dir
, local_n
, perp
);
1831 v3_muls( local_dir
, vg_signf(local_dir
[0]), dir
);
1832 v3_muls( perp
, vg_signf(perp
[2]), perp
);
1834 m3x3_mulv( player
->rb
.to_world
, dir
, dir
);
1835 m3x3_mulv( player
->rb
.to_world
, perp
, perp
);
1838 q_axis_angle( qbalance
, dir
, local_co
[0]*k_grind_balance
);
1839 q_mulv( qbalance
, perp
, perp
);
1841 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[0],
1843 k_grind_spring
, k_grind_dampener
,
1846 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[2],
1848 k_grind_spring
, k_grind_dampener
,
1851 vg_line_arrow( player
->rb
.co
, dir
, 0.5f
, VG__GREEN
);
1852 vg_line_arrow( player
->rb
.co
, perp
, 0.5f
, VG__BLUE
);
1854 v3_copy( inf
->dir
, s
->grind_dir
);
1857 VG_STATIC
int skate_boardslide_entry( player_instance
*player
,
1858 struct grind_info
*inf
)
1860 struct player_skate
*s
= &player
->_skate
;
1862 if( skate_grind_scansq( player
, player
->rb
.co
,
1863 player
->rb
.to_world
[0], k_board_length
,
1866 v3f local_co
, local_dir
;
1867 m4x3_mulv( player
->rb
.to_local
, inf
->co
, local_co
);
1868 m3x3_mulv( player
->rb
.to_local
, inf
->dir
, local_dir
);
1870 if( (fabsf(local_co
[2]) <= k_board_length
) && /* within wood area */
1871 (local_co
[1] >= 0.0f
) && /* at deck level */
1872 (fabsf(local_dir
[0]) >= 0.25f
) ) /* perpendicular to us */
1874 if( fabsf(v3_dot( player
->rb
.v
, inf
->dir
)) < k_grind_axel_min_vel
)
1884 VG_STATIC
int skate_boardslide_renew( player_instance
*player
,
1885 struct grind_info
*inf
)
1887 struct player_skate
*s
= &player
->_skate
;
1889 if( !skate_grind_scansq( player
, player
->rb
.co
,
1890 player
->rb
.to_world
[0], k_board_length
,
1894 /* Exit condition: cant see grind target directly */
1896 v3_muladds( player
->rb
.co
, player
->rb
.to_world
[1], 0.2f
, vis
);
1897 if( !skate_point_visible( vis
, inf
->co
) )
1900 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1901 float dv
= fabsf(v3_dot( player
->rb
.v
, inf
->dir
)),
1902 minv
= k_grind_axel_min_vel
*0.8f
;
1907 if( fabsf(v3_dot( inf
->dir
, s
->grind_dir
)) < k_grind_max_edge_angle
)
1913 VG_STATIC
void skate_store_grind_vec( player_instance
*player
,
1914 struct grind_info
*inf
)
1916 struct player_skate
*s
= &player
->_skate
;
1919 skate_grind_orient( inf
, mtx
);
1920 m3x3_transpose( mtx
, mtx
);
1923 v3_sub( inf
->co
, player
->rb
.co
, raw
);
1925 m3x3_mulv( mtx
, raw
, s
->grind_vec
);
1926 v3_normalize( s
->grind_vec
);
1927 v3_copy( inf
->dir
, s
->grind_dir
);
1930 VG_STATIC
enum skate_activity
skate_availible_grind( player_instance
*player
)
1932 struct player_skate
*s
= &player
->_skate
;
1934 if( s
->state
.grind_cooldown
> 100 ){
1935 vg_fatal_error( "wth!\n" );
1938 /* debounces this state manager a little bit */
1939 if( s
->state
.grind_cooldown
){
1940 s
->state
.grind_cooldown
--;
1941 return k_skate_activity_undefined
;
1944 struct grind_info inf_back50
,
1956 joystick_state( k_srjoystick_steer
, steer
);
1958 if( s
->state
.activity
== k_skate_activity_grind_5050
||
1959 s
->state
.activity
== k_skate_activity_grind_back50
||
1960 s
->state
.activity
== k_skate_activity_grind_front50
)
1962 float tilt
= steer
[1];
1964 if( fabsf(tilt
) >= 0.25f
){
1965 v3f raw
= {0.0f
,0.0f
,tilt
};
1966 m3x3_mulv( player
->rb
.to_world
, raw
, raw
);
1968 float way
= tilt
* vg_signf( v3_dot( raw
, player
->rb
.v
) );
1970 if( way
< 0.0f
) allow_front
= 0;
1971 else allow_back
= 0;
1975 if( s
->state
.activity
== k_skate_activity_grind_boardslide
){
1976 res_slide
= skate_boardslide_renew( player
, &inf_slide
);
1978 else if( s
->state
.activity
== k_skate_activity_grind_back50
){
1979 res_back50
= skate_grind_truck_renew( player
, 1.0f
, &inf_back50
);
1982 res_front50
= skate_grind_truck_entry( player
, -1.0f
, &inf_front50
);
1984 else if( s
->state
.activity
== k_skate_activity_grind_front50
){
1985 res_front50
= skate_grind_truck_renew( player
, -1.0f
, &inf_front50
);
1988 res_back50
= skate_grind_truck_entry( player
, 1.0f
, &inf_back50
);
1990 else if( s
->state
.activity
== k_skate_activity_grind_5050
){
1992 res_front50
= skate_grind_truck_renew( player
, -1.0f
, &inf_front50
);
1994 res_back50
= skate_grind_truck_renew( player
, 1.0f
, &inf_back50
);
1997 res_slide
= skate_boardslide_entry( player
, &inf_slide
);
2000 res_back50
= skate_grind_truck_entry( player
, 1.0f
, &inf_back50
);
2003 res_front50
= skate_grind_truck_entry( player
, -1.0f
, &inf_front50
);
2005 if( res_back50
!= res_front50
){
2006 int wants_to_do_that
= fabsf(steer
[1]) >= 0.25f
;
2008 res_back50
&= wants_to_do_that
;
2009 res_front50
&= wants_to_do_that
;
2013 const enum skate_activity table
[] =
2014 { /* slide | back | front */
2015 k_skate_activity_undefined
, /* 0 0 0 */
2016 k_skate_activity_grind_front50
, /* 0 0 1 */
2017 k_skate_activity_grind_back50
, /* 0 1 0 */
2018 k_skate_activity_grind_5050
, /* 0 1 1 */
2020 /* slide has priority always */
2021 k_skate_activity_grind_boardslide
, /* 1 0 0 */
2022 k_skate_activity_grind_boardslide
, /* 1 0 1 */
2023 k_skate_activity_grind_boardslide
, /* 1 1 0 */
2024 k_skate_activity_grind_boardslide
, /* 1 1 1 */
2026 , new_activity
= table
[ res_slide
<< 2 | res_back50
<< 1 | res_front50
];
2028 if( new_activity
== k_skate_activity_undefined
){
2029 if( s
->state
.activity
>= k_skate_activity_grind_any
){
2030 s
->state
.grind_cooldown
= 15;
2031 s
->state
.surface_cooldown
= 10;
2034 else if( new_activity
== k_skate_activity_grind_boardslide
){
2035 skate_boardslide_apply( player
, &inf_slide
);
2037 else if( new_activity
== k_skate_activity_grind_back50
){
2038 if( s
->state
.activity
!= k_skate_activity_grind_back50
)
2039 skate_store_grind_vec( player
, &inf_back50
);
2041 skate_grind_truck_apply( player
, 1.0f
, &inf_back50
, 1.0f
);
2043 else if( new_activity
== k_skate_activity_grind_front50
){
2044 if( s
->state
.activity
!= k_skate_activity_grind_front50
)
2045 skate_store_grind_vec( player
, &inf_front50
);
2047 skate_grind_truck_apply( player
, -1.0f
, &inf_front50
, 1.0f
);
2049 else if( new_activity
== k_skate_activity_grind_5050
)
2050 skate_5050_apply( player
, &inf_front50
, &inf_back50
);
2052 return new_activity
;
2055 VG_STATIC
void player__skate_update( player_instance
*player
)
2057 struct player_skate
*s
= &player
->_skate
;
2058 world_instance
*world
= world_current_instance();
2060 if( world
->water
.enabled
){
2061 if( player
->rb
.co
[1]+0.25f
< world
->water
.height
){
2062 audio_oneshot_3d( &audio_splash
, player
->rb
.co
, 40.0f
, 1.0f
);
2063 player__skate_kill_audio( player
);
2064 player__dead_transition( player
);
2069 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
2070 s
->state
.activity_prev
= s
->state
.activity
;
2072 v3_zero( normal_total
);
2074 struct board_collider
2081 enum board_collider_state
2083 k_collider_state_default
,
2084 k_collider_state_disabled
,
2085 k_collider_state_colliding
2092 { 0.0f
, 0.0f
, -k_board_length
},
2093 .radius
= k_board_radius
,
2097 { 0.0f
, 0.0f
, k_board_length
},
2098 .radius
= k_board_radius
,
2105 if( s
->state
.activity
<= k_skate_activity_air_to_grind
){
2106 float min_dist
= 0.6f
;
2107 for( int i
=0; i
<2; i
++ ){
2109 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, wpos
);
2111 if( bh_closest_point( world
->geo_bh
, wpos
, closest
, min_dist
) != -1 ){
2112 min_dist
= vg_minf( min_dist
, v3_dist( closest
, wpos
) );
2116 float vy
= v3_dot( player
->basis
[1], player
->rb
.v
);
2117 vy
= vg_maxf( 0.0f
, vy
);
2119 slap
= vg_clampf( (min_dist
/0.5f
) + vy
, 0.0f
, 1.0f
)*0.3f
;
2121 s
->state
.slap
= vg_lerpf( s
->state
.slap
, slap
, 10.0f
*k_rb_delta
);
2123 wheels
[0].pos
[1] = s
->state
.slap
;
2124 wheels
[1].pos
[1] = s
->state
.slap
;
2127 const int k_wheel_count
= 2;
2129 s
->substep
= k_rb_delta
;
2130 s
->substep_delta
= s
->substep
;
2133 int substep_count
= 0;
2135 v3_zero( s
->surface_picture
);
2137 int prev_contacts
[2];
2139 for( int i
=0; i
<k_wheel_count
; i
++ ){
2140 wheels
[i
].state
= k_collider_state_default
;
2141 prev_contacts
[i
] = s
->wheel_contacts
[i
];
2144 /* check if we can enter or continue grind */
2145 enum skate_activity grindable_activity
= skate_availible_grind( player
);
2146 if( grindable_activity
!= k_skate_activity_undefined
){
2147 s
->state
.activity
= grindable_activity
;
2151 int contact_count
= 0;
2152 for( int i
=0; i
<2; i
++ ){
2154 v3_copy( player
->rb
.to_world
[0], axel
);
2156 if( skate_compute_surface_alignment( player
, wheels
[i
].pos
,
2157 wheels
[i
].colour
, normal
, axel
) )
2159 rb_effect_spring_target_vector( &player
->rb
, player
->rb
.to_world
[0],
2161 k_surface_spring
, k_surface_dampener
,
2164 v3_add( normal
, s
->surface_picture
, s
->surface_picture
);
2166 s
->wheel_contacts
[i
] = 1;
2169 s
->wheel_contacts
[i
] = 0;
2172 m3x3_mulv( player
->rb
.to_local
, axel
, s
->truckv0
[i
] );
2175 if( s
->state
.surface_cooldown
){
2176 s
->state
.surface_cooldown
--;
2180 if( (prev_contacts
[0]+prev_contacts
[1] == 1) && (contact_count
== 2) ){
2182 for( int i
=0; i
<2; i
++ ){
2183 if( !prev_contacts
[i
] ){
2185 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, co
);
2186 audio_oneshot_3d( &audio_taps
[vg_randu32()%4], co
, 40.0f
, 0.75f
);
2192 if( contact_count
){
2193 s
->state
.activity
= k_skate_activity_ground
;
2194 s
->state
.gravity_bias
= k_gravity
;
2195 v3_normalize( s
->surface_picture
);
2197 skate_apply_friction_model( player
);
2198 skate_weight_distribute( player
);
2201 if( s
->state
.activity
> k_skate_activity_air_to_grind
)
2202 s
->state
.activity
= k_skate_activity_air
;
2204 v3_zero( s
->weight_distribution
);
2205 skate_apply_air_model( player
);
2210 if( s
->state
.activity
== k_skate_activity_grind_back50
)
2211 wheels
[1].state
= k_collider_state_disabled
;
2212 if( s
->state
.activity
== k_skate_activity_grind_front50
)
2213 wheels
[0].state
= k_collider_state_disabled
;
2214 if( s
->state
.activity
== k_skate_activity_grind_5050
){
2215 wheels
[0].state
= k_collider_state_disabled
;
2216 wheels
[1].state
= k_collider_state_disabled
;
2219 /* all activities */
2220 skate_apply_steering_model( player
);
2221 skate_adjust_up_direction( player
);
2222 skate_apply_cog_model( player
);
2223 skate_apply_jump_model( player
);
2224 skate_apply_grab_model( player
);
2225 skate_apply_trick_model( player
);
2226 skate_apply_pump_model( player
);
2231 * Phase 0: Continous collision detection
2232 * --------------------------------------------------------------------------
2235 v3f head_wp0
, head_wp1
, start_co
;
2236 m4x3_mulv( player
->rb
.to_world
, s
->state
.head_position
, head_wp0
);
2237 v3_copy( player
->rb
.co
, start_co
);
2239 /* calculate transform one step into future */
2242 v3_muladds( player
->rb
.co
, player
->rb
.v
, s
->substep
, future_co
);
2244 if( v3_length2( player
->rb
.w
) > 0.0f
){
2247 v3_copy( player
->rb
.w
, axis
);
2249 float mag
= v3_length( axis
);
2250 v3_divs( axis
, mag
, axis
);
2251 q_axis_angle( rotation
, axis
, mag
*s
->substep
);
2252 q_mul( rotation
, player
->rb
.q
, future_q
);
2253 q_normalize( future_q
);
2256 v4_copy( player
->rb
.q
, future_q
);
2258 v3f future_cg
, current_cg
, cg_offset
;
2259 q_mulv( player
->rb
.q
, s
->weight_distribution
, current_cg
);
2260 q_mulv( future_q
, s
->weight_distribution
, future_cg
);
2261 v3_sub( future_cg
, current_cg
, cg_offset
);
2263 /* calculate the minimum time we can move */
2264 float max_time
= s
->substep
;
2266 for( int i
=0; i
<k_wheel_count
; i
++ ){
2267 if( wheels
[i
].state
== k_collider_state_disabled
)
2270 v3f current
, future
, r_cg
;
2272 q_mulv( future_q
, wheels
[i
].pos
, future
);
2273 v3_add( future
, future_co
, future
);
2274 v3_add( cg_offset
, future
, future
);
2276 q_mulv( player
->rb
.q
, wheels
[i
].pos
, current
);
2277 v3_add( current
, player
->rb
.co
, current
);
2282 float cast_radius
= wheels
[i
].radius
- k_penetration_slop
* 2.0f
;
2283 if( spherecast_world( world
, current
, future
, cast_radius
, &t
, n
) != -1)
2284 max_time
= vg_minf( max_time
, t
* s
->substep
);
2287 /* clamp to a fraction of delta, to prevent locking */
2288 float rate_lock
= substep_count
;
2289 rate_lock
*= k_rb_delta
* 0.1f
;
2290 rate_lock
*= rate_lock
;
2292 max_time
= vg_maxf( max_time
, rate_lock
);
2293 s
->substep_delta
= max_time
;
2296 v3_muladds( player
->rb
.co
, player
->rb
.v
, s
->substep_delta
, player
->rb
.co
);
2297 if( v3_length2( player
->rb
.w
) > 0.0f
){
2300 v3_copy( player
->rb
.w
, axis
);
2302 float mag
= v3_length( axis
);
2303 v3_divs( axis
, mag
, axis
);
2304 q_axis_angle( rotation
, axis
, mag
*s
->substep_delta
);
2305 q_mul( rotation
, player
->rb
.q
, player
->rb
.q
);
2306 q_normalize( player
->rb
.q
);
2308 q_mulv( player
->rb
.q
, s
->weight_distribution
, future_cg
);
2309 v3_sub( current_cg
, future_cg
, cg_offset
);
2310 v3_add( player
->rb
.co
, cg_offset
, player
->rb
.co
);
2313 rb_update_transform( &player
->rb
);
2314 v3_muladds( player
->rb
.v
, player
->basis
[1],
2315 -s
->state
.gravity_bias
* s
->substep_delta
, player
->rb
.v
);
2317 s
->substep
-= s
->substep_delta
;
2319 rb_ct manifold
[128];
2320 int manifold_len
= 0;
2322 * Phase -1: head detection
2323 * --------------------------------------------------------------------------
2325 m4x3_mulv( player
->rb
.to_world
, s
->state
.head_position
, head_wp1
);
2329 if( (v3_dist2( head_wp0
, head_wp1
) > 0.001f
) &&
2330 (spherecast_world( world
, head_wp0
, head_wp1
, 0.2f
, &t
, n
) != -1) )
2332 v3_lerp( start_co
, player
->rb
.co
, t
, player
->rb
.co
);
2333 rb_update_transform( &player
->rb
);
2335 player__skate_kill_audio( player
);
2336 player__dead_transition( player
);
2341 * Phase 1: Regular collision detection
2342 * --------------------------------------------------------------------------
2345 for( int i
=0; i
<k_wheel_count
; i
++ ){
2346 if( wheels
[i
].state
== k_collider_state_disabled
)
2350 m3x3_identity( mtx
);
2351 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, mtx
[3] );
2353 rb_sphere collider
= { .radius
= wheels
[i
].radius
};
2355 rb_ct
*man
= &manifold
[ manifold_len
];
2357 int l
= skate_collide_smooth( player
, mtx
, &collider
, man
);
2359 wheels
[i
].state
= k_collider_state_colliding
;
2364 float grind_radius
= k_board_radius
* 0.75f
;
2365 rb_capsule capsule
= { .height
= (k_board_length
+0.2f
)*2.0f
,
2366 .radius
=grind_radius
};
2368 v3_muls( player
->rb
.to_world
[0], 1.0f
, mtx
[0] );
2369 v3_muls( player
->rb
.to_world
[2], -1.0f
, mtx
[1] );
2370 v3_muls( player
->rb
.to_world
[1], 1.0f
, mtx
[2] );
2371 v3_muladds( player
->rb
.to_world
[3], player
->rb
.to_world
[1],
2372 grind_radius
+ k_board_radius
*0.25f
+s
->state
.slap
, mtx
[3] );
2374 rb_ct
*cman
= &manifold
[manifold_len
];
2376 int l
= rb_capsule__scene( mtx
, &capsule
, NULL
, &world
->rb_geo
.inf
.scene
,
2380 for( int i
=0; i
<l
; i
++ )
2381 cman
[l
].type
= k_contact_type_edge
;
2382 rb_manifold_filter_joint_edges( cman
, l
, 0.03f
);
2383 l
= rb_manifold_apply_filtered( cman
, l
);
2388 vg_line_capsule( mtx
, capsule
.radius
, capsule
.height
, VG__WHITE
);
2391 if( s
->state
.activity
>= k_skate_activity_grind_any
){
2392 for( int i
=0; i
<s
->limit_count
; i
++ ){
2393 struct grind_limit
*limit
= &s
->limits
[i
];
2394 rb_ct
*ct
= &manifold
[ manifold_len
++ ];
2395 m4x3_mulv( player
->rb
.to_world
, limit
->ra
, ct
->co
);
2396 m3x3_mulv( player
->rb
.to_world
, limit
->n
, ct
->n
);
2398 ct
->type
= k_contact_type_default
;
2404 * --------------------------------------------------------------------------
2409 m4x3_mulv( player
->rb
.to_world
, s
->weight_distribution
, world_cog
);
2410 vg_line_point( world_cog
, 0.02f
, VG__BLACK
);
2412 for( int i
=0; i
<manifold_len
; i
++ ){
2413 rb_prepare_contact( &manifold
[i
], s
->substep_delta
);
2414 rb_debug_contact( &manifold
[i
] );
2417 /* yes, we are currently rebuilding mass matrices every frame. too bad! */
2418 v3f extent
= { k_board_width
, 0.1f
, k_board_length
};
2419 float ex2
= k_board_interia
*extent
[0]*extent
[0],
2420 ey2
= k_board_interia
*extent
[1]*extent
[1],
2421 ez2
= k_board_interia
*extent
[2]*extent
[2];
2423 float mass
= 2.0f
* (extent
[0]*extent
[1]*extent
[2]);
2424 float inv_mass
= 1.0f
/mass
;
2427 I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
2428 I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
2429 I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
2432 m3x3_identity( iI
);
2439 m3x3_mul( iI
, player
->rb
.to_local
, iIw
);
2440 m3x3_mul( player
->rb
.to_world
, iIw
, iIw
);
2442 for( int j
=0; j
<10; j
++ ){
2443 for( int i
=0; i
<manifold_len
; i
++ ){
2445 * regular dance; calculate velocity & total mass, apply impulse.
2448 struct contact
*ct
= &manifold
[i
];
2451 v3_sub( ct
->co
, world_cog
, delta
);
2452 v3_cross( player
->rb
.w
, delta
, rv
);
2453 v3_add( player
->rb
.v
, rv
, rv
);
2456 v3_cross( delta
, ct
->n
, raCn
);
2459 m3x3_mulv( iIw
, raCn
, raCnI
);
2461 float normal_mass
= 1.0f
/ (inv_mass
+ v3_dot(raCn
,raCnI
)),
2462 vn
= v3_dot( rv
, ct
->n
),
2463 lambda
= normal_mass
* ( -vn
);
2465 float temp
= ct
->norm_impulse
;
2466 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
2467 lambda
= ct
->norm_impulse
- temp
;
2470 v3_muls( ct
->n
, lambda
, impulse
);
2472 v3_muladds( normal_total
, impulse
, inv_mass
, normal_total
);
2473 v3_muladds( player
->rb
.v
, impulse
, inv_mass
, player
->rb
.v
);
2474 v3_cross( delta
, impulse
, impulse
);
2475 m3x3_mulv( iIw
, impulse
, impulse
);
2476 v3_add( impulse
, player
->rb
.w
, player
->rb
.w
);
2478 v3_cross( player
->rb
.w
, delta
, rv
);
2479 v3_add( player
->rb
.v
, rv
, rv
);
2480 vn
= v3_dot( rv
, ct
->n
);
2485 rb_depenetrate( manifold
, manifold_len
, dt
);
2486 v3_add( dt
, player
->rb
.co
, player
->rb
.co
);
2487 rb_update_transform( &player
->rb
);
2491 if( s
->substep
>= 0.0001f
)
2492 goto begin_collision
; /* again! */
2495 * End of collision and dynamics routine
2496 * --------------------------------------------------------------------------
2499 f32 nforce
= v3_length(normal_total
);
2500 if( nforce
> 4.0f
){
2501 if( nforce
> 17.6f
){
2502 v3_muladds( player
->rb
.v
, normal_total
, -1.0f
, player
->rb
.v
);
2503 player__dead_transition(player
);
2504 player__skate_kill_audio(player
);
2508 f32 amt
= k_cam_punch
;
2509 if( player
->cam_control
.camera_mode
== k_cam_firstperson
){
2513 v3_muladds( player
->cam_land_punch_v
, normal_total
, amt
,
2514 player
->cam_land_punch_v
);
2517 s
->surface
= k_surface_prop_concrete
;
2519 for( int i
=0; i
<manifold_len
; i
++ ){
2520 rb_ct
*ct
= &manifold
[i
];
2521 struct world_surface
*surf
= world_contact_surface( world
, ct
);
2523 if( surf
->info
.surface_prop
> s
->surface
)
2524 s
->surface
= surf
->info
.surface_prop
;
2527 for( int i
=0; i
<k_wheel_count
; i
++ ){
2529 m3x3_copy( player
->rb
.to_world
, mtx
);
2530 m4x3_mulv( player
->rb
.to_world
, wheels
[i
].pos
, mtx
[3] );
2531 vg_line_sphere( mtx
, wheels
[i
].radius
,
2532 (u32
[]){ VG__WHITE
, VG__BLACK
,
2533 wheels
[i
].colour
}[ wheels
[i
].state
]);
2536 skate_integrate( player
);
2537 vg_line_point( s
->state
.cog
, 0.02f
, VG__WHITE
);
2540 world_intersect_gates(world
, player
->rb
.co
, s
->state
.prev_pos
);
2543 m4x3_mulv( gate
->transport
, player
->rb
.co
, player
->rb
.co
);
2544 m3x3_mulv( gate
->transport
, player
->rb
.v
, player
->rb
.v
);
2545 m4x3_mulv( gate
->transport
, s
->state
.cog
, s
->state
.cog
);
2546 m3x3_mulv( gate
->transport
, s
->state
.cog_v
, s
->state
.cog_v
);
2547 m3x3_mulv( gate
->transport
, s
->state
.throw_v
, s
->state
.throw_v
);
2548 m3x3_mulv( gate
->transport
, s
->state
.head_position
,
2549 s
->state
.head_position
);
2550 m3x3_mulv( gate
->transport
, s
->state
.up_dir
, s
->state
.up_dir
);
2552 v4f transport_rotation
;
2553 m3x3_q( gate
->transport
, transport_rotation
);
2554 q_mul( transport_rotation
, player
->rb
.q
, player
->rb
.q
);
2555 q_mul( transport_rotation
, s
->state
.smoothed_rotation
,
2556 s
->state
.smoothed_rotation
);
2557 rb_update_transform( &player
->rb
);
2558 player__pass_gate( player
, gate
);
2561 /* FIXME: Rate limit */
2562 static int stick_frames
= 0;
2564 if( s
->state
.activity
>= k_skate_activity_ground
)
2569 if( stick_frames
> 5 ) stick_frames
= 5;
2571 if( stick_frames
== 4 ){
2574 if( s
->state
.activity
== k_skate_activity_ground
){
2575 if( (fabsf(s
->state
.slip
) > 0.75f
) ){
2576 audio_oneshot_3d( &audio_lands
[vg_randu32()%2+3], player
->rb
.co
,
2580 audio_oneshot_3d( &audio_lands
[vg_randu32()%3], player
->rb
.co
,
2584 else if( s
->surface
== k_surface_prop_metal
){
2585 audio_oneshot_3d( &audio_board
[3], player
->rb
.co
, 40.0f
, 1.0f
);
2588 audio_oneshot_3d( &audio_board
[8], player
->rb
.co
, 40.0f
, 1.0f
);
2592 } else if( stick_frames
== 0 ){
2597 VG_STATIC
void player__skate_im_gui( player_instance
*player
){
2598 struct player_skate
*s
= &player
->_skate
;
2599 player__debugtext( 1, "V: %5.2f %5.2f %5.2f",player
->rb
.v
[0],
2602 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",player
->rb
.co
[0],
2605 player__debugtext( 1, "W: %5.2f %5.2f %5.2f",player
->rb
.w
[0],
2609 const char *activity_txt
[] =
2614 "undefined (INVALID)",
2615 "grind_any (INVALID)",
2617 "grind_metallic (INVALID)",
2623 player__debugtext( 1, "activity: %s", activity_txt
[s
->state
.activity
] );
2625 player__debugtext( 1, "steer_s: %5.2f %5.2f [%.2f %.2f]",
2626 s
->state
.steerx_s
, s
->state
.steery_s
,
2627 k_steer_ground
, k_steer_air
);
2629 player__debugtext( 1, "flip: %.4f %.4f", s
->state
.flip_rate
,
2630 s
->state
.flip_time
);
2631 player__debugtext( 1, "trickv: %.2f %.2f %.2f",
2632 s
->state
.trick_vel
[0],
2633 s
->state
.trick_vel
[1],
2634 s
->state
.trick_vel
[2] );
2635 player__debugtext( 1, "tricke: %.2f %.2f %.2f",
2636 s
->state
.trick_euler
[0],
2637 s
->state
.trick_euler
[1],
2638 s
->state
.trick_euler
[2] );
2641 VG_STATIC
void player__skate_animate( player_instance
*player
){
2642 struct player_skate
*s
= &player
->_skate
;
2643 struct player_skate_state
*state
= &player
->_skate
.state
;
2644 struct player_skate_animator
*animator
= &s
->animator
;
2647 float kheight
= 2.0f
,
2650 v3_zero( animator
->offset
);
2652 v3f cog_local
, cog_ideal
;
2653 m4x3_mulv( player
->rb
.to_local
, s
->state
.cog
, cog_local
);
2655 v3_copy( s
->state
.up_dir
, cog_ideal
);
2656 v3_normalize( cog_ideal
);
2657 m3x3_mulv( player
->rb
.to_local
, cog_ideal
, cog_ideal
);
2659 v3_sub( cog_ideal
, cog_local
, animator
->offset
);
2661 v3_muls( animator
->offset
, 4.0f
, animator
->offset
);
2662 animator
->offset
[1] *= -1.0f
;
2664 float curspeed
= v3_length( player
->rb
.v
),
2665 kickspeed
= vg_clampf( curspeed
*(1.0f
/40.0f
), 0.0f
, 1.0f
),
2666 kicks
= (vg_randf64()-0.5f
)*2.0f
*kickspeed
,
2667 sign
= vg_signf( kicks
);
2669 animator
->wobble
[0] = vg_lerpf( animator
->wobble
[0], kicks
*kicks
*sign
,
2670 6.0f
*vg
.time_delta
);
2671 animator
->wobble
[1] = vg_lerpf( animator
->wobble
[1], animator
->wobble
[0],
2672 2.4f
*vg
.time_delta
);
2674 animator
->offset
[0] *= 0.26f
;
2675 animator
->offset
[0] += animator
->wobble
[1]*3.0f
;
2677 animator
->offset
[1] *= -0.3f
;
2678 animator
->offset
[2] *= 0.01f
;
2680 animator
->offset
[0]=vg_clampf(animator
->offset
[0],-0.8f
,0.8f
)*
2681 (1.0f
-fabsf(animator
->slide
)*0.9f
);
2682 animator
->offset
[1]=vg_clampf(animator
->offset
[1],-0.5f
,0.0f
);
2684 v3_muls( animator
->offset
, 0.3f
, player
->cam_control
.tpv_offset_extra
);
2686 /* localized vectors */
2687 m4x3_mulv( player
->rb
.to_local
, s
->state
.cog
, animator
->local_cog
);
2690 * Animation blending
2691 * ===========================================
2696 float desired
= 0.0f
;
2697 if( s
->state
.activity
== k_skate_activity_ground
)
2698 desired
= vg_clampf( fabsf( s
->state
.slip
), 0.0f
, 1.0f
);
2700 animator
->slide
= vg_lerpf( animator
->slide
, desired
, 2.4f
*vg
.time_delta
);
2703 /* movement information */
2704 int iair
= s
->state
.activity
<= k_skate_activity_air_to_grind
;
2706 float dirz
= s
->state
.reverse
> 0.0f
? 0.0f
: 1.0f
,
2707 dirx
= s
->state
.slip
< 0.0f
? 0.0f
: 1.0f
,
2708 fly
= iair
? 1.0f
: 0.0f
,
2709 wdist
= s
->weight_distribution
[2] / k_board_length
;
2711 if( s
->state
.activity
>= k_skate_activity_grind_any
)
2714 animator
->z
= vg_lerpf( animator
->z
, dirz
, 2.4f
*vg
.time_delta
);
2715 animator
->x
= vg_lerpf( animator
->x
, dirx
, 0.6f
*vg
.time_delta
);
2716 animator
->fly
= vg_lerpf( animator
->fly
, fly
, 3.4f
*vg
.time_delta
);
2717 animator
->weight
= vg_lerpf( animator
->weight
, wdist
, 9.0f
*vg
.time_delta
);
2719 float stand
= 1.0f
- vg_clampf( curspeed
* 0.03f
, 0.0f
, 1.0f
);
2720 animator
->stand
= vg_lerpf( animator
->stand
, stand
, 6.0f
*vg
.time_delta
);
2721 animator
->reverse
= s
->state
.reverse
;
2723 if( fabsf(s
->state
.slip
) > 0.3f
){
2724 f32 slide_dir
= vg_signf(v3_dot(player
->rb
.v
,player
->rb
.to_world
[0]));
2725 s
->state
.delayed_slip_dir
= slide_dir
;
2729 f32 grind
=s
->state
.activity
>= k_skate_activity_grind_any
? 1.0f
: 0.0f
;
2730 animator
->grind
= vg_lerpf( animator
->grind
, grind
, 5.0f
*vg
.time_delta
);
2732 f32 grind_frame
= 0.5f
;
2734 if( s
->state
.activity
== k_skate_activity_grind_front50
)
2736 else if( s
->state
.activity
== k_skate_activity_grind_back50
)
2739 animator
->grind_balance
= vg_lerpf( animator
->grind_balance
, grind_frame
,
2740 5.0f
*vg
.time_delta
);
2743 animator
->push_time
= vg
.time
- s
->state
.start_push
;
2744 animator
->push
= vg_lerpf( animator
->push
,
2745 (vg
.time
- s
->state
.cur_push
) < 0.125,
2746 6.0f
*vg
.time_delta
);
2749 animator
->jump_charge
= s
->state
.jump_charge
;
2750 animator
->jump
= vg_lerpf( animator
->jump
, animator
->jump_charge
,
2751 8.4f
*vg
.time_delta
);
2754 animator
->jump_dir
= s
->state
.jump_dir
;
2755 f32 jump_start_frame
= 14.0f
/30.0f
;
2756 animator
->jump_time
= animator
->jump_charge
* jump_start_frame
;
2757 f32 jump_frame
= (vg
.time
- s
->state
.jump_time
) + jump_start_frame
;
2758 if( jump_frame
>= jump_start_frame
&& jump_frame
<= (40.0f
/30.0f
) )
2759 animator
->jump_time
= jump_frame
;
2762 float jump_t
= vg
.time
-s
->state
.jump_time
;
2765 float extra
= h
*exp(1.0-h
) * (s
->state
.jump_dir
?1.0f
:-1.0f
);
2766 extra
*= s
->state
.slap
* 4.0f
;
2768 v3_add( s
->state
.trick_euler
, s
->state
.trick_residuald
,
2769 animator
->board_euler
);
2770 v3_muls( animator
->board_euler
, VG_TAUf
, animator
->board_euler
);
2772 animator
->board_euler
[0] *= 0.5f
;
2773 animator
->board_euler
[1] += extra
;
2774 animator
->trick_type
= s
->state
.trick_type
;
2777 f32 lean1
, lean2
= animator
->steer
[0] * animator
->reverse
* -0.36f
,
2780 lean1
= animator
->slide
* animator
->delayed_slip_dir
;
2781 if( fabsf(lean1
)>fabsf(lean2
) ) lean
= lean1
;
2784 if( ((int)roundf(animator
->board_euler
[0])) % 2 ) lean
= -lean
;
2785 lean
= vg_clampf( lean
, -1.0f
, 1.0f
);
2786 animator
->board_lean
=
2787 vg_lerpf(animator
->board_lean
, lean
, vg
.time_delta
*18.0f
);
2789 /* feet placement */
2790 struct player_board
*board
=
2791 addon_cache_item_if_loaded( k_addon_type_board
,
2792 player
->board_view_slot
);
2794 if( animator
->weight
> 0.0f
){
2795 animator
->foot_offset
[0] =
2796 board
->truck_positions
[k_board_truck_back
][2]+0.3f
;
2799 animator
->foot_offset
[1] =
2800 board
->truck_positions
[k_board_truck_front
][2]-0.3f
;
2804 f32 slapm
= vg_maxf( 1.0f
-v3_length2( s
->state
.trick_vel
), 0.0f
);
2805 animator
->slap
= s
->state
.slap
;
2806 animator
->subslap
= vg_lerpf( animator
->subslap
, slapm
,
2807 vg
.time_delta
*10.0f
);
2809 f32 l
= ((s
->state
.activity
< k_skate_activity_ground
) &&
2810 v3_length2(s
->state
.trick_vel
) > 0.1f
)? 1: 0;
2811 animator
->trick_foot
= vg_lerpf( animator
->trick_foot
, l
,
2812 8.4f
*vg
.time_delta
);
2816 joystick_state( k_srjoystick_grab
, grab_input
);
2817 v2_add( s
->state
.grab_mouse_delta
, grab_input
, grab_input
);
2819 if( v2_length2( grab_input
) <= 0.001f
) grab_input
[0] = -1.0f
;
2820 else v2_normalize_clamp( grab_input
);
2821 v2_lerp( animator
->grab
, grab_input
, 2.4f
*vg
.time_delta
, animator
->grab
);
2822 animator
->grabbing
= s
->state
.grabbing
;
2825 joystick_state( k_srjoystick_steer
, animator
->steer
);
2828 if( (s
->state
.activity
<= k_skate_activity_air_to_grind
) &&
2829 (fabsf(s
->state
.flip_rate
) > 0.01f
) ){
2830 float substep
= vg
.time_fixed_extrapolate
;
2831 float t
= s
->state
.flip_time
+s
->state
.flip_rate
*substep
*k_rb_delta
;
2832 sign
= vg_signf( t
);
2834 t
= 1.0f
- vg_minf( 1.0f
, fabsf( t
* 1.1f
) );
2835 t
= sign
* (1.0f
-t
*t
);
2837 f32 angle
= vg_clampf( t
, -1.0f
, 1.0f
) * VG_TAUf
,
2838 distm
= s
->state
.land_dist
* fabsf(s
->state
.flip_rate
) * 3.0f
,
2839 blend
= vg_clampf( 1.0f
-distm
, 0.0f
, 1.0f
);
2840 angle
= vg_lerpf( angle
, vg_signf(s
->state
.flip_rate
)*VG_TAUf
, blend
);
2841 q_axis_angle( animator
->qflip
, s
->state
.flip_axis
, angle
);
2844 q_identity( animator
->qflip
);
2846 /* counter-rotation */
2847 if( v3_length2( s
->state
.up_dir
) > 0.001f
){
2848 if( v4_length(s
->state
.smoothed_rotation
) <= 0.1f
||
2849 v4_length(s
->state
.smoothed_rotation
) >= 1.1f
){
2850 vg_warn( "FIX THIS! CARROT\n" ); /* this never happens anymore? */
2851 v4_copy( player
->rb
.q
, s
->state
.smoothed_rotation
);
2853 v4_lerp( s
->state
.smoothed_rotation
, player
->rb
.q
,
2854 2.0f
*vg
.time_frame_delta
,
2855 s
->state
.smoothed_rotation
);
2856 q_normalize( s
->state
.smoothed_rotation
);
2858 v3f yaw_ref
= {1.0f
,0.0f
,0.0f
},
2859 yaw_smooth
= {1.0f
,0.0f
,0.0f
};
2860 q_mulv( player
->rb
.q
, yaw_ref
, yaw_ref
);
2861 q_mulv( s
->state
.smoothed_rotation
, yaw_smooth
, yaw_smooth
);
2862 m3x3_mulv( player
->rb
.to_local
, yaw_smooth
, yaw_smooth
);
2863 m3x3_mulv( player
->rb
.to_local
, yaw_ref
, yaw_ref
);
2865 f32 yaw_counter_rotate
= v3_dot(yaw_ref
,yaw_smooth
);
2866 yaw_counter_rotate
= vg_clampf(yaw_counter_rotate
,-0.7f
,0.7f
);
2867 yaw_counter_rotate
= acosf( yaw_counter_rotate
);
2868 yaw_counter_rotate
*= 1.0f
-animator
->fly
;
2871 m3x3_mulv( player
->rb
.to_local
, s
->state
.up_dir
, ndir
);
2872 v3_normalize( ndir
);
2874 v3f up
= { 0.0f
, 1.0f
, 0.0f
};
2876 float a
= v3_dot( ndir
, up
);
2877 a
= acosf( vg_clampf( a
, -1.0f
, 1.0f
) );
2880 v4f qcounteryaw
, qfixup
;
2882 v3_cross( up
, ndir
, axis
);
2883 q_axis_angle( qfixup
, axis
, a
);
2885 q_axis_angle( qcounteryaw
, (v3f
){0.0f
,1.0f
,0.0f
}, yaw_counter_rotate
);
2886 q_mul( qcounteryaw
, qfixup
, animator
->qfixuptotal
);
2887 q_normalize( animator
->qfixuptotal
);
2890 m3x3_mulv( player
->rb
.to_world
, up
, p1
);
2891 m3x3_mulv( player
->rb
.to_world
, ndir
, p2
);
2893 vg_line_arrow( player
->rb
.co
, p1
, 0.25f
, VG__PINK
);
2894 vg_line_arrow( player
->rb
.co
, p2
, 0.25f
, VG__PINK
);
2897 else q_identity( animator
->qfixuptotal
);
2898 q_identity( animator
->qfixuptotal
);
2899 rb_extrapolate( &player
->rb
, animator
->root_co
, animator
->root_q
);
2902 VG_STATIC
void player__skate_pose( player_instance
*player
, player_pose
*pose
){
2903 struct player_avatar
*av
= player
->playeravatar
;
2904 struct skeleton
*sk
= &av
->sk
;
2905 struct player_skate
*s
= &player
->_skate
;
2906 struct player_skate_animator
*animator
= &s
->animator
;
2907 pose
->type
= k_player_pose_type_ik
;
2908 v3_copy( animator
->root_co
, pose
->root_co
);
2909 v4_copy( animator
->root_q
, pose
->root_q
);
2913 q_mulv( pose
->root_q
, (v3f
){0.0f
,1.0f
,0.0f
}, ext_up
);
2914 v3_copy( pose
->root_co
, ext_co
);
2915 v3_muladds( pose
->root_co
, ext_up
, -0.1f
, pose
->root_co
);
2917 /* apply flip rotation at midpoint */
2918 q_mul( animator
->qflip
, pose
->root_q
, pose
->root_q
);
2919 q_normalize( pose
->root_q
);
2921 v3f rotation_point
, rco
;
2922 v3_muladds( ext_co
, ext_up
, 0.5f
, rotation_point
);
2923 v3_sub( pose
->root_co
, rotation_point
, rco
);
2925 q_mulv( animator
->qflip
, rco
, rco
);
2926 v3_add( rco
, rotation_point
, pose
->root_co
);
2929 * ---------------------------------------------------------------------- */
2931 mdl_keyframe apose
[32], bpose
[32];
2932 mdl_keyframe ground_pose
[32];
2935 f32 dir_frame
= animator
->z
* (15.0f
/30.0f
),
2936 stand_blend
= animator
->offset
[1]*-2.0f
;
2938 pose
->board
.lean
= animator
->board_lean
;
2940 stand_blend
= vg_clampf( 1.0f
-animator
->local_cog
[1], 0, 1 );
2942 skeleton_sample_anim( sk
, s
->anim_stand
, dir_frame
, apose
);
2943 skeleton_sample_anim( sk
, s
->anim_highg
, dir_frame
, bpose
);
2944 skeleton_lerp_pose( sk
, apose
, bpose
, stand_blend
, apose
);
2947 skeleton_sample_anim( sk
, s
->anim_slide
, animator
->x
* 0.5f
, bpose
);
2948 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->slide
, apose
);
2950 if( animator
->reverse
> 0.0f
)
2951 skeleton_sample_anim( sk
, s
->anim_push
, animator
->push_time
, bpose
);
2953 skeleton_sample_anim( sk
, s
->anim_push_reverse
, animator
->push_time
,
2956 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->push
, apose
);
2958 struct skeleton_anim
*jump_anim
= animator
->jump_dir
?
2960 s
->anim_ollie_reverse
;
2962 f32 setup_blend
= vg_minf( animator
->jump
, 1.0f
);
2963 skeleton_sample_anim_clamped( sk
, jump_anim
, animator
->jump_time
, bpose
);
2964 skeleton_lerp_pose( sk
, apose
, bpose
, setup_blend
, ground_pose
);
2967 mdl_keyframe air_pose
[32];
2969 float target
= -animator
->steer
[1];
2970 animator
->airdir
= vg_lerpf( animator
->airdir
, target
,
2971 2.4f
*vg
.time_delta
);
2973 float air_frame
= (animator
->airdir
*0.5f
+0.5f
) * (15.0f
/30.0f
);
2974 skeleton_sample_anim( sk
, s
->anim_air
, air_frame
, apose
);
2976 float ang
= atan2f( animator
->grab
[0], animator
->grab
[1] ),
2977 ang_unit
= (ang
+VG_PIf
) * (1.0f
/VG_TAUf
),
2978 grab_frame
= ang_unit
* (15.0f
/30.0f
);
2980 skeleton_sample_anim( sk
, s
->anim_grabs
, grab_frame
, bpose
);
2981 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->grabbing
, air_pose
);
2984 skeleton_lerp_pose( sk
, ground_pose
, air_pose
, animator
->fly
,
2987 mdl_keyframe
*kf_board
= &pose
->keyframes
[av
->id_board
-1],
2988 *kf_foot_l
= &pose
->keyframes
[av
->id_ik_foot_l
-1],
2989 *kf_foot_r
= &pose
->keyframes
[av
->id_ik_foot_r
-1],
2990 *kf_knee_l
= &pose
->keyframes
[av
->id_ik_knee_l
-1],
2991 *kf_knee_r
= &pose
->keyframes
[av
->id_ik_knee_r
-1],
2992 *kf_hip
= &pose
->keyframes
[av
->id_hip
-1],
2993 *kf_wheels
[] = { &pose
->keyframes
[av
->id_wheel_r
-1],
2994 &pose
->keyframes
[av
->id_wheel_l
-1] };
2997 mdl_keyframe grind_pose
[32];
2999 f32 frame
= animator
->grind_balance
* 0.5f
;
3001 skeleton_sample_anim( sk
, s
->anim_grind
, frame
, apose
);
3002 skeleton_sample_anim( sk
, s
->anim_grind_jump
, frame
, bpose
);
3003 skeleton_lerp_pose( sk
, apose
, bpose
, animator
->jump
, grind_pose
);
3005 skeleton_lerp_pose( sk
, pose
->keyframes
, grind_pose
,
3006 animator
->grind
, pose
->keyframes
);
3007 float add_grab_mod
= 1.0f
- animator
->fly
;
3009 /* additive effects */
3010 u32 apply_to
[] = { av
->id_hip
,
3014 av
->id_ik_elbow_r
};
3016 float apply_rates
[] = { 1.0f
,
3022 for( int i
=0; i
<vg_list_size(apply_to
); i
++ ){
3023 pose
->keyframes
[apply_to
[i
]-1].co
[0] += animator
->offset
[0]*add_grab_mod
;
3024 pose
->keyframes
[apply_to
[i
]-1].co
[2] += animator
->offset
[2]*add_grab_mod
;
3027 /* angle 'correction' */
3029 v3_add( av
->sk
.bones
[av
->id_hip
].co
, kf_hip
->co
, origin
);
3031 for( int i
=0; i
<vg_list_size(apply_to
); i
++ ){
3032 mdl_keyframe
*kf
= &pose
->keyframes
[apply_to
[i
]-1];
3033 keyframe_rotate_around( kf
, origin
, av
->sk
.bones
[apply_to
[i
]].co
,
3034 animator
->qfixuptotal
);
3037 /* trick rotation */
3038 v4f qtrick
, qyaw
, qpitch
, qroll
;
3039 q_axis_angle( qyaw
, (v3f
){0.0f
,1.0f
,0.0f
}, animator
->board_euler
[0] );
3040 q_axis_angle( qpitch
, (v3f
){1.0f
,0.0f
,0.0f
}, animator
->board_euler
[1] );
3041 q_axis_angle( qroll
, (v3f
){0.0f
,0.0f
,1.0f
}, animator
->board_euler
[2] );
3043 q_mul( qyaw
, qroll
, qtrick
);
3044 q_mul( qpitch
, qtrick
, qtrick
);
3045 q_mul( kf_board
->q
, qtrick
, kf_board
->q
);
3046 q_normalize( kf_board
->q
);
3048 kf_foot_l
->co
[2] = vg_lerpf( kf_foot_l
->co
[2], animator
->foot_offset
[0],
3049 0.5f
* animator
->weight
);
3050 kf_foot_r
->co
[2] = vg_lerpf( kf_foot_r
->co
[2], animator
->foot_offset
[1],
3051 -0.5f
* animator
->weight
);
3053 kf_foot_l
->co
[1] += animator
->slap
;
3054 kf_foot_r
->co
[1] += animator
->slap
;
3055 kf_knee_l
->co
[1] += animator
->slap
;
3056 kf_knee_r
->co
[1] += animator
->slap
;
3057 kf_board
->co
[1] += animator
->slap
* animator
->subslap
;
3058 kf_hip
->co
[1] += animator
->slap
* 0.25f
;
3060 if( animator
->trick_type
== k_trick_type_kickflip
){
3061 kf_foot_l
->co
[0] += animator
->trick_foot
* 0.2f
;
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
;
3067 else if( animator
->trick_type
== k_trick_type_treflip
){
3068 kf_foot_l
->co
[0] += animator
->trick_foot
* 0.2f
;
3069 kf_foot_r
->co
[0] -= animator
->trick_foot
* 0.15f
;
3073 * animation wishlist:
3074 * boardslide/grind jump animations
3075 * when tricking the slap should not appply or less apply
3076 * not animations however DONT target grinds that are vertically down.
3079 /* truck rotation */
3080 for( int i
=0; i
<2; i
++ ){
3081 float a
= vg_minf( s
->truckv0
[i
][0], 1.0f
);
3082 a
= -acosf( a
) * vg_signf( s
->truckv0
[i
][1] );
3085 q_axis_angle( q
, (v3f
){0.0f
,0.0f
,1.0f
}, a
);
3086 q_mul( q
, kf_wheels
[i
]->q
, kf_wheels
[i
]->q
);
3087 q_normalize( kf_wheels
[i
]->q
);
3092 *kf_head
= &pose
->keyframes
[av
->id_head
-1],
3093 *kf_elbow_l
= &pose
->keyframes
[av
->id_ik_elbow_l
-1],
3094 *kf_elbow_r
= &pose
->keyframes
[av
->id_ik_elbow_r
-1],
3095 *kf_hand_l
= &pose
->keyframes
[av
->id_ik_hand_l
-1],
3096 *kf_hand_r
= &pose
->keyframes
[av
->id_ik_hand_r
-1];
3098 float warble
= perlin1d( vg
.time
, 2.0f
, 2, 300 );
3099 warble
*= vg_maxf(animator
->grind
, fabsf(animator
->weight
)) * 0.3f
;
3102 q_axis_angle( qrot
, (v3f
){0.8f
,0.7f
,0.6f
}, warble
);
3104 v3f origin
= {0.0f
,0.2f
,0.0f
};
3105 keyframe_rotate_around( kf_hand_l
, origin
,
3106 av
->sk
.bones
[av
->id_ik_hand_l
].co
, qrot
);
3107 keyframe_rotate_around( kf_hand_r
, origin
,
3108 av
->sk
.bones
[av
->id_ik_hand_r
].co
, qrot
);
3109 keyframe_rotate_around( kf_hip
, origin
,
3110 av
->sk
.bones
[av
->id_hip
].co
, qrot
);
3111 keyframe_rotate_around( kf_elbow_r
, origin
,
3112 av
->sk
.bones
[av
->id_ik_elbow_r
].co
, qrot
);
3113 keyframe_rotate_around( kf_elbow_l
, origin
,
3114 av
->sk
.bones
[av
->id_ik_elbow_l
].co
, qrot
);
3116 q_inv( qrot
, qrot
);
3117 q_mul( qrot
, kf_head
->q
, kf_head
->q
);
3118 q_normalize( kf_head
->q
);
3122 VG_STATIC
void player__skate_post_animate( player_instance
*player
)
3124 struct player_skate
*s
= &player
->_skate
;
3125 struct player_avatar
*av
= player
->playeravatar
;
3127 player
->cam_velocity_influence
= 1.0f
;
3129 v3f head
= { 0.0f
, 1.8f
, 0.0f
};
3130 m4x3_mulv( av
->sk
.final_mtx
[ av
->id_head
], head
, s
->state
.head_position
);
3131 m4x3_mulv( player
->rb
.to_local
, s
->state
.head_position
,
3132 s
->state
.head_position
);
3135 VG_STATIC
void player__skate_reset_animator( player_instance
*player
){
3136 struct player_skate
*s
= &player
->_skate
;
3137 struct player_skate_state
*state
= &s
->state
;
3139 memset( &s
->animator
, 0, sizeof(s
->animator
) );
3141 if( s
->state
.activity
<= k_skate_activity_air_to_grind
)
3142 s
->animator
.fly
= 1.0f
;
3144 s
->animator
.fly
= 0.0f
;
3147 VG_STATIC
void player__skate_clear_mechanics( player_instance
*player
)
3149 struct player_skate
*s
= &player
->_skate
;
3150 s
->state
.jump_charge
= 0.0f
;
3151 s
->state
.charging_jump
= 0;
3152 s
->state
.jump_dir
= 0;
3153 v3_zero( s
->state
.flip_axis
);
3154 s
->state
.flip_time
= 0.0f
;
3155 s
->state
.flip_rate
= 0.0f
;
3156 s
->state
.reverse
= 0.0f
;
3157 s
->state
.slip
= 0.0f
;
3158 s
->state
.grabbing
= 0.0f
;
3159 v2_zero( s
->state
.grab_mouse_delta
);
3160 s
->state
.slap
= 0.0f
;
3161 s
->state
.jump_time
= 0.0;
3162 s
->state
.start_push
= 0.0;
3163 s
->state
.cur_push
= 0.0;
3164 s
->state
.air_start
= 0.0;
3166 v3_zero( s
->state
.air_init_v
);
3167 v3_zero( s
->state
.air_init_co
);
3169 s
->state
.gravity_bias
= k_gravity
;
3170 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
3171 v4_copy( player
->rb
.q
, s
->state
.smoothed_rotation
);
3172 v3_zero( s
->state
.throw_v
);
3173 v3_zero( s
->state
.trick_vel
);
3174 v3_zero( s
->state
.trick_euler
);
3175 v3_zero( s
->state
.cog_v
);
3176 s
->state
.grind_cooldown
= 0;
3177 s
->state
.surface_cooldown
= 0;
3178 v3_muladds( player
->rb
.co
, player
->rb
.to_world
[1], 1.0f
, s
->state
.cog
);
3179 v3_copy( player
->rb
.to_world
[1], s
->state
.up_dir
);
3180 v3_copy( player
->rb
.to_world
[1], s
->surface_picture
);
3181 v3_zero( s
->weight_distribution
);
3182 v3_copy( player
->rb
.co
, s
->state
.prev_pos
);
3185 VG_STATIC
void player__skate_reset( player_instance
*player
,
3188 struct player_skate
*s
= &player
->_skate
;
3189 v3_zero( player
->rb
.v
);
3190 v4_copy( rp
->transform
.q
, player
->rb
.q
);
3192 s
->state
.activity
= k_skate_activity_air
;
3193 s
->state
.activity_prev
= k_skate_activity_air
;
3195 player__skate_clear_mechanics( player
);
3196 player__skate_reset_animator( player
);
3198 v3_zero( s
->state
.head_position
);
3199 s
->state
.head_position
[1] = 1.8f
;
3202 #endif /* PLAYER_SKATE_C */