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