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