move everything to animator based
[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
64 for( int i=0; i<len; i++ )
65 {
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 {
74 rb_manifold_filter_backface( man, len );
75 rb_manifold_filter_joint_edges( man, len, 0.03f );
76 rb_manifold_filter_pairs( man, len, 0.03f );
77 }
78 int new_len = rb_manifold_apply_filtered( man, len );
79 if( len && !new_len )
80 len = 1;
81 else
82 len = new_len;
83
84 return len;
85 }
86
87 struct grind_info
88 {
89 v3f co, dir, n;
90 };
91
92 VG_STATIC int skate_grind_scansq( player_instance *player,
93 v3f pos, v3f dir, float r,
94 struct grind_info *inf )
95 {
96 world_instance *world = world_current_instance();
97
98 v4f plane;
99 v3_copy( dir, plane );
100 v3_normalize( plane );
101 plane[3] = v3_dot( plane, pos );
102
103 boxf box;
104 v3_add( pos, (v3f){ r, r, r }, box[1] );
105 v3_sub( pos, (v3f){ r, r, r }, box[0] );
106
107 struct grind_sample{
108 v2f co;
109 v2f normal;
110 v3f normal3,
111 centroid;
112 }
113 samples[48];
114 int sample_count = 0;
115
116 v2f support_min,
117 support_max;
118
119 v3f support_axis;
120 v3_cross( plane, player->basis[1], support_axis );
121 v3_normalize( support_axis );
122
123 bh_iter it;
124 bh_iter_init_box( 0, &it, box );
125 i32 idx;
126
127 while( bh_next( world->geo_bh, &it, &idx ) ){
128 u32 *ptri = &world->scene_geo.arrindices[ idx*3 ];
129 v3f tri[3];
130
131 struct world_surface *surf = world_tri_index_surface(world,ptri[0]);
132 if( !(surf->info.flags & k_material_flag_grindable) )
133 continue;
134
135 for( int j=0; j<3; j++ )
136 v3_copy( world->scene_geo.arrvertices[ptri[j]].co, tri[j] );
137
138 for( int j=0; j<3; j++ ){
139 int i0 = j,
140 i1 = (j+1) % 3;
141
142 struct grind_sample *sample = &samples[ sample_count ];
143 v3f co;
144
145 if( plane_segment( plane, tri[i0], tri[i1], co ) ){
146 v3f d;
147 v3_sub( co, pos, d );
148 if( v3_length2( d ) > r*r )
149 continue;
150
151 v3f va, vb, normal;
152 v3_sub( tri[1], tri[0], va );
153 v3_sub( tri[2], tri[0], vb );
154 v3_cross( va, vb, normal );
155
156 sample->normal[0] = v3_dot( support_axis, normal );
157 sample->normal[1] = v3_dot( player->basis[1], normal );
158 sample->co[0] = v3_dot( support_axis, d );
159 sample->co[1] = v3_dot( player->basis[1], d );
160
161 v3_copy( normal, sample->normal3 ); /* normalize later
162 if we want to us it */
163
164 v3_muls( tri[0], 1.0f/3.0f, sample->centroid );
165 v3_muladds( sample->centroid, tri[1], 1.0f/3.0f, sample->centroid );
166 v3_muladds( sample->centroid, tri[2], 1.0f/3.0f, sample->centroid );
167
168 v2_normalize( sample->normal );
169 sample_count ++;
170
171 if( sample_count == vg_list_size( samples ) )
172 goto too_many_samples;
173 }
174 }
175 }
176
177 too_many_samples:
178
179 if( sample_count < 2 )
180 return 0;
181
182 v3f
183 average_direction,
184 average_normal;
185
186 v2f min_co, max_co;
187 v2_fill( min_co, INFINITY );
188 v2_fill( max_co, -INFINITY );
189
190 v3_zero( average_direction );
191 v3_zero( average_normal );
192
193 int passed_samples = 0;
194
195 for( int i=0; i<sample_count-1; i++ ){
196 struct grind_sample *si, *sj;
197
198 si = &samples[i];
199
200 for( int j=i+1; j<sample_count; j++ ){
201 if( i == j )
202 continue;
203
204 sj = &samples[j];
205
206 /* non overlapping */
207 if( v2_dist2( si->co, sj->co ) >= (0.01f*0.01f) )
208 continue;
209
210 /* not sharp angle */
211 if( v2_dot( si->normal, sj->normal ) >= 0.7f )
212 continue;
213
214 /* not convex */
215 v3f v0;
216 v3_sub( sj->centroid, si->centroid, v0 );
217 if( v3_dot( v0, si->normal3 ) >= 0.0f ||
218 v3_dot( v0, sj->normal3 ) <= 0.0f )
219 continue;
220
221 v2_minv( sj->co, min_co, min_co );
222 v2_maxv( sj->co, max_co, max_co );
223
224 v3f n0, n1, dir;
225 v3_copy( si->normal3, n0 );
226 v3_copy( sj->normal3, n1 );
227 v3_cross( n0, n1, dir );
228 v3_normalize( dir );
229
230 /* make sure the directions all face a common hemisphere */
231 v3_muls( dir, vg_signf(v3_dot(dir,plane)), dir );
232 v3_add( average_direction, dir, average_direction );
233
234 float yi = v3_dot( player->basis[1], si->normal3 ),
235 yj = v3_dot( player->basis[1], sj->normal3 );
236
237 if( yi > yj ) v3_add( si->normal3, average_normal, average_normal );
238 else v3_add( sj->normal3, average_normal, average_normal );
239
240 passed_samples ++;
241 }
242 }
243
244 if( !passed_samples )
245 return 0;
246
247 if( (v3_length2( average_direction ) <= 0.001f) ||
248 (v3_length2( average_normal ) <= 0.001f ) )
249 return 0;
250
251 float div = 1.0f/(float)passed_samples;
252 v3_normalize( average_direction );
253 v3_normalize( average_normal );
254
255 v2f average_coord;
256 v2_add( min_co, max_co, average_coord );
257 v2_muls( average_coord, 0.5f, average_coord );
258
259 v3_muls( support_axis, average_coord[0], inf->co );
260 inf->co[1] += average_coord[1];
261 v3_add( pos, inf->co, inf->co );
262 v3_copy( average_normal, inf->n );
263 v3_copy( average_direction, inf->dir );
264
265 vg_line_point( inf->co, 0.02f, VG__GREEN );
266 vg_line_arrow( inf->co, average_direction, 0.3f, VG__GREEN );
267 vg_line_arrow( inf->co, inf->n, 0.2f, VG__CYAN );
268
269 return passed_samples;
270 }
271
272 VG_STATIC void reset_jump_info( jump_info *inf )
273 {
274 inf->log_length = 0;
275 inf->land_dist = 0.0f;
276 inf->score = 0.0f;
277 inf->type = k_prediction_unset;
278 v3_zero( inf->apex );
279 }
280
281 VG_STATIC int create_jumps_to_hit_target( player_instance *player,
282 jump_info *jumps,
283 v3f target, float max_angle_delta,
284 float gravity )
285 {
286 struct player_skate *s = &player->_skate;
287
288 /* calculate the exact 2 solutions to jump onto that grind spot */
289
290 v3f v0;
291 v3_sub( target, player->rb.co, v0 );
292 m3x3_mulv( player->invbasis, v0, v0 );
293
294 v3f ax;
295 v3_copy( v0, ax );
296 ax[1] = 0.0f;
297 v3_normalize( ax );
298
299 v3f v_local;
300 m3x3_mulv( player->invbasis, player->rb.v, v_local );
301
302 v2f d = { v3_dot( ax, v0 ), v0[1] },
303 v = { v3_dot( ax, v_local ), v_local[1] };
304
305 float a = atan2f( v[1], v[0] ),
306 m = v2_length( v ),
307 root = m*m*m*m - gravity*(gravity*d[0]*d[0] + 2.0f*d[1]*m*m);
308
309 int valid_count = 0;
310
311 if( root > 0.0f ){
312 root = sqrtf( root );
313 float a0 = atanf( (m*m + root) / (gravity * d[0]) ),
314 a1 = atanf( (m*m - root) / (gravity * d[0]) );
315
316 if( fabsf(a0-a) < max_angle_delta ){
317 jump_info *inf = &jumps[ valid_count ++ ];
318 reset_jump_info( inf );
319
320 v3_muls( ax, cosf( a0 ) * m, inf->v );
321 inf->v[1] += sinf( a0 ) * m;
322 m3x3_mulv( player->basis, inf->v, inf->v );
323 inf->land_dist = d[0] / (cosf(a0)*m);
324 inf->gravity = gravity;
325
326 v3_copy( target, inf->log[inf->log_length ++] );
327 }
328
329 if( fabsf(a1-a) < max_angle_delta ){
330 jump_info *inf = &jumps[ valid_count ++ ];
331 reset_jump_info( inf );
332
333 v3_muls( ax, cosf( a1 ) * m, inf->v );
334 inf->v[1] += sinf( a1 ) * m;
335 m3x3_mulv( player->basis, inf->v, inf->v );
336 inf->land_dist = d[0] / (cosf(a1)*m);
337 inf->gravity = gravity;
338
339 v3_copy( target, inf->log[inf->log_length ++] );
340 }
341 }
342
343 return valid_count;
344 }
345
346 VG_STATIC
347 void player__approximate_best_trajectory( player_instance *player )
348 {
349 world_instance *world0 = world_current_instance();
350
351 struct player_skate *s = &player->_skate;
352 float k_trace_delta = k_rb_delta * 10.0f;
353
354 s->state.air_start = vg.time;
355 v3_copy( player->rb.v, s->state.air_init_v );
356 v3_copy( player->rb.co, s->state.air_init_co );
357
358 s->possible_jump_count = 0;
359
360 v3f axis;
361 v3_cross( player->rb.v, player->rb.to_world[1], axis );
362 v3_normalize( axis );
363
364 /* at high slopes, Y component is low */
365 float upness = v3_dot( player->rb.to_world[1], player->basis[1] ),
366 angle_begin = -(1.0f-fabsf( upness )),
367 angle_end = 1.0f;
368
369 struct grind_info grind;
370 int grind_located = 0;
371 float grind_located_gravity = k_gravity;
372
373
374 v3f launch_v_bounds[2];
375
376 for( int i=0; i<2; i++ ){
377 v3_copy( player->rb.v, launch_v_bounds[i] );
378 float ang = (float[]){ angle_begin, angle_end }[ i ];
379 ang *= 0.15f;
380
381 v4f qbias;
382 q_axis_angle( qbias, axis, ang );
383 q_mulv( qbias, launch_v_bounds[i], launch_v_bounds[i] );
384 }
385
386 for( int m=0;m<=30; m++ ){
387 jump_info *inf = &s->possible_jumps[ s->possible_jump_count ++ ];
388 reset_jump_info( inf );
389
390 v3f launch_co, launch_v, co0, co1;
391 v3_copy( player->rb.co, launch_co );
392 v3_copy( player->rb.v, launch_v );
393 v3_copy( launch_co, co0 );
394 world_instance *trace_world = world0;
395
396 float vt = (float)m * (1.0f/30.0f),
397 ang = vg_lerpf( angle_begin, angle_end, vt ) * 0.15f;
398
399 v4f qbias;
400 q_axis_angle( qbias, axis, ang );
401 q_mulv( qbias, launch_v, launch_v );
402
403 float yaw_sketch = 1.0f-fabsf(upness);
404
405 float yaw_bias = ((float)(m%3) - 1.0f) * 0.08f * yaw_sketch;
406 q_axis_angle( qbias, player->rb.to_world[1], yaw_bias );
407 q_mulv( qbias, launch_v, launch_v );
408
409 float gravity_bias = vg_lerpf( 0.85f, 1.4f, vt ),
410 gravity = k_gravity * gravity_bias;
411 inf->gravity = gravity;
412 v3_copy( launch_v, inf->v );
413
414 m3x3f basis;
415 m3x3_copy( player->basis, basis );
416
417 for( int i=1; i<=50; i++ ){
418 float t = (float)i * k_trace_delta;
419
420 v3_muls( launch_v, t, co1 );
421 v3_muladds( co1, basis[1], -0.5f * gravity * t*t, co1 );
422 v3_add( launch_co, co1, co1 );
423
424 float launch_vy = v3_dot( launch_v,basis[1] );
425
426 int search_for_grind = 1;
427 if( grind_located ) search_for_grind = 0;
428 if( launch_vy - gravity*t > 0.0f ) search_for_grind = 0;
429
430 /* REFACTOR */
431
432 v3f closest={0.0f,0.0f,0.0f};
433 if( search_for_grind ){
434 if( bh_closest_point(trace_world->geo_bh,co1,closest,1.0f) != -1 ){
435 float min_dist = 0.75f;
436 min_dist *= min_dist;
437
438 if( v3_dist2( closest, launch_co ) < min_dist )
439 search_for_grind = 0;
440
441 v3f bound[2];
442
443 for( int j=0; j<2; j++ ){
444 v3_muls( launch_v_bounds[j], t, bound[j] );
445 v3_muladds( bound[j], basis[1], -0.5f*gravity*t*t, bound[j] );
446 v3_add( launch_co, bound[j], bound[j] );
447 }
448
449 float limh = vg_minf( 2.0f, t ),
450 minh = vg_minf( bound[0][1], bound[1][1] )-limh,
451 maxh = vg_maxf( bound[0][1], bound[1][1] )+limh;
452
453 if( (closest[1] < minh) || (closest[1] > maxh) ){
454 search_for_grind = 0;
455 }
456 }
457 else
458 search_for_grind = 0;
459 }
460
461 if( search_for_grind ){
462 v3f ve;
463 v3_copy( launch_v, ve );
464 v3_muladds( ve, basis[1], -gravity * t, ve );
465
466 if( skate_grind_scansq( player, closest, ve, 0.5f, &grind ) ){
467 /* check alignment */
468 v2f v0 = { v3_dot( ve, basis[0] ),
469 v3_dot( ve, basis[2] ) },
470 v1 = { v3_dot( grind.dir, basis[0] ),
471 v3_dot( grind.dir, basis[2] ) };
472
473 v2_normalize( v0 );
474 v2_normalize( v1 );
475
476 float a = v2_dot( v0, v1 );
477
478 float a_min = cosf( VG_PIf * 0.185f );
479 if( s->state.grind_cooldown )
480 a_min = cosf( VG_PIf * 0.05f );
481
482 /* check speed */
483 if( (fabsf(v3_dot( ve, grind.dir ))>=k_grind_axel_min_vel) &&
484 (a >= a_min) &&
485 (fabsf(grind.dir[1]) < 0.70710678118654752f))
486 {
487 grind_located = 1;
488 grind_located_gravity = inf->gravity;
489 }
490 }
491 }
492
493 if( trace_world->rendering_gate ){
494 ent_gate *gate = trace_world->rendering_gate;
495 if( gate_intersect( gate, co1, co0 ) ){
496 m4x3_mulv( gate->transport, co0, co0 );
497 m4x3_mulv( gate->transport, co1, co1 );
498 m3x3_mulv( gate->transport, launch_v, launch_v);
499 m4x3_mulv( gate->transport, launch_co, launch_co );
500 m3x3_mul( gate->transport, basis, basis );
501
502 if( gate->type == k_gate_type_nonlocel ){
503 trace_world = &world_static.worlds[ gate->target ];
504 }
505 }
506 }
507
508 float t1;
509 v3f n;
510
511 float scan_radius = k_board_radius;
512 scan_radius *= vg_clampf( t, 0.02f, 1.0f );
513
514 int idx = spherecast_world(trace_world, co0, co1, scan_radius, &t1, n);
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 if( idx != -1 ){
603 goto invalidated_grind;
604 }
605
606 v3_copy( co1, co0 );
607 }
608
609 v3_copy( grind.n, jump->n );
610
611 /* determine score */
612 v3f ve;
613 v3_copy( jump->v, ve );
614 v3_muladds( ve, player->basis[1], -jump->gravity*jump->land_dist, ve );
615 jump->score = -v3_dot( ve, grind.n ) * 0.9f;
616
617 s->possible_jumps[ s->possible_jump_count ++ ] = *jump;
618
619 continue;
620 invalidated_grind:;
621 }
622 }
623
624
625 float score_min = INFINITY,
626 score_max = -INFINITY;
627
628 jump_info *best = NULL;
629
630 for( int i=0; i<s->possible_jump_count; i ++ ){
631 jump_info *jump = &s->possible_jumps[i];
632
633 if( jump->score < score_min )
634 best = jump;
635
636 score_min = vg_minf( score_min, jump->score );
637 score_max = vg_maxf( score_max, jump->score );
638 }
639
640 for( int i=0; i<s->possible_jump_count; i ++ ){
641 jump_info *jump = &s->possible_jumps[i];
642 float s = jump->score;
643
644 s -= score_min;
645 s /= (score_max-score_min);
646 s = 1.0f - s;
647
648 jump->score = s;
649 jump->colour = s * 255.0f;
650
651 if( jump == best )
652 jump->colour <<= 16;
653 else if( jump->type == k_prediction_land )
654 jump->colour <<= 8;
655
656 jump->colour |= 0xff000000;
657 }
658
659 if( best ){
660 v3_copy( best->n, s->state.land_normal );
661 v3_copy( best->v, player->rb.v );
662 s->state.land_dist = best->land_dist;
663
664 s->state.gravity_bias = best->gravity;
665
666 if( best->type == k_prediction_grind ){
667 s->state.activity = k_skate_activity_air_to_grind;
668 }
669
670 v2f steer;
671 joystick_state( k_srjoystick_steer, steer );
672 v2_normalize_clamp( steer );
673
674 if( (fabsf(steer[1]) > 0.5f) && (s->state.land_dist >= 1.5f) ){
675 s->state.flip_rate = (1.0f/s->state.land_dist) * vg_signf(steer[1]) *
676 s->state.reverse ;
677 s->state.flip_time = 0.0f;
678 v3_copy( player->rb.to_world[0], s->state.flip_axis );
679 }
680 else{
681 s->state.flip_rate = 0.0f;
682 v3_zero( s->state.flip_axis );
683 }
684 }
685 else{
686 v3_copy( player->basis[1], s->state.land_normal );
687 }
688 }
689
690 /*
691 *
692 * Varius physics models
693 * ------------------------------------------------
694 */
695
696 /*
697 * Air control, no real physics
698 */
699 VG_STATIC void skate_apply_air_model( player_instance *player )
700 {
701 struct player_skate *s = &player->_skate;
702
703 if( s->state.activity_prev > k_skate_activity_air_to_grind )
704 player__approximate_best_trajectory( player );
705
706 float angle = v3_dot( player->rb.to_world[1], s->state.land_normal );
707 angle = vg_clampf( angle, -1.0f, 1.0f );
708 v3f axis;
709 v3_cross( player->rb.to_world[1], s->state.land_normal, axis );
710
711 v4f correction;
712 q_axis_angle( correction, axis,
713 acosf(angle)*2.0f*VG_TIMESTEP_FIXED );
714 q_mul( correction, player->rb.q, player->rb.q );
715 }
716
717 VG_STATIC enum trick_type player_skate_trick_input( player_instance *player );
718 VG_STATIC void skate_apply_trick_model( player_instance *player )
719 {
720 struct player_skate *s = &player->_skate;
721
722 v3f Fd, Fs, F;
723 v3f strength = { 3.7f, 3.6f, 8.0f };
724
725 v3_muls( s->state.trick_residualv, -4.0f , Fd );
726 v3_muls( s->state.trick_residuald, -10.0f, Fs );
727 v3_add( Fd, Fs, F );
728 v3_mul( strength, F, F );
729
730 v3_muladds( s->state.trick_residualv, F, k_rb_delta,
731 s->state.trick_residualv );
732 v3_muladds( s->state.trick_residuald, s->state.trick_residualv,
733 k_rb_delta, s->state.trick_residuald );
734
735 if( s->state.activity <= k_skate_activity_air_to_grind ){
736 if( v3_length2( s->state.trick_vel ) < 0.0001f )
737 return;
738
739 int carry_on = player_skate_trick_input( player );
740
741 /* we assume velocities share a common divisor, in which case the
742 * interval is the minimum value (if not zero) */
743
744 float min_rate = 99999.0f;
745
746 for( int i=0; i<3; i++ ){
747 float v = s->state.trick_vel[i];
748 if( (v > 0.0f) && (v < min_rate) )
749 min_rate = v;
750 }
751
752 float interval = 1.0f / min_rate,
753 current = floorf( s->state.trick_time / interval ),
754 next_end = (current+1.0f) * interval;
755
756
757 /* integrate trick velocities */
758 v3_muladds( s->state.trick_euler, s->state.trick_vel, k_rb_delta,
759 s->state.trick_euler );
760
761 if( !carry_on && (s->state.trick_time + k_rb_delta >= next_end) ){
762 s->state.trick_time = 0.0f;
763 s->state.trick_euler[0] = roundf( s->state.trick_euler[0] );
764 s->state.trick_euler[1] = roundf( s->state.trick_euler[1] );
765 s->state.trick_euler[2] = roundf( s->state.trick_euler[2] );
766 v3_copy( s->state.trick_vel, s->state.trick_residualv );
767 v3_zero( s->state.trick_vel );
768 }
769
770 s->state.trick_time += k_rb_delta;
771 }
772 else{
773 if( (v3_length2(s->state.trick_vel) >= 0.0001f ) &&
774 s->state.trick_time > 0.2f)
775 {
776 player__skate_kill_audio( player );
777 player__dead_transition( player );
778 }
779
780 s->state.trick_euler[0] = roundf( s->state.trick_euler[0] );
781 s->state.trick_euler[1] = roundf( s->state.trick_euler[1] );
782 s->state.trick_euler[2] = roundf( s->state.trick_euler[2] );
783 s->state.trick_time = 0.0f;
784 v3_zero( s->state.trick_vel );
785 }
786 }
787
788 VG_STATIC void skate_apply_grab_model( player_instance *player )
789 {
790 struct player_skate *s = &player->_skate;
791
792 float grabt = axis_state( k_sraxis_grab );
793
794 if( grabt > 0.5f ){
795 v2_muladds( s->state.grab_mouse_delta, vg.mouse_delta, 0.02f,
796 s->state.grab_mouse_delta );
797
798 v2_normalize_clamp( s->state.grab_mouse_delta );
799 }
800 else
801 v2_zero( s->state.grab_mouse_delta );
802
803 s->state.grabbing = vg_lerpf( s->state.grabbing, grabt, 8.4f*k_rb_delta );
804 }
805
806 VG_STATIC void skate_apply_steering_model( player_instance *player )
807 {
808 struct player_skate *s = &player->_skate;
809
810 v2f jsteer;
811 joystick_state( k_srjoystick_steer, jsteer );
812
813 /* Steering */
814 float steer = jsteer[0],
815 grab = axis_state( k_sraxis_grab );
816
817 steer = vg_signf( steer ) * steer*steer * k_steer_ground;
818
819 v3f steer_axis;
820 v3_muls( player->rb.to_world[1], -vg_signf( steer ), steer_axis );
821
822 float rate = 26.0f,
823 top = 1.0f;
824
825 if( s->state.activity <= k_skate_activity_air_to_grind ){
826 rate = 6.0f * fabsf(steer);
827 top = 1.5f;
828 }
829 else{
830 /* rotate slower when grabbing on ground */
831 steer *= (1.0f-(s->state.jump_charge+grab)*0.4f);
832
833 if( s->state.activity == k_skate_activity_grind_5050 ){
834 rate = 0.0f;
835 top = 0.0f;
836 }
837
838 else if( s->state.activity >= k_skate_activity_grind_any ){
839 rate *= fabsf(steer);
840
841 float a = 0.8f * -steer * k_rb_delta;
842
843 v4f q;
844 q_axis_angle( q, player->rb.to_world[1], a );
845 q_mulv( q, s->grind_vec, s->grind_vec );
846
847 v3_normalize( s->grind_vec );
848 }
849
850 else if( s->state.manual_direction ){
851 rate = 35.0f;
852 top = 1.5f;
853 }
854
855 if( grab < 0.5f ){
856 top *= 1.0f+v3_length( s->state.throw_v )*k_mmthrow_steer;
857 }
858 }
859
860 float current = v3_dot( player->rb.to_world[1], player->rb.w ),
861 addspeed = (steer * -top) - current,
862 maxaccel = rate * k_rb_delta,
863 accel = vg_clampf( addspeed, -maxaccel, maxaccel );
864
865 v3_muladds( player->rb.w, player->rb.to_world[1], accel, player->rb.w );
866 }
867
868 /*
869 * Computes friction and surface interface model
870 */
871 VG_STATIC void skate_apply_friction_model( player_instance *player )
872 {
873 struct player_skate *s = &player->_skate;
874
875 /*
876 * Computing localized friction forces for controlling the character
877 * Friction across X is significantly more than Z
878 */
879
880 v3f vel;
881 m3x3_mulv( player->rb.to_local, player->rb.v, vel );
882 float slip = 0.0f;
883
884 if( fabsf(vel[2]) > 0.01f )
885 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
886
887 if( fabsf( slip ) > 1.2f )
888 slip = vg_signf( slip ) * 1.2f;
889
890 s->state.slip = slip;
891 s->state.reverse = -vg_signf(vel[2]);
892
893 vel[0] += vg_cfrictf( vel[0], k_friction_lat * k_rb_delta );
894 vel[2] += vg_cfrictf( vel[2], k_friction_resistance * k_rb_delta );
895
896 /* Pushing additive force */
897
898 if( !button_press( k_srbind_jump ) ){
899 if( button_press( k_srbind_push ) || (vg.time-s->state.start_push<0.75) )
900 {
901 if( (vg.time - s->state.cur_push) > 0.25 )
902 s->state.start_push = vg.time;
903
904 s->state.cur_push = vg.time;
905
906 double push_time = vg.time - s->state.start_push;
907
908 float cycle_time = push_time*k_push_cycle_rate,
909 accel = k_push_accel * (sinf(cycle_time)*0.5f+0.5f),
910 amt = accel * VG_TIMESTEP_FIXED,
911 current = v3_length( vel ),
912 new_vel = vg_minf( current + amt, k_max_push_speed ),
913 delta = new_vel - vg_minf( current, k_max_push_speed );
914
915 vel[2] += delta * -s->state.reverse;
916 }
917 }
918
919 /* Send back to velocity */
920 m3x3_mulv( player->rb.to_world, vel, player->rb.v );
921 }
922
923 VG_STATIC void skate_apply_jump_model( player_instance *player )
924 {
925 struct player_skate *s = &player->_skate;
926 int charging_jump_prev = s->state.charging_jump;
927 s->state.charging_jump = button_press( k_srbind_jump );
928
929 /* Cannot charge this in air */
930 if( s->state.activity <= k_skate_activity_air_to_grind ){
931 s->state.charging_jump = 0;
932 return;
933 }
934
935 if( s->state.charging_jump ){
936 s->state.jump_charge += k_rb_delta * k_jump_charge_speed;
937
938 if( !charging_jump_prev )
939 s->state.jump_dir = s->state.reverse>0.0f? 1: 0;
940 }
941 else{
942 s->state.jump_charge -= k_jump_charge_speed * k_rb_delta;
943 }
944
945 s->state.jump_charge = vg_clampf( s->state.jump_charge, 0.0f, 1.0f );
946
947 /* player let go after charging past 0.2: trigger jump */
948 if( (!s->state.charging_jump) && (s->state.jump_charge > 0.2f) ){
949 v3f jumpdir;
950
951 /* Launch more up if alignment is up else improve velocity */
952 float aup = v3_dot( player->basis[1], player->rb.to_world[1] ),
953 mod = 0.5f,
954 dir = mod + fabsf(aup)*(1.0f-mod);
955
956 if( s->state.activity == k_skate_activity_ground ){
957 v3_copy( player->rb.v, jumpdir );
958 v3_normalize( jumpdir );
959 v3_muls( jumpdir, 1.0f-dir, jumpdir );
960 v3_muladds( jumpdir, player->rb.to_world[1], dir, jumpdir );
961 v3_normalize( jumpdir );
962 }else{
963 v3_copy( s->state.up_dir, jumpdir );
964 s->state.grind_cooldown = 30;
965 s->state.activity = k_skate_activity_ground;
966
967 v2f steer;
968 joystick_state( k_srjoystick_steer, steer );
969
970 float tilt = steer[0] * 0.3f;
971 tilt *= vg_signf(v3_dot( player->rb.v, s->grind_dir ));
972
973 v4f qtilt;
974 q_axis_angle( qtilt, s->grind_dir, tilt );
975 q_mulv( qtilt, jumpdir, jumpdir );
976 }
977 s->state.surface_cooldown = 10;
978
979 float force = k_jump_force*s->state.jump_charge;
980 v3_muladds( player->rb.v, jumpdir, force, player->rb.v );
981 s->state.jump_charge = 0.0f;
982 s->state.jump_time = vg.time;
983
984 audio_lock();
985 audio_oneshot_3d( &audio_jumps[vg_randu32()%2], player->rb.co,40.0f,1.0f);
986 audio_unlock();
987 }
988 }
989
990 VG_STATIC void skate_apply_pump_model( player_instance *player )
991 {
992 struct player_skate *s = &player->_skate;
993
994 if( s->state.activity != k_skate_activity_ground ){
995 v3_zero( s->state.throw_v );
996 return;
997 }
998
999 /* Throw / collect routine
1000 */
1001 if( axis_state( k_sraxis_grab ) > 0.5f ){
1002 if( s->state.activity == k_skate_activity_ground ){
1003 /* Throw */
1004 v3_muls( player->rb.to_world[1], k_mmthrow_scale, s->state.throw_v );
1005 }
1006 }
1007 else{
1008 /* Collect */
1009 float doty = v3_dot( player->rb.to_world[1], s->state.throw_v );
1010
1011 v3f Fl, Fv;
1012 v3_muladds( s->state.throw_v, player->rb.to_world[1], -doty, Fl);
1013
1014 if( s->state.activity == k_skate_activity_ground ){
1015 if( v3_length2(player->rb.v)<(20.0f*20.0f) )
1016 v3_muladds( player->rb.v, Fl, k_mmcollect_lat, player->rb.v );
1017 v3_muladds( s->state.throw_v, Fl, -k_mmcollect_lat, s->state.throw_v );
1018 }
1019
1020 v3_muls( player->rb.to_world[1], -doty, Fv );
1021 v3_muladds( player->rb.v, Fv, k_mmcollect_vert, player->rb.v );
1022 v3_muladds( s->state.throw_v, Fv, k_mmcollect_vert, s->state.throw_v );
1023 }
1024
1025 /* Decay */
1026 if( v3_length2( s->state.throw_v ) > 0.0001f ){
1027 v3f dir;
1028 v3_copy( s->state.throw_v, dir );
1029 v3_normalize( dir );
1030
1031 float max = v3_dot( dir, s->state.throw_v ),
1032 amt = vg_minf( k_mmdecay * k_rb_delta, max );
1033 v3_muladds( s->state.throw_v, dir, -amt, s->state.throw_v );
1034 }
1035 }
1036
1037 VG_STATIC void skate_apply_cog_model( player_instance *player )
1038 {
1039 struct player_skate *s = &player->_skate;
1040
1041 v3f ideal_cog, ideal_diff, ideal_dir;
1042 v3_copy( s->state.up_dir, ideal_dir );
1043 v3_normalize( ideal_dir );
1044
1045 float grab = axis_state( k_sraxis_grab );
1046 v3_muladds( player->rb.co, ideal_dir, 1.0f-grab, ideal_cog );
1047 v3_sub( ideal_cog, s->state.cog, ideal_diff );
1048
1049 /* Apply velocities */
1050 v3f rv;
1051 v3_sub( player->rb.v, s->state.cog_v, rv );
1052
1053 v3f F;
1054 v3_muls( ideal_diff, -k_cog_spring * k_rb_rate, F );
1055 v3_muladds( F, rv, -k_cog_damp * k_rb_rate, F );
1056
1057 float ra = k_cog_mass_ratio,
1058 rb = 1.0f-k_cog_mass_ratio;
1059
1060 /* Apply forces & intergrate */
1061 v3_muladds( s->state.cog_v, F, -rb, s->state.cog_v );
1062 v3_muladds( s->state.cog_v, player->basis[1], -9.8f * k_rb_delta,
1063 s->state.cog_v );
1064
1065 v3_muladds( s->state.cog, s->state.cog_v, k_rb_delta, s->state.cog );
1066 }
1067
1068
1069 VG_STATIC void skate_integrate( player_instance *player )
1070 {
1071 struct player_skate *s = &player->_skate;
1072
1073 float decay_rate_x = 1.0f - (k_rb_delta * 3.0f),
1074 decay_rate_z = decay_rate_x,
1075 decay_rate_y = 1.0f;
1076
1077 if( s->state.activity >= k_skate_activity_grind_any ){
1078 #if 0
1079 decay_rate = 1.0f-vg_lerpf( 3.0f, 20.0f, s->grind_strength ) * k_rb_delta;
1080 decay_rate_y = decay_rate;
1081 #endif
1082 decay_rate_x = 1.0f-(16.0f*k_rb_delta);
1083 decay_rate_y = 1.0f-(10.0f*k_rb_delta);
1084 decay_rate_z = 1.0f-(40.0f*k_rb_delta);
1085 }
1086
1087 float wx = v3_dot( player->rb.w, player->rb.to_world[0] ) * decay_rate_x,
1088 wy = v3_dot( player->rb.w, player->rb.to_world[1] ) * decay_rate_y,
1089 wz = v3_dot( player->rb.w, player->rb.to_world[2] ) * decay_rate_z;
1090
1091 v3_muls( player->rb.to_world[0], wx, player->rb.w );
1092 v3_muladds( player->rb.w, player->rb.to_world[1], wy, player->rb.w );
1093 v3_muladds( player->rb.w, player->rb.to_world[2], wz, player->rb.w );
1094
1095 s->state.flip_time += s->state.flip_rate * k_rb_delta;
1096 rb_update_transform( &player->rb );
1097 }
1098
1099 VG_STATIC enum trick_type player_skate_trick_input( player_instance *player ){
1100 return (button_press( k_srbind_trick0 ) ) |
1101 (button_press( k_srbind_trick1 ) << 1) |
1102 (button_press( k_srbind_trick2 ) << 1) |
1103 (button_press( k_srbind_trick2 ) );
1104 }
1105
1106 VG_STATIC void player__skate_pre_update( player_instance *player ){
1107 struct player_skate *s = &player->_skate;
1108
1109 if( button_down( k_srbind_use ) ){
1110 player->subsystem = k_player_subsystem_walk;
1111
1112 v3f angles;
1113 v3_copy( player->cam.angles, player->angles );
1114 player->angles[2] = 0.0f;
1115
1116 player__begin_holdout( player );
1117 player__skate_kill_audio( player );
1118 player__walk_transition( player );
1119 return;
1120 }
1121
1122 enum trick_type trick = k_trick_type_none;
1123 if( (s->state.activity <= k_skate_activity_air_to_grind) &&
1124 (trick = player_skate_trick_input( player )) )
1125 {
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
1145 VG_STATIC void player__skate_post_update( player_instance *player ){
1146 struct player_skate *s = &player->_skate;
1147
1148 for( int i=0; i<s->possible_jump_count; i++ ){
1149 jump_info *jump = &s->possible_jumps[i];
1150
1151 if( jump->log_length == 0 ){
1152 vg_fatal_error( "assert: jump->log_length == 0\n" );
1153 }
1154
1155 for( int j=0; j<jump->log_length - 1; j ++ ){
1156 float brightness = jump->score*jump->score*jump->score;
1157 v3f p1;
1158 v3_lerp( jump->log[j], jump->log[j+1], brightness, p1 );
1159 vg_line( jump->log[j], p1, jump->colour );
1160 }
1161
1162 vg_line_cross( jump->log[jump->log_length-1], jump->colour, 0.25f );
1163
1164 v3f p1;
1165 v3_add( jump->log[jump->log_length-1], jump->n, p1 );
1166 vg_line( jump->log[jump->log_length-1], p1, 0xffffffff );
1167
1168 vg_line_point( jump->apex, 0.02f, 0xffffffff );
1169 }
1170
1171 audio_lock();
1172
1173 float air = s->state.activity <= k_skate_activity_air_to_grind? 1.0f: 0.0f,
1174 speed = v3_length( player->rb.v ),
1175 attn = vg_minf( 1.0f, speed*0.1f ),
1176 slide = vg_clampf( fabsf(s->state.slip), 0.0f, 1.0f );
1177
1178 if( s->state.activity >= k_skate_activity_grind_any ){
1179 slide = 0.0f;
1180 }
1181
1182 f32 gate = skaterift.time_rate,
1183 vol_main = sqrtf( (1.0f-air)*attn*(1.0f-slide) * 0.4f ) * gate,
1184 vol_air = sqrtf( air *attn * 0.5f ) * gate,
1185 vol_slide = sqrtf( (1.0f-air)*attn*slide * 0.25f ) * gate;
1186
1187 const u32 flags = AUDIO_FLAG_SPACIAL_3D|AUDIO_FLAG_LOOP;
1188
1189 if( !s->aud_air ){
1190 s->aud_air = audio_get_first_idle_channel();
1191 if( s->aud_air )
1192 audio_channel_init( s->aud_air, &audio_board[1], flags );
1193 }
1194
1195 if( !s->aud_slide ){
1196 s->aud_slide = audio_get_first_idle_channel();
1197 if( s->aud_slide )
1198 audio_channel_init( s->aud_slide, &audio_board[2], flags );
1199 }
1200
1201
1202 /* brrrrrrrrrrrt sound for tiles and stuff
1203 * --------------------------------------------------------*/
1204 float sidechain_amt = 0.0f,
1205 hz = vg_maxf( speed * 2.0f, 2.0f );
1206
1207 if( (s->surface == k_surface_prop_tiles) &&
1208 (s->state.activity < k_skate_activity_grind_any) )
1209 sidechain_amt = 1.0f;
1210 else
1211 sidechain_amt = 0.0f;
1212
1213 audio_set_lfo_frequency( 0, hz );
1214 audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar,
1215 vg_lerpf( 250.0f, 80.0f, attn ) );
1216
1217 if( s->sample_change_cooldown > 0.0f ){
1218 s->sample_change_cooldown -= vg.time_frame_delta;
1219 }
1220 else{
1221 int sample_type = k_skate_sample_concrete;
1222
1223 if( s->state.activity == k_skate_activity_grind_5050 ){
1224 if( s->surface == k_surface_prop_metal )
1225 sample_type = k_skate_sample_metal_scrape_generic;
1226 else
1227 sample_type = k_skate_sample_concrete_scrape_metal;
1228 }
1229 else if( (s->state.activity == k_skate_activity_grind_back50) ||
1230 (s->state.activity == k_skate_activity_grind_front50) )
1231 {
1232 if( s->surface == k_surface_prop_metal ){
1233 sample_type = k_skate_sample_metal_scrape_generic;
1234 }
1235 else{
1236 float a = v3_dot( player->rb.to_world[2], s->grind_dir );
1237 if( fabsf(a) > 0.70710678118654752f )
1238 sample_type = k_skate_sample_concrete_scrape_wood;
1239 else
1240 sample_type = k_skate_sample_concrete_scrape_metal;
1241 }
1242 }
1243 else if( s->state.activity == k_skate_activity_grind_boardslide ){
1244 if( s->surface == k_surface_prop_metal )
1245 sample_type = k_skate_sample_metal_scrape_generic;
1246 else
1247 sample_type = k_skate_sample_concrete_scrape_wood;
1248 }
1249
1250 audio_clip *relevant_samples[] = {
1251 &audio_board[0],
1252 &audio_board[0],
1253 &audio_board[7],
1254 &audio_board[6],
1255 &audio_board[5]
1256 };
1257
1258 if( (s->main_sample_type != sample_type) || (!s->aud_main) ){
1259 s->aud_main =
1260 audio_channel_crossfade( s->aud_main, relevant_samples[sample_type],
1261 0.06f, flags );
1262 s->sample_change_cooldown = 0.1f;
1263 s->main_sample_type = sample_type;
1264 }
1265 }
1266
1267 if( s->aud_main ){
1268 s->aud_main->colour = 0x00103efe;
1269 audio_channel_set_spacial( s->aud_main, player->rb.co, 40.0f );
1270 //audio_channel_slope_volume( s->aud_main, 0.05f, vol_main );
1271 audio_channel_edit_volume( s->aud_main, vol_main, 1 );
1272 audio_channel_sidechain_lfo( s->aud_main, 0, sidechain_amt );
1273
1274 float rate = 1.0f + (attn-0.5f)*0.2f;
1275 audio_channel_set_sampling_rate( s->aud_main, rate );
1276 }
1277
1278 if( s->aud_slide ){
1279 s->aud_slide->colour = 0x00103efe;
1280 audio_channel_set_spacial( s->aud_slide, player->rb.co, 40.0f );
1281 //audio_channel_slope_volume( s->aud_slide, 0.05f, vol_slide );
1282 audio_channel_edit_volume( s->aud_slide, vol_slide, 1 );
1283 audio_channel_sidechain_lfo( s->aud_slide, 0, sidechain_amt );
1284 }
1285
1286 if( s->aud_air ){
1287 s->aud_air->colour = 0x00103efe;
1288 audio_channel_set_spacial( s->aud_air, player->rb.co, 40.0f );
1289 //audio_channel_slope_volume( s->aud_air, 0.05f, vol_air );
1290 audio_channel_edit_volume( s->aud_air, vol_air, 1 );
1291 }
1292
1293 audio_unlock();
1294 }
1295
1296 /*
1297 * truck alignment model at ra(local)
1298 * returns 1 if valid surface:
1299 * surface_normal will be filled out with an averaged normal vector
1300 * axel_dir will be the direction from left to right wheels
1301 *
1302 * returns 0 if no good surface found
1303 */
1304 VG_STATIC
1305 int skate_compute_surface_alignment( player_instance *player,
1306 v3f ra, u32 colour,
1307 v3f surface_normal, v3f axel_dir ){
1308 struct player_skate *s = &player->_skate;
1309 world_instance *world = world_current_instance();
1310
1311 v3f truck, left, right;
1312 m4x3_mulv( player->rb.to_world, ra, truck );
1313
1314 v3_muladds( truck, player->rb.to_world[0], -k_board_width, left );
1315 v3_muladds( truck, player->rb.to_world[0], k_board_width, right );
1316 vg_line( left, right, colour );
1317
1318 float k_max_truck_flex = VG_PIf * 0.25f;
1319
1320 ray_hit ray_l, ray_r;
1321
1322 v3f dir;
1323 v3_muls( player->rb.to_world[1], -1.0f, dir );
1324
1325 int res_l = 0, res_r = 0;
1326
1327 for( int i=0; i<8; i++ ){
1328 float t = 1.0f - (float)i * (1.0f/8.0f);
1329 v3_muladds( truck, player->rb.to_world[0], -k_board_radius*t, left );
1330 v3_muladds( left, player->rb.to_world[1], k_board_radius, left );
1331 ray_l.dist = 2.1f * k_board_radius;
1332
1333 res_l = ray_world( world, left, dir, &ray_l );
1334
1335 if( res_l )
1336 break;
1337 }
1338
1339 for( int i=0; i<8; i++ ){
1340 float t = 1.0f - (float)i * (1.0f/8.0f);
1341 v3_muladds( truck, player->rb.to_world[0], k_board_radius*t, right );
1342 v3_muladds( right, player->rb.to_world[1], k_board_radius, right );
1343 ray_r.dist = 2.1f * k_board_radius;
1344
1345 res_r = ray_world( world, right, dir, &ray_r );
1346
1347 if( res_r )
1348 break;
1349 }
1350
1351 v3f v0;
1352 v3f midpoint;
1353 v3f tangent_average;
1354 v3_muladds( truck, player->rb.to_world[1], -k_board_radius, midpoint );
1355 v3_zero( tangent_average );
1356
1357 if( res_l || res_r ){
1358 v3f p0, p1, t;
1359 v3_copy( midpoint, p0 );
1360 v3_copy( midpoint, p1 );
1361
1362 if( res_l ){
1363 v3_copy( ray_l.pos, p0 );
1364 v3_cross( ray_l.normal, player->rb.to_world[0], t );
1365 v3_add( t, tangent_average, tangent_average );
1366 }
1367 if( res_r ){
1368 v3_copy( ray_r.pos, p1 );
1369 v3_cross( ray_r.normal, player->rb.to_world[0], t );
1370 v3_add( t, tangent_average, tangent_average );
1371 }
1372
1373 v3_sub( p1, p0, v0 );
1374 v3_normalize( v0 );
1375 }
1376 else{
1377 /* fallback: use the closes point to the trucks */
1378 v3f closest;
1379 int idx = bh_closest_point( world->geo_bh, midpoint, closest, 0.1f );
1380
1381 if( idx != -1 ){
1382 u32 *tri = &world->scene_geo.arrindices[ idx * 3 ];
1383 v3f verts[3];
1384
1385 for( int j=0; j<3; j++ )
1386 v3_copy( world->scene_geo.arrvertices[ tri[j] ].co, verts[j] );
1387
1388 v3f vert0, vert1, n;
1389 v3_sub( verts[1], verts[0], vert0 );
1390 v3_sub( verts[2], verts[0], vert1 );
1391 v3_cross( vert0, vert1, n );
1392 v3_normalize( n );
1393
1394 if( v3_dot( n, player->rb.to_world[1] ) < 0.3f )
1395 return 0;
1396
1397 v3_cross( n, player->rb.to_world[2], v0 );
1398 v3_muladds( v0, player->rb.to_world[2],
1399 -v3_dot( player->rb.to_world[2], v0 ), v0 );
1400 v3_normalize( v0 );
1401
1402 v3f t;
1403 v3_cross( n, player->rb.to_world[0], t );
1404 v3_add( t, tangent_average, tangent_average );
1405 }
1406 else
1407 return 0;
1408 }
1409
1410 v3_muladds( truck, v0, k_board_width, right );
1411 v3_muladds( truck, v0, -k_board_width, left );
1412
1413 vg_line( left, right, VG__WHITE );
1414
1415 v3_normalize( tangent_average );
1416 v3_cross( v0, tangent_average, surface_normal );
1417 v3_copy( v0, axel_dir );
1418
1419 return 1;
1420 }
1421
1422 VG_STATIC void skate_weight_distribute( player_instance *player ){
1423 struct player_skate *s = &player->_skate;
1424 v3_zero( s->weight_distribution );
1425
1426 int reverse_dir = v3_dot( player->rb.to_world[2], player->rb.v ) < 0.0f?1:-1;
1427
1428 v2f steer;
1429 joystick_state( k_srjoystick_steer, steer );
1430
1431 if( s->state.manual_direction == 0 ){
1432 if( (steer[1] > 0.7f) && (s->state.activity == k_skate_activity_ground) &&
1433 (s->state.jump_charge <= 0.01f) )
1434 s->state.manual_direction = reverse_dir;
1435 }
1436 else{
1437 if( steer[1] < 0.1f ){
1438 s->state.manual_direction = 0;
1439 }
1440 else{
1441 if( reverse_dir != s->state.manual_direction ){
1442 return;
1443 }
1444 }
1445 }
1446
1447 if( s->state.manual_direction ){
1448 float amt = vg_minf( steer[1] * 8.0f, 1.0f );
1449 s->weight_distribution[2] = k_board_length * amt *
1450 (float)s->state.manual_direction;
1451 }
1452
1453 if( s->state.manual_direction ){
1454 v3f plane_z;
1455
1456 m3x3_mulv( player->rb.to_world, s->weight_distribution, plane_z );
1457 v3_negate( plane_z, plane_z );
1458
1459 v3_muladds( plane_z, s->surface_picture,
1460 -v3_dot( plane_z, s->surface_picture ), plane_z );
1461 v3_normalize( plane_z );
1462
1463 v3_muladds( plane_z, s->surface_picture, 0.3f, plane_z );
1464 v3_normalize( plane_z );
1465
1466 v3f p1;
1467 v3_muladds( player->rb.co, plane_z, 1.5f, p1 );
1468 vg_line( player->rb.co, p1, VG__GREEN );
1469
1470 v3f refdir;
1471 v3_muls( player->rb.to_world[2], -(float)s->state.manual_direction,
1472 refdir );
1473
1474 rb_effect_spring_target_vector( &player->rb, refdir, plane_z,
1475 k_manul_spring, k_manul_dampener,
1476 s->substep_delta );
1477 }
1478 }
1479
1480 VG_STATIC void skate_adjust_up_direction( player_instance *player ){
1481 struct player_skate *s = &player->_skate;
1482
1483 if( s->state.activity == k_skate_activity_ground ){
1484 v3f target;
1485 v3_copy( s->surface_picture, target );
1486
1487 target[1] += 2.0f * s->surface_picture[1];
1488 v3_normalize( target );
1489
1490 v3_lerp( s->state.up_dir, target,
1491 8.0f * s->substep_delta, s->state.up_dir );
1492 }
1493 else if( s->state.activity <= k_skate_activity_air_to_grind ){
1494 v3_lerp( s->state.up_dir, player->rb.to_world[1],
1495 8.0f * s->substep_delta, s->state.up_dir );
1496 }
1497 else{
1498 v3_lerp( s->state.up_dir, player->basis[1],
1499 12.0f * s->substep_delta, s->state.up_dir );
1500 }
1501 }
1502
1503 VG_STATIC int skate_point_visible( v3f origin, v3f target ){
1504 v3f dir;
1505 v3_sub( target, origin, dir );
1506
1507 ray_hit ray;
1508 ray.dist = v3_length( dir );
1509 v3_muls( dir, 1.0f/ray.dist, dir );
1510 ray.dist -= 0.025f;
1511
1512 if( ray_world( world_current_instance(), origin, dir, &ray ) )
1513 return 0;
1514
1515 return 1;
1516 }
1517
1518 VG_STATIC void skate_grind_orient( struct grind_info *inf, m3x3f mtx ){
1519 v3_copy( inf->dir, mtx[0] );
1520 v3_copy( inf->n, mtx[1] );
1521 v3_cross( mtx[0], mtx[1], mtx[2] );
1522 }
1523
1524 VG_STATIC void skate_grind_friction( player_instance *player,
1525 struct grind_info *inf, float strength ){
1526 v3f v2;
1527 v3_muladds( player->rb.to_world[2], inf->n,
1528 -v3_dot( player->rb.to_world[2], inf->n ), v2 );
1529
1530 float a = 1.0f-fabsf( v3_dot( v2, inf->dir ) ),
1531 dir = vg_signf( v3_dot( player->rb.v, inf->dir ) ),
1532 F = a * -dir * k_grind_max_friction;
1533
1534 v3_muladds( player->rb.v, inf->dir, F*k_rb_delta*strength, player->rb.v );
1535 }
1536
1537 VG_STATIC void skate_grind_decay( player_instance *player,
1538 struct grind_info *inf, float strength ){
1539 m3x3f mtx, mtx_inv;
1540 skate_grind_orient( inf, mtx );
1541 m3x3_transpose( mtx, mtx_inv );
1542
1543 v3f v_grind;
1544 m3x3_mulv( mtx_inv, player->rb.v, v_grind );
1545
1546 float decay = 1.0f - ( k_rb_delta * k_grind_decayxy * strength );
1547 v3_mul( v_grind, (v3f){ 1.0f, decay, decay }, v_grind );
1548 m3x3_mulv( mtx, v_grind, player->rb.v );
1549 }
1550
1551 VG_STATIC void skate_grind_truck_apply( player_instance *player,
1552 float sign, struct grind_info *inf,
1553 float strength ){
1554 struct player_skate *s = &player->_skate;
1555
1556 /* REFACTOR */
1557 v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
1558 v3f raw, wsp;
1559 m3x3_mulv( player->rb.to_world, ra, raw );
1560 v3_add( player->rb.co, raw, wsp );
1561
1562 v3_copy( ra, s->weight_distribution );
1563
1564 v3f delta;
1565 v3_sub( inf->co, wsp, delta );
1566
1567 /* spring force */
1568 v3_muladds( player->rb.v, delta, k_spring_force*strength*k_rb_delta,
1569 player->rb.v );
1570
1571 skate_grind_decay( player, inf, strength );
1572 skate_grind_friction( player, inf, strength );
1573
1574 /* yeah yeah yeah yeah */
1575 v3f raw_nplane, axis;
1576 v3_muladds( raw, inf->n, -v3_dot( inf->n, raw ), raw_nplane );
1577 v3_cross( raw_nplane, inf->n, axis );
1578 v3_normalize( axis );
1579
1580 /* orientation */
1581 m3x3f mtx;
1582 skate_grind_orient( inf, mtx );
1583 v3f target_fwd, fwd, up, target_up;
1584 m3x3_mulv( mtx, s->grind_vec, target_fwd );
1585 v3_copy( raw_nplane, fwd );
1586 v3_copy( player->rb.to_world[1], up );
1587 v3_copy( inf->n, target_up );
1588
1589 v3_muladds( target_fwd, inf->n, -v3_dot(inf->n,target_fwd), target_fwd );
1590 v3_muladds( fwd, inf->n, -v3_dot(inf->n,fwd), fwd );
1591
1592 v3_normalize( target_fwd );
1593 v3_normalize( fwd );
1594
1595 v2f steer;
1596 joystick_state( k_srjoystick_steer, steer );
1597
1598 float way = steer[1] * vg_signf( v3_dot( raw_nplane, player->rb.v ) );
1599
1600 v4f q;
1601 q_axis_angle( q, axis, VG_PIf*0.125f * way );
1602 q_mulv( q, target_up, target_up );
1603 q_mulv( q, target_fwd, target_fwd );
1604
1605 rb_effect_spring_target_vector( &player->rb, up, target_up,
1606 k_grind_spring,
1607 k_grind_dampener,
1608 k_rb_delta );
1609
1610 rb_effect_spring_target_vector( &player->rb, fwd, target_fwd,
1611 k_grind_spring*strength,
1612 k_grind_dampener*strength,
1613 k_rb_delta );
1614
1615 vg_line_arrow( player->rb.co, target_up, 1.0f, VG__GREEN );
1616 vg_line_arrow( player->rb.co, fwd, 0.8f, VG__RED );
1617 vg_line_arrow( player->rb.co, target_fwd, 1.0f, VG__YELOW );
1618
1619 s->grind_strength = strength;
1620
1621 /* Fake contact */
1622 struct grind_limit *limit = &s->limits[ s->limit_count ++ ];
1623 m4x3_mulv( player->rb.to_local, wsp, limit->ra );
1624 m3x3_mulv( player->rb.to_local, inf->n, limit->n );
1625 limit->p = 0.0f;
1626
1627 v3_copy( inf->dir, s->grind_dir );
1628 }
1629
1630 VG_STATIC void skate_5050_apply( player_instance *player,
1631 struct grind_info *inf_front,
1632 struct grind_info *inf_back )
1633 {
1634 struct player_skate *s = &player->_skate;
1635 struct grind_info inf_avg;
1636
1637 v3_sub( inf_front->co, inf_back->co, inf_avg.dir );
1638 v3_muladds( inf_back->co, inf_avg.dir, 0.5f, inf_avg.co );
1639 v3_normalize( inf_avg.dir );
1640
1641 /* dont ask */
1642 v3_muls( inf_avg.dir, vg_signf(v3_dot(inf_avg.dir,player->rb.v)),
1643 inf_avg.dir );
1644
1645 v3f axis_front, axis_back, axis;
1646 v3_cross( inf_front->dir, inf_front->n, axis_front );
1647 v3_cross( inf_back->dir, inf_back->n, axis_back );
1648 v3_add( axis_front, axis_back, axis );
1649 v3_normalize( axis );
1650
1651 v3_cross( axis, inf_avg.dir, inf_avg.n );
1652 skate_grind_decay( player, &inf_avg, 1.0f );
1653
1654 v2f steer;
1655 joystick_state( k_srjoystick_steer, steer );
1656
1657 float way = steer[1] *
1658 vg_signf( v3_dot( player->rb.to_world[2], player->rb.v ) );
1659 v4f q;
1660 v3f up, target_up;
1661 v3_copy( player->rb.to_world[1], up );
1662 v3_copy( inf_avg.n, target_up );
1663 q_axis_angle( q, player->rb.to_world[0], VG_PIf*0.25f * -way );
1664 q_mulv( q, target_up, target_up );
1665
1666 v3_zero( s->weight_distribution );
1667 s->weight_distribution[2] = k_board_length * -way;
1668
1669 rb_effect_spring_target_vector( &player->rb, up, target_up,
1670 k_grind_spring,
1671 k_grind_dampener,
1672 k_rb_delta );
1673 vg_line_arrow( player->rb.co, up, 1.0f, VG__GREEN );
1674 vg_line_arrow( player->rb.co, target_up, 1.0f, VG__GREEN );
1675
1676 v3f fwd_nplane, dir_nplane;
1677 v3_muladds( player->rb.to_world[2], inf_avg.n,
1678 -v3_dot( player->rb.to_world[2], inf_avg.n ), fwd_nplane );
1679
1680 v3f dir;
1681 v3_muls( inf_avg.dir, v3_dot( fwd_nplane, inf_avg.dir ), dir );
1682 v3_muladds( dir, inf_avg.n, -v3_dot( dir, inf_avg.n ), dir_nplane );
1683
1684 v3_normalize( fwd_nplane );
1685 v3_normalize( dir_nplane );
1686
1687 rb_effect_spring_target_vector( &player->rb, fwd_nplane, dir_nplane,
1688 1000.0f,
1689 k_grind_dampener,
1690 k_rb_delta );
1691 vg_line_arrow( player->rb.co, fwd_nplane, 0.8f, VG__RED );
1692 vg_line_arrow( player->rb.co, dir_nplane, 0.8f, VG__RED );
1693
1694 v3f pos_front = { 0.0f, -k_board_radius, -1.0f * k_board_length },
1695 pos_back = { 0.0f, -k_board_radius, 1.0f * k_board_length },
1696 delta_front, delta_back, delta_total;
1697
1698 m4x3_mulv( player->rb.to_world, pos_front, pos_front );
1699 m4x3_mulv( player->rb.to_world, pos_back, pos_back );
1700
1701 v3_sub( inf_front->co, pos_front, delta_front );
1702 v3_sub( inf_back->co, pos_back, delta_back );
1703 v3_add( delta_front, delta_back, delta_total );
1704
1705 v3_muladds( player->rb.v, delta_total, 50.0f * k_rb_delta, player->rb.v );
1706
1707 /* Fake contact */
1708 struct grind_limit *limit = &s->limits[ s->limit_count ++ ];
1709 v3_zero( limit->ra );
1710 m3x3_mulv( player->rb.to_local, inf_avg.n, limit->n );
1711 limit->p = 0.0f;
1712
1713 v3_copy( inf_avg.dir, s->grind_dir );
1714 }
1715
1716 VG_STATIC int skate_grind_truck_renew( player_instance *player, float sign,
1717 struct grind_info *inf )
1718 {
1719 struct player_skate *s = &player->_skate;
1720
1721 v3f wheel_co = { 0.0f, 0.0f, sign * k_board_length },
1722 grind_co = { 0.0f, -k_board_radius, sign * k_board_length };
1723
1724 m4x3_mulv( player->rb.to_world, wheel_co, wheel_co );
1725 m4x3_mulv( player->rb.to_world, grind_co, grind_co );
1726
1727 /* Exit condition: lost grind tracking */
1728 if( !skate_grind_scansq( player, grind_co, player->rb.v, 0.3f, inf ) )
1729 return 0;
1730
1731 /* Exit condition: cant see grind target directly */
1732 if( !skate_point_visible( wheel_co, inf->co ) )
1733 return 0;
1734
1735 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1736 float dv = fabsf(v3_dot( player->rb.v, inf->dir )),
1737 minv = k_grind_axel_min_vel*0.8f;
1738
1739 if( dv < minv )
1740 return 0;
1741
1742 if( fabsf(v3_dot( inf->dir, s->grind_dir )) < k_grind_max_edge_angle )
1743 return 0;
1744
1745 v3_copy( inf->dir, s->grind_dir );
1746 return 1;
1747 }
1748
1749 VG_STATIC int skate_grind_truck_entry( player_instance *player, float sign,
1750 struct grind_info *inf )
1751 {
1752 struct player_skate *s = &player->_skate;
1753
1754 /* REFACTOR */
1755 v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
1756
1757 v3f raw, wsp;
1758 m3x3_mulv( player->rb.to_world, ra, raw );
1759 v3_add( player->rb.co, raw, wsp );
1760
1761 if( skate_grind_scansq( player, wsp, player->rb.v, 0.3, inf ) )
1762 {
1763 if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
1764 return 0;
1765
1766 /* velocity should be at least 60% aligned */
1767 v3f pv, axis;
1768 v3_cross( inf->n, inf->dir, axis );
1769 v3_muladds( player->rb.v, inf->n, -v3_dot( player->rb.v, inf->n ), pv );
1770
1771 if( v3_length2( pv ) < 0.0001f )
1772 return 0;
1773 v3_normalize( pv );
1774
1775 if( fabsf(v3_dot( pv, inf->dir )) < k_grind_axel_max_angle )
1776 return 0;
1777
1778 if( v3_dot( player->rb.v, inf->n ) > 0.5f )
1779 return 0;
1780
1781 #if 0
1782 /* check for vertical alignment */
1783 if( v3_dot( player->rb.to_world[1], inf->n ) < k_grind_axel_max_vangle )
1784 return 0;
1785 #endif
1786
1787 v3f local_co, local_dir, local_n;
1788 m4x3_mulv( player->rb.to_local, inf->co, local_co );
1789 m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
1790 m3x3_mulv( player->rb.to_local, inf->n, local_n );
1791
1792 v2f delta = { local_co[0], local_co[2] - k_board_length*sign };
1793
1794 float truck_height = -(k_board_radius+0.03f);
1795
1796 v3f rv;
1797 v3_cross( player->rb.w, raw, rv );
1798 v3_add( player->rb.v, rv, rv );
1799
1800 if( (local_co[1] >= truck_height) &&
1801 (v2_length2( delta ) <= k_board_radius*k_board_radius) )
1802 {
1803 return 1;
1804 }
1805 }
1806
1807 return 0;
1808 }
1809
1810 VG_STATIC void skate_boardslide_apply( player_instance *player,
1811 struct grind_info *inf )
1812 {
1813 struct player_skate *s = &player->_skate;
1814
1815 v3f local_co, local_dir, local_n;
1816 m4x3_mulv( player->rb.to_local, inf->co, local_co );
1817 m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
1818 m3x3_mulv( player->rb.to_local, inf->n, local_n );
1819
1820 v3f intersection;
1821 v3_muladds( local_co, local_dir, local_co[0]/-local_dir[0],
1822 intersection );
1823 v3_copy( intersection, s->weight_distribution );
1824
1825 skate_grind_decay( player, inf, 0.0125f );
1826 skate_grind_friction( player, inf, 0.25f );
1827
1828 /* direction alignment */
1829 v3f dir, perp;
1830 v3_cross( local_dir, local_n, perp );
1831 v3_muls( local_dir, vg_signf(local_dir[0]), dir );
1832 v3_muls( perp, vg_signf(perp[2]), perp );
1833
1834 m3x3_mulv( player->rb.to_world, dir, dir );
1835 m3x3_mulv( player->rb.to_world, perp, perp );
1836
1837 v4f qbalance;
1838 q_axis_angle( qbalance, dir, local_co[0]*k_grind_balance );
1839 q_mulv( qbalance, perp, perp );
1840
1841 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0],
1842 dir,
1843 k_grind_spring, k_grind_dampener,
1844 k_rb_delta );
1845
1846 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[2],
1847 perp,
1848 k_grind_spring, k_grind_dampener,
1849 k_rb_delta );
1850
1851 vg_line_arrow( player->rb.co, dir, 0.5f, VG__GREEN );
1852 vg_line_arrow( player->rb.co, perp, 0.5f, VG__BLUE );
1853
1854 v3_copy( inf->dir, s->grind_dir );
1855 }
1856
1857 VG_STATIC int skate_boardslide_entry( player_instance *player,
1858 struct grind_info *inf )
1859 {
1860 struct player_skate *s = &player->_skate;
1861
1862 if( skate_grind_scansq( player, player->rb.co,
1863 player->rb.to_world[0], k_board_length,
1864 inf ) )
1865 {
1866 v3f local_co, local_dir;
1867 m4x3_mulv( player->rb.to_local, inf->co, local_co );
1868 m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
1869
1870 if( (fabsf(local_co[2]) <= k_board_length) && /* within wood area */
1871 (local_co[1] >= 0.0f) && /* at deck level */
1872 (fabsf(local_dir[0]) >= 0.25f) ) /* perpendicular to us */
1873 {
1874 if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
1875 return 0;
1876
1877 return 1;
1878 }
1879 }
1880
1881 return 0;
1882 }
1883
1884 VG_STATIC int skate_boardslide_renew( player_instance *player,
1885 struct grind_info *inf )
1886 {
1887 struct player_skate *s = &player->_skate;
1888
1889 if( !skate_grind_scansq( player, player->rb.co,
1890 player->rb.to_world[0], k_board_length,
1891 inf ) )
1892 return 0;
1893
1894 /* Exit condition: cant see grind target directly */
1895 v3f vis;
1896 v3_muladds( player->rb.co, player->rb.to_world[1], 0.2f, vis );
1897 if( !skate_point_visible( vis, inf->co ) )
1898 return 0;
1899
1900 /* Exit condition: minimum velocity not reached, but allow a bit of error */
1901 float dv = fabsf(v3_dot( player->rb.v, inf->dir )),
1902 minv = k_grind_axel_min_vel*0.8f;
1903
1904 if( dv < minv )
1905 return 0;
1906
1907 if( fabsf(v3_dot( inf->dir, s->grind_dir )) < k_grind_max_edge_angle )
1908 return 0;
1909
1910 return 1;
1911 }
1912
1913 VG_STATIC void skate_store_grind_vec( player_instance *player,
1914 struct grind_info *inf )
1915 {
1916 struct player_skate *s = &player->_skate;
1917
1918 m3x3f mtx;
1919 skate_grind_orient( inf, mtx );
1920 m3x3_transpose( mtx, mtx );
1921
1922 v3f raw;
1923 v3_sub( inf->co, player->rb.co, raw );
1924
1925 m3x3_mulv( mtx, raw, s->grind_vec );
1926 v3_normalize( s->grind_vec );
1927 v3_copy( inf->dir, s->grind_dir );
1928 }
1929
1930 VG_STATIC enum skate_activity skate_availible_grind( player_instance *player )
1931 {
1932 struct player_skate *s = &player->_skate;
1933
1934 if( s->state.grind_cooldown > 100 ){
1935 vg_fatal_error( "wth!\n" );
1936 }
1937
1938 /* debounces this state manager a little bit */
1939 if( s->state.grind_cooldown ){
1940 s->state.grind_cooldown --;
1941 return k_skate_activity_undefined;
1942 }
1943
1944 struct grind_info inf_back50,
1945 inf_front50,
1946 inf_slide;
1947
1948 int res_back50 = 0,
1949 res_front50 = 0,
1950 res_slide = 0;
1951
1952 int allow_back = 1,
1953 allow_front = 1;
1954
1955 v2f steer;
1956 joystick_state( k_srjoystick_steer, steer );
1957
1958 if( s->state.activity == k_skate_activity_grind_5050 ||
1959 s->state.activity == k_skate_activity_grind_back50 ||
1960 s->state.activity == k_skate_activity_grind_front50 )
1961 {
1962 float tilt = steer[1];
1963
1964 if( fabsf(tilt) >= 0.25f ){
1965 v3f raw = {0.0f,0.0f,tilt};
1966 m3x3_mulv( player->rb.to_world, raw, raw );
1967
1968 float way = tilt * vg_signf( v3_dot( raw, player->rb.v ) );
1969
1970 if( way < 0.0f ) allow_front = 0;
1971 else allow_back = 0;
1972 }
1973 }
1974
1975 if( s->state.activity == k_skate_activity_grind_boardslide ){
1976 res_slide = skate_boardslide_renew( player, &inf_slide );
1977 }
1978 else if( s->state.activity == k_skate_activity_grind_back50 ){
1979 res_back50 = skate_grind_truck_renew( player, 1.0f, &inf_back50 );
1980
1981 if( allow_front )
1982 res_front50 = skate_grind_truck_entry( player, -1.0f, &inf_front50 );
1983 }
1984 else if( s->state.activity == k_skate_activity_grind_front50 ){
1985 res_front50 = skate_grind_truck_renew( player, -1.0f, &inf_front50 );
1986
1987 if( allow_back )
1988 res_back50 = skate_grind_truck_entry( player, 1.0f, &inf_back50 );
1989 }
1990 else if( s->state.activity == k_skate_activity_grind_5050 ){
1991 if( allow_front )
1992 res_front50 = skate_grind_truck_renew( player, -1.0f, &inf_front50 );
1993 if( allow_back )
1994 res_back50 = skate_grind_truck_renew( player, 1.0f, &inf_back50 );
1995 }
1996 else{
1997 res_slide = skate_boardslide_entry( player, &inf_slide );
1998
1999 if( allow_back )
2000 res_back50 = skate_grind_truck_entry( player, 1.0f, &inf_back50 );
2001
2002 if( allow_front )
2003 res_front50 = skate_grind_truck_entry( player, -1.0f, &inf_front50 );
2004
2005 if( res_back50 != res_front50 ){
2006 int wants_to_do_that = fabsf(steer[1]) >= 0.25f;
2007
2008 res_back50 &= wants_to_do_that;
2009 res_front50 &= wants_to_do_that;
2010 }
2011 }
2012
2013 const enum skate_activity table[] =
2014 { /* slide | back | front */
2015 k_skate_activity_undefined, /* 0 0 0 */
2016 k_skate_activity_grind_front50, /* 0 0 1 */
2017 k_skate_activity_grind_back50, /* 0 1 0 */
2018 k_skate_activity_grind_5050, /* 0 1 1 */
2019
2020 /* slide has priority always */
2021 k_skate_activity_grind_boardslide, /* 1 0 0 */
2022 k_skate_activity_grind_boardslide, /* 1 0 1 */
2023 k_skate_activity_grind_boardslide, /* 1 1 0 */
2024 k_skate_activity_grind_boardslide, /* 1 1 1 */
2025 }
2026 , new_activity = table[ res_slide << 2 | res_back50 << 1 | res_front50 ];
2027
2028 if( new_activity == k_skate_activity_undefined ){
2029 if( s->state.activity >= k_skate_activity_grind_any ){
2030 s->state.grind_cooldown = 15;
2031 s->state.surface_cooldown = 10;
2032 }
2033 }
2034 else if( new_activity == k_skate_activity_grind_boardslide ){
2035 skate_boardslide_apply( player, &inf_slide );
2036 }
2037 else if( new_activity == k_skate_activity_grind_back50 ){
2038 if( s->state.activity != k_skate_activity_grind_back50 )
2039 skate_store_grind_vec( player, &inf_back50 );
2040
2041 skate_grind_truck_apply( player, 1.0f, &inf_back50, 1.0f );
2042 }
2043 else if( new_activity == k_skate_activity_grind_front50 ){
2044 if( s->state.activity != k_skate_activity_grind_front50 )
2045 skate_store_grind_vec( player, &inf_front50 );
2046
2047 skate_grind_truck_apply( player, -1.0f, &inf_front50, 1.0f );
2048 }
2049 else if( new_activity == k_skate_activity_grind_5050 )
2050 skate_5050_apply( player, &inf_front50, &inf_back50 );
2051
2052 return new_activity;
2053 }
2054
2055 VG_STATIC void player__skate_update( player_instance *player )
2056 {
2057 struct player_skate *s = &player->_skate;
2058 world_instance *world = world_current_instance();
2059
2060 if( world->water.enabled ){
2061 if( player->rb.co[1]+0.25f < world->water.height ){
2062 audio_oneshot_3d( &audio_splash, player->rb.co, 40.0f, 1.0f );
2063 player__skate_kill_audio( player );
2064 player__dead_transition( player );
2065 return;
2066 }
2067 }
2068
2069 v3_copy( player->rb.co, s->state.prev_pos );
2070 s->state.activity_prev = s->state.activity;
2071 v3f normal_total;
2072 v3_zero( normal_total );
2073
2074 struct board_collider
2075 {
2076 v3f pos;
2077 float radius;
2078
2079 u32 colour;
2080
2081 enum board_collider_state
2082 {
2083 k_collider_state_default,
2084 k_collider_state_disabled,
2085 k_collider_state_colliding
2086 }
2087 state;
2088 }
2089 wheels[] =
2090 {
2091 {
2092 { 0.0f, 0.0f, -k_board_length },
2093 .radius = k_board_radius,
2094 .colour = VG__RED
2095 },
2096 {
2097 { 0.0f, 0.0f, k_board_length },
2098 .radius = k_board_radius,
2099 .colour = VG__GREEN
2100 }
2101 };
2102
2103 float slap = 0.0f;
2104
2105 if( s->state.activity <= k_skate_activity_air_to_grind ){
2106 float min_dist = 0.6f;
2107 for( int i=0; i<2; i++ ){
2108 v3f wpos, closest;
2109 m4x3_mulv( player->rb.to_world, wheels[i].pos, wpos );
2110
2111 if( bh_closest_point( world->geo_bh, wpos, closest, min_dist ) != -1 ){
2112 min_dist = vg_minf( min_dist, v3_dist( closest, wpos ) );
2113 }
2114 }
2115 min_dist -= 0.2f;
2116 float vy = v3_dot( player->basis[1], player->rb.v );
2117 vy = vg_maxf( 0.0f, vy );
2118
2119 slap = vg_clampf( (min_dist/0.5f) + vy, 0.0f, 1.0f )*0.3f;
2120 }
2121 s->state.slap = vg_lerpf( s->state.slap, slap, 10.0f*k_rb_delta );
2122
2123 wheels[0].pos[1] = s->state.slap;
2124 wheels[1].pos[1] = s->state.slap;
2125
2126
2127 const int k_wheel_count = 2;
2128
2129 s->substep = k_rb_delta;
2130 s->substep_delta = s->substep;
2131 s->limit_count = 0;
2132
2133 int substep_count = 0;
2134
2135 v3_zero( s->surface_picture );
2136
2137 int prev_contacts[2];
2138
2139 for( int i=0; i<k_wheel_count; i++ ){
2140 wheels[i].state = k_collider_state_default;
2141 prev_contacts[i] = s->wheel_contacts[i];
2142 }
2143
2144 /* check if we can enter or continue grind */
2145 enum skate_activity grindable_activity = skate_availible_grind( player );
2146 if( grindable_activity != k_skate_activity_undefined ){
2147 s->state.activity = grindable_activity;
2148 goto grinding;
2149 }
2150
2151 int contact_count = 0;
2152 for( int i=0; i<2; i++ ){
2153 v3f normal, axel;
2154 v3_copy( player->rb.to_world[0], axel );
2155
2156 if( skate_compute_surface_alignment( player, wheels[i].pos,
2157 wheels[i].colour, normal, axel ) )
2158 {
2159 rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0],
2160 axel,
2161 k_surface_spring, k_surface_dampener,
2162 s->substep_delta );
2163
2164 v3_add( normal, s->surface_picture, s->surface_picture );
2165 contact_count ++;
2166 s->wheel_contacts[i] = 1;
2167 }
2168 else{
2169 s->wheel_contacts[i] = 0;
2170 }
2171
2172 m3x3_mulv( player->rb.to_local, axel, s->truckv0[i] );
2173 }
2174
2175 if( s->state.surface_cooldown ){
2176 s->state.surface_cooldown --;
2177 contact_count = 0;
2178 }
2179
2180 if( (prev_contacts[0]+prev_contacts[1] == 1) && (contact_count == 2) ){
2181 audio_lock();
2182 for( int i=0; i<2; i++ ){
2183 if( !prev_contacts[i] ){
2184 v3f co;
2185 m4x3_mulv( player->rb.to_world, wheels[i].pos, co );
2186 audio_oneshot_3d( &audio_taps[vg_randu32()%4], co, 40.0f, 0.75f );
2187 }
2188 }
2189 audio_unlock();
2190 }
2191
2192 if( contact_count ){
2193 s->state.activity = k_skate_activity_ground;
2194 s->state.gravity_bias = k_gravity;
2195 v3_normalize( s->surface_picture );
2196
2197 skate_apply_friction_model( player );
2198 skate_weight_distribute( player );
2199 }
2200 else{
2201 if( s->state.activity > k_skate_activity_air_to_grind )
2202 s->state.activity = k_skate_activity_air;
2203
2204 v3_zero( s->weight_distribution );
2205 skate_apply_air_model( player );
2206 }
2207
2208 grinding:;
2209
2210 if( s->state.activity == k_skate_activity_grind_back50 )
2211 wheels[1].state = k_collider_state_disabled;
2212 if( s->state.activity == k_skate_activity_grind_front50 )
2213 wheels[0].state = k_collider_state_disabled;
2214 if( s->state.activity == k_skate_activity_grind_5050 ){
2215 wheels[0].state = k_collider_state_disabled;
2216 wheels[1].state = k_collider_state_disabled;
2217 }
2218
2219 /* all activities */
2220 skate_apply_steering_model( player );
2221 skate_adjust_up_direction( player );
2222 skate_apply_cog_model( player );
2223 skate_apply_jump_model( player );
2224 skate_apply_grab_model( player );
2225 skate_apply_trick_model( player );
2226 skate_apply_pump_model( player );
2227
2228 begin_collision:;
2229
2230 /*
2231 * Phase 0: Continous collision detection
2232 * --------------------------------------------------------------------------
2233 */
2234
2235 v3f head_wp0, head_wp1, start_co;
2236 m4x3_mulv( player->rb.to_world, s->state.head_position, head_wp0 );
2237 v3_copy( player->rb.co, start_co );
2238
2239 /* calculate transform one step into future */
2240 v3f future_co;
2241 v4f future_q;
2242 v3_muladds( player->rb.co, player->rb.v, s->substep, future_co );
2243
2244 if( v3_length2( player->rb.w ) > 0.0f ){
2245 v4f rotation;
2246 v3f axis;
2247 v3_copy( player->rb.w, axis );
2248
2249 float mag = v3_length( axis );
2250 v3_divs( axis, mag, axis );
2251 q_axis_angle( rotation, axis, mag*s->substep );
2252 q_mul( rotation, player->rb.q, future_q );
2253 q_normalize( future_q );
2254 }
2255 else
2256 v4_copy( player->rb.q, future_q );
2257
2258 v3f future_cg, current_cg, cg_offset;
2259 q_mulv( player->rb.q, s->weight_distribution, current_cg );
2260 q_mulv( future_q, s->weight_distribution, future_cg );
2261 v3_sub( future_cg, current_cg, cg_offset );
2262
2263 /* calculate the minimum time we can move */
2264 float max_time = s->substep;
2265
2266 for( int i=0; i<k_wheel_count; i++ ){
2267 if( wheels[i].state == k_collider_state_disabled )
2268 continue;
2269
2270 v3f current, future, r_cg;
2271
2272 q_mulv( future_q, wheels[i].pos, future );
2273 v3_add( future, future_co, future );
2274 v3_add( cg_offset, future, future );
2275
2276 q_mulv( player->rb.q, wheels[i].pos, current );
2277 v3_add( current, player->rb.co, current );
2278
2279 float t;
2280 v3f n;
2281
2282 float cast_radius = wheels[i].radius - k_penetration_slop * 2.0f;
2283 if( spherecast_world( world, current, future, cast_radius, &t, n ) != -1)
2284 max_time = vg_minf( max_time, t * s->substep );
2285 }
2286
2287 /* clamp to a fraction of delta, to prevent locking */
2288 float rate_lock = substep_count;
2289 rate_lock *= k_rb_delta * 0.1f;
2290 rate_lock *= rate_lock;
2291
2292 max_time = vg_maxf( max_time, rate_lock );
2293 s->substep_delta = max_time;
2294
2295 /* integrate */
2296 v3_muladds( player->rb.co, player->rb.v, s->substep_delta, player->rb.co );
2297 if( v3_length2( player->rb.w ) > 0.0f ){
2298 v4f rotation;
2299 v3f axis;
2300 v3_copy( player->rb.w, axis );
2301
2302 float mag = v3_length( axis );
2303 v3_divs( axis, mag, axis );
2304 q_axis_angle( rotation, axis, mag*s->substep_delta );
2305 q_mul( rotation, player->rb.q, player->rb.q );
2306 q_normalize( player->rb.q );
2307
2308 q_mulv( player->rb.q, s->weight_distribution, future_cg );
2309 v3_sub( current_cg, future_cg, cg_offset );
2310 v3_add( player->rb.co, cg_offset, player->rb.co );
2311 }
2312
2313 rb_update_transform( &player->rb );
2314 v3_muladds( player->rb.v, player->basis[1],
2315 -s->state.gravity_bias * s->substep_delta, player->rb.v );
2316
2317 s->substep -= s->substep_delta;
2318
2319 rb_ct manifold[128];
2320 int manifold_len = 0;
2321 /*
2322 * Phase -1: head detection
2323 * --------------------------------------------------------------------------
2324 */
2325 m4x3_mulv( player->rb.to_world, s->state.head_position, head_wp1 );
2326
2327 float t;
2328 v3f n;
2329 if( (v3_dist2( head_wp0, head_wp1 ) > 0.001f) &&
2330 (spherecast_world( world, head_wp0, head_wp1, 0.2f, &t, n ) != -1) )
2331 {
2332 v3_lerp( start_co, player->rb.co, t, player->rb.co );
2333 rb_update_transform( &player->rb );
2334
2335 player__skate_kill_audio( player );
2336 player__dead_transition( player );
2337 return;
2338 }
2339
2340 /*
2341 * Phase 1: Regular collision detection
2342 * --------------------------------------------------------------------------
2343 */
2344
2345 for( int i=0; i<k_wheel_count; i++ ){
2346 if( wheels[i].state == k_collider_state_disabled )
2347 continue;
2348
2349 m4x3f mtx;
2350 m3x3_identity( mtx );
2351 m4x3_mulv( player->rb.to_world, wheels[i].pos, mtx[3] );
2352
2353 rb_sphere collider = { .radius = wheels[i].radius };
2354
2355 rb_ct *man = &manifold[ manifold_len ];
2356
2357 int l = skate_collide_smooth( player, mtx, &collider, man );
2358 if( l )
2359 wheels[i].state = k_collider_state_colliding;
2360
2361 manifold_len += l;
2362 }
2363
2364 float grind_radius = k_board_radius * 0.75f;
2365 rb_capsule capsule = { .height = (k_board_length+0.2f)*2.0f,
2366 .radius=grind_radius };
2367 m4x3f mtx;
2368 v3_muls( player->rb.to_world[0], 1.0f, mtx[0] );
2369 v3_muls( player->rb.to_world[2], -1.0f, mtx[1] );
2370 v3_muls( player->rb.to_world[1], 1.0f, mtx[2] );
2371 v3_muladds( player->rb.to_world[3], player->rb.to_world[1],
2372 grind_radius + k_board_radius*0.25f+s->state.slap, mtx[3] );
2373
2374 rb_ct *cman = &manifold[manifold_len];
2375
2376 int l = rb_capsule__scene( mtx, &capsule, NULL, &world->rb_geo.inf.scene,
2377 cman );
2378
2379 /* weld joints */
2380 for( int i=0; i<l; i ++ )
2381 cman[l].type = k_contact_type_edge;
2382 rb_manifold_filter_joint_edges( cman, l, 0.03f );
2383 l = rb_manifold_apply_filtered( cman, l );
2384
2385 manifold_len += l;
2386
2387 if( vg_lines.draw )
2388 vg_line_capsule( mtx, capsule.radius, capsule.height, VG__WHITE );
2389
2390 /* add limits */
2391 if( s->state.activity >= k_skate_activity_grind_any ){
2392 for( int i=0; i<s->limit_count; i++ ){
2393 struct grind_limit *limit = &s->limits[i];
2394 rb_ct *ct = &manifold[ manifold_len ++ ];
2395 m4x3_mulv( player->rb.to_world, limit->ra, ct->co );
2396 m3x3_mulv( player->rb.to_world, limit->n, ct->n );
2397 ct->p = limit->p;
2398 ct->type = k_contact_type_default;
2399 }
2400 }
2401
2402 /*
2403 * Phase 3: Dynamics
2404 * --------------------------------------------------------------------------
2405 */
2406
2407
2408 v3f world_cog;
2409 m4x3_mulv( player->rb.to_world, s->weight_distribution, world_cog );
2410 vg_line_point( world_cog, 0.02f, VG__BLACK );
2411
2412 for( int i=0; i<manifold_len; i ++ ){
2413 rb_prepare_contact( &manifold[i], s->substep_delta );
2414 rb_debug_contact( &manifold[i] );
2415 }
2416
2417 /* yes, we are currently rebuilding mass matrices every frame. too bad! */
2418 v3f extent = { k_board_width, 0.1f, k_board_length };
2419 float ex2 = k_board_interia*extent[0]*extent[0],
2420 ey2 = k_board_interia*extent[1]*extent[1],
2421 ez2 = k_board_interia*extent[2]*extent[2];
2422
2423 float mass = 2.0f * (extent[0]*extent[1]*extent[2]);
2424 float inv_mass = 1.0f/mass;
2425
2426 v3f I;
2427 I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
2428 I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
2429 I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
2430
2431 m3x3f iI;
2432 m3x3_identity( iI );
2433 iI[0][0] = I[0];
2434 iI[1][1] = I[1];
2435 iI[2][2] = I[2];
2436 m3x3_inv( iI, iI );
2437
2438 m3x3f iIw;
2439 m3x3_mul( iI, player->rb.to_local, iIw );
2440 m3x3_mul( player->rb.to_world, iIw, iIw );
2441
2442 for( int j=0; j<10; j++ ){
2443 for( int i=0; i<manifold_len; i++ ){
2444 /*
2445 * regular dance; calculate velocity & total mass, apply impulse.
2446 */
2447
2448 struct contact *ct = &manifold[i];
2449
2450 v3f rv, delta;
2451 v3_sub( ct->co, world_cog, delta );
2452 v3_cross( player->rb.w, delta, rv );
2453 v3_add( player->rb.v, rv, rv );
2454
2455 v3f raCn;
2456 v3_cross( delta, ct->n, raCn );
2457
2458 v3f raCnI, rbCnI;
2459 m3x3_mulv( iIw, raCn, raCnI );
2460
2461 float normal_mass = 1.0f / (inv_mass + v3_dot(raCn,raCnI)),
2462 vn = v3_dot( rv, ct->n ),
2463 lambda = normal_mass * ( -vn );
2464
2465 float temp = ct->norm_impulse;
2466 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
2467 lambda = ct->norm_impulse - temp;
2468
2469 v3f impulse;
2470 v3_muls( ct->n, lambda, impulse );
2471
2472 v3_muladds( normal_total, impulse, inv_mass, normal_total );
2473 v3_muladds( player->rb.v, impulse, inv_mass, player->rb.v );
2474 v3_cross( delta, impulse, impulse );
2475 m3x3_mulv( iIw, impulse, impulse );
2476 v3_add( impulse, player->rb.w, player->rb.w );
2477
2478 v3_cross( player->rb.w, delta, rv );
2479 v3_add( player->rb.v, rv, rv );
2480 vn = v3_dot( rv, ct->n );
2481 }
2482 }
2483
2484 v3f dt;
2485 rb_depenetrate( manifold, manifold_len, dt );
2486 v3_add( dt, player->rb.co, player->rb.co );
2487 rb_update_transform( &player->rb );
2488
2489 substep_count ++;
2490
2491 if( s->substep >= 0.0001f )
2492 goto begin_collision; /* again! */
2493
2494 /*
2495 * End of collision and dynamics routine
2496 * --------------------------------------------------------------------------
2497 */
2498
2499 f32 nforce = v3_length(normal_total);
2500 if( nforce > 4.0f ){
2501 if( nforce > 17.6f ){
2502 v3_muladds( player->rb.v, normal_total, -1.0f, player->rb.v );
2503 player__dead_transition(player);
2504 player__skate_kill_audio(player);
2505 return;
2506 }
2507
2508 f32 amt = k_cam_punch;
2509 if( player->cam_control.camera_mode == k_cam_firstperson ){
2510 amt *= 0.25f;
2511 }
2512
2513 v3_muladds( player->cam_land_punch_v, normal_total, amt,
2514 player->cam_land_punch_v );
2515 }
2516
2517 s->surface = k_surface_prop_concrete;
2518
2519 for( int i=0; i<manifold_len; i++ ){
2520 rb_ct *ct = &manifold[i];
2521 struct world_surface *surf = world_contact_surface( world, ct );
2522
2523 if( surf->info.surface_prop > s->surface )
2524 s->surface = surf->info.surface_prop;
2525 }
2526
2527 for( int i=0; i<k_wheel_count; i++ ){
2528 m4x3f mtx;
2529 m3x3_copy( player->rb.to_world, mtx );
2530 m4x3_mulv( player->rb.to_world, wheels[i].pos, mtx[3] );
2531 vg_line_sphere( mtx, wheels[i].radius,
2532 (u32[]){ VG__WHITE, VG__BLACK,
2533 wheels[i].colour }[ wheels[i].state ]);
2534 }
2535
2536 skate_integrate( player );
2537 vg_line_point( s->state.cog, 0.02f, VG__WHITE );
2538
2539 ent_gate *gate =
2540 world_intersect_gates(world, player->rb.co, s->state.prev_pos );
2541
2542 if( gate ){
2543 m4x3_mulv( gate->transport, player->rb.co, player->rb.co );
2544 m3x3_mulv( gate->transport, player->rb.v, player->rb.v );
2545 m4x3_mulv( gate->transport, s->state.cog, s->state.cog );
2546 m3x3_mulv( gate->transport, s->state.cog_v, s->state.cog_v );
2547 m3x3_mulv( gate->transport, s->state.throw_v, s->state.throw_v );
2548 m3x3_mulv( gate->transport, s->state.head_position,
2549 s->state.head_position );
2550 m3x3_mulv( gate->transport, s->state.up_dir, s->state.up_dir );
2551
2552 v4f transport_rotation;
2553 m3x3_q( gate->transport, transport_rotation );
2554 q_mul( transport_rotation, player->rb.q, player->rb.q );
2555 q_mul( transport_rotation, s->state.smoothed_rotation,
2556 s->state.smoothed_rotation );
2557 rb_update_transform( &player->rb );
2558 player__pass_gate( player, gate );
2559 }
2560
2561 /* FIXME: Rate limit */
2562 static int stick_frames = 0;
2563
2564 if( s->state.activity >= k_skate_activity_ground )
2565 stick_frames ++;
2566 else
2567 stick_frames = 0;
2568
2569 if( stick_frames > 5 ) stick_frames = 5;
2570
2571 if( stick_frames == 4 ){
2572 audio_lock();
2573
2574 if( s->state.activity == k_skate_activity_ground ){
2575 if( (fabsf(s->state.slip) > 0.75f) ){
2576 audio_oneshot_3d( &audio_lands[vg_randu32()%2+3], player->rb.co,
2577 40.0f, 1.0f );
2578 }
2579 else{
2580 audio_oneshot_3d( &audio_lands[vg_randu32()%3], player->rb.co,
2581 40.0f, 1.0f );
2582 }
2583 }
2584 else if( s->surface == k_surface_prop_metal ){
2585 audio_oneshot_3d( &audio_board[3], player->rb.co, 40.0f, 1.0f );
2586 }
2587 else{
2588 audio_oneshot_3d( &audio_board[8], player->rb.co, 40.0f, 1.0f );
2589 }
2590
2591 audio_unlock();
2592 } else if( stick_frames == 0 ){
2593
2594 }
2595 }
2596
2597 VG_STATIC void player__skate_im_gui( player_instance *player ){
2598 struct player_skate *s = &player->_skate;
2599 player__debugtext( 1, "V: %5.2f %5.2f %5.2f",player->rb.v[0],
2600 player->rb.v[1],
2601 player->rb.v[2] );
2602 player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",player->rb.co[0],
2603 player->rb.co[1],
2604 player->rb.co[2] );
2605 player__debugtext( 1, "W: %5.2f %5.2f %5.2f",player->rb.w[0],
2606 player->rb.w[1],
2607 player->rb.w[2] );
2608
2609 const char *activity_txt[] =
2610 {
2611 "air",
2612 "air_to_grind",
2613 "ground",
2614 "undefined (INVALID)",
2615 "grind_any (INVALID)",
2616 "grind_boardslide",
2617 "grind_metallic (INVALID)",
2618 "grind_back50",
2619 "grind_front50",
2620 "grind_5050"
2621 };
2622
2623 player__debugtext( 1, "activity: %s", activity_txt[s->state.activity] );
2624 #if 0
2625 player__debugtext( 1, "steer_s: %5.2f %5.2f [%.2f %.2f]",
2626 s->state.steerx_s, s->state.steery_s,
2627 k_steer_ground, k_steer_air );
2628 #endif
2629 player__debugtext( 1, "flip: %.4f %.4f", s->state.flip_rate,
2630 s->state.flip_time );
2631 player__debugtext( 1, "trickv: %.2f %.2f %.2f",
2632 s->state.trick_vel[0],
2633 s->state.trick_vel[1],
2634 s->state.trick_vel[2] );
2635 player__debugtext( 1, "tricke: %.2f %.2f %.2f",
2636 s->state.trick_euler[0],
2637 s->state.trick_euler[1],
2638 s->state.trick_euler[2] );
2639 }
2640
2641 VG_STATIC void player__skate_animate( player_instance *player ){
2642 struct player_skate *s = &player->_skate;
2643 struct player_skate_state *state = &player->_skate.state;
2644 struct player_skate_animator *animator = &s->animator;
2645
2646 /* Head */
2647 float kheight = 2.0f,
2648 kleg = 0.6f;
2649
2650 v3_zero( animator->offset );
2651
2652 v3f cog_local, cog_ideal;
2653 m4x3_mulv( player->rb.to_local, s->state.cog, cog_local );
2654
2655 v3_copy( s->state.up_dir, cog_ideal );
2656 v3_normalize( cog_ideal );
2657 m3x3_mulv( player->rb.to_local, cog_ideal, cog_ideal );
2658
2659 v3_sub( cog_ideal, cog_local, animator->offset );
2660
2661 v3_muls( animator->offset, 4.0f, animator->offset );
2662 animator->offset[1] *= -1.0f;
2663
2664 float curspeed = v3_length( player->rb.v ),
2665 kickspeed = vg_clampf( curspeed*(1.0f/40.0f), 0.0f, 1.0f ),
2666 kicks = (vg_randf64()-0.5f)*2.0f*kickspeed,
2667 sign = vg_signf( kicks );
2668
2669 animator->wobble[0] = vg_lerpf( animator->wobble[0], kicks*kicks*sign,
2670 6.0f*vg.time_delta);
2671 animator->wobble[1] = vg_lerpf( animator->wobble[1], animator->wobble[0],
2672 2.4f*vg.time_delta);
2673
2674 animator->offset[0] *= 0.26f;
2675 animator->offset[0] += animator->wobble[1]*3.0f;
2676
2677 animator->offset[1] *= -0.3f;
2678 animator->offset[2] *= 0.01f;
2679
2680 animator->offset[0]=vg_clampf(animator->offset[0],-0.8f,0.8f)*
2681 (1.0f-fabsf(animator->slide)*0.9f);
2682 animator->offset[1]=vg_clampf(animator->offset[1],-0.5f,0.0f);
2683
2684 v3_muls( animator->offset, 0.3f, player->cam_control.tpv_offset_extra );
2685
2686 /* localized vectors */
2687 m4x3_mulv( player->rb.to_local, s->state.cog, animator->local_cog );
2688
2689 /*
2690 * Animation blending
2691 * ===========================================
2692 */
2693
2694 /* sliding */
2695 {
2696 float desired = 0.0f;
2697 if( s->state.activity == k_skate_activity_ground )
2698 desired = vg_clampf( fabsf( s->state.slip ), 0.0f, 1.0f );
2699
2700 animator->slide = vg_lerpf( animator->slide, desired, 2.4f*vg.time_delta);
2701 }
2702
2703 /* movement information */
2704 int iair = s->state.activity <= k_skate_activity_air_to_grind;
2705
2706 float dirz = s->state.reverse > 0.0f? 0.0f: 1.0f,
2707 dirx = s->state.slip < 0.0f? 0.0f: 1.0f,
2708 fly = iair? 1.0f: 0.0f,
2709 wdist= s->weight_distribution[2] / k_board_length;
2710
2711 if( s->state.activity >= k_skate_activity_grind_any )
2712 wdist = 0.0f;
2713
2714 animator->z = vg_lerpf( animator->z, dirz, 2.4f*vg.time_delta );
2715 animator->x = vg_lerpf( animator->x, dirx, 0.6f*vg.time_delta );
2716 animator->fly = vg_lerpf( animator->fly, fly, 3.4f*vg.time_delta );
2717 animator->weight = vg_lerpf( animator->weight, wdist, 9.0f*vg.time_delta );
2718
2719 float stand = 1.0f - vg_clampf( curspeed * 0.03f, 0.0f, 1.0f );
2720 animator->stand = vg_lerpf( animator->stand, stand, 6.0f*vg.time_delta );
2721 animator->reverse = s->state.reverse;
2722
2723 if( fabsf(s->state.slip) > 0.3f ){
2724 f32 slide_dir = vg_signf(v3_dot(player->rb.v,player->rb.to_world[0]));
2725 s->state.delayed_slip_dir = slide_dir;
2726 }
2727
2728 /* grinding */
2729 f32 grind=s->state.activity >= k_skate_activity_grind_any? 1.0f: 0.0f;
2730 animator->grind = vg_lerpf( animator->grind, grind, 5.0f*vg.time_delta );
2731
2732 f32 grind_frame = 0.5f;
2733
2734 if( s->state.activity == k_skate_activity_grind_front50 )
2735 grind_frame = 0.0f;
2736 else if( s->state.activity == k_skate_activity_grind_back50 )
2737 grind_frame = 1.0f;
2738
2739 animator->grind_balance = vg_lerpf( animator->grind_balance, grind_frame,
2740 5.0f*vg.time_delta );
2741
2742 /* pushing */
2743 animator->push_time = vg.time - s->state.start_push;
2744 animator->push = vg_lerpf( animator->push,
2745 (vg.time - s->state.cur_push) < 0.125,
2746 6.0f*vg.time_delta );
2747
2748 /* jumping */
2749 animator->jump_charge = s->state.jump_charge;
2750 animator->jump = vg_lerpf( animator->jump, animator->jump_charge,
2751 8.4f*vg.time_delta );
2752
2753 /* trick setup */
2754 animator->jump_dir = s->state.jump_dir;
2755 f32 jump_start_frame = 14.0f/30.0f;
2756 animator->jump_time = animator->jump_charge * jump_start_frame;
2757 f32 jump_frame = (vg.time - s->state.jump_time) + jump_start_frame;
2758 if( jump_frame >= jump_start_frame && jump_frame <= (40.0f/30.0f) )
2759 animator->jump_time = jump_frame;
2760
2761 /* trick */
2762 float jump_t = vg.time-s->state.jump_time;
2763 float k=17.0f;
2764 float h = k*jump_t;
2765 float extra = h*exp(1.0-h) * (s->state.jump_dir?1.0f:-1.0f);
2766 extra *= s->state.slap * 4.0f;
2767
2768 v3_add( s->state.trick_euler, s->state.trick_residuald,
2769 animator->board_euler );
2770 v3_muls( animator->board_euler, VG_TAUf, animator->board_euler );
2771
2772 animator->board_euler[0] *= 0.5f;
2773 animator->board_euler[1] += extra;
2774 animator->trick_type = s->state.trick_type;
2775
2776 /* board lean */
2777 f32 lean1, lean2 = animator->steer[0] * animator->reverse * -0.36f,
2778 lean;
2779
2780 lean1 = animator->slide * animator->delayed_slip_dir;
2781 if( fabsf(lean1)>fabsf(lean2) ) lean = lean1;
2782 else lean = lean2;
2783
2784 if( ((int)roundf(animator->board_euler[0])) % 2 ) lean = -lean;
2785 lean = vg_clampf( lean, -1.0f, 1.0f );
2786 animator->board_lean =
2787 vg_lerpf(animator->board_lean, lean, vg.time_delta*18.0f);
2788
2789 /* feet placement */
2790 struct player_board *board =
2791 addon_cache_item_if_loaded( k_addon_type_board,
2792 player->board_view_slot );
2793 if( board ){
2794 if( animator->weight > 0.0f ){
2795 animator->foot_offset[0] =
2796 board->truck_positions[k_board_truck_back][2]+0.3f;
2797 }
2798 else{
2799 animator->foot_offset[1] =
2800 board->truck_positions[k_board_truck_front][2]-0.3f;
2801 }
2802 }
2803
2804 f32 slapm = vg_maxf( 1.0f-v3_length2( s->state.trick_vel ), 0.0f );
2805 animator->slap = s->state.slap;
2806 animator->subslap = vg_lerpf( animator->subslap, slapm,
2807 vg.time_delta*10.0f );
2808
2809 f32 l = ((s->state.activity < k_skate_activity_ground) &&
2810 v3_length2(s->state.trick_vel) > 0.1f )? 1: 0;
2811 animator->trick_foot = vg_lerpf( animator->trick_foot, l,
2812 8.4f*vg.time_delta );
2813
2814 /* grab */
2815 v2f grab_input;
2816 joystick_state( k_srjoystick_grab, grab_input );
2817 v2_add( s->state.grab_mouse_delta, grab_input, grab_input );
2818
2819 if( v2_length2( grab_input ) <= 0.001f ) grab_input[0] = -1.0f;
2820 else v2_normalize_clamp( grab_input );
2821 v2_lerp( animator->grab, grab_input, 2.4f*vg.time_delta, animator->grab );
2822 animator->grabbing = s->state.grabbing;
2823
2824 /* steer */
2825 joystick_state( k_srjoystick_steer, animator->steer );
2826
2827 /* flip angle */
2828 if( (s->state.activity <= k_skate_activity_air_to_grind) &&
2829 (fabsf(s->state.flip_rate) > 0.01f) ){
2830 float substep = vg.time_fixed_extrapolate;
2831 float t = s->state.flip_time+s->state.flip_rate*substep*k_rb_delta;
2832 sign = vg_signf( t );
2833
2834 t = 1.0f - vg_minf( 1.0f, fabsf( t * 1.1f ) );
2835 t = sign * (1.0f-t*t);
2836
2837 f32 angle = vg_clampf( t, -1.0f, 1.0f ) * VG_TAUf,
2838 distm = s->state.land_dist * fabsf(s->state.flip_rate) * 3.0f,
2839 blend = vg_clampf( 1.0f-distm, 0.0f, 1.0f );
2840 angle = vg_lerpf( angle, vg_signf(s->state.flip_rate)*VG_TAUf, blend );
2841 q_axis_angle( animator->qflip, s->state.flip_axis, angle );
2842 }
2843 else
2844 q_identity( animator->qflip );
2845
2846 /* counter-rotation */
2847 if( v3_length2( s->state.up_dir ) > 0.001f ){
2848 if( v4_length(s->state.smoothed_rotation) <= 0.1f ||
2849 v4_length(s->state.smoothed_rotation) >= 1.1f ){
2850 vg_warn( "FIX THIS! CARROT\n" ); /* this never happens anymore? */
2851 v4_copy( player->rb.q, s->state.smoothed_rotation );
2852 }
2853 v4_lerp( s->state.smoothed_rotation, player->rb.q,
2854 2.0f*vg.time_frame_delta,
2855 s->state.smoothed_rotation );
2856 q_normalize( s->state.smoothed_rotation );
2857
2858 v3f yaw_ref = {1.0f,0.0f,0.0f},
2859 yaw_smooth = {1.0f,0.0f,0.0f};
2860 q_mulv( player->rb.q, yaw_ref, yaw_ref );
2861 q_mulv( s->state.smoothed_rotation, yaw_smooth, yaw_smooth );
2862 m3x3_mulv( player->rb.to_local, yaw_smooth, yaw_smooth );
2863 m3x3_mulv( player->rb.to_local, yaw_ref, yaw_ref );
2864
2865 f32 yaw_counter_rotate = v3_dot(yaw_ref,yaw_smooth);
2866 yaw_counter_rotate = vg_clampf(yaw_counter_rotate,-0.7f,0.7f);
2867 yaw_counter_rotate = acosf( yaw_counter_rotate );
2868 yaw_counter_rotate *= 1.0f-animator->fly;
2869
2870 v3f ndir;
2871 m3x3_mulv( player->rb.to_local, s->state.up_dir, ndir );
2872 v3_normalize( ndir );
2873
2874 v3f up = { 0.0f, 1.0f, 0.0f };
2875
2876 float a = v3_dot( ndir, up );
2877 a = acosf( vg_clampf( a, -1.0f, 1.0f ) );
2878
2879 v3f axis;
2880 v4f qcounteryaw, qfixup;
2881
2882 v3_cross( up, ndir, axis );
2883 q_axis_angle( qfixup, axis, a );
2884
2885 q_axis_angle( qcounteryaw, (v3f){0.0f,1.0f,0.0f}, yaw_counter_rotate );
2886 q_mul( qcounteryaw, qfixup, animator->qfixuptotal );
2887 q_normalize( animator->qfixuptotal );
2888
2889 v3f p1, p2;
2890 m3x3_mulv( player->rb.to_world, up, p1 );
2891 m3x3_mulv( player->rb.to_world, ndir, p2 );
2892
2893 vg_line_arrow( player->rb.co, p1, 0.25f, VG__PINK );
2894 vg_line_arrow( player->rb.co, p2, 0.25f, VG__PINK );
2895
2896 }
2897 else q_identity( animator->qfixuptotal );
2898 q_identity( animator->qfixuptotal );
2899 rb_extrapolate( &player->rb, animator->root_co, animator->root_q );
2900 }
2901
2902 VG_STATIC void player__skate_pose( player_instance *player, player_pose *pose ){
2903 struct player_avatar *av = player->playeravatar;
2904 struct skeleton *sk = &av->sk;
2905 struct player_skate *s = &player->_skate;
2906 struct player_skate_animator *animator = &s->animator;
2907 pose->type = k_player_pose_type_ik;
2908 v3_copy( animator->root_co, pose->root_co );
2909 v4_copy( animator->root_q, pose->root_q );
2910
2911 /* transform */
2912 v3f ext_up,ext_co;
2913 q_mulv( pose->root_q, (v3f){0.0f,1.0f,0.0f}, ext_up );
2914 v3_copy( pose->root_co, ext_co );
2915 v3_muladds( pose->root_co, ext_up, -0.1f, pose->root_co );
2916
2917 /* apply flip rotation at midpoint */
2918 q_mul( animator->qflip, pose->root_q, pose->root_q );
2919 q_normalize( pose->root_q );
2920
2921 v3f rotation_point, rco;
2922 v3_muladds( ext_co, ext_up, 0.5f, rotation_point );
2923 v3_sub( pose->root_co, rotation_point, rco );
2924
2925 q_mulv( animator->qflip, rco, rco );
2926 v3_add( rco, rotation_point, pose->root_co );
2927
2928 /* ANIMATIONS
2929 * ---------------------------------------------------------------------- */
2930
2931 mdl_keyframe apose[32], bpose[32];
2932 mdl_keyframe ground_pose[32];
2933 {
2934 /* stand/crouch */
2935 f32 dir_frame = animator->z * (15.0f/30.0f),
2936 stand_blend = animator->offset[1]*-2.0f;
2937
2938 pose->board.lean = animator->board_lean;
2939
2940 stand_blend = vg_clampf( 1.0f-animator->local_cog[1], 0, 1 );
2941
2942 skeleton_sample_anim( sk, s->anim_stand, dir_frame, apose );
2943 skeleton_sample_anim( sk, s->anim_highg, dir_frame, bpose );
2944 skeleton_lerp_pose( sk, apose, bpose, stand_blend, apose );
2945
2946 /* sliding */
2947 skeleton_sample_anim( sk, s->anim_slide, animator->x * 0.5f, bpose );
2948 skeleton_lerp_pose( sk, apose, bpose, animator->slide, apose );
2949
2950 if( animator->reverse > 0.0f )
2951 skeleton_sample_anim( sk, s->anim_push, animator->push_time, bpose );
2952 else{
2953 skeleton_sample_anim( sk, s->anim_push_reverse, animator->push_time,
2954 bpose );
2955 }
2956 skeleton_lerp_pose( sk, apose, bpose, animator->push, apose );
2957
2958 struct skeleton_anim *jump_anim = animator->jump_dir?
2959 s->anim_ollie:
2960 s->anim_ollie_reverse;
2961
2962 f32 setup_blend = vg_minf( animator->jump, 1.0f );
2963 skeleton_sample_anim_clamped( sk, jump_anim, animator->jump_time, bpose );
2964 skeleton_lerp_pose( sk, apose, bpose, setup_blend, ground_pose );
2965 }
2966
2967 mdl_keyframe air_pose[32];
2968 {
2969 float target = -animator->steer[1];
2970 animator->airdir = vg_lerpf( animator->airdir, target,
2971 2.4f*vg.time_delta );
2972
2973 float air_frame = (animator->airdir*0.5f+0.5f) * (15.0f/30.0f);
2974 skeleton_sample_anim( sk, s->anim_air, air_frame, apose );
2975
2976 float ang = atan2f( animator->grab[0], animator->grab[1] ),
2977 ang_unit = (ang+VG_PIf) * (1.0f/VG_TAUf),
2978 grab_frame = ang_unit * (15.0f/30.0f);
2979
2980 skeleton_sample_anim( sk, s->anim_grabs, grab_frame, bpose );
2981 skeleton_lerp_pose( sk, apose, bpose, animator->grabbing, air_pose );
2982 }
2983
2984 skeleton_lerp_pose( sk, ground_pose, air_pose, animator->fly,
2985 pose->keyframes );
2986
2987 mdl_keyframe *kf_board = &pose->keyframes[av->id_board-1],
2988 *kf_foot_l = &pose->keyframes[av->id_ik_foot_l-1],
2989 *kf_foot_r = &pose->keyframes[av->id_ik_foot_r-1],
2990 *kf_knee_l = &pose->keyframes[av->id_ik_knee_l-1],
2991 *kf_knee_r = &pose->keyframes[av->id_ik_knee_r-1],
2992 *kf_hip = &pose->keyframes[av->id_hip-1],
2993 *kf_wheels[] = { &pose->keyframes[av->id_wheel_r-1],
2994 &pose->keyframes[av->id_wheel_l-1] };
2995
2996
2997 mdl_keyframe grind_pose[32];
2998 {
2999 f32 frame = animator->grind_balance * 0.5f;
3000
3001 skeleton_sample_anim( sk, s->anim_grind, frame, apose );
3002 skeleton_sample_anim( sk, s->anim_grind_jump, frame, bpose );
3003 skeleton_lerp_pose( sk, apose, bpose, animator->jump, grind_pose );
3004 }
3005 skeleton_lerp_pose( sk, pose->keyframes, grind_pose,
3006 animator->grind, pose->keyframes );
3007 float add_grab_mod = 1.0f - animator->fly;
3008
3009 /* additive effects */
3010 u32 apply_to[] = { av->id_hip,
3011 av->id_ik_hand_l,
3012 av->id_ik_hand_r,
3013 av->id_ik_elbow_l,
3014 av->id_ik_elbow_r };
3015
3016 float apply_rates[] = { 1.0f,
3017 0.75f,
3018 0.75f,
3019 0.75f,
3020 0.75f };
3021
3022 for( int i=0; i<vg_list_size(apply_to); i ++ ){
3023 pose->keyframes[apply_to[i]-1].co[0] += animator->offset[0]*add_grab_mod;
3024 pose->keyframes[apply_to[i]-1].co[2] += animator->offset[2]*add_grab_mod;
3025 }
3026
3027 /* angle 'correction' */
3028 v3f origin;
3029 v3_add( av->sk.bones[av->id_hip].co, kf_hip->co, origin );
3030
3031 for( int i=0; i<vg_list_size(apply_to); i ++ ){
3032 mdl_keyframe *kf = &pose->keyframes[apply_to[i]-1];
3033 keyframe_rotate_around( kf, origin, av->sk.bones[apply_to[i]].co,
3034 animator->qfixuptotal );
3035 }
3036
3037 /* trick rotation */
3038 v4f qtrick, qyaw, qpitch, qroll;
3039 q_axis_angle( qyaw, (v3f){0.0f,1.0f,0.0f}, animator->board_euler[0] );
3040 q_axis_angle( qpitch, (v3f){1.0f,0.0f,0.0f}, animator->board_euler[1] );
3041 q_axis_angle( qroll, (v3f){0.0f,0.0f,1.0f}, animator->board_euler[2] );
3042
3043 q_mul( qyaw, qroll, qtrick );
3044 q_mul( qpitch, qtrick, qtrick );
3045 q_mul( kf_board->q, qtrick, kf_board->q );
3046 q_normalize( kf_board->q );
3047
3048 kf_foot_l->co[2] = vg_lerpf( kf_foot_l->co[2], animator->foot_offset[0],
3049 0.5f * animator->weight );
3050 kf_foot_r->co[2] = vg_lerpf( kf_foot_r->co[2], animator->foot_offset[1],
3051 -0.5f * animator->weight );
3052
3053 kf_foot_l->co[1] += animator->slap;
3054 kf_foot_r->co[1] += animator->slap;
3055 kf_knee_l->co[1] += animator->slap;
3056 kf_knee_r->co[1] += animator->slap;
3057 kf_board->co[1] += animator->slap * animator->subslap;
3058 kf_hip->co[1] += animator->slap * 0.25f;
3059
3060 if( animator->trick_type == k_trick_type_kickflip ){
3061 kf_foot_l->co[0] += animator->trick_foot * 0.2f;
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 }
3067 else if( animator->trick_type == k_trick_type_treflip ){
3068 kf_foot_l->co[0] += animator->trick_foot * 0.2f;
3069 kf_foot_r->co[0] -= animator->trick_foot * 0.15f;
3070 }
3071
3072 /*
3073 * animation wishlist:
3074 * boardslide/grind jump animations
3075 * when tricking the slap should not appply or less apply
3076 * not animations however DONT target grinds that are vertically down.
3077 */
3078
3079 /* truck rotation */
3080 for( int i=0; i<2; i++ ){
3081 float a = vg_minf( s->truckv0[i][0], 1.0f );
3082 a = -acosf( a ) * vg_signf( s->truckv0[i][1] );
3083
3084 v4f q;
3085 q_axis_angle( q, (v3f){0.0f,0.0f,1.0f}, a );
3086 q_mul( q, kf_wheels[i]->q, kf_wheels[i]->q );
3087 q_normalize( kf_wheels[i]->q );
3088 }
3089
3090 {
3091 mdl_keyframe
3092 *kf_head = &pose->keyframes[av->id_head-1],
3093 *kf_elbow_l = &pose->keyframes[av->id_ik_elbow_l-1],
3094 *kf_elbow_r = &pose->keyframes[av->id_ik_elbow_r-1],
3095 *kf_hand_l = &pose->keyframes[av->id_ik_hand_l-1],
3096 *kf_hand_r = &pose->keyframes[av->id_ik_hand_r-1];
3097
3098 float warble = perlin1d( vg.time, 2.0f, 2, 300 );
3099 warble *= vg_maxf(animator->grind, fabsf(animator->weight)) * 0.3f;
3100
3101 v4f qrot;
3102 q_axis_angle( qrot, (v3f){0.8f,0.7f,0.6f}, warble );
3103
3104 v3f origin = {0.0f,0.2f,0.0f};
3105 keyframe_rotate_around( kf_hand_l, origin,
3106 av->sk.bones[av->id_ik_hand_l].co, qrot );
3107 keyframe_rotate_around( kf_hand_r, origin,
3108 av->sk.bones[av->id_ik_hand_r].co, qrot );
3109 keyframe_rotate_around( kf_hip, origin,
3110 av->sk.bones[av->id_hip].co, qrot );
3111 keyframe_rotate_around( kf_elbow_r, origin,
3112 av->sk.bones[av->id_ik_elbow_r].co, qrot );
3113 keyframe_rotate_around( kf_elbow_l, origin,
3114 av->sk.bones[av->id_ik_elbow_l].co, qrot );
3115
3116 q_inv( qrot, qrot );
3117 q_mul( qrot, kf_head->q, kf_head->q );
3118 q_normalize( kf_head->q );
3119 }
3120 }
3121
3122 VG_STATIC void player__skate_post_animate( player_instance *player )
3123 {
3124 struct player_skate *s = &player->_skate;
3125 struct player_avatar *av = player->playeravatar;
3126
3127 player->cam_velocity_influence = 1.0f;
3128
3129 v3f head = { 0.0f, 1.8f, 0.0f };
3130 m4x3_mulv( av->sk.final_mtx[ av->id_head ], head, s->state.head_position );
3131 m4x3_mulv( player->rb.to_local, s->state.head_position,
3132 s->state.head_position );
3133 }
3134
3135 VG_STATIC void player__skate_reset_animator( player_instance *player ){
3136 struct player_skate *s = &player->_skate;
3137 struct player_skate_state *state = &s->state;
3138
3139 memset( &s->animator, 0, sizeof(s->animator) );
3140
3141 if( s->state.activity <= k_skate_activity_air_to_grind )
3142 s->animator.fly = 1.0f;
3143 else
3144 s->animator.fly = 0.0f;
3145 }
3146
3147 VG_STATIC void player__skate_clear_mechanics( player_instance *player )
3148 {
3149 struct player_skate *s = &player->_skate;
3150 s->state.jump_charge = 0.0f;
3151 s->state.charging_jump = 0;
3152 s->state.jump_dir = 0;
3153 v3_zero( s->state.flip_axis );
3154 s->state.flip_time = 0.0f;
3155 s->state.flip_rate = 0.0f;
3156 s->state.reverse = 0.0f;
3157 s->state.slip = 0.0f;
3158 s->state.grabbing = 0.0f;
3159 v2_zero( s->state.grab_mouse_delta );
3160 s->state.slap = 0.0f;
3161 s->state.jump_time = 0.0;
3162 s->state.start_push = 0.0;
3163 s->state.cur_push = 0.0;
3164 s->state.air_start = 0.0;
3165
3166 v3_zero( s->state.air_init_v );
3167 v3_zero( s->state.air_init_co );
3168
3169 s->state.gravity_bias = k_gravity;
3170 v3_copy( player->rb.co, s->state.prev_pos );
3171 v4_copy( player->rb.q, s->state.smoothed_rotation );
3172 v3_zero( s->state.throw_v );
3173 v3_zero( s->state.trick_vel );
3174 v3_zero( s->state.trick_euler );
3175 v3_zero( s->state.cog_v );
3176 s->state.grind_cooldown = 0;
3177 s->state.surface_cooldown = 0;
3178 v3_muladds( player->rb.co, player->rb.to_world[1], 1.0f, s->state.cog );
3179 v3_copy( player->rb.to_world[1], s->state.up_dir );
3180 v3_copy( player->rb.to_world[1], s->surface_picture );
3181 v3_zero( s->weight_distribution );
3182 v3_copy( player->rb.co, s->state.prev_pos );
3183 }
3184
3185 VG_STATIC void player__skate_reset( player_instance *player,
3186 ent_spawn *rp )
3187 {
3188 struct player_skate *s = &player->_skate;
3189 v3_zero( player->rb.v );
3190 v4_copy( rp->transform.q, player->rb.q );
3191
3192 s->state.activity = k_skate_activity_air;
3193 s->state.activity_prev = k_skate_activity_air;
3194
3195 player__skate_clear_mechanics( player );
3196 player__skate_reset_animator( player );
3197
3198 v3_zero( s->state.head_position );
3199 s->state.head_position[1] = 1.8f;
3200 }
3201
3202 #endif /* PLAYER_SKATE_C */