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