clear runs when respawning
[carveJwlIkooP6JGAAIwe30JlM.git] / player_skate.c
1 #ifndef PLAYER_SKATE_C
2 #define PLAYER_SKATE_C
3
4 #include "player.h"
5 #include "audio.h"
6 #include "vg/vg_perlin.h"
7 #include "menu.h"
8 #include "ent_skateshop.h"
9 #include "addon.h"
10
11 VG_STATIC void player__skate_bind( player_instance *player )
12 {
13 struct player_skate *s = &player->_skate;
14 struct player_avatar *av = player->playeravatar;
15 struct skeleton *sk = &av->sk;
16
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" );
29 }
30
31 VG_STATIC void player__skate_kill_audio( player_instance *player )
32 {
33 struct player_skate *s = &player->_skate;
34
35 audio_lock();
36 if( s->aud_main )
37 s->aud_main = audio_channel_fadeout( s->aud_main, 0.1f );
38 if( s->aud_air )
39 s->aud_air = audio_channel_fadeout( s->aud_air, 0.1f );
40 if( s->aud_slide )
41 s->aud_slide = audio_channel_fadeout( s->aud_slide, 0.1f );
42 audio_unlock();
43 }
44
45 /*
46 * Collision detection routines
47 *
48 *
49 */
50
51 /*
52 * Does collision detection on a sphere vs world, and applies some smoothing
53 * filters to the manifold afterwards
54 */
55 VG_STATIC int skate_collide_smooth( player_instance *player,
56 m4x3f mtx, rb_sphere *sphere,
57 rb_ct *man )
58 {
59 world_instance *world = world_current_instance();
60
61 int len = 0;
62 len = rb_sphere__scene( mtx, sphere, NULL, &world->rb_geo.inf.scene, man,
63 k_material_flag_walking );
64
65 for( int i=0; i<len; i++ ){
66 man[i].rba = &player->rb;
67 man[i].rbb = NULL;
68 }
69
70 rb_manifold_filter_coplanar( man, len, 0.03f );
71
72 if( len > 1 ){
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 );
76 }
77 int new_len = rb_manifold_apply_filtered( man, len );
78 if( len && !new_len )
79 len = 1;
80 else
81 len = new_len;
82
83 return len;
84 }
85
86 struct grind_info
87 {
88 v3f co, dir, n;
89 };
90
91 VG_STATIC int skate_grind_scansq( player_instance *player,
92 v3f pos, v3f dir, float r,
93 struct grind_info *inf )
94 {
95 world_instance *world = world_current_instance();
96
97 v4f plane;
98 v3_copy( dir, plane );
99 v3_normalize( plane );
100 plane[3] = v3_dot( plane, pos );
101
102 boxf box;
103 v3_add( pos, (v3f){ r, r, r }, box[1] );
104 v3_sub( pos, (v3f){ r, r, r }, box[0] );
105
106 struct grind_sample{
107 v2f co;
108 v2f normal;
109 v3f normal3,
110 centroid;
111 }
112 samples[48];
113 int sample_count = 0;
114
115 v2f support_min,
116 support_max;
117
118 v3f support_axis;
119 v3_cross( plane, player->basis[1], support_axis );
120 v3_normalize( support_axis );
121
122 bh_iter it;
123 bh_iter_init_box( 0, &it, box );
124 i32 idx;
125
126 while( bh_next( world->geo_bh, &it, &idx ) ){
127 u32 *ptri = &world->scene_geo.arrindices[ idx*3 ];
128 v3f tri[3];
129
130 struct world_surface *surf = world_tri_index_surface(world,ptri[0]);
131 if( !(surf->info.flags & k_material_flag_grindable) )
132 continue;
133
134 for( int j=0; j<3; j++ )
135 v3_copy( world->scene_geo.arrvertices[ptri[j]].co, tri[j] );
136
137 for( int j=0; j<3; j++ ){
138 int i0 = j,
139 i1 = (j+1) % 3;
140
141 struct grind_sample *sample = &samples[ sample_count ];
142 v3f co;
143
144 if( plane_segment( plane, tri[i0], tri[i1], co ) ){
145 v3f d;
146 v3_sub( co, pos, d );
147 if( v3_length2( d ) > r*r )
148 continue;
149
150 v3f va, vb, normal;
151 v3_sub( tri[1], tri[0], va );
152 v3_sub( tri[2], tri[0], vb );
153 v3_cross( va, vb, normal );
154
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 );
159
160 v3_copy( normal, sample->normal3 ); /* normalize later
161 if we want to us it */
162
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 );
166
167 v2_normalize( sample->normal );
168 sample_count ++;
169
170 if( sample_count == vg_list_size( samples ) )
171 goto too_many_samples;
172 }
173 }
174 }
175
176 too_many_samples:
177
178 if( sample_count < 2 )
179 return 0;
180
181 v3f
182 average_direction,
183 average_normal;
184
185 v2f min_co, max_co;
186 v2_fill( min_co, INFINITY );
187 v2_fill( max_co, -INFINITY );
188
189 v3_zero( average_direction );
190 v3_zero( average_normal );
191
192 int passed_samples = 0;
193
194 for( int i=0; i<sample_count-1; i++ ){
195 struct grind_sample *si, *sj;
196
197 si = &samples[i];
198
199 for( int j=i+1; j<sample_count; j++ ){
200 if( i == j )
201 continue;
202
203 sj = &samples[j];
204
205 /* non overlapping */
206 if( v2_dist2( si->co, sj->co ) >= (0.01f*0.01f) )
207 continue;
208
209 /* not sharp angle */
210 if( v2_dot( si->normal, sj->normal ) >= 0.7f )
211 continue;
212
213 /* not convex */
214 v3f v0;
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 )
218 continue;
219
220 v2_minv( sj->co, min_co, min_co );
221 v2_maxv( sj->co, max_co, max_co );
222
223 v3f n0, n1, dir;
224 v3_copy( si->normal3, n0 );
225 v3_copy( sj->normal3, n1 );
226 v3_cross( n0, n1, dir );
227 v3_normalize( dir );
228
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 );
232
233 float yi = v3_dot( player->basis[1], si->normal3 ),
234 yj = v3_dot( player->basis[1], sj->normal3 );
235
236 if( yi > yj ) v3_add( si->normal3, average_normal, average_normal );
237 else v3_add( sj->normal3, average_normal, average_normal );
238
239 passed_samples ++;
240 }
241 }
242
243 if( !passed_samples )
244 return 0;
245
246 if( (v3_length2( average_direction ) <= 0.001f) ||
247 (v3_length2( average_normal ) <= 0.001f ) )
248 return 0;
249
250 float div = 1.0f/(float)passed_samples;
251 v3_normalize( average_direction );
252 v3_normalize( average_normal );
253
254 v2f average_coord;
255 v2_add( min_co, max_co, average_coord );
256 v2_muls( average_coord, 0.5f, average_coord );
257
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 );
263
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 );
267
268 return passed_samples;
269 }
270
271 VG_STATIC void reset_jump_info( jump_info *inf )
272 {
273 inf->log_length = 0;
274 inf->land_dist = 0.0f;
275 inf->score = 0.0f;
276 inf->type = k_prediction_unset;
277 v3_zero( inf->apex );
278 }
279
280 VG_STATIC int create_jumps_to_hit_target( player_instance *player,
281 jump_info *jumps,
282 v3f target, float max_angle_delta,
283 float gravity )
284 {
285 struct player_skate *s = &player->_skate;
286
287 /* calculate the exact 2 solutions to jump onto that grind spot */
288
289 v3f v0;
290 v3_sub( target, player->rb.co, v0 );
291 m3x3_mulv( player->invbasis, v0, v0 );
292
293 v3f ax;
294 v3_copy( v0, ax );
295 ax[1] = 0.0f;
296 v3_normalize( ax );
297
298 v3f v_local;
299 m3x3_mulv( player->invbasis, player->rb.v, v_local );
300
301 v2f d = { v3_dot( ax, v0 ), v0[1] },
302 v = { v3_dot( ax, v_local ), v_local[1] };
303
304 float a = atan2f( v[1], v[0] ),
305 m = v2_length( v ),
306 root = m*m*m*m - gravity*(gravity*d[0]*d[0] + 2.0f*d[1]*m*m);
307
308 int valid_count = 0;
309
310 if( root > 0.0f ){
311 root = sqrtf( root );
312 float a0 = atanf( (m*m + root) / (gravity * d[0]) ),
313 a1 = atanf( (m*m - root) / (gravity * d[0]) );
314
315 if( fabsf(a0-a) < max_angle_delta ){
316 jump_info *inf = &jumps[ valid_count ++ ];
317 reset_jump_info( inf );
318
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;
324
325 v3_copy( target, inf->log[inf->log_length ++] );
326 }
327
328 if( fabsf(a1-a) < max_angle_delta ){
329 jump_info *inf = &jumps[ valid_count ++ ];
330 reset_jump_info( inf );
331
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;
337
338 v3_copy( target, inf->log[inf->log_length ++] );
339 }
340 }
341
342 return valid_count;
343 }
344
345 VG_STATIC
346 void player__approximate_best_trajectory( player_instance *player )
347 {
348 world_instance *world0 = world_current_instance();
349
350 struct player_skate *s = &player->_skate;
351 float k_trace_delta = k_rb_delta * 10.0f;
352
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 );
356
357 s->possible_jump_count = 0;
358
359 v3f axis;
360 v3_cross( player->rb.v, player->rb.to_world[1], axis );
361 v3_normalize( axis );
362
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 )),
366 angle_end = 1.0f;
367
368 struct grind_info grind;
369 int grind_located = 0;
370 float grind_located_gravity = k_gravity;
371
372
373 v3f launch_v_bounds[2];
374
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 ];
378 ang *= 0.15f;
379
380 v4f qbias;
381 q_axis_angle( qbias, axis, ang );
382 q_mulv( qbias, launch_v_bounds[i], launch_v_bounds[i] );
383 }
384
385 for( int m=0;m<=30; m++ ){
386 jump_info *inf = &s->possible_jumps[ s->possible_jump_count ++ ];
387 reset_jump_info( inf );
388
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;
394
395 float vt = (float)m * (1.0f/30.0f),
396 ang = vg_lerpf( angle_begin, angle_end, vt ) * 0.15f;
397
398 v4f qbias;
399 q_axis_angle( qbias, axis, ang );
400 q_mulv( qbias, launch_v, launch_v );
401
402 float yaw_sketch = 1.0f-fabsf(upness);
403
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 );
407
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 );
412
413 m3x3f basis;
414 m3x3_copy( player->basis, basis );
415
416 for( int i=1; i<=50; i++ ){
417 float t = (float)i * k_trace_delta;
418
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 );
422
423 float launch_vy = v3_dot( launch_v,basis[1] );
424
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;
428
429 /* REFACTOR */
430
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;
436
437 if( v3_dist2( closest, launch_co ) < min_dist )
438 search_for_grind = 0;
439
440 v3f bound[2];
441
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] );
446 }
447
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;
451
452 if( (closest[1] < minh) || (closest[1] > maxh) ){
453 search_for_grind = 0;
454 }
455 }
456 else
457 search_for_grind = 0;
458 }
459
460 if( search_for_grind ){
461 v3f ve;
462 v3_copy( launch_v, ve );
463 v3_muladds( ve, basis[1], -gravity * t, ve );
464
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] ) };
471
472 v2_normalize( v0 );
473 v2_normalize( v1 );
474
475 float a = v2_dot( v0, v1 );
476
477 float a_min = cosf( VG_PIf * 0.185f );
478 if( s->state.grind_cooldown )
479 a_min = cosf( VG_PIf * 0.05f );
480
481 /* check speed */
482 if( (fabsf(v3_dot( ve, grind.dir ))>=k_grind_axel_min_vel) &&
483 (a >= a_min) &&
484 (fabsf(grind.dir[1]) < 0.70710678118654752f))
485 {
486 grind_located = 1;
487 grind_located_gravity = inf->gravity;
488 }
489 }
490 }
491
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 );
500
501 if( gate->flags & k_ent_gate_nonlocal ){
502 trace_world = &world_static.instances[ gate->target ];
503 }
504 }
505 }
506
507 float t1;
508 v3f n;
509
510 float scan_radius = k_board_radius;
511 scan_radius *= vg_clampf( t, 0.02f, 1.0f );
512
513 int idx = spherecast_world( trace_world, co0, co1, scan_radius, &t1, n,
514 k_material_flag_walking );
515 if( idx != -1 ){
516 v3f co;
517 v3_lerp( co0, co1, t1, co );
518 v3_copy( co, inf->log[ inf->log_length ++ ] );
519
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] );
524
525 inf->type = k_prediction_land;
526
527 v3f ve;
528 v3_copy( launch_v, ve );
529 v3_muladds( ve, player->basis[1], -gravity * t, ve );
530
531 inf->score = -v3_dot( ve, inf->n );
532 inf->land_dist = t + k_trace_delta * t1;
533
534 /* Bias prediction towords ramps */
535 if( !(surf->info.flags & k_material_flag_skate_target) )
536 inf->score *= 10.0f;
537
538 if( surf->info.flags & k_material_flag_boundary )
539 s->possible_jump_count --;
540
541 break;
542 }
543
544 if( i % 3 == 0 )
545 v3_copy( co1, inf->log[ inf->log_length ++ ] );
546
547 v3_copy( co1, co0 );
548 }
549
550 if( inf->type == k_prediction_unset )
551 s->possible_jump_count --;
552 }
553
554 if( grind_located ){
555 jump_info grind_jumps[2];
556
557 int valid_count =
558 create_jumps_to_hit_target( player, grind_jumps, grind.co,
559 0.175f*VG_PIf, grind_located_gravity );
560
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;
567 }
568
569 for( int i=0; i<valid_count; i++ ){
570 jump_info *jump = &grind_jumps[i];
571 jump->type = k_prediction_grind;
572
573 v3f launch_v, launch_co, co0, co1;
574
575 v3_copy( jump->v, launch_v );
576 v3_copy( player->rb.co, launch_co );
577
578 m3x3f basis;
579 m3x3_copy( player->basis, basis );
580
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 );
585
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);
589 t *= 0.9f;
590 t += 0.05f;
591 t *= jump->land_dist;
592
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 );
596
597 float t1;
598 v3f n;
599
600 int idx = spherecast_world( world0, co0,co1,
601 k_board_radius*0.1f, &t1, n,
602 k_material_flag_walking );
603 if( idx != -1 ){
604 goto invalidated_grind;
605 }
606
607 v3_copy( co1, co0 );
608 }
609
610 v3_copy( grind.n, jump->n );
611
612 /* determine score */
613 v3f ve;
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;
617
618 s->possible_jumps[ s->possible_jump_count ++ ] = *jump;
619
620 continue;
621 invalidated_grind:;
622 }
623 }
624
625
626 float score_min = INFINITY,
627 score_max = -INFINITY;
628
629 jump_info *best = NULL;
630
631 for( int i=0; i<s->possible_jump_count; i ++ ){
632 jump_info *jump = &s->possible_jumps[i];
633
634 if( jump->score < score_min )
635 best = jump;
636
637 score_min = vg_minf( score_min, jump->score );
638 score_max = vg_maxf( score_max, jump->score );
639 }
640
641 for( int i=0; i<s->possible_jump_count; i ++ ){
642 jump_info *jump = &s->possible_jumps[i];
643 float s = jump->score;
644
645 s -= score_min;
646 s /= (score_max-score_min);
647 s = 1.0f - s;
648
649 jump->score = s;
650 jump->colour = s * 255.0f;
651
652 if( jump == best )
653 jump->colour <<= 16;
654 else if( jump->type == k_prediction_land )
655 jump->colour <<= 8;
656
657 jump->colour |= 0xff000000;
658 }
659
660 if( best ){
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;
664
665 s->state.gravity_bias = best->gravity;
666
667 if( best->type == k_prediction_grind ){
668 s->state.activity = k_skate_activity_air_to_grind;
669 }
670
671 v2f steer;
672 joystick_state( k_srjoystick_steer, steer );
673 v2_normalize_clamp( steer );
674
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]) *
677 s->state.reverse ;
678 s->state.flip_time = 0.0f;
679 v3_copy( player->rb.to_world[0], s->state.flip_axis );
680 }
681 else{
682 s->state.flip_rate = 0.0f;
683 v3_zero( s->state.flip_axis );
684 }
685 }
686 else{
687 v3_copy( player->basis[1], s->state.land_normal );
688 }
689 }
690
691 /*
692 *
693 * Varius physics models
694 * ------------------------------------------------
695 */
696
697 /*
698 * Air control, no real physics
699 */
700 VG_STATIC void skate_apply_air_model( player_instance *player )
701 {
702 struct player_skate *s = &player->_skate;
703
704 if( s->state.activity_prev > k_skate_activity_air_to_grind )
705 player__approximate_best_trajectory( player );
706
707 float angle = v3_dot( player->rb.to_world[1], s->state.land_normal );
708 angle = vg_clampf( angle, -1.0f, 1.0f );
709 v3f axis;
710 v3_cross( player->rb.to_world[1], s->state.land_normal, axis );
711
712 v4f correction;
713 q_axis_angle( correction, axis,
714 acosf(angle)*2.0f*VG_TIMESTEP_FIXED );
715 q_mul( correction, player->rb.q, player->rb.q );
716 }
717
718 VG_STATIC enum trick_type player_skate_trick_input( player_instance *player );
719 VG_STATIC void skate_apply_trick_model( player_instance *player )
720 {
721 struct player_skate *s = &player->_skate;
722
723 v3f Fd, Fs, F;
724 v3f strength = { 3.7f, 3.6f, 8.0f };
725
726 v3_muls( s->state.trick_residualv, -4.0f , Fd );
727 v3_muls( s->state.trick_residuald, -10.0f, Fs );
728 v3_add( Fd, Fs, F );
729 v3_mul( strength, F, F );
730
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 );
735
736 if( s->state.activity <= k_skate_activity_air_to_grind ){
737 if( v3_length2( s->state.trick_vel ) < 0.0001f )
738 return;
739
740 int carry_on = player_skate_trick_input( player );
741
742 /* we assume velocities share a common divisor, in which case the
743 * interval is the minimum value (if not zero) */
744
745 float min_rate = 99999.0f;
746
747 for( int i=0; i<3; i++ ){
748 float v = s->state.trick_vel[i];
749 if( (v > 0.0f) && (v < min_rate) )
750 min_rate = v;
751 }
752
753 float interval = 1.0f / min_rate,
754 current = floorf( s->state.trick_time / interval ),
755 next_end = (current+1.0f) * interval;
756
757
758 /* integrate trick velocities */
759 v3_muladds( s->state.trick_euler, s->state.trick_vel, k_rb_delta,
760 s->state.trick_euler );
761
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 );
769 }
770
771 s->state.trick_time += k_rb_delta;
772 }
773 else{
774 if( (v3_length2(s->state.trick_vel) >= 0.0001f ) &&
775 s->state.trick_time > 0.2f)
776 {
777 player__skate_kill_audio( player );
778 player__dead_transition( player );
779 }
780
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 );
786 }
787 }
788
789 VG_STATIC void skate_apply_grab_model( player_instance *player )
790 {
791 struct player_skate *s = &player->_skate;
792
793 float grabt = axis_state( k_sraxis_grab );
794
795 if( grabt > 0.5f ){
796 v2_muladds( s->state.grab_mouse_delta, vg.mouse_delta, 0.02f,
797 s->state.grab_mouse_delta );
798
799 v2_normalize_clamp( s->state.grab_mouse_delta );
800 }
801 else
802 v2_zero( s->state.grab_mouse_delta );
803
804 s->state.grabbing = vg_lerpf( s->state.grabbing, grabt, 8.4f*k_rb_delta );
805 }
806
807 VG_STATIC void skate_apply_steering_model( player_instance *player )
808 {
809 struct player_skate *s = &player->_skate;
810
811 v2f jsteer;
812 joystick_state( k_srjoystick_steer, jsteer );
813
814 /* Steering */
815 float steer = jsteer[0],
816 grab = axis_state( k_sraxis_grab );
817
818 steer = vg_signf( steer ) * steer*steer * k_steer_ground;
819
820 v3f steer_axis;
821 v3_muls( player->rb.to_world[1], -vg_signf( steer ), steer_axis );
822
823 float rate = 26.0f,
824 top = 1.0f;
825
826 if( s->state.activity <= k_skate_activity_air_to_grind ){
827 rate = 6.0f * fabsf(steer);
828 top = 1.5f;
829 }
830 else{
831 /* rotate slower when grabbing on ground */
832 steer *= (1.0f-(s->state.jump_charge+grab)*0.4f);
833
834 if( s->state.activity == k_skate_activity_grind_5050 ){
835 rate = 0.0f;
836 top = 0.0f;
837 }
838
839 else if( s->state.activity >= k_skate_activity_grind_any ){
840 rate *= fabsf(steer);
841
842 float a = 0.8f * -steer * k_rb_delta;
843
844 v4f q;
845 q_axis_angle( q, player->rb.to_world[1], a );
846 q_mulv( q, s->grind_vec, s->grind_vec );
847
848 v3_normalize( s->grind_vec );
849 }
850
851 else if( s->state.manual_direction ){
852 rate = 35.0f;
853 top = 1.5f;
854 }
855
856 if( grab < 0.5f ){
857 top *= 1.0f+v3_length( s->state.throw_v )*k_mmthrow_steer;
858 }
859 }
860
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 );
865
866 v3_muladds( player->rb.w, player->rb.to_world[1], accel, player->rb.w );
867 }
868
869 /*
870 * Computes friction and surface interface model
871 */
872 VG_STATIC void skate_apply_friction_model( player_instance *player )
873 {
874 struct player_skate *s = &player->_skate;
875
876 /*
877 * Computing localized friction forces for controlling the character
878 * Friction across X is significantly more than Z
879 */
880
881 v3f vel;
882 m3x3_mulv( player->rb.to_local, player->rb.v, vel );
883 float slip = 0.0f;
884
885 if( fabsf(vel[2]) > 0.01f )
886 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
887
888 if( fabsf( slip ) > 1.2f )
889 slip = vg_signf( slip ) * 1.2f;
890
891 s->state.slip = slip;
892 s->state.reverse = -vg_signf(vel[2]);
893
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 );
896
897 /* Pushing additive force */
898
899 if( !button_press( k_srbind_jump ) ){
900 if( button_press( k_srbind_push ) || (vg.time-s->state.start_push<0.75) )
901 {
902 if( (vg.time - s->state.cur_push) > 0.25 )
903 s->state.start_push = vg.time;
904
905 s->state.cur_push = vg.time;
906
907 double push_time = vg.time - s->state.start_push;
908
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 );
915
916 vel[2] += delta * -s->state.reverse;
917 }
918 }
919
920 /* Send back to velocity */
921 m3x3_mulv( player->rb.to_world, vel, player->rb.v );
922 }
923
924 VG_STATIC void skate_apply_jump_model( player_instance *player )
925 {
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 );
929
930 /* Cannot charge this in air */
931 if( s->state.activity <= k_skate_activity_air_to_grind ){
932 s->state.charging_jump = 0;
933 return;
934 }
935
936 if( s->state.charging_jump ){
937 s->state.jump_charge += k_rb_delta * k_jump_charge_speed;
938
939 if( !charging_jump_prev )
940 s->state.jump_dir = s->state.reverse>0.0f? 1: 0;
941 }
942 else{
943 s->state.jump_charge -= k_jump_charge_speed * k_rb_delta;
944 }
945
946 s->state.jump_charge = vg_clampf( s->state.jump_charge, 0.0f, 1.0f );
947
948 /* player let go after charging past 0.2: trigger jump */
949 if( (!s->state.charging_jump) && (s->state.jump_charge > 0.2f) ){
950 v3f jumpdir;
951
952 /* Launch more up if alignment is up else improve velocity */
953 float aup = v3_dot( player->basis[1], player->rb.to_world[1] ),
954 mod = 0.5f,
955 dir = mod + fabsf(aup)*(1.0f-mod);
956
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 );
963 }else{
964 v3_copy( s->state.up_dir, jumpdir );
965 s->state.grind_cooldown = 30;
966 s->state.activity = k_skate_activity_ground;
967
968 v2f steer;
969 joystick_state( k_srjoystick_steer, steer );
970
971 float tilt = steer[0] * 0.3f;
972 tilt *= vg_signf(v3_dot( player->rb.v, s->grind_dir ));
973
974 v4f qtilt;
975 q_axis_angle( qtilt, s->grind_dir, tilt );
976 q_mulv( qtilt, jumpdir, jumpdir );
977 }
978 s->state.surface_cooldown = 10;
979
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;
984
985 audio_lock();
986 audio_oneshot_3d( &audio_jumps[vg_randu32()%2], player->rb.co,40.0f,1.0f);
987 audio_unlock();
988 }
989 }
990
991 VG_STATIC void skate_apply_pump_model( player_instance *player )
992 {
993 struct player_skate *s = &player->_skate;
994
995 if( s->state.activity != k_skate_activity_ground ){
996 v3_zero( s->state.throw_v );
997 return;
998 }
999
1000 /* Throw / collect routine
1001 */
1002 if( axis_state( k_sraxis_grab ) > 0.5f ){
1003 if( s->state.activity == k_skate_activity_ground ){
1004 /* Throw */
1005 v3_muls( player->rb.to_world[1], k_mmthrow_scale, s->state.throw_v );
1006 }
1007 }
1008 else{
1009 /* Collect */
1010 float doty = v3_dot( player->rb.to_world[1], s->state.throw_v );
1011
1012 v3f Fl, Fv;
1013 v3_muladds( s->state.throw_v, player->rb.to_world[1], -doty, Fl);
1014
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 );
1019 }
1020
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 );
1024 }
1025
1026 /* Decay */
1027 if( v3_length2( s->state.throw_v ) > 0.0001f ){
1028 v3f dir;
1029 v3_copy( s->state.throw_v, dir );
1030 v3_normalize( dir );
1031
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 );
1035 }
1036 }
1037
1038 VG_STATIC void skate_apply_cog_model( player_instance *player )
1039 {
1040 struct player_skate *s = &player->_skate;
1041
1042 v3f ideal_cog, ideal_diff, ideal_dir;
1043 v3_copy( s->state.up_dir, ideal_dir );
1044 v3_normalize( ideal_dir );
1045
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 );
1049
1050 /* Apply velocities */
1051 v3f rv;
1052 v3_sub( player->rb.v, s->state.cog_v, rv );
1053
1054 v3f F;
1055 v3_muls( ideal_diff, -k_cog_spring * k_rb_rate, F );
1056 v3_muladds( F, rv, -k_cog_damp * k_rb_rate, F );
1057
1058 float ra = k_cog_mass_ratio,
1059 rb = 1.0f-k_cog_mass_ratio;
1060
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,
1064 s->state.cog_v );
1065
1066 v3_muladds( s->state.cog, s->state.cog_v, k_rb_delta, s->state.cog );
1067 }
1068
1069
1070 VG_STATIC void skate_integrate( player_instance *player )
1071 {
1072 struct player_skate *s = &player->_skate;
1073
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;
1077
1078 if( s->state.activity >= k_skate_activity_grind_any ){
1079 #if 0
1080 decay_rate = 1.0f-vg_lerpf( 3.0f, 20.0f, s->grind_strength ) * k_rb_delta;
1081 decay_rate_y = decay_rate;
1082 #endif
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);
1086 }
1087
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;
1091
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 );
1095
1096 s->state.flip_time += s->state.flip_rate * k_rb_delta;
1097 rb_update_transform( &player->rb );
1098 }
1099
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 ) );
1105 }
1106
1107 VG_STATIC void player__skate_pre_update( player_instance *player ){
1108 struct player_skate *s = &player->_skate;
1109
1110 if( button_down( k_srbind_use ) ){
1111 player->subsystem = k_player_subsystem_walk;
1112
1113 v3f angles;
1114 v3_copy( player->cam.angles, player->angles );
1115 player->angles[2] = 0.0f;
1116
1117 player__begin_holdout( player );
1118 player__skate_kill_audio( player );
1119 player__walk_transition( player );
1120 return;
1121 }
1122
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;
1129
1130 if( trick == k_trick_type_kickflip ){
1131 s->state.trick_vel[0] = 3.0f;
1132 }
1133 else if( trick == k_trick_type_shuvit ){
1134 s->state.trick_vel[2] = 3.0f;
1135 }
1136 else if( trick == k_trick_type_treflip ){
1137 s->state.trick_vel[0] = 2.0f;
1138 s->state.trick_vel[2] = 2.0f;
1139 }
1140 s->state.trick_type = trick;
1141 }
1142 }
1143 }
1144 else
1145 s->state.trick_type = k_trick_type_none;
1146 }
1147
1148 VG_STATIC void player__skate_post_update( player_instance *player ){
1149 struct player_skate *s = &player->_skate;
1150
1151 for( int i=0; i<s->possible_jump_count; i++ ){
1152 jump_info *jump = &s->possible_jumps[i];
1153
1154 if( jump->log_length == 0 ){
1155 vg_fatal_error( "assert: jump->log_length == 0\n" );
1156 }
1157
1158 for( int j=0; j<jump->log_length - 1; j ++ ){
1159 float brightness = jump->score*jump->score*jump->score;
1160 v3f p1;
1161 v3_lerp( jump->log[j], jump->log[j+1], brightness, p1 );
1162 vg_line( jump->log[j], p1, jump->colour );
1163 }
1164
1165 vg_line_cross( jump->log[jump->log_length-1], jump->colour, 0.25f );
1166
1167 v3f p1;
1168 v3_add( jump->log[jump->log_length-1], jump->n, p1 );
1169 vg_line( jump->log[jump->log_length-1], p1, 0xffffffff );
1170
1171 vg_line_point( jump->apex, 0.02f, 0xffffffff );
1172 }
1173
1174 audio_lock();
1175
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 );
1180
1181 if( s->state.activity >= k_skate_activity_grind_any ){
1182 slide = 0.0f;
1183 }
1184
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;
1189
1190 const u32 flags = AUDIO_FLAG_SPACIAL_3D|AUDIO_FLAG_LOOP;
1191
1192 if( !s->aud_air ){
1193 s->aud_air = audio_get_first_idle_channel();
1194 if( s->aud_air )
1195 audio_channel_init( s->aud_air, &audio_board[1], flags );
1196 }
1197
1198 if( !s->aud_slide ){
1199 s->aud_slide = audio_get_first_idle_channel();
1200 if( s->aud_slide )
1201 audio_channel_init( s->aud_slide, &audio_board[2], flags );
1202 }
1203
1204
1205 /* brrrrrrrrrrrt sound for tiles and stuff
1206 * --------------------------------------------------------*/
1207 float sidechain_amt = 0.0f,
1208 hz = vg_maxf( speed * 2.0f, 2.0f );
1209
1210 if( (s->surface == k_surface_prop_tiles) &&
1211 (s->state.activity < k_skate_activity_grind_any) )
1212 sidechain_amt = 1.0f;
1213 else
1214 sidechain_amt = 0.0f;
1215
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 ) );
1219
1220 if( s->sample_change_cooldown > 0.0f ){
1221 s->sample_change_cooldown -= vg.time_frame_delta;
1222 }
1223 else{
1224 int sample_type = k_skate_sample_concrete;
1225
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;
1229 else
1230 sample_type = k_skate_sample_concrete_scrape_metal;
1231 }
1232 else if( (s->state.activity == k_skate_activity_grind_back50) ||
1233 (s->state.activity == k_skate_activity_grind_front50) )
1234 {
1235 if( s->surface == k_surface_prop_metal ){
1236 sample_type = k_skate_sample_metal_scrape_generic;
1237 }
1238 else{
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;
1242 else
1243 sample_type = k_skate_sample_concrete_scrape_metal;
1244 }
1245 }
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;
1249 else
1250 sample_type = k_skate_sample_concrete_scrape_wood;
1251 }
1252
1253 audio_clip *relevant_samples[] = {
1254 &audio_board[0],
1255 &audio_board[0],
1256 &audio_board[7],
1257 &audio_board[6],
1258 &audio_board[5]
1259 };
1260
1261 if( (s->main_sample_type != sample_type) || (!s->aud_main) ){
1262 s->aud_main =
1263 audio_channel_crossfade( s->aud_main, relevant_samples[sample_type],
1264 0.06f, flags );
1265 s->sample_change_cooldown = 0.1f;
1266 s->main_sample_type = sample_type;
1267 }
1268 }
1269
1270 if( s->aud_main ){
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 );
1276
1277 float rate = 1.0f + (attn-0.5f)*0.2f;
1278 audio_channel_set_sampling_rate( s->aud_main, rate );
1279 }
1280
1281 if( s->aud_slide ){
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 );
1287 }
1288
1289 if( s->aud_air ){
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 );
1294 }
1295
1296 audio_unlock();
1297 }
1298
1299 /*
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
1304 *
1305 * returns 0 if no good surface found
1306 */
1307 VG_STATIC
1308 int skate_compute_surface_alignment( player_instance *player,
1309 v3f ra, u32 colour,
1310 v3f surface_normal, v3f axel_dir ){
1311 struct player_skate *s = &player->_skate;
1312 world_instance *world = world_current_instance();
1313
1314 v3f truck, left, right;
1315 m4x3_mulv( player->rb.to_world, ra, truck );
1316
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 );
1320
1321 float k_max_truck_flex = VG_PIf * 0.25f;
1322
1323 ray_hit ray_l, ray_r;
1324
1325 v3f dir;
1326 v3_muls( player->rb.to_world[1], -1.0f, dir );
1327
1328 int res_l = 0, res_r = 0;
1329
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;
1335
1336 res_l = ray_world( world, left, dir, &ray_l, k_material_flag_walking );
1337
1338 if( res_l )
1339 break;
1340 }
1341
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;
1347
1348 res_r = ray_world( world, right, dir, &ray_r, k_material_flag_walking );
1349
1350 if( res_r )
1351 break;
1352 }
1353
1354 v3f v0;
1355 v3f midpoint;
1356 v3f tangent_average;
1357 v3_muladds( truck, player->rb.to_world[1], -k_board_radius, midpoint );
1358 v3_zero( tangent_average );
1359
1360 if( res_l || res_r ){
1361 v3f p0, p1, t;
1362 v3_copy( midpoint, p0 );
1363 v3_copy( midpoint, p1 );
1364
1365 if( res_l ){
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 );
1369 }
1370 if( res_r ){
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 );
1374 }
1375
1376 v3_sub( p1, p0, v0 );
1377 v3_normalize( v0 );
1378 }
1379 else{
1380 /* fallback: use the closes point to the trucks */
1381 v3f closest;
1382 int idx = bh_closest_point( world->geo_bh, midpoint, closest, 0.1f );
1383
1384 if( idx != -1 ){
1385 u32 *tri = &world->scene_geo.arrindices[ idx * 3 ];
1386 v3f verts[3];
1387
1388 for( int j=0; j<3; j++ )
1389 v3_copy( world->scene_geo.arrvertices[ tri[j] ].co, verts[j] );
1390
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 );
1395 v3_normalize( n );
1396
1397 if( v3_dot( n, player->rb.to_world[1] ) < 0.3f )
1398 return 0;
1399
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 );
1403 v3_normalize( v0 );
1404
1405 v3f t;
1406 v3_cross( n, player->rb.to_world[0], t );
1407 v3_add( t, tangent_average, tangent_average );
1408 }
1409 else
1410 return 0;
1411 }
1412
1413 v3_muladds( truck, v0, k_board_width, right );
1414 v3_muladds( truck, v0, -k_board_width, left );
1415
1416 vg_line( left, right, VG__WHITE );
1417
1418 v3_normalize( tangent_average );
1419 v3_cross( v0, tangent_average, surface_normal );
1420 v3_copy( v0, axel_dir );
1421
1422 return 1;
1423 }
1424
1425 VG_STATIC void skate_weight_distribute( player_instance *player ){
1426 struct player_skate *s = &player->_skate;
1427 v3_zero( s->weight_distribution );
1428
1429 int reverse_dir = v3_dot( player->rb.to_world[2], player->rb.v ) < 0.0f?1:-1;
1430
1431 v2f steer;
1432 joystick_state( k_srjoystick_steer, steer );
1433
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;
1438 }
1439 else{
1440 if( steer[1] < 0.1f ){
1441 s->state.manual_direction = 0;
1442 }
1443 else{
1444 if( reverse_dir != s->state.manual_direction ){
1445 return;
1446 }
1447 }
1448 }
1449
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;
1454 }
1455
1456 if( s->state.manual_direction ){
1457 v3f plane_z;
1458
1459 m3x3_mulv( player->rb.to_world, s->weight_distribution, plane_z );
1460 v3_negate( plane_z, plane_z );
1461
1462 v3_muladds( plane_z, s->surface_picture,
1463 -v3_dot( plane_z, s->surface_picture ), plane_z );
1464 v3_normalize( plane_z );
1465
1466 v3_muladds( plane_z, s->surface_picture, 0.3f, plane_z );
1467 v3_normalize( plane_z );
1468
1469 v3f p1;
1470 v3_muladds( player->rb.co, plane_z, 1.5f, p1 );
1471 vg_line( player->rb.co, p1, VG__GREEN );
1472
1473 v3f refdir;
1474 v3_muls( player->rb.to_world[2], -(float)s->state.manual_direction,
1475 refdir );
1476
1477 rb_effect_spring_target_vector( &player->rb, refdir, plane_z,
1478 k_manul_spring, k_manul_dampener,
1479 s->substep_delta );
1480 }
1481 }
1482
1483 VG_STATIC void skate_adjust_up_direction( player_instance *player ){
1484 struct player_skate *s = &player->_skate;
1485
1486 if( s->state.activity == k_skate_activity_ground ){
1487 v3f target;
1488 v3_copy( s->surface_picture, target );
1489
1490 target[1] += 2.0f * s->surface_picture[1];
1491 v3_normalize( target );
1492
1493 v3_lerp( s->state.up_dir, target,
1494 8.0f * s->substep_delta, s->state.up_dir );
1495 }
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 );
1499 }
1500 else{
1501 v3_lerp( s->state.up_dir, player->basis[1],
1502 12.0f * s->substep_delta, s->state.up_dir );
1503 }
1504 }
1505
1506 VG_STATIC int skate_point_visible( v3f origin, v3f target ){
1507 v3f dir;
1508 v3_sub( target, origin, dir );
1509
1510 ray_hit ray;
1511 ray.dist = v3_length( dir );
1512 v3_muls( dir, 1.0f/ray.dist, dir );
1513 ray.dist -= 0.025f;
1514
1515 if( ray_world( world_current_instance(), origin, dir, &ray,
1516 k_material_flag_walking ) )
1517 return 0;
1518
1519 return 1;
1520 }
1521
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] );
1526 }
1527
1528 VG_STATIC void skate_grind_friction( player_instance *player,
1529 struct grind_info *inf, float strength ){
1530 v3f v2;
1531 v3_muladds( player->rb.to_world[2], inf->n,
1532 -v3_dot( player->rb.to_world[2], inf->n ), v2 );
1533
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;
1537
1538 v3_muladds( player->rb.v, inf->dir, F*k_rb_delta*strength, player->rb.v );
1539 }
1540
1541 VG_STATIC void skate_grind_decay( player_instance *player,
1542 struct grind_info *inf, float strength ){
1543 m3x3f mtx, mtx_inv;
1544 skate_grind_orient( inf, mtx );
1545 m3x3_transpose( mtx, mtx_inv );
1546
1547 v3f v_grind;
1548 m3x3_mulv( mtx_inv, player->rb.v, v_grind );
1549
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 );
1553 }
1554
1555 VG_STATIC void skate_grind_truck_apply( player_instance *player,
1556 float sign, struct grind_info *inf,
1557 float strength ){
1558 struct player_skate *s = &player->_skate;
1559
1560 /* REFACTOR */
1561 v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
1562 v3f raw, wsp;
1563 m3x3_mulv( player->rb.to_world, ra, raw );
1564 v3_add( player->rb.co, raw, wsp );
1565
1566 v3_copy( ra, s->weight_distribution );
1567
1568 v3f delta;
1569 v3_sub( inf->co, wsp, delta );
1570
1571 /* spring force */
1572 v3_muladds( player->rb.v, delta, k_spring_force*strength*k_rb_delta,
1573 player->rb.v );
1574
1575 skate_grind_decay( player, inf, strength );
1576 skate_grind_friction( player, inf, strength );
1577
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 );
1583
1584 /* orientation */
1585 m3x3f mtx;
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 );
1592
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 );
1595
1596 v3_normalize( target_fwd );
1597 v3_normalize( fwd );
1598
1599 v2f steer;
1600 joystick_state( k_srjoystick_steer, steer );
1601
1602 float way = steer[1] * vg_signf( v3_dot( raw_nplane, player->rb.v ) );
1603
1604 v4f q;
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 );
1608
1609 rb_effect_spring_target_vector( &player->rb, up, target_up,
1610 k_grind_spring,
1611 k_grind_dampener,
1612 k_rb_delta );
1613
1614 rb_effect_spring_target_vector( &player->rb, fwd, target_fwd,
1615 k_grind_spring*strength,
1616 k_grind_dampener*strength,
1617 k_rb_delta );
1618
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 );
1622
1623 s->grind_strength = strength;
1624
1625 /* Fake contact */
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 );
1629 limit->p = 0.0f;
1630
1631 v3_copy( inf->dir, s->grind_dir );
1632 }
1633
1634 VG_STATIC void skate_5050_apply( player_instance *player,
1635 struct grind_info *inf_front,
1636 struct grind_info *inf_back )
1637 {
1638 struct player_skate *s = &player->_skate;
1639 struct grind_info inf_avg;
1640
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 );
1644
1645 /* dont ask */
1646 v3_muls( inf_avg.dir, vg_signf(v3_dot(inf_avg.dir,player->rb.v)),
1647 inf_avg.dir );
1648
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 );
1654
1655 v3_cross( axis, inf_avg.dir, inf_avg.n );
1656 skate_grind_decay( player, &inf_avg, 1.0f );
1657
1658 v2f steer;
1659 joystick_state( k_srjoystick_steer, steer );
1660
1661 float way = steer[1] *
1662 vg_signf( v3_dot( player->rb.to_world[2], player->rb.v ) );
1663 v4f q;
1664 v3f up, target_up;
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 );
1669
1670 v3_zero( s->weight_distribution );
1671 s->weight_distribution[2] = k_board_length * -way;
1672
1673 rb_effect_spring_target_vector( &player->rb, up, target_up,
1674 k_grind_spring,
1675 k_grind_dampener,
1676 k_rb_delta );
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 );
1679
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 );
1683
1684 v3f dir;
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 );
1687
1688 v3_normalize( fwd_nplane );
1689 v3_normalize( dir_nplane );
1690
1691 rb_effect_spring_target_vector( &player->rb, fwd_nplane, dir_nplane,
1692 1000.0f,
1693 k_grind_dampener,
1694 k_rb_delta );
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 );
1697
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;
1701
1702 m4x3_mulv( player->rb.to_world, pos_front, pos_front );
1703 m4x3_mulv( player->rb.to_world, pos_back, pos_back );
1704
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 );
1708
1709 v3_muladds( player->rb.v, delta_total, 50.0f * k_rb_delta, player->rb.v );
1710
1711 /* Fake contact */
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 );
1715 limit->p = 0.0f;
1716
1717 v3_copy( inf_avg.dir, s->grind_dir );
1718 }
1719
1720 VG_STATIC int skate_grind_truck_renew( player_instance *player, float sign,
1721 struct grind_info *inf )
1722 {
1723 struct player_skate *s = &player->_skate;
1724
1725 v3f wheel_co = { 0.0f, 0.0f, sign * k_board_length },
1726 grind_co = { 0.0f, -k_board_radius, sign * k_board_length };
1727
1728 m4x3_mulv( player->rb.to_world, wheel_co, wheel_co );
1729 m4x3_mulv( player->rb.to_world, grind_co, grind_co );
1730
1731 /* Exit condition: lost grind tracking */
1732 if( !skate_grind_scansq( player, grind_co, player->rb.v, 0.3f, inf ) )
1733 return 0;
1734
1735 /* Exit condition: cant see grind target directly */
1736 if( !skate_point_visible( wheel_co, inf->co ) )
1737 return 0;
1738
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;
1742
1743 if( dv < minv )
1744 return 0;
1745
1746 if( fabsf(v3_dot( inf->dir, s->grind_dir )) < k_grind_max_edge_angle )
1747 return 0;
1748
1749 v3_copy( inf->dir, s->grind_dir );
1750 return 1;
1751 }
1752
1753 VG_STATIC int skate_grind_truck_entry( player_instance *player, float sign,
1754 struct grind_info *inf )
1755 {
1756 struct player_skate *s = &player->_skate;
1757
1758 /* REFACTOR */
1759 v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
1760
1761 v3f raw, wsp;
1762 m3x3_mulv( player->rb.to_world, ra, raw );
1763 v3_add( player->rb.co, raw, wsp );
1764
1765 if( skate_grind_scansq( player, wsp, player->rb.v, 0.3, inf ) )
1766 {
1767 if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
1768 return 0;
1769
1770 /* velocity should be at least 60% aligned */
1771 v3f pv, axis;
1772 v3_cross( inf->n, inf->dir, axis );
1773 v3_muladds( player->rb.v, inf->n, -v3_dot( player->rb.v, inf->n ), pv );
1774
1775 if( v3_length2( pv ) < 0.0001f )
1776 return 0;
1777 v3_normalize( pv );
1778
1779 if( fabsf(v3_dot( pv, inf->dir )) < k_grind_axel_max_angle )
1780 return 0;
1781
1782 if( v3_dot( player->rb.v, inf->n ) > 0.5f )
1783 return 0;
1784
1785 #if 0
1786 /* check for vertical alignment */
1787 if( v3_dot( player->rb.to_world[1], inf->n ) < k_grind_axel_max_vangle )
1788 return 0;
1789 #endif
1790
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 );
1795
1796 v2f delta = { local_co[0], local_co[2] - k_board_length*sign };
1797
1798 float truck_height = -(k_board_radius+0.03f);
1799
1800 v3f rv;
1801 v3_cross( player->rb.w, raw, rv );
1802 v3_add( player->rb.v, rv, rv );
1803
1804 if( (local_co[1] >= truck_height) &&
1805 (v2_length2( delta ) <= k_board_radius*k_board_radius) )
1806 {
1807 return 1;
1808 }
1809 }
1810
1811 return 0;
1812 }
1813
1814 VG_STATIC void skate_boardslide_apply( player_instance *player,
1815 struct grind_info *inf )
1816 {
1817 struct player_skate *s = &player->_skate;
1818
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 );
1823
1824 v3f intersection;
1825 v3_muladds( local_co, local_dir, local_co[0]/-local_dir[0],
1826 intersection );
1827 v3_copy( intersection, s->weight_distribution );
1828
1829 skate_grind_decay( player, inf, 0.0125f );
1830 skate_grind_friction( player, inf, 0.25f );
1831
1832 /* direction alignment */
1833 v3f dir, perp;
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 );
1837
1838 m3x3_mulv( player->rb.to_world, dir, dir );
1839 m3x3_mulv( player->rb.to_world, perp, perp );
1840
1841 v4f qbalance;
1842 q_axis_angle( qbalance, dir, local_co[0]*k_grind_balance );
1843 q_mulv( qbalance, perp, perp );
1844
1845 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0],
1846 dir,
1847 k_grind_spring, k_grind_dampener,
1848 k_rb_delta );
1849
1850 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[2],
1851 perp,
1852 k_grind_spring, k_grind_dampener,
1853 k_rb_delta );
1854
1855 vg_line_arrow( player->rb.co, dir, 0.5f, VG__GREEN );
1856 vg_line_arrow( player->rb.co, perp, 0.5f, VG__BLUE );
1857
1858 v3_copy( inf->dir, s->grind_dir );
1859 }
1860
1861 VG_STATIC int skate_boardslide_entry( player_instance *player,
1862 struct grind_info *inf )
1863 {
1864 struct player_skate *s = &player->_skate;
1865
1866 if( skate_grind_scansq( player, player->rb.co,
1867 player->rb.to_world[0], k_board_length,
1868 inf ) )
1869 {
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 );
1873
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 */
1877 {
1878 if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
1879 return 0;
1880
1881 return 1;
1882 }
1883 }
1884
1885 return 0;
1886 }
1887
1888 VG_STATIC int skate_boardslide_renew( player_instance *player,
1889 struct grind_info *inf )
1890 {
1891 struct player_skate *s = &player->_skate;
1892
1893 if( !skate_grind_scansq( player, player->rb.co,
1894 player->rb.to_world[0], k_board_length,
1895 inf ) )
1896 return 0;
1897
1898 /* Exit condition: cant see grind target directly */
1899 v3f vis;
1900 v3_muladds( player->rb.co, player->rb.to_world[1], 0.2f, vis );
1901 if( !skate_point_visible( vis, inf->co ) )
1902 return 0;
1903
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;
1907
1908 if( dv < minv )
1909 return 0;
1910
1911 if( fabsf(v3_dot( inf->dir, s->grind_dir )) < k_grind_max_edge_angle )
1912 return 0;
1913
1914 return 1;
1915 }
1916
1917 VG_STATIC void skate_store_grind_vec( player_instance *player,
1918 struct grind_info *inf )
1919 {
1920 struct player_skate *s = &player->_skate;
1921
1922 m3x3f mtx;
1923 skate_grind_orient( inf, mtx );
1924 m3x3_transpose( mtx, mtx );
1925
1926 v3f raw;
1927 v3_sub( inf->co, player->rb.co, raw );
1928
1929 m3x3_mulv( mtx, raw, s->grind_vec );
1930 v3_normalize( s->grind_vec );
1931 v3_copy( inf->dir, s->grind_dir );
1932 }
1933
1934 VG_STATIC enum skate_activity skate_availible_grind( player_instance *player )
1935 {
1936 struct player_skate *s = &player->_skate;
1937
1938 if( s->state.grind_cooldown > 100 ){
1939 vg_fatal_error( "wth!\n" );
1940 }
1941
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;
1946 }
1947
1948 struct grind_info inf_back50,
1949 inf_front50,
1950 inf_slide;
1951
1952 int res_back50 = 0,
1953 res_front50 = 0,
1954 res_slide = 0;
1955
1956 int allow_back = 1,
1957 allow_front = 1;
1958
1959 v2f steer;
1960 joystick_state( k_srjoystick_steer, steer );
1961
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 )
1965 {
1966 float tilt = steer[1];
1967
1968 if( fabsf(tilt) >= 0.25f ){
1969 v3f raw = {0.0f,0.0f,tilt};
1970 m3x3_mulv( player->rb.to_world, raw, raw );
1971
1972 float way = tilt * vg_signf( v3_dot( raw, player->rb.v ) );
1973
1974 if( way < 0.0f ) allow_front = 0;
1975 else allow_back = 0;
1976 }
1977 }
1978
1979 if( s->state.activity == k_skate_activity_grind_boardslide ){
1980 res_slide = skate_boardslide_renew( player, &inf_slide );
1981 }
1982 else if( s->state.activity == k_skate_activity_grind_back50 ){
1983 res_back50 = skate_grind_truck_renew( player, 1.0f, &inf_back50 );
1984
1985 if( allow_front )
1986 res_front50 = skate_grind_truck_entry( player, -1.0f, &inf_front50 );
1987 }
1988 else if( s->state.activity == k_skate_activity_grind_front50 ){
1989 res_front50 = skate_grind_truck_renew( player, -1.0f, &inf_front50 );
1990
1991 if( allow_back )
1992 res_back50 = skate_grind_truck_entry( player, 1.0f, &inf_back50 );
1993 }
1994 else if( s->state.activity == k_skate_activity_grind_5050 ){
1995 if( allow_front )
1996 res_front50 = skate_grind_truck_renew( player, -1.0f, &inf_front50 );
1997 if( allow_back )
1998 res_back50 = skate_grind_truck_renew( player, 1.0f, &inf_back50 );
1999 }
2000 else{
2001 res_slide = skate_boardslide_entry( player, &inf_slide );
2002
2003 if( allow_back )
2004 res_back50 = skate_grind_truck_entry( player, 1.0f, &inf_back50 );
2005
2006 if( allow_front )
2007 res_front50 = skate_grind_truck_entry( player, -1.0f, &inf_front50 );
2008
2009 if( res_back50 != res_front50 ){
2010 int wants_to_do_that = fabsf(steer[1]) >= 0.25f;
2011
2012 res_back50 &= wants_to_do_that;
2013 res_front50 &= wants_to_do_that;
2014 }
2015 }
2016
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 */
2023
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 */
2029 }
2030 , new_activity = table[ res_slide << 2 | res_back50 << 1 | res_front50 ];
2031
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;
2036 }
2037 }
2038 else if( new_activity == k_skate_activity_grind_boardslide ){
2039 skate_boardslide_apply( player, &inf_slide );
2040 }
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 );
2044
2045 skate_grind_truck_apply( player, 1.0f, &inf_back50, 1.0f );
2046 }
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 );
2050
2051 skate_grind_truck_apply( player, -1.0f, &inf_front50, 1.0f );
2052 }
2053 else if( new_activity == k_skate_activity_grind_5050 )
2054 skate_5050_apply( player, &inf_front50, &inf_back50 );
2055
2056 return new_activity;
2057 }
2058
2059 VG_STATIC void player__skate_update( player_instance *player )
2060 {
2061 struct player_skate *s = &player->_skate;
2062 world_instance *world = world_current_instance();
2063
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 );
2069 return;
2070 }
2071 }
2072
2073 v3_copy( player->rb.co, s->state.prev_pos );
2074 s->state.activity_prev = s->state.activity;
2075 v3f normal_total;
2076 v3_zero( normal_total );
2077
2078 struct board_collider
2079 {
2080 v3f pos;
2081 float radius;
2082
2083 u32 colour;
2084
2085 enum board_collider_state
2086 {
2087 k_collider_state_default,
2088 k_collider_state_disabled,
2089 k_collider_state_colliding
2090 }
2091 state;
2092 }
2093 wheels[] =
2094 {
2095 {
2096 { 0.0f, 0.0f, -k_board_length },
2097 .radius = k_board_radius,
2098 .colour = VG__RED
2099 },
2100 {
2101 { 0.0f, 0.0f, k_board_length },
2102 .radius = k_board_radius,
2103 .colour = VG__GREEN
2104 }
2105 };
2106
2107 float slap = 0.0f;
2108
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++ ){
2112 v3f wpos, closest;
2113 m4x3_mulv( player->rb.to_world, wheels[i].pos, wpos );
2114
2115 if( bh_closest_point( world->geo_bh, wpos, closest, min_dist ) != -1 ){
2116 min_dist = vg_minf( min_dist, v3_dist( closest, wpos ) );
2117 }
2118 }
2119 min_dist -= 0.2f;
2120 float vy = v3_dot( player->basis[1], player->rb.v );
2121 vy = vg_maxf( 0.0f, vy );
2122
2123 slap = vg_clampf( (min_dist/0.5f) + vy, 0.0f, 1.0f )*0.3f;
2124 }
2125 s->state.slap = vg_lerpf( s->state.slap, slap, 10.0f*k_rb_delta );
2126
2127 wheels[0].pos[1] = s->state.slap;
2128 wheels[1].pos[1] = s->state.slap;
2129
2130
2131 const int k_wheel_count = 2;
2132
2133 s->substep = k_rb_delta;
2134 s->substep_delta = s->substep;
2135 s->limit_count = 0;
2136
2137 int substep_count = 0;
2138
2139 v3_zero( s->surface_picture );
2140
2141 int prev_contacts[2];
2142
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];
2146 }
2147
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;
2152 goto grinding;
2153 }
2154
2155 int contact_count = 0;
2156 for( int i=0; i<2; i++ ){
2157 v3f normal, axel;
2158 v3_copy( player->rb.to_world[0], axel );
2159
2160 if( skate_compute_surface_alignment( player, wheels[i].pos,
2161 wheels[i].colour, normal, axel ) )
2162 {
2163 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0],
2164 axel,
2165 k_surface_spring, k_surface_dampener,
2166 s->substep_delta );
2167
2168 v3_add( normal, s->surface_picture, s->surface_picture );
2169 contact_count ++;
2170 s->wheel_contacts[i] = 1;
2171 }
2172 else{
2173 s->wheel_contacts[i] = 0;
2174 }
2175
2176 m3x3_mulv( player->rb.to_local, axel, s->truckv0[i] );
2177 }
2178
2179 if( s->state.surface_cooldown ){
2180 s->state.surface_cooldown --;
2181 contact_count = 0;
2182 }
2183
2184 if( (prev_contacts[0]+prev_contacts[1] == 1) && (contact_count == 2) ){
2185 audio_lock();
2186 for( int i=0; i<2; i++ ){
2187 if( !prev_contacts[i] ){
2188 v3f co;
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 );
2191 }
2192 }
2193 audio_unlock();
2194 }
2195
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 );
2200
2201 skate_apply_friction_model( player );
2202 skate_weight_distribute( player );
2203 }
2204 else{
2205 if( s->state.activity > k_skate_activity_air_to_grind )
2206 s->state.activity = k_skate_activity_air;
2207
2208 v3_zero( s->weight_distribution );
2209 skate_apply_air_model( player );
2210 }
2211
2212 grinding:;
2213
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;
2221 }
2222
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 );
2231
2232 begin_collision:;
2233
2234 /*
2235 * Phase 0: Continous collision detection
2236 * --------------------------------------------------------------------------
2237 */
2238
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 );
2242
2243 /* calculate transform one step into future */
2244 v3f future_co;
2245 v4f future_q;
2246 v3_muladds( player->rb.co, player->rb.v, s->substep, future_co );
2247
2248 if( v3_length2( player->rb.w ) > 0.0f ){
2249 v4f rotation;
2250 v3f axis;
2251 v3_copy( player->rb.w, axis );
2252
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 );
2258 }
2259 else
2260 v4_copy( player->rb.q, future_q );
2261
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 );
2266
2267 /* calculate the minimum time we can move */
2268 float max_time = s->substep;
2269
2270 for( int i=0; i<k_wheel_count; i++ ){
2271 if( wheels[i].state == k_collider_state_disabled )
2272 continue;
2273
2274 v3f current, future, r_cg;
2275
2276 q_mulv( future_q, wheels[i].pos, future );
2277 v3_add( future, future_co, future );
2278 v3_add( cg_offset, future, future );
2279
2280 q_mulv( player->rb.q, wheels[i].pos, current );
2281 v3_add( current, player->rb.co, current );
2282
2283 float t;
2284 v3f n;
2285
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 );
2290 }
2291
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;
2296
2297 max_time = vg_maxf( max_time, rate_lock );
2298 s->substep_delta = max_time;
2299
2300 /* integrate */
2301 v3_muladds( player->rb.co, player->rb.v, s->substep_delta, player->rb.co );
2302 if( v3_length2( player->rb.w ) > 0.0f ){
2303 v4f rotation;
2304 v3f axis;
2305 v3_copy( player->rb.w, axis );
2306
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 );
2312
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 );
2316 }
2317
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 );
2321
2322 s->substep -= s->substep_delta;
2323
2324 rb_ct manifold[128];
2325 int manifold_len = 0;
2326 /*
2327 * Phase -1: head detection
2328 * --------------------------------------------------------------------------
2329 */
2330 m4x3_mulv( player->rb.to_world, s->state.head_position, head_wp1 );
2331
2332 float t;
2333 v3f n;
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) )
2337 {
2338 v3_lerp( start_co, player->rb.co, t, player->rb.co );
2339 rb_update_transform( &player->rb );
2340
2341 player__skate_kill_audio( player );
2342 player__dead_transition( player );
2343 return;
2344 }
2345
2346 /*
2347 * Phase 1: Regular collision detection
2348 * --------------------------------------------------------------------------
2349 */
2350
2351 for( int i=0; i<k_wheel_count; i++ ){
2352 if( wheels[i].state == k_collider_state_disabled )
2353 continue;
2354
2355 m4x3f mtx;
2356 m3x3_identity( mtx );
2357 m4x3_mulv( player->rb.to_world, wheels[i].pos, mtx[3] );
2358
2359 rb_sphere collider = { .radius = wheels[i].radius };
2360
2361 rb_ct *man = &manifold[ manifold_len ];
2362
2363 int l = skate_collide_smooth( player, mtx, &collider, man );
2364 if( l )
2365 wheels[i].state = k_collider_state_colliding;
2366
2367 manifold_len += l;
2368 }
2369
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 };
2373 m4x3f mtx;
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] );
2379
2380 rb_ct *cman = &manifold[manifold_len];
2381
2382 int l = rb_capsule__scene( mtx, &capsule, NULL, &world->rb_geo.inf.scene,
2383 cman, k_material_flag_walking );
2384
2385 /* weld joints */
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 );
2390
2391 manifold_len += l;
2392
2393 if( vg_lines.draw )
2394 vg_line_capsule( mtx, capsule.radius, capsule.height, VG__WHITE );
2395
2396 /* add limits */
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 );
2403 ct->p = limit->p;
2404 ct->type = k_contact_type_default;
2405 }
2406 }
2407
2408 /*
2409 * Phase 3: Dynamics
2410 * --------------------------------------------------------------------------
2411 */
2412
2413
2414 v3f world_cog;
2415 m4x3_mulv( player->rb.to_world, s->weight_distribution, world_cog );
2416 vg_line_point( world_cog, 0.02f, VG__BLACK );
2417
2418 for( int i=0; i<manifold_len; i ++ ){
2419 rb_prepare_contact( &manifold[i], s->substep_delta );
2420 rb_debug_contact( &manifold[i] );
2421 }
2422
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];
2428
2429 float mass = 2.0f * (extent[0]*extent[1]*extent[2]);
2430 float inv_mass = 1.0f/mass;
2431
2432 v3f I;
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));
2436
2437 m3x3f iI;
2438 m3x3_identity( iI );
2439 iI[0][0] = I[0];
2440 iI[1][1] = I[1];
2441 iI[2][2] = I[2];
2442 m3x3_inv( iI, iI );
2443
2444 m3x3f iIw;
2445 m3x3_mul( iI, player->rb.to_local, iIw );
2446 m3x3_mul( player->rb.to_world, iIw, iIw );
2447
2448 for( int j=0; j<10; j++ ){
2449 for( int i=0; i<manifold_len; i++ ){
2450 /*
2451 * regular dance; calculate velocity & total mass, apply impulse.
2452 */
2453
2454 struct contact *ct = &manifold[i];
2455
2456 v3f rv, delta;
2457 v3_sub( ct->co, world_cog, delta );
2458 v3_cross( player->rb.w, delta, rv );
2459 v3_add( player->rb.v, rv, rv );
2460
2461 v3f raCn;
2462 v3_cross( delta, ct->n, raCn );
2463
2464 v3f raCnI, rbCnI;
2465 m3x3_mulv( iIw, raCn, raCnI );
2466
2467 float normal_mass = 1.0f / (inv_mass + v3_dot(raCn,raCnI)),
2468 vn = v3_dot( rv, ct->n ),
2469 lambda = normal_mass * ( -vn );
2470
2471 float temp = ct->norm_impulse;
2472 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
2473 lambda = ct->norm_impulse - temp;
2474
2475 v3f impulse;
2476 v3_muls( ct->n, lambda, impulse );
2477
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 );
2483
2484 v3_cross( player->rb.w, delta, rv );
2485 v3_add( player->rb.v, rv, rv );
2486 vn = v3_dot( rv, ct->n );
2487 }
2488 }
2489
2490 v3f dt;
2491 rb_depenetrate( manifold, manifold_len, dt );
2492 v3_add( dt, player->rb.co, player->rb.co );
2493 rb_update_transform( &player->rb );
2494
2495 substep_count ++;
2496
2497 if( s->substep >= 0.0001f )
2498 goto begin_collision; /* again! */
2499
2500 /*
2501 * End of collision and dynamics routine
2502 * --------------------------------------------------------------------------
2503 */
2504
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);
2511 return;
2512 }
2513
2514 f32 amt = k_cam_punch;
2515 if( player->cam_control.camera_mode == k_cam_firstperson ){
2516 amt *= 0.25f;
2517 }
2518
2519 v3_muladds( player->cam_land_punch_v, normal_total, amt,
2520 player->cam_land_punch_v );
2521 }
2522
2523 s->surface = k_surface_prop_concrete;
2524
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 );
2528
2529 if( surf->info.surface_prop > s->surface )
2530 s->surface = surf->info.surface_prop;
2531 }
2532
2533 for( int i=0; i<k_wheel_count; i++ ){
2534 m4x3f mtx;
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 ]);
2540 }
2541
2542 skate_integrate( player );
2543 vg_line_point( s->state.cog, 0.02f, VG__WHITE );
2544
2545 ent_gate *gate =
2546 world_intersect_gates(world, player->rb.co, s->state.prev_pos );
2547
2548 if( gate ){
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 );
2557
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 );
2565 }
2566
2567 /* FIXME: Rate limit */
2568 static int stick_frames = 0;
2569
2570 if( s->state.activity >= k_skate_activity_ground )
2571 stick_frames ++;
2572 else
2573 stick_frames = 0;
2574
2575 if( stick_frames > 5 ) stick_frames = 5;
2576
2577 if( stick_frames == 4 ){
2578 audio_lock();
2579
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,
2583 40.0f, 1.0f );
2584 }
2585 else{
2586 audio_oneshot_3d( &audio_lands[vg_randu32()%3], player->rb.co,
2587 40.0f, 1.0f );
2588 }
2589 }
2590 else if( s->surface == k_surface_prop_metal ){
2591 audio_oneshot_3d( &audio_board[3], player->rb.co, 40.0f, 1.0f );
2592 }
2593 else{
2594 audio_oneshot_3d( &audio_board[8], player->rb.co, 40.0f, 1.0f );
2595 }
2596
2597 audio_unlock();
2598 } else if( stick_frames == 0 ){
2599
2600 }
2601 }
2602
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],
2606 player->rb.v[1],
2607 player->rb.v[2] );
2608 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",player->rb.co[0],
2609 player->rb.co[1],
2610 player->rb.co[2] );
2611 player__debugtext( 1, "W: %5.2f %5.2f %5.2f",player->rb.w[0],
2612 player->rb.w[1],
2613 player->rb.w[2] );
2614
2615 const char *activity_txt[] =
2616 {
2617 "air",
2618 "air_to_grind",
2619 "ground",
2620 "undefined (INVALID)",
2621 "grind_any (INVALID)",
2622 "grind_boardslide",
2623 "grind_metallic (INVALID)",
2624 "grind_back50",
2625 "grind_front50",
2626 "grind_5050"
2627 };
2628
2629 player__debugtext( 1, "activity: %s", activity_txt[s->state.activity] );
2630 #if 0
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 );
2634 #endif
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] );
2645 }
2646
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;
2651
2652 /* Head */
2653 float kheight = 2.0f,
2654 kleg = 0.6f;
2655
2656 v3_zero( animator->offset );
2657
2658 v3f cog_local, cog_ideal;
2659 m4x3_mulv( player->rb.to_local, s->state.cog, cog_local );
2660
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 );
2664
2665 v3_sub( cog_ideal, cog_local, animator->offset );
2666
2667 v3_muls( animator->offset, 4.0f, animator->offset );
2668 animator->offset[1] *= -1.0f;
2669
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 );
2674
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);
2679
2680 animator->offset[0] *= 0.26f;
2681 animator->offset[0] += animator->wobble[1]*3.0f;
2682
2683 animator->offset[1] *= -0.3f;
2684 animator->offset[2] *= 0.01f;
2685
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);
2689
2690 v3_muls( animator->offset, 0.3f, player->cam_control.tpv_offset_extra );
2691
2692 /* localized vectors */
2693 m4x3_mulv( player->rb.to_local, s->state.cog, animator->local_cog );
2694
2695 /*
2696 * Animation blending
2697 * ===========================================
2698 */
2699
2700 /* sliding */
2701 {
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 );
2705
2706 animator->slide = vg_lerpf( animator->slide, desired, 2.4f*vg.time_delta);
2707 }
2708
2709 /* movement information */
2710 int iair = s->state.activity <= k_skate_activity_air_to_grind;
2711
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;
2716
2717 if( s->state.activity >= k_skate_activity_grind_any )
2718 wdist = 0.0f;
2719
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 );
2724
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;
2728
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;
2732 }
2733
2734 /* grinding */
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 );
2737
2738 f32 grind_frame = 0.5f;
2739
2740 if( s->state.activity == k_skate_activity_grind_front50 )
2741 grind_frame = 0.0f;
2742 else if( s->state.activity == k_skate_activity_grind_back50 )
2743 grind_frame = 1.0f;
2744
2745 animator->grind_balance = vg_lerpf( animator->grind_balance, grind_frame,
2746 5.0f*vg.time_delta );
2747
2748 /* pushing */
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 );
2753
2754 /* jumping */
2755 animator->jump_charge = s->state.jump_charge;
2756 animator->jump = vg_lerpf( animator->jump, animator->jump_charge,
2757 8.4f*vg.time_delta );
2758
2759 /* trick setup */
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;
2766
2767 /* trick */
2768 float jump_t = vg.time-s->state.jump_time;
2769 float k=17.0f;
2770 float h = k*jump_t;
2771 float extra = h*exp(1.0-h) * (s->state.jump_dir?1.0f:-1.0f);
2772 extra *= s->state.slap * 4.0f;
2773
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 );
2777
2778 animator->board_euler[0] *= 0.5f;
2779 animator->board_euler[1] += extra;
2780 animator->trick_type = s->state.trick_type;
2781
2782 /* board lean */
2783 f32 lean1, lean2 = animator->steer[0] * animator->reverse * -0.36f,
2784 lean;
2785
2786 lean1 = animator->slide * animator->delayed_slip_dir;
2787 if( fabsf(lean1)>fabsf(lean2) ) lean = lean1;
2788 else lean = lean2;
2789
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);
2794
2795 /* feet placement */
2796 struct player_board *board =
2797 addon_cache_item_if_loaded( k_addon_type_board,
2798 player->board_view_slot );
2799 if( board ){
2800 if( animator->weight > 0.0f ){
2801 animator->foot_offset[0] =
2802 board->truck_positions[k_board_truck_back][2]+0.3f;
2803 }
2804 else{
2805 animator->foot_offset[1] =
2806 board->truck_positions[k_board_truck_front][2]-0.3f;
2807 }
2808 }
2809
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 );
2814
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 );
2819
2820 /* grab */
2821 v2f grab_input;
2822 joystick_state( k_srjoystick_grab, grab_input );
2823 v2_add( s->state.grab_mouse_delta, grab_input, grab_input );
2824
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;
2829
2830 /* steer */
2831 joystick_state( k_srjoystick_steer, animator->steer );
2832
2833 animator->airdir = vg_lerpf( animator->airdir, -animator->steer[0],
2834 2.4f*vg.time_delta );
2835
2836
2837 /* flip angle */
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 );
2843
2844 t = 1.0f - vg_minf( 1.0f, fabsf( t * 1.1f ) );
2845 t = sign * (1.0f-t*t);
2846
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 );
2852 }
2853 else
2854 q_identity( animator->qflip );
2855
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 );
2862
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 );
2868
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;
2873
2874 v3f ndir;
2875 m3x3_mulv( player->rb.to_local, s->state.up_dir, ndir );
2876 v3_normalize( ndir );
2877
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 ) );
2881
2882 v3f axis;
2883 v4f qcounteryaw, qfixup;
2884
2885 v3_cross( up, ndir, axis );
2886 q_axis_angle( qfixup, axis, a*2.0f );
2887
2888 v3_cross( (v3f){1.0f,0.0f,0.0f}, yaw_smooth, axis );
2889 q_axis_angle( qcounteryaw, axis, yaw_counter_rotate );
2890
2891 q_mul( qcounteryaw, qfixup, animator->qfixuptotal );
2892 q_normalize( animator->qfixuptotal );
2893
2894 v3f p1, p2;
2895 m3x3_mulv( player->rb.to_world, up, p1 );
2896 m3x3_mulv( player->rb.to_world, ndir, p2 );
2897
2898 vg_line_arrow( player->rb.co, p1, 0.5f, VG__PINK );
2899 vg_line_arrow( player->rb.co, p2, 0.5f, VG__PINK );
2900 }
2901 else q_identity( animator->qfixuptotal );
2902 rb_extrapolate( &player->rb, animator->root_co, animator->root_q );
2903 }
2904
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 );
2913
2914 /* transform */
2915 v3f ext_up,ext_co;
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 );
2919
2920 /* apply flip rotation at midpoint */
2921 q_mul( animator->qflip, pose->root_q, pose->root_q );
2922 q_normalize( pose->root_q );
2923
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 );
2927
2928 q_mulv( animator->qflip, rco, rco );
2929 v3_add( rco, rotation_point, pose->root_co );
2930
2931 /* ANIMATIONS
2932 * ---------------------------------------------------------------------- */
2933
2934 mdl_keyframe apose[32], bpose[32];
2935 mdl_keyframe ground_pose[32];
2936 {
2937 /* stand/crouch */
2938 f32 dir_frame = animator->z * (15.0f/30.0f),
2939 stand_blend = animator->offset[1]*-2.0f;
2940
2941 pose->board.lean = animator->board_lean;
2942
2943 stand_blend = vg_clampf( 1.0f-animator->local_cog[1], 0, 1 );
2944
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 );
2948
2949 /* sliding */
2950 skeleton_sample_anim( sk, s->anim_slide, animator->x * 0.5f, bpose );
2951 skeleton_lerp_pose( sk, apose, bpose, animator->slide, apose );
2952
2953 if( animator->reverse > 0.0f )
2954 skeleton_sample_anim( sk, s->anim_push, animator->push_time, bpose );
2955 else{
2956 skeleton_sample_anim( sk, s->anim_push_reverse, animator->push_time,
2957 bpose );
2958 }
2959 skeleton_lerp_pose( sk, apose, bpose, animator->push, apose );
2960
2961 struct skeleton_anim *jump_anim = animator->jump_dir?
2962 s->anim_ollie:
2963 s->anim_ollie_reverse;
2964
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 );
2968 }
2969
2970 mdl_keyframe air_pose[32];
2971 {
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 );
2974
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);
2978
2979 skeleton_sample_anim( sk, s->anim_grabs, grab_frame, bpose );
2980 skeleton_lerp_pose( sk, apose, bpose, animator->grabbing, air_pose );
2981 }
2982
2983 skeleton_lerp_pose( sk, ground_pose, air_pose, animator->fly,
2984 pose->keyframes );
2985
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] };
2994
2995
2996 mdl_keyframe grind_pose[32];
2997 {
2998 f32 frame = animator->grind_balance * 0.5f;
2999
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 );
3003 }
3004 skeleton_lerp_pose( sk, pose->keyframes, grind_pose,
3005 animator->grind, pose->keyframes );
3006 float add_grab_mod = 1.0f - animator->fly;
3007
3008 /* additive effects */
3009 u32 apply_to[] = { av->id_hip,
3010 av->id_ik_hand_l,
3011 av->id_ik_hand_r,
3012 av->id_ik_elbow_l,
3013 av->id_ik_elbow_r };
3014
3015 float apply_rates[] = { 1.0f,
3016 0.75f,
3017 0.75f,
3018 0.75f,
3019 0.75f };
3020
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;
3024 }
3025
3026 /* angle 'correction' */
3027 v3f origin;
3028 v3_add( av->sk.bones[av->id_hip].co, kf_hip->co, origin );
3029
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 );
3034 }
3035
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] );
3041
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 );
3046
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 );
3051
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;
3058
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;
3062 }
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;
3068 }
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;
3074 }
3075
3076 /*
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.
3081 */
3082
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] );
3087
3088 v4f q;
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 );
3092 }
3093
3094 #if 0
3095 {
3096 mdl_keyframe
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];
3102
3103 float warble = perlin1d( vg.time, 2.0f, 2, 300 );
3104 warble *= vg_maxf(animator->grind, fabsf(animator->weight)) * 0.3f;
3105
3106 v4f qrot;
3107 q_axis_angle( qrot, (v3f){0.8f,0.7f,0.6f}, warble );
3108
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 );
3120
3121 q_inv( qrot, qrot );
3122 q_mul( qrot, kf_head->q, kf_head->q );
3123 q_normalize( kf_head->q );
3124 }
3125 #endif
3126 }
3127
3128 VG_STATIC void player__skate_post_animate( player_instance *player )
3129 {
3130 struct player_skate *s = &player->_skate;
3131 struct player_avatar *av = player->playeravatar;
3132
3133 player->cam_velocity_influence = 1.0f;
3134
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 );
3139 }
3140
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;
3144
3145 memset( &s->animator, 0, sizeof(s->animator) );
3146
3147 if( s->state.activity <= k_skate_activity_air_to_grind )
3148 s->animator.fly = 1.0f;
3149 else
3150 s->animator.fly = 0.0f;
3151 }
3152
3153 VG_STATIC void player__skate_clear_mechanics( player_instance *player )
3154 {
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;
3171
3172 v3_zero( s->state.air_init_v );
3173 v3_zero( s->state.air_init_co );
3174
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 );
3189 }
3190
3191 VG_STATIC void player__skate_reset( player_instance *player,
3192 ent_spawn *rp )
3193 {
3194 struct player_skate *s = &player->_skate;
3195 v3_zero( player->rb.v );
3196 v4_copy( rp->transform.q, player->rb.q );
3197
3198 s->state.activity = k_skate_activity_air;
3199 s->state.activity_prev = k_skate_activity_air;
3200
3201 player__skate_clear_mechanics( player );
3202 player__skate_reset_animator( player );
3203
3204 v3_zero( s->state.head_position );
3205 s->state.head_position[1] = 1.8f;
3206 }
3207
3208 #endif /* PLAYER_SKATE_C */