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