checkin
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 #ifndef PLAYER_H
2 #define PLAYER_H
3
4 #include "audio.h"
5 #include "common.h"
6 #include "character.h"
7 #include "bvh.h"
8
9 static int freecam = 0;
10 static float k_walkspeed = 2.0f;
11 static int walk_grid_iterations = 1;
12
13 static struct gplayer
14 {
15 /* Physics */
16 v3f co, v, a, v_last, m, bob, vl;
17 v4f rot;
18 float vswitch, slip, slip_last,
19 reverse;
20
21 float iY; /* Yaw inertia */
22 int in_air, is_dead, on_board;
23
24 v2f board_xy;
25 float grab;
26 float pitch;
27
28 v3f land_target;
29 v3f land_target_log[22];
30 u32 land_target_colours[22];
31 int land_log_count;
32 m3x3f vr,vr_pstep;
33
34 m4x3f to_world, to_local;
35
36 struct character mdl;
37
38 v3f handl_target, handr_target,
39 handl, handr;
40
41 /* Camera */
42 float air_blend;
43
44 v3f camera_pos, smooth_localcam;
45 v2f angles;
46 m4x3f camera, camera_inverse;
47 }
48 player =
49 {
50 .on_board = 1
51 };
52
53 static float *player_cam_pos(void)
54 {
55 return player.camera_pos;
56 }
57
58 static void player_transform_update(void)
59 {
60 q_normalize( player.rot );
61 q_m3x3( player.rot, player.to_world );
62 v3_copy( player.co, player.to_world[3] );
63
64 m4x3_invert_affine( player.to_world, player.to_local );
65 }
66
67 static int reset_player( int argc, char const *argv[] )
68 {
69 v3_copy( world.tutorial, player.co );
70 if( argc == 1 )
71 {
72 if( !strcmp( argv[0], "tutorial" ))
73 v3_copy( world.tutorial, player.co );
74 }
75
76 v3_copy( (v3f){ 0.0f, 0.0f, -0.2f }, player.v );
77 q_identity( player.rot );
78 player.vswitch = 1.0f;
79 player.slip_last = 0.0f;
80 player.is_dead = 0;
81 player.in_air = 1;
82 m3x3_identity( player.vr );
83
84 player.mdl.shoes[0] = 1;
85 player.mdl.shoes[1] = 1;
86
87 player_transform_update();
88 return 0;
89 }
90
91 static void player_mouseview(void)
92 {
93 if( gui_want_mouse() )
94 return;
95
96 static v2f mouse_last,
97 view_vel = { 0.0f, 0.0f };
98
99 if( vg_get_button_down( "primary" ) )
100 v2_copy( vg_mouse, mouse_last );
101
102 else if( vg_get_button( "primary" ) )
103 {
104 v2f delta;
105 v2_sub( vg_mouse, mouse_last, delta );
106 v2_copy( vg_mouse, mouse_last );
107
108 v2_muladds( view_vel, delta, 0.005f, view_vel );
109 }
110
111 v2_muladds( view_vel,
112 (v2f){ vg_get_axis("h1"), vg_get_axis("v1") },
113 0.05f, view_vel );
114 v2_muls( view_vel, 0.7f, view_vel );
115 v2_add( view_vel, player.angles, player.angles );
116 player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
117 }
118
119 static void player_freecam(void)
120 {
121 player_mouseview();
122
123 float movespeed = 25.0f;
124 v3f lookdir = { 0.0f, 0.0f, -1.0f },
125 sidedir = { 1.0f, 0.0f, 0.0f };
126
127 m3x3_mulv( player.camera, lookdir, lookdir );
128 m3x3_mulv( player.camera, sidedir, sidedir );
129
130 static v3f move_vel = { 0.0f, 0.0f, 0.0f };
131 if( vg_get_button( "forward" ) )
132 v3_muladds( move_vel, lookdir, ktimestep * movespeed, move_vel );
133 if( vg_get_button( "back" ) )
134 v3_muladds( move_vel, lookdir, ktimestep *-movespeed, move_vel );
135 if( vg_get_button( "left" ) )
136 v3_muladds( move_vel, sidedir, ktimestep *-movespeed, move_vel );
137 if( vg_get_button( "right" ) )
138 v3_muladds( move_vel, sidedir, ktimestep * movespeed, move_vel );
139
140 v3_muls( move_vel, 0.7f, move_vel );
141 v3_add( move_vel, player.camera_pos, player.camera_pos );
142 }
143
144 static void apply_gravity( v3f vel, float const timestep )
145 {
146 v3f gravity = { 0.0f, -9.6f, 0.0f };
147 v3_muladds( vel, gravity, timestep, vel );
148 }
149
150 /*
151 * TODO: The angle bias should become greater when launching from a steeper
152 * angle and skewed towords more 'downwards' angles when launching from
153 * shallower trajectories
154 */
155 static void player_start_air(void)
156 {
157 player.in_air = 1;
158
159 float pstep = ktimestep*10.0f;
160
161 float best_velocity_mod = 0.0f,
162 best_velocity_delta = -9999.9f;
163
164 float k_bias = 0.97f;
165
166 v3f axis, vup;
167 m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, vup );
168 v3_cross( vup, player.v, axis );
169 v3_normalize( axis );
170 player.land_log_count = 0;
171
172 m3x3_identity( player.vr );
173
174 for( int m=-3;m<=12; m++ )
175 {
176 float vmod = ((float)m / 15.0f)*0.09f;
177
178 v3f pco, pco1, pv;
179 v3_copy( player.co, pco );
180 v3_muls( player.v, k_bias, pv );
181
182 /*
183 * Try different 'rotations' of the velocity to find the best possible
184 * landing normal. This conserves magnitude at the expense of slightly
185 * unrealistic results
186 */
187
188 m3x3f vr;
189 v4f vr_q;
190
191 q_axis_angle( vr_q, axis, vmod );
192 q_m3x3( vr_q, vr );
193
194 m3x3_mulv( vr, pv, pv );
195 v3_muladds( pco, pv, pstep, pco );
196
197 for( int i=0; i<50; i++ )
198 {
199 v3_copy( pco, pco1 );
200 apply_gravity( pv, pstep );
201
202 m3x3_mulv( vr, pv, pv );
203 v3_muladds( pco, pv, pstep, pco );
204
205 ray_hit contact;
206 v3f vdir;
207
208 v3_sub( pco, pco1, vdir );
209 contact.dist = v3_length( vdir );
210 v3_divs( vdir, contact.dist, vdir);
211
212 if( ray_world( pco1, vdir, &contact ))
213 {
214 float land_delta = v3_dot( pv, contact.normal );
215 u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
216
217 /* Bias prediction towords ramps */
218 if( ray_hit_is_ramp( &contact ) )
219 {
220 land_delta *= 0.1f;
221 scolour |= 0x0000a000;
222 }
223
224 if( (land_delta < 0.0f) && (land_delta > best_velocity_delta) )
225 {
226 best_velocity_delta = land_delta;
227 best_velocity_mod = vmod;
228
229 v3_copy( contact.pos, player.land_target );
230
231 m3x3_copy( vr, player.vr_pstep );
232 q_axis_angle( vr_q, axis, vmod*0.1f );
233 q_m3x3( vr_q, player.vr );
234 }
235
236 v3_copy( contact.pos,
237 player.land_target_log[player.land_log_count] );
238 player.land_target_colours[player.land_log_count] =
239 0xff000000 | scolour;
240
241 player.land_log_count ++;
242
243 break;
244 }
245 }
246 }
247
248 //v3_rotate( player.v, best_velocity_mod, axis, player.v );
249
250 return;
251 v3_muls( player.v, best_velocity_mod, player.v );
252 }
253
254 static int sample_if_resistant( v3f pos )
255 {
256 v3f ground;
257 v3_copy( pos, ground );
258 ground[1] += 4.0f;
259
260 ray_hit hit;
261 hit.dist = INFINITY;
262
263 if( ray_world( ground, (v3f){0.0f,-1.0f,0.0f}, &hit ))
264 {
265 v3f angle;
266 v3_copy( player.v, angle );
267 v3_normalize( angle );
268 float resistance = v3_dot( hit.normal, angle );
269
270 if( resistance < 0.25f )
271 {
272 v3_copy( hit.pos, pos );
273 return 1;
274 }
275 }
276
277 return 0;
278 }
279
280 static float stable_force( float current, float diff )
281 {
282 float new = current + diff;
283
284 if( new * current < 0.0f )
285 return 0.0f;
286
287 return new;
288 }
289
290 static void player_physics_ground(void)
291 {
292 /*
293 * Getting surface collision points,
294 * the contact manifold is a triangle for simplicity.
295 */
296 v3f contact_front, contact_back, contact_norm, vup, vside,
297 axis;
298
299 float klength = 0.65f;
300 m4x3_mulv( player.to_world, (v3f){ 0.15f,0.0f,-klength}, contact_norm );
301 m4x3_mulv( player.to_world, (v3f){-0.15f,0.0f,-klength}, contact_front );
302 m4x3_mulv( player.to_world, (v3f){ 0.00f,0.0f, klength}, contact_back );
303 m3x3_mulv( player.to_world, (v3f){ 0.0f, 1.0f, 0.0f}, vup );
304 m3x3_mulv( player.to_world, (v3f){ 1.0f, 0.0f, 0.0f}, vside );
305
306 v3f cn0, cn1, cn2;
307
308 int contact_count =
309 sample_if_resistant( contact_front ) +
310 sample_if_resistant( contact_back ) +
311 sample_if_resistant( contact_norm );
312
313 if( contact_count < 3 )
314 {
315 player_start_air();
316 return;
317 }
318
319 v3f norm;
320 v3f v0, v1;
321 v3_sub( contact_norm, contact_front, v0 );
322 v3_sub( contact_back, contact_front, v1 );
323 v3_cross( v1, v0, norm );
324 v3_normalize( norm );
325
326 vg_line( contact_norm, contact_front, 0xff00ff00 );
327 vg_line( contact_back, contact_front, 0xff0000ff );
328
329 /* Surface alignment */
330 float angle = v3_dot( vup, norm );
331 v3_cross( vup, norm, axis );
332
333 if( angle < 0.999f )
334 {
335 v4f correction;
336 q_axis_angle( correction, axis, acosf(angle) );
337 q_mul( correction, player.rot, player.rot );
338 }
339
340 float resistance = v3_dot( norm, player.v );
341 if( resistance >= 0.0f )
342 {
343 player_start_air();
344 return;
345 }
346 else
347 {
348 v3_muladds( player.v, norm, -resistance, player.v );
349 }
350
351 /* This is where velocity integration used to be */
352
353 float slip = 0.0f;
354
355 player.co[1] = (contact_front[1]+contact_back[1])*0.5f;
356
357 v3f vel;
358 m3x3_mulv( player.to_local, player.v, vel );
359
360 /* Calculate local forces */
361
362 if( fabsf(vel[2]) > 0.01f )
363 slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
364
365 if( fabsf( slip ) > 1.2f )
366 slip = vg_signf( slip ) * 1.2f;
367 player.slip = slip;
368 player.reverse = -vg_signf(vel[2]);
369
370 float substep = ktimestep * 0.2f;
371 float fwd_resistance = (vg_get_button( "break" )? 5.0f: 0.02f) * -substep;
372
373 for( int i=0; i<5; i++ )
374 {
375 vel[2] = stable_force( vel[2], vg_signf( vel[2] ) * fwd_resistance );
376
377 /* This used to be -7.0, then -10.0 */
378 vel[0] = stable_force( vel[0], vg_signf( vel[0] ) * -8.5f *substep );
379 }
380
381 static double start_push = 0.0;
382 if( vg_get_button_down( "push" ) )
383 start_push = vg_time;
384
385 if( !vg_get_button("break") && vg_get_button( "push" ) )
386 {
387 float const k_maxpush = 16.0f,
388 k_pushaccel = 5.0f;
389
390 float cycle_time = vg_time-start_push,
391 amt = k_pushaccel * (sinf( cycle_time * 8.0f )*0.5f+0.5f)*ktimestep,
392 current = v3_length( vel ),
393 new_vel = vg_minf( current + amt, k_maxpush );
394 new_vel -= vg_minf(current, k_maxpush);
395 vel[2] -= new_vel * player.reverse;
396 }
397
398 m3x3_mulv( player.to_world, vel, player.v );
399
400 if( vg_get_button( "yawl" ) )
401 player.iY += 3.6f * ktimestep;
402 if( vg_get_button( "yawr" ) )
403 player.iY -= 3.6f * ktimestep;
404
405 float steer = vg_get_axis( "horizontal" );
406 player.iY -= vg_signf(steer)*powf(steer,2.0f) * 2.5f * ktimestep;
407
408 /* Too much lean and it starts to look like a snowboard here */
409 v2_lerp( player.board_xy, (v2f){ slip*0.25f, 0.0f },
410 ktimestep*5.0f, player.board_xy);
411 }
412
413 static void draw_cross(v3f pos,u32 colour, float scale)
414 {
415 v3f p0, p1;
416 v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
417 v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
418 vg_line( p0, p1, colour );
419 v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 );
420 v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 );
421 vg_line( p0, p1, colour );
422 v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 );
423 v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 );
424 vg_line( p0, p1, colour );
425 }
426
427 static void player_physics_air(void)
428 {
429 m3x3_mulv( player.vr, player.v, player.v );
430 draw_cross( player.land_target, 0xff0000ff, 1 );
431
432 v3f ground_pos;
433 v3_copy( player.co, ground_pos );
434 ground_pos[1] += 4.0f;
435
436 ray_hit hit;
437 hit.dist = INFINITY;
438 if( ray_world( ground_pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
439 {
440 if( hit.pos[1] > player.co[1] )
441 {
442 player.in_air = 0;
443
444 if( !ray_hit_is_ramp( &hit ) )
445 {
446 player.is_dead = 1;
447 character_ragdoll_copypose( &player.mdl, player.v );
448 }
449
450 return;
451 }
452 }
453
454 /* Prediction
455 */
456 float pstep = ktimestep*10.0f;
457
458 v3f pco, pco1, pv;
459 v3_copy( player.co, pco );
460 v3_copy( player.v, pv );
461
462 float time_to_impact = 0.0f;
463 float limiter = 1.0f;
464
465 for( int i=0; i<50; i++ )
466 {
467 v3_copy( pco, pco1 );
468 m3x3_mulv( player.vr_pstep, pv, pv );
469 apply_gravity( pv, pstep );
470 v3_muladds( pco, pv, pstep, pco );
471
472 //vg_line( pco, pco1, i&0x1?0xff000000:0xffffffff );
473
474 ray_hit contact;
475 v3f vdir;
476
477 v3_sub( pco, pco1, vdir );
478 contact.dist = v3_length( vdir );
479 v3_divs( vdir, contact.dist, vdir);
480
481 float orig_dist = contact.dist;
482 if( ray_world( pco1, vdir, &contact ))
483 {
484 v3f localup;
485 m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, localup );
486
487 float angle = v3_dot( localup, contact.normal );
488 v3f axis;
489 v3_cross( localup, contact.normal, axis );
490
491 time_to_impact += (contact.dist/orig_dist)*pstep;
492 limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
493 limiter = 1.0f-limiter;
494 limiter *= limiter;
495 limiter = 1.0f-limiter;
496
497 if( angle < 0.99f )
498 {
499 v4f correction;
500 q_axis_angle( correction, axis, acosf(angle)*0.05f*(1.0f-limiter) );
501 q_mul( correction, player.rot, player.rot );
502 }
503
504 draw_cross( contact.pos, 0xffff0000, 1 );
505 break;
506 }
507 time_to_impact += pstep;
508 }
509
510 player.iY -= vg_get_axis( "horizontal" ) * 3.6f * ktimestep;
511 {
512
513 float iX = vg_get_axis( "vertical" ) *
514 player.reverse * 3.6f * limiter * ktimestep;
515 static float siX = 0.0f;
516 siX = vg_lerpf( siX, iX, 0.3f );
517
518 v4f rotate;
519 v3f vside;
520
521 m3x3_mulv( player.to_world, (v3f){1.0f,0.0f,0.0f}, vside );
522
523 q_axis_angle( rotate, vside, siX );
524 q_mul( rotate, player.rot, player.rot );
525 }
526
527 v2f target = {0.0f,0.0f};
528 v2_muladds( target, (v2f){ vg_get_axis("h1"), vg_get_axis("v1") },
529 player.grab, target );
530 v2_lerp( player.board_xy, target, ktimestep*3.0f, player.board_xy );
531 }
532
533 static void player_do_motion(void)
534 {
535 float horizontal = vg_get_axis("horizontal"),
536 vertical = vg_get_axis("vertical");
537
538 if( player.in_air )
539 player_physics_air();
540
541 if( !player.in_air )
542 player_physics_ground();
543
544 /* Integrate velocity */
545 v3f prevco;
546 v3_copy( player.co, prevco );
547
548 apply_gravity( player.v, ktimestep );
549 v3_muladds( player.co, player.v, ktimestep, player.co );
550
551 /* Integrate inertia */
552 v4f rotate; v3f vup = {0.0f,1.0f,0.0f};
553 m3x3_mulv( player.to_world, vup, vup );
554
555 static float siY = 0.0f;
556
557 float lerpq = player.in_air? 0.04f: 0.3f;
558 siY = vg_lerpf( siY, player.iY, lerpq );
559
560 q_axis_angle( rotate, vup, siY );
561 q_mul( rotate, player.rot, player.rot );
562
563 player.iY = 0.0f; /* temp */
564
565 /* GATE COLLISION */
566
567 for( int i=0; i<world.gate_count; i++ )
568 {
569 teleport_gate *gate = &world.gates[i];
570
571 if( gate_intersect( gate, player.co, prevco ) )
572 {
573 m4x3_mulv( gate->transport, player.co, player.co );
574 m3x3_mulv( gate->transport, player.v, player.v );
575 m3x3_mulv( gate->transport, player.vl, player.vl );
576 m3x3_mulv( gate->transport, player.v_last, player.v_last );
577 m3x3_mulv( gate->transport, player.m, player.m );
578 m3x3_mulv( gate->transport, player.bob, player.bob );
579
580 v4f transport_rotation;
581 m3x3_q( gate->transport, transport_rotation );
582 q_mul( transport_rotation, player.rot, player.rot );
583
584 break;
585 }
586 }
587
588 /* Camera and character */
589 player_transform_update();
590
591 v3_lerp( player.vl, player.v, 0.05f, player.vl );
592
593 player.angles[0] = atan2f( player.vl[0], -player.vl[2] );
594 player.angles[1] = atan2f( -player.vl[1], sqrtf(player.vl[0]*player.vl[0]+
595 player.vl[2]*player.vl[2]) ) * 0.7f;
596 }
597
598 static int player_walkgrid_tri_walkable( u32 tri[3] )
599 {
600 return tri[0] < world.sm_road.vertex_count;
601 }
602
603 #define WALKGRID_SIZE 16
604 struct walkgrid
605 {
606 struct grid_sample
607 {
608 enum sample_type
609 {
610 k_sample_type_air, /* Nothing was hit. */
611 k_sample_type_invalid, /* The point is invalid, but there is a sample
612 underneath that can be used */
613 k_sample_type_valid, /* This point is good */
614 }
615 type;
616
617 v3f clip[2];
618 v3f pos;
619
620 enum traverse_state
621 {
622 k_traverse_none = 0x00,
623 k_traverse_h = 0x01,
624 k_traverse_v = 0x02
625 }
626 state;
627 }
628 samples[WALKGRID_SIZE][WALKGRID_SIZE];
629
630 boxf region;
631
632 float move; /* Current amount of movement we have left to apply */
633 v2f dir; /* The movement delta */
634 v2i cell_id;/* Current cell */
635 v2f pos; /* Local position (in cell) */
636 float h;
637 };
638
639 /*
640 * Get a sample at this pole location, will return 1 if the sample is valid,
641 * and pos will be updated to be the intersection location.
642 */
643 static void player_walkgrid_samplepole( struct grid_sample *s )
644 {
645 boxf region = {{ s->pos[0] -0.01f, s->pos[1] - 4.0f, s->pos[2] -0.01f},
646 { s->pos[0] +0.01f, s->pos[1] + 4.0f, s->pos[2] +0.01f}};
647
648 u32 geo[256];
649 v3f tri[3];
650 int len = bh_select( &world.geo.bhtris, region, geo, 256 );
651
652 const float k_minworld_y = -2000.0f;
653
654 float walk_height = k_minworld_y,
655 block_height = k_minworld_y;
656
657 s->type = k_sample_type_air;
658
659 for( int i=0; i<len; i++ )
660 {
661 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
662
663 for( int j=0; j<3; j++ )
664 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
665
666 v3f vdown = {0.0f,-1.0f,0.0f};
667 v3f sample_from;
668 v3_copy( s->pos, sample_from );
669 sample_from[1] = region[1][1];
670
671 float dist;
672 if( ray_tri( tri, sample_from, vdown, &dist ))
673 {
674 v3f p0;
675 v3_muladds( sample_from, vdown, dist, p0 );
676
677 if( player_walkgrid_tri_walkable(ptri) )
678 {
679 if( p0[1] > walk_height )
680 {
681 walk_height = p0[1];
682 }
683 }
684 else
685 {
686 if( p0[1] > block_height )
687 block_height = p0[1];
688 }
689 }
690 }
691
692 s->pos[1] = walk_height;
693
694 if( walk_height > k_minworld_y )
695 if( block_height > walk_height )
696 s->type = k_sample_type_invalid;
697 else
698 s->type = k_sample_type_valid;
699 else
700 s->type = k_sample_type_air;
701 }
702
703 float const k_gridscale = 0.5f;
704
705 enum eclipdir
706 {
707 k_eclipdir_h = 0,
708 k_eclipdir_v = 1
709 };
710
711 static void player_walkgrid_clip_blocker( struct grid_sample *sa,
712 struct grid_sample *sb,
713 struct grid_sample *st,
714 enum eclipdir dir )
715 {
716 v3f clipdir, pos;
717 int valid_a = sa->type == k_sample_type_valid,
718 valid_b = sb->type == k_sample_type_valid;
719 struct grid_sample *target = valid_a? sa: sb,
720 *other = valid_a? sb: sa;
721 v3_copy( target->pos, pos );
722 v3_sub( other->pos, target->pos, clipdir );
723
724 boxf cell_region;
725 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*2.1f, cell_region[0]);
726 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*2.1f, cell_region[1]);
727
728 u32 geo[256];
729 v3f tri[3];
730 int len = bh_select( &world.geo.bhtris, cell_region, geo, 256 );
731
732 float start_time = v3_length( clipdir ),
733 min_time = start_time;
734 v3_normalize( clipdir );
735 v3_muls( clipdir, 0.0001f, st->clip[dir] );
736
737 for( int i=0; i<len; i++ )
738 {
739 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
740 for( int j=0; j<3; j++ )
741 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
742
743 if( player_walkgrid_tri_walkable(ptri) )
744 continue;
745
746 float dist;
747 if(ray_tri( tri, pos, clipdir, &dist ))
748 {
749 if( dist > 0.0f && dist < min_time )
750 {
751 min_time = dist;
752 sb->type = k_sample_type_air;
753 }
754 }
755 }
756
757 if( !(min_time < start_time) )
758 min_time = 0.5f * k_gridscale;
759
760 min_time = vg_clampf( min_time/k_gridscale, 0.01f, 0.99f );
761
762 v3_muls( clipdir, min_time, st->clip[dir] );
763
764 v3f p0;
765 v3_muladds( target->pos, st->clip[dir], k_gridscale, p0 );
766 }
767
768 static void player_walkgrid_clip_edge( struct grid_sample *sa,
769 struct grid_sample *sb,
770 struct grid_sample *st, /* data store */
771 enum eclipdir dir )
772 {
773 v3f clipdir = { 0.0f, 0.0f, 0.0f }, pos;
774 int valid_a = sa->type == k_sample_type_valid,
775 valid_b = sb->type == k_sample_type_valid;
776
777 struct grid_sample *target = valid_a? sa: sb,
778 *other = valid_a? sb: sa;
779
780 v3_sub( other->pos, target->pos, clipdir );
781 clipdir[1] = 0.0f;
782
783 v3_copy( target->pos, pos );
784
785 boxf cell_region;
786 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*1.1f, cell_region[0]);
787 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*1.1f, cell_region[1]);
788
789 u32 geo[256];
790 int len = bh_select( &world.geo.bhtris, cell_region, geo, 256 );
791
792 float max_dist = 0.0f;
793 v3f tri[3];
794 v3f perp;
795 v3_cross( clipdir,(v3f){0.0f,1.0f,0.0f},perp );
796 v3_muls( clipdir, 0.001f, st->clip[dir] );
797
798 for( int i=0; i<len; i++ )
799 {
800 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
801 for( int j=0; j<3; j++ )
802 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
803
804 if( !player_walkgrid_tri_walkable(ptri) )
805 continue;
806
807 for( int k=0; k<3; k++ )
808 {
809 int ia = k,
810 ib = (k+1)%3;
811
812 v3f v0, v1;
813 v3_sub( tri[ia], pos, v0 );
814 v3_sub( tri[ib], pos, v1 );
815
816 if( (clipdir[2]*v0[0] - clipdir[0]*v0[2]) *
817 (clipdir[2]*v1[0] - clipdir[0]*v1[2]) < 0.0f )
818 {
819 float da = v3_dot(v0,perp),
820 db = v3_dot(v1,perp),
821 d = da-db,
822 qa = da/d;
823
824 v3f p0;
825 v3_muls( v1, qa, p0 );
826 v3_muladds( p0, v0, 1.0f-qa, p0 );
827
828 float h = v3_dot(p0,clipdir)/v3_dot(clipdir,clipdir);
829
830 if( h >= max_dist && h <= 1.0f )
831 {
832 max_dist = h;
833 float l = 1.0f/v3_length(clipdir);
834 v3_muls( p0, l, st->clip[dir] );
835 }
836 }
837 }
838 }
839 }
840
841 static const struct conf
842 {
843 struct confedge
844 {
845 /* i: sample index
846 * d: data index
847 * a: axis index
848 * o: the 'other' point to do a A/B test with
849 * if its -1, all AB is done.
850 */
851 int i0, i1,
852 d0, d1,
853 a0, a1,
854 o0, o1;
855 }
856 edges[2];
857 int edge_count;
858 }
859 k_walkgrid_configs[16] = {
860 {{},0},
861 {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
862 {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
863 {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
864
865 {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
866 {{{ 3,3, 3,0, 1,0, -1,-1 },
867 { 1,1, 0,1, 1,0, -1,-1 }}, 2},
868 {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
869 {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
870
871 {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
872 {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
873 {{{ 2,2, 1,3, 0,1, -1,-1 },
874 { 0,0, 0,0, 0,1, -1,-1 }}, 2},
875 {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
876
877 {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
878 {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
879 {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
880 {{},0},
881 };
882
883 /*
884 * Get a buffer of edges from cell location
885 */
886 static const struct conf *player_walkgrid_conf( struct walkgrid *wg,
887 v2i cell,
888 struct grid_sample *corners[4] )
889 {
890 corners[0] = &wg->samples[cell[1] ][cell[0] ];
891 corners[1] = &wg->samples[cell[1]+1][cell[0] ];
892 corners[2] = &wg->samples[cell[1]+1][cell[0]+1];
893 corners[3] = &wg->samples[cell[1] ][cell[0]+1];
894
895 u32 vd0 = corners[0]->type == k_sample_type_valid,
896 vd1 = corners[1]->type == k_sample_type_valid,
897 vd2 = corners[2]->type == k_sample_type_valid,
898 vd3 = corners[3]->type == k_sample_type_valid,
899 config = (vd0<<3) | (vd1<<2) | (vd2<<1) | vd3;
900
901 return &k_walkgrid_configs[ config ];
902 }
903
904 static void player_walkgrid_floor(v3f pos)
905 {
906 v3_muls( pos, 1.0f/k_gridscale, pos );
907 v3_floor( pos, pos );
908 v3_muls( pos, k_gridscale, pos );
909 }
910
911 /*
912 * Computes the barycentric coordinate of location on a triangle (vertical),
913 * then sets the Y position to the interpolation of the three points
914 */
915 static void player_walkgrid_stand_tri( v3f a, v3f b, v3f c, v3f pos )
916 {
917 v3f v0,v1,v2;
918 v3_sub( b, a, v0 );
919 v3_sub( c, a, v1 );
920 v3_sub( pos, a, v2 );
921
922 float d = v0[0]*v1[2] - v1[0]*v0[2],
923 v = (v2[0]*v1[2] - v1[0]*v2[2]) / d,
924 w = (v0[0]*v2[2] - v2[0]*v0[2]) / d,
925 u = 1.0f - v - w;
926
927 vg_line( pos, a, 0xffff0000 );
928 vg_line( pos, b, 0xff00ff00 );
929 vg_line( pos, c, 0xff0000ff );
930 pos[1] = u*a[1] + v*b[1] + w*c[1];
931 }
932
933 /*
934 * Get the minimum time value of pos+dir until a cell edge
935 *
936 * t[0] -> t[3] are the individual time values
937 * t[5] & t[6] are the maximum axis values
938 * t[6] is the minimum value
939 *
940 */
941 static void player_walkgrid_min_cell( float t[7], v2f pos, v2f dir )
942 {
943 v2f frac = { 1.0f/dir[0], 1.0f/dir[1] };
944
945 t[0] = 999.9f;
946 t[1] = 999.9f;
947 t[2] = 999.9f;
948 t[3] = 999.9f;
949
950 if( fabsf(dir[0]) > 0.0001f )
951 {
952 t[0] = (0.0f-pos[0]) * frac[0];
953 t[1] = (1.0f-pos[0]) * frac[0];
954 }
955 if( fabsf(dir[1]) > 0.0001f )
956 {
957 t[2] = (0.0f-pos[1]) * frac[1];
958 t[3] = (1.0f-pos[1]) * frac[1];
959 }
960
961 t[4] = vg_maxf(t[0],t[1]);
962 t[5] = vg_maxf(t[2],t[3]);
963 t[6] = vg_minf(t[4],t[5]);
964 }
965
966 static void player_walkgrid_iter(struct walkgrid *wg, int iter)
967 {
968
969 /*
970 * For each walkgrid iteration we are stepping through cells and determining
971 * the intersections with the grid, and any edges that are present
972 */
973
974 u32 icolours[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
975
976 v3f pa, pb, pc, pd, pl0, pl1;
977 pa[0] = wg->region[0][0] + (float)wg->cell_id[0] *k_gridscale;
978 pa[1] = (wg->region[0][1] + wg->region[1][1]) * 0.5f + k_gridscale;
979 pa[2] = wg->region[0][2] + (float)wg->cell_id[1] *k_gridscale;
980 pb[0] = pa[0];
981 pb[1] = pa[1];
982 pb[2] = pa[2] + k_gridscale;
983 pc[0] = pa[0] + k_gridscale;
984 pc[1] = pa[1];
985 pc[2] = pa[2] + k_gridscale;
986 pd[0] = pa[0] + k_gridscale;
987 pd[1] = pa[1];
988 pd[2] = pa[2];
989 #if 0
990 /* if you want to draw the current cell */
991 vg_line( pa, pb, 0xff00ffff );
992 vg_line( pb, pc, 0xff00ffff );
993 vg_line( pc, pd, 0xff00ffff );
994 vg_line( pd, pa, 0xff00ffff );
995 #endif
996 pl0[0] = pa[0] + wg->pos[0]*k_gridscale;
997 pl0[1] = pa[1];
998 pl0[2] = pa[2] + wg->pos[1]*k_gridscale;
999
1000 /*
1001 * If there are edges present, we need to create a 'substep' event, where
1002 * we find the intersection point, find the fully resolved position,
1003 * then the new pos dir is the intersection->resolution
1004 *
1005 * the resolution is applied in non-discretized space in order to create a
1006 * suitable vector for finding outflow, we want it to leave the cell so it
1007 * can be used by the quad
1008 */
1009
1010 v2f pos, dir;
1011 v2_copy( wg->pos, pos );
1012 v2_muls( wg->dir, wg->move, dir );
1013
1014 struct grid_sample *corners[4];
1015 v2f corners2d[4] = {{0.0f,0.0f},{0.0f,1.0f},{1.0f,1.0f},{1.0f,0.0f}};
1016 const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
1017
1018 float t[7];
1019 player_walkgrid_min_cell( t, pos, dir );
1020
1021 for( int i=0; i<conf->edge_count; i++ )
1022 {
1023 const struct confedge *edge = &conf->edges[i];
1024
1025 v2f e0, e1, n, r, target, res, tangent;
1026 e0[0] = corners2d[edge->i0][0] + corners[edge->d0]->clip[edge->a0][0];
1027 e0[1] = corners2d[edge->i0][1] + corners[edge->d0]->clip[edge->a0][2];
1028 e1[0] = corners2d[edge->i1][0] + corners[edge->d1]->clip[edge->a1][0];
1029 e1[1] = corners2d[edge->i1][1] + corners[edge->d1]->clip[edge->a1][2];
1030
1031 v3f pe0 = { pa[0] + e0[0]*k_gridscale,
1032 pa[1],
1033 pa[2] + e0[1]*k_gridscale };
1034 v3f pe1 = { pa[0] + e1[0]*k_gridscale,
1035 pa[1],
1036 pa[2] + e1[1]*k_gridscale };
1037
1038 v2_sub( e1, e0, tangent );
1039 n[0] = -tangent[1];
1040 n[1] = tangent[0];
1041 v2_normalize( n );
1042
1043 /*
1044 * If we find ourselfs already penetrating the edge, move back out a
1045 * little
1046 */
1047 v2_sub( e0, pos, r );
1048 float p1 = v2_dot(r,n);
1049
1050 if( -p1 < 0.0001f )
1051 {
1052 v2_muladds( pos, n, p1+0.0001f, pos );
1053 v2_copy( pos, wg->pos );
1054 v3f p_new = { pa[0] + pos[0]*k_gridscale,
1055 pa[1],
1056 pa[2] + pos[1]*k_gridscale };
1057 v3_copy( p_new, pl0 );
1058 }
1059
1060 v2_add( pos, dir, target );
1061
1062 v2f v1, v2, v3;
1063 v2_sub( e0, pos, v1 );
1064 v2_sub( target, pos, v2 );
1065
1066 v2_copy( n, v3 );
1067
1068 v2_sub( e0, target, r );
1069 float p = v2_dot(r,n),
1070 t1 = v2_dot(v1,v3)/v2_dot(v2,v3);
1071
1072 if( t1 < t[6] && t1 > 0.0f && -p < 0.001f )
1073 {
1074 v2_muladds( target, n, p+0.0001f, res );
1075
1076 v2f intersect;
1077 v2_muladds( pos, dir, t1, intersect );
1078 v2_copy( intersect, pos );
1079 v2_sub( res, intersect, dir );
1080
1081 v3f p_res = { pa[0] + res[0]*k_gridscale,
1082 pa[1],
1083 pa[2] + res[1]*k_gridscale };
1084 v3f p_int = { pa[0] + intersect[0]*k_gridscale,
1085 pa[1],
1086 pa[2] + intersect[1]*k_gridscale };
1087
1088 vg_line( pl0, p_int, icolours[iter%3] );
1089 v3_copy( p_int, pl0 );
1090 v2_copy( pos, wg->pos );
1091
1092 player_walkgrid_min_cell( t, pos, dir );
1093 }
1094 }
1095
1096 /*
1097 * Compute intersection with grid cell moving outwards
1098 */
1099 t[6] = vg_minf( t[6], 1.0f );
1100
1101 pl1[0] = pl0[0] + dir[0]*k_gridscale*t[6];
1102 pl1[1] = pl0[1];
1103 pl1[2] = pl0[2] + dir[1]*k_gridscale*t[6];
1104 vg_line( pl0, pl1, icolours[iter%3] );
1105
1106 if( t[6] < 1.0f )
1107 {
1108 /*
1109 * To figure out what t value created the clip so we know which edge
1110 * to wrap around
1111 */
1112
1113 if( t[4] < t[5] )
1114 {
1115 wg->pos[1] = pos[1] + dir[1]*t[6];
1116
1117 if( t[0] > t[1] ) /* left edge */
1118 {
1119 wg->pos[0] = 0.9999f;
1120 wg->cell_id[0] --;
1121
1122 if( wg->cell_id[0] == 0 )
1123 wg->move = -1.0f;
1124 }
1125 else /* Right edge */
1126 {
1127 wg->pos[0] = 0.0001f;
1128 wg->cell_id[0] ++;
1129
1130 if( wg->cell_id[0] == WALKGRID_SIZE-2 )
1131 wg->move = -1.0f;
1132 }
1133 }
1134 else
1135 {
1136 wg->pos[0] = pos[0] + dir[0]*t[6];
1137
1138 if( t[2] > t[3] ) /* bottom edge */
1139 {
1140 wg->pos[1] = 0.9999f;
1141 wg->cell_id[1] --;
1142
1143 if( wg->cell_id[1] == 0 )
1144 wg->move = -1.0f;
1145 }
1146 else /* top edge */
1147 {
1148 wg->pos[1] = 0.0001f;
1149 wg->cell_id[1] ++;
1150
1151 if( wg->cell_id[1] == WALKGRID_SIZE-2 )
1152 wg->move = -1.0f;
1153 }
1154 }
1155
1156 wg->move -= t[6];
1157 }
1158 else
1159 {
1160 v2_muladds( wg->pos, dir, wg->move, wg->pos );
1161 wg->move = 0.0f;
1162 }
1163 }
1164
1165 static void player_walkgrid_stand_cell(struct walkgrid *wg)
1166 {
1167 /*
1168 * NOTE: as opposed to the other function which is done in discretized space
1169 * this use a combination of both.
1170 */
1171
1172 v3f world;
1173 world[0] = wg->region[0][0]+((float)wg->cell_id[0]+wg->pos[0])*k_gridscale;
1174 world[1] = player.co[1];
1175 world[2] = wg->region[0][2]+((float)wg->cell_id[1]+wg->pos[1])*k_gridscale;
1176
1177 struct grid_sample *corners[4];
1178 const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
1179
1180 if( conf != k_walkgrid_configs )
1181 {
1182 if( conf->edge_count == 0 )
1183 {
1184 v3f v0;
1185
1186 /* Split the basic quad along the shortest diagonal */
1187 if( fabsf(corners[2]->pos[1] - corners[0]->pos[1]) <
1188 fabsf(corners[3]->pos[1] - corners[1]->pos[1]) )
1189 {
1190 vg_line( corners[2]->pos, corners[0]->pos, 0xffaaaaaa );
1191
1192 if( wg->pos[0] > wg->pos[1] )
1193 player_walkgrid_stand_tri( corners[0]->pos,
1194 corners[3]->pos,
1195 corners[2]->pos, world );
1196 else
1197 player_walkgrid_stand_tri( corners[0]->pos,
1198 corners[2]->pos,
1199 corners[1]->pos, world );
1200 }
1201 else
1202 {
1203 vg_line( corners[3]->pos, corners[1]->pos, 0xffaaaaaa );
1204
1205 if( wg->pos[0] < 1.0f-wg->pos[1] )
1206 player_walkgrid_stand_tri( corners[0]->pos,
1207 corners[3]->pos,
1208 corners[1]->pos, world );
1209 else
1210 player_walkgrid_stand_tri( corners[3]->pos,
1211 corners[2]->pos,
1212 corners[1]->pos, world );
1213 }
1214 }
1215 else
1216 {
1217 for( int i=0; i<conf->edge_count; i++ )
1218 {
1219 const struct confedge *edge = &conf->edges[i];
1220
1221 v3f p0, p1;
1222 v3_muladds( corners[edge->i0]->pos,
1223 corners[edge->d0]->clip[edge->a0], k_gridscale, p0 );
1224 v3_muladds( corners[edge->i1]->pos,
1225 corners[edge->d1]->clip[edge->a1], k_gridscale, p1 );
1226
1227 /*
1228 * Find penetration distance between player position and the edge
1229 */
1230
1231 v2f normal = { -(p1[2]-p0[2]), p1[0]-p0[0] },
1232 rel = { world[0]-p0[0], world[2]-p0[2] };
1233
1234 if( edge->o0 == -1 )
1235 {
1236 /* No subregions (default case), just use triangle created by
1237 * i0, e0, e1 */
1238 player_walkgrid_stand_tri( corners[edge->i0]->pos,
1239 p0,
1240 p1, world );
1241 }
1242 else
1243 {
1244 /*
1245 * Test if we are in the first region, which is
1246 * edge.i0, edge.e0, edge.o0,
1247 */
1248 v3f v0, ref;
1249 v3_sub( p0, corners[edge->o0]->pos, ref );
1250 v3_sub( world, corners[edge->o0]->pos, v0 );
1251
1252 vg_line( corners[edge->o0]->pos, p0, 0xffffff00 );
1253 vg_line( corners[edge->o0]->pos, world, 0xff000000 );
1254
1255 if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
1256 {
1257 player_walkgrid_stand_tri( corners[edge->i0]->pos,
1258 p0,
1259 corners[edge->o0]->pos, world );
1260 }
1261 else
1262 {
1263 if( edge->o1 == -1 )
1264 {
1265 /*
1266 * No other edges mean we just need to use the opposite
1267 *
1268 * e0, e1, o0 (in our case, also i1)
1269 */
1270 player_walkgrid_stand_tri( p0,
1271 p1,
1272 corners[edge->o0]->pos, world );
1273 }
1274 else
1275 {
1276 /*
1277 * Note: this v0 calculation can be ommited with the
1278 * current tileset.
1279 *
1280 * the last two triangles we have are:
1281 * e0, e1, o1
1282 * and
1283 * e1, i1, o1
1284 */
1285 v3_sub( p1, corners[edge->o1]->pos, ref );
1286 v3_sub( world, corners[edge->o1]->pos, v0 );
1287 vg_line( corners[edge->o1]->pos, p1, 0xff00ffff );
1288
1289 if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
1290 {
1291 player_walkgrid_stand_tri( p0,
1292 p1,
1293 corners[edge->o1]->pos,
1294 world );
1295 }
1296 else
1297 {
1298 player_walkgrid_stand_tri( p1,
1299 corners[edge->i1]->pos,
1300 corners[edge->o1]->pos,
1301 world );
1302 }
1303 }
1304 }
1305 }
1306 }
1307 }
1308 }
1309
1310 v3_copy( world, player.co );
1311 }
1312
1313 static void player_walkgrid_getsurface(void)
1314 {
1315 float const k_stepheight = 0.5f;
1316 float const k_miny = 0.6f;
1317 float const k_height = 1.78f;
1318 float const k_region_size = (float)WALKGRID_SIZE/2.0f * k_gridscale;
1319
1320 static struct walkgrid wg;
1321
1322 v3f cell;
1323 v3_copy( player.co, cell );
1324 player_walkgrid_floor( cell );
1325
1326 v3_muladds( cell, (v3f){-1.0f,-1.0f,-1.0f}, k_region_size, wg.region[0] );
1327 v3_muladds( cell, (v3f){ 1.0f, 1.0f, 1.0f}, k_region_size, wg.region[1] );
1328
1329
1330 /*
1331 * Create player input vector
1332 */
1333 v3f delta = {0.0f,0.0f,0.0f};
1334 v3f fwd = { -sinf(-player.angles[0]), 0.0f, -cosf(-player.angles[0]) },
1335 side = { -fwd[2], 0.0f, fwd[0] };
1336
1337 /* Temp */
1338 if( !vg_console_enabled() )
1339 {
1340 if( glfwGetKey( vg_window, GLFW_KEY_W ) )
1341 v3_muladds( delta, fwd, ktimestep*k_walkspeed, delta );
1342 if( glfwGetKey( vg_window, GLFW_KEY_S ) )
1343 v3_muladds( delta, fwd, -ktimestep*k_walkspeed, delta );
1344
1345 if( glfwGetKey( vg_window, GLFW_KEY_A ) )
1346 v3_muladds( delta, side, -ktimestep*k_walkspeed, delta );
1347 if( glfwGetKey( vg_window, GLFW_KEY_D ) )
1348 v3_muladds( delta, side, ktimestep*k_walkspeed, delta );
1349
1350 v3_muladds( delta, fwd,
1351 vg_get_axis("vertical")*-ktimestep*k_walkspeed, delta );
1352 v3_muladds( delta, side,
1353 vg_get_axis("horizontal")*ktimestep*k_walkspeed, delta );
1354 }
1355
1356 /*
1357 * Create our move in grid space
1358 */
1359 wg.dir[0] = delta[0] * (1.0f/k_gridscale);
1360 wg.dir[1] = delta[2] * (1.0f/k_gridscale);
1361 wg.move = 1.0f;
1362
1363 v2f region_pos =
1364 {
1365 (player.co[0] - wg.region[0][0]) * (1.0f/k_gridscale),
1366 (player.co[2] - wg.region[0][2]) * (1.0f/k_gridscale)
1367 };
1368 v2f region_cell_pos;
1369 v2_floor( region_pos, region_cell_pos );
1370 v2_sub( region_pos, region_cell_pos, wg.pos );
1371
1372 wg.cell_id[0] = region_cell_pos[0];
1373 wg.cell_id[1] = region_cell_pos[1];
1374
1375 for(int y=0; y<WALKGRID_SIZE; y++ )
1376 {
1377 for(int x=0; x<WALKGRID_SIZE; x++ )
1378 {
1379 struct grid_sample *s = &wg.samples[y][x];
1380 v3_muladds( wg.region[0], (v3f){ x, 0, y }, k_gridscale, s->pos );
1381 s->state = k_traverse_none;
1382 s->type = k_sample_type_air;
1383 v3_zero( s->clip[0] );
1384 v3_zero( s->clip[1] );
1385 }
1386 }
1387
1388 v2i border[WALKGRID_SIZE*WALKGRID_SIZE];
1389 v2i *cborder = border;
1390 u32 border_length = 1;
1391
1392 struct grid_sample *base = NULL;
1393
1394 v2i starters[] = {{0,0},{1,1},{0,1},{1,0}};
1395
1396 for( int i=0;i<4;i++ )
1397 {
1398 v2i test;
1399 v2i_add( wg.cell_id, starters[i], test );
1400 v2i_copy( test, border[0] );
1401 base = &wg.samples[test[1]][test[0]];
1402
1403 base->pos[1] = cell[1];
1404 player_walkgrid_samplepole( base );
1405
1406 if( base->type == k_sample_type_valid )
1407 break;
1408 else
1409 base->type = k_sample_type_air;
1410 }
1411
1412 vg_line_pt3( base->pos, 0.1f, 0xffffffff );
1413
1414 int iter = 0;
1415
1416 while( border_length )
1417 {
1418 v2i directions[] = {{1,0},{0,1},{-1,0},{0,-1}};
1419
1420 v2i *old_border = cborder;
1421 int len = border_length;
1422
1423 border_length = 0;
1424 cborder = old_border+len;
1425
1426 for( int i=0; i<len; i++ )
1427 {
1428 v2i co;
1429 v2i_copy( old_border[i], co );
1430 struct grid_sample *sa = &wg.samples[co[1]][co[0]];
1431
1432 for( int j=0; j<4; j++ )
1433 {
1434 v2i newp;
1435 v2i_add( co, directions[j], newp );
1436
1437 if( newp[0] < 0 || newp[1] < 0 ||
1438 newp[0] == WALKGRID_SIZE || newp[1] == WALKGRID_SIZE )
1439 continue;
1440
1441 struct grid_sample *sb = &wg.samples[newp[1]][newp[0]];
1442 enum traverse_state thismove = j%2==0? 1: 2;
1443
1444 if( (sb->state & thismove) == 0x00 ||
1445 sb->type == k_sample_type_air )
1446 {
1447 sb->pos[1] = sa->pos[1];
1448
1449 player_walkgrid_samplepole( sb );
1450
1451 if( sb->type != k_sample_type_air )
1452 {
1453 /*
1454 * Need to do a blocker pass
1455 */
1456
1457 struct grid_sample *store = (j>>1 == 0)? sa: sb;
1458 player_walkgrid_clip_blocker( sa, sb, store, j%2 );
1459
1460
1461 if( sb->type != k_sample_type_air )
1462 {
1463 vg_line( sa->pos, sb->pos, 0xffffffff );
1464
1465 if( sb->state == k_traverse_none )
1466 v2i_copy( newp, cborder[ border_length ++ ] );
1467 }
1468 else
1469 {
1470 v3f p1;
1471 v3_muladds( sa->pos, store->clip[j%2], k_gridscale, p1 );
1472 vg_line( sa->pos, p1, 0xffffffff );
1473 }
1474 }
1475 else
1476 {
1477 /*
1478 * A clipping pass is now done on the edge of the walkable
1479 * surface
1480 */
1481
1482 struct grid_sample *store = (j>>1 == 0)? sa: sb;
1483 player_walkgrid_clip_edge( sa, sb, store, j%2 );
1484
1485 v3f p1;
1486 v3_muladds( sa->pos, store->clip[j%2], k_gridscale, p1 );
1487 vg_line( sa->pos, p1, 0xffffffff );
1488 }
1489
1490 sb->state |= thismove;
1491 }
1492 }
1493
1494 sa->state = k_traverse_h|k_traverse_v;
1495 }
1496
1497 iter ++;
1498 if( iter == walk_grid_iterations )
1499 break;
1500 }
1501
1502 /* Draw connections */
1503 struct grid_sample *corners[4];
1504 for( int x=0; x<WALKGRID_SIZE-1; x++ )
1505 {
1506 for( int z=0; z<WALKGRID_SIZE-1; z++ )
1507 {
1508 const struct conf *conf =
1509 player_walkgrid_conf( &wg, (v2i){x,z}, corners );
1510
1511 for( int i=0; i<conf->edge_count; i++ )
1512 {
1513 const struct confedge *edge = &conf->edges[i];
1514
1515 v3f p0, p1;
1516 v3_muladds( corners[edge->i0]->pos,
1517 corners[edge->d0]->clip[edge->a0], k_gridscale, p0 );
1518 v3_muladds( corners[edge->i1]->pos,
1519 corners[edge->d1]->clip[edge->a1], k_gridscale, p1 );
1520
1521 vg_line( p0, p1, 0xff0000ff );
1522 }
1523 }
1524 }
1525
1526 /*
1527 * Commit player movement into the grid
1528 */
1529
1530 if( v3_length2(delta) <= 0.00001f )
1531 return;
1532
1533 int i=0;
1534 for(; i<8 && wg.move > 0.001f; i++ )
1535 player_walkgrid_iter( &wg, i );
1536
1537 player_walkgrid_stand_cell( &wg );
1538 }
1539
1540 static void player_walkgrid(void)
1541 {
1542 player_walkgrid_getsurface();
1543
1544 m4x3_mulv( player.to_world, (v3f){0.0f,1.8f,0.0f}, player.camera_pos );
1545 player_mouseview();
1546 player_transform_update();
1547 }
1548
1549 static void player_animate(void)
1550 {
1551 /* Camera position */
1552 v3_sub( player.v, player.v_last, player.a );
1553 v3_copy( player.v, player.v_last );
1554
1555 v3_add( player.m, player.a, player.m );
1556 v3_lerp( player.m, (v3f){0.0f,0.0f,0.0f}, 0.1f, player.m );
1557 v3f target;
1558
1559 player.m[0] = vg_clampf( player.m[0], -2.0f, 2.0f );
1560 player.m[1] = vg_clampf( player.m[1], -0.2f, 5.0f );
1561 player.m[2] = vg_clampf( player.m[2], -2.0f, 2.0f );
1562 v3_copy( player.m, target );
1563 v3_lerp( player.bob, target, 0.2f, player.bob );
1564
1565 /* Head */
1566 float lslip = fabsf(player.slip); //vg_minf( 0.4f, slip );
1567
1568 float grabt = vg_get_axis( "grabr" )*0.5f+0.5f;
1569 player.grab = vg_lerpf( player.grab, grabt, 0.04f );
1570
1571 float kheight = 2.0f,
1572 kleg = 0.6f;
1573
1574 v3f head;
1575 head[0] = 0.0f;
1576 head[1] = (0.3f+cosf(lslip)*0.5f*(1.0f-player.grab*0.7f)) * kheight;
1577 head[2] = 0.0f;
1578
1579 v3f offset;
1580 m3x3_mulv( player.to_local, player.bob, offset );
1581
1582 offset[0] *= 0.3333f;
1583 offset[1] *= -0.25f;
1584 offset[2] *= 0.7f;
1585 v3_muladds( head, offset, 0.7f, head );
1586 head[1] = vg_clampf( head[1], 0.3f, kheight );
1587
1588 /*
1589 * Animation blending
1590 * ===========================================
1591 */
1592
1593 static float fslide = 0.0f;
1594 static float fdirz = 0.0f;
1595 static float fdirx = 0.0f;
1596 static float fstand = 0.0f;
1597 static float ffly = 0.0f;
1598
1599 float speed = v3_length( player.v );
1600
1601 fstand = vg_lerpf(fstand, 1.0f-vg_clampf(speed*0.03f,0.0f,1.0f),0.1f);
1602 fslide = vg_lerpf(fslide, vg_clampf(lslip+fabsf(offset[0])*0.2f,
1603 0.0f,1.0f), 0.04f);
1604 fdirz = vg_lerpf(fdirz, player.reverse > 0.0f? 1.0f: 0.0f, 0.04f );
1605 fdirx = vg_lerpf(fdirx, player.slip < 0.0f? 1.0f: 0.0f, 0.04f );
1606 ffly = vg_lerpf(ffly, player.in_air? 1.0f: 0.0f, 0.04f );
1607
1608 character_pose_reset( &player.mdl );
1609
1610 /* TODO */
1611 fstand = 1.0f;
1612
1613 float amt_air = ffly*ffly,
1614 amt_ground = 1.0f-amt_air,
1615 amt_std = (1.0f-fslide) * amt_ground,
1616 amt_stand = amt_std * fstand,
1617 amt_aero = amt_std * (1.0f-fstand),
1618 amt_slide = amt_ground * fslide;
1619
1620 character_final_pose( &player.mdl, offset, &pose_stand, amt_stand*fdirz );
1621 character_final_pose( &player.mdl, offset,
1622 &pose_stand_reverse, amt_stand * (1.0f-fdirz) );
1623
1624 character_final_pose( &player.mdl, offset, &pose_aero, amt_aero*fdirz );
1625 character_final_pose( &player.mdl, offset,
1626 &pose_aero_reverse, amt_aero * (1.0f-fdirz) );
1627
1628 character_final_pose( &player.mdl, offset, &pose_slide, amt_slide*fdirx );
1629 character_final_pose( &player.mdl, offset,
1630 &pose_slide1, amt_slide*(1.0f-fdirx) );
1631
1632 character_final_pose( &player.mdl, (v3f){0.0f,0.0f,0.0f},
1633 &pose_fly, amt_air );
1634
1635 #if 0
1636 static float fupper = 0.0f;
1637 fupper = vg_lerpf( fupper, -vg_get_axis("horizontal")*0.2f, 0.1f );
1638 character_yaw_upper( &player.mdl, fupper );
1639 #endif
1640
1641 /* Camera position */
1642 v3_lerp( player.smooth_localcam, player.mdl.cam_pos, 0.08f,
1643 player.smooth_localcam );
1644 v3_muladds( player.smooth_localcam, offset, 0.7f, player.camera_pos );
1645 player.camera_pos[1] = vg_clampf( player.camera_pos[1], 0.3f, kheight );
1646
1647 m4x3_mulv( player.to_world, player.camera_pos, player.camera_pos );
1648
1649 player.air_blend = vg_lerpf( player.air_blend, player.in_air, 0.04f );
1650 v3_muladds( player.camera_pos, player.v, -0.05f*player.air_blend,
1651 player.camera_pos );
1652
1653 /*
1654 * Additive effects
1655 * ==========================
1656 */
1657 struct ik_basic *arm_l = &player.mdl.ik_arm_l,
1658 *arm_r = &player.mdl.ik_arm_r;
1659
1660 v3f localv;
1661 m3x3_mulv( player.to_local, player.v, localv );
1662
1663 #if 0
1664 v3_muladds( arm_l->end, localv, -0.01f, arm_l->end );
1665 v3_muladds( arm_r->end, localv, -0.01f, arm_r->end );
1666 #endif
1667
1668 /* New board transformation */
1669 v4f board_rotation; v3f board_location;
1670
1671 v4f rz, rx;
1672 q_axis_angle( rz, (v3f){ 0.0f, 0.0f, 1.0f }, player.board_xy[0] );
1673 q_axis_angle( rx, (v3f){ 1.0f, 0.0f, 0.0f }, player.board_xy[1] );
1674 q_mul( rx, rz, board_rotation );
1675
1676 v3f *mboard = player.mdl.matrices[k_chpart_board];// player.mboard;
1677 q_m3x3( board_rotation, mboard );
1678 m3x3_mulv( mboard, (v3f){ 0.0f, -0.5f, 0.0f }, board_location );
1679 v3_add( (v3f){0.0f,0.5f,0.0f}, board_location, board_location );
1680 v3_copy( board_location, mboard[3] );
1681
1682
1683 float wheel_r = offset[0]*-0.4f;
1684 v4f qwheel;
1685 q_axis_angle( qwheel, (v3f){0.0f,1.0f,0.0f}, wheel_r );
1686
1687 q_m3x3( qwheel, player.mdl.matrices[k_chpart_wb] );
1688
1689 m3x3_transpose( player.mdl.matrices[k_chpart_wb],
1690 player.mdl.matrices[k_chpart_wf] );
1691 v3_copy( player.mdl.offsets[k_chpart_wb],
1692 player.mdl.matrices[k_chpart_wb][3] );
1693 v3_copy( player.mdl.offsets[k_chpart_wf],
1694 player.mdl.matrices[k_chpart_wf][3] );
1695
1696 m4x3_mul( mboard, player.mdl.matrices[k_chpart_wb],
1697 player.mdl.matrices[k_chpart_wb] );
1698 m4x3_mul( mboard, player.mdl.matrices[k_chpart_wf],
1699 player.mdl.matrices[k_chpart_wf] );
1700
1701 m4x3_mulv( mboard, player.mdl.ik_leg_l.end, player.mdl.ik_leg_l.end );
1702 m4x3_mulv( mboard, player.mdl.ik_leg_r.end, player.mdl.ik_leg_r.end );
1703
1704
1705 v3_copy( player.mdl.ik_arm_l.end, player.handl_target );
1706 v3_copy( player.mdl.ik_arm_r.end, player.handr_target );
1707
1708 if( 1||player.in_air )
1709 {
1710 float tuck = player.board_xy[1],
1711 tuck_amt = fabsf( tuck ) * (1.0f-fabsf(player.board_xy[0]));
1712
1713 float crouch = player.grab*0.3f;
1714 v3_muladds( player.mdl.ik_body.base, (v3f){0.0f,-1.0f,0.0f},
1715 crouch, player.mdl.ik_body.base );
1716 v3_muladds( player.mdl.ik_body.end, (v3f){0.0f,-1.0f,0.0f},
1717 crouch*1.2f, player.mdl.ik_body.end );
1718
1719 if( tuck < 0.0f )
1720 {
1721 //foot_l *= 1.0f-tuck_amt*1.5f;
1722
1723 if( player.grab > 0.1f )
1724 {
1725 m4x3_mulv( mboard, (v3f){0.1f,0.14f,0.6f},
1726 player.handl_target );
1727 }
1728 }
1729 else
1730 {
1731 //foot_r *= 1.0f-tuck_amt*1.4f;
1732
1733 if( player.grab > 0.1f )
1734 {
1735 m4x3_mulv( mboard, (v3f){0.1f,0.14f,-0.6f},
1736 player.handr_target );
1737 }
1738 }
1739 }
1740
1741 v3_lerp( player.handl, player.handl_target, 0.1f, player.handl );
1742 v3_lerp( player.handr, player.handr_target, 0.1f, player.handr );
1743
1744 v3_copy( player.handl, player.mdl.ik_arm_l.end );
1745 v3_copy( player.handr, player.mdl.ik_arm_r.end );
1746
1747 /* Head rotation */
1748
1749 static float rhead = 0.0f;
1750 static const float klook_max = 0.8f;
1751 rhead = vg_lerpf( rhead,
1752 vg_clampf( atan2f(localv[2],-localv[0]),-klook_max,klook_max), 0.04f );
1753 player.mdl.rhead = rhead;
1754 }
1755
1756 static int giftwrapXZ( v3f *points, int *output, int len )
1757 {
1758 int l, p, q, count;
1759
1760 if( len < 3 )
1761 return 0;
1762
1763 l = 0;
1764 for( int i=1; i<len; i++ )
1765 if( points[i][0] < points[l][0] )
1766 l = i;
1767
1768 p = l;
1769 count = 0;
1770 do
1771 {
1772 if( count >= len )
1773 {
1774 vg_error ("MANIFOLD ERR (%d)\n", count );
1775 return 0;
1776 }
1777 output[ count ++ ] = p;
1778
1779 q = (p+1)%len;
1780
1781 for( int i=0; i<len; i++ )
1782 {
1783 float orient =
1784 (points[i][2]-points[p][2])*(points[q][0]-points[i][0]) -
1785 (points[i][0]-points[p][0])*(points[q][2]-points[i][2]);
1786
1787 if( orient > 0.0001f )
1788 {
1789 q = i;
1790 }
1791 }
1792 p = q;
1793 }
1794 while( p != l );
1795
1796 return count;
1797 }
1798
1799 static void player_do_collision( rigidbody *rb )
1800 {
1801 /*
1802 * If point is inside box
1803 * find normal (theres 8 simple pyramid regions for this, x>y/dim .. etc)
1804 * find distance (same sorta thing)
1805 *
1806 * apply normal impulse to rotation
1807 * correct position based on new penetration amount if needed
1808 * apply normal impulse to velocity
1809 */
1810
1811 v3f pfront, pback;
1812 m4x3_mulv( player.to_world, (v3f){ 0.0f,0.0f,-1.0f }, pfront );
1813 m4x3_mulv( player.to_world, (v3f){ 0.0f,0.0f, 1.0f }, pback );
1814
1815 float const kheight = 2.0f;
1816
1817 v3f verts[8];
1818
1819 v3f a, b;
1820 v3_copy( rb->bbx[0], a );
1821 v3_copy( rb->bbx[1], b );
1822
1823 m4x3f compound;
1824 m4x3_mul( player.to_local, rb->to_world, compound );
1825
1826 m4x3_mulv( compound, (v3f){ a[0], a[1], a[2] }, verts[0] );
1827 m4x3_mulv( compound, (v3f){ a[0], b[1], a[2] }, verts[1] );
1828 m4x3_mulv( compound, (v3f){ b[0], b[1], a[2] }, verts[2] );
1829 m4x3_mulv( compound, (v3f){ b[0], a[1], a[2] }, verts[3] );
1830 m4x3_mulv( compound, (v3f){ a[0], a[1], b[2] }, verts[4] );
1831 m4x3_mulv( compound, (v3f){ a[0], b[1], b[2] }, verts[5] );
1832 m4x3_mulv( compound, (v3f){ b[0], b[1], b[2] }, verts[6] );
1833 m4x3_mulv( compound, (v3f){ b[0], a[1], b[2] }, verts[7] );
1834
1835 int const indices[12][2] = {
1836 {0,1},{1,2},{2,3},{3,0},{4,5},{5,6},{6,7},{7,4},
1837 {0,4},{1,5},{2,6},{3,7}
1838 };
1839
1840 v3f hull[12*2 + 8];
1841 int hull_indices[12*2 + 8];
1842 int hull_len = 0;
1843
1844 for( int i=0; i<8; i++ )
1845 {
1846 int ia = indices[i][0];
1847 float ya = verts[ia][1];
1848
1849 if( ya > 0.2f && ya < kheight )
1850 {
1851 int add_point = 1;
1852 for( int j=0; j<hull_len; j++ )
1853 {
1854 v2f delta = { verts[ia][0]-hull[j][0], verts[ia][2]-hull[j][2] };
1855 if( v2_length2( delta ) < 0.0004f )
1856 {
1857 add_point = 0;
1858 break;
1859 }
1860 }
1861
1862 if( add_point )
1863 v3_copy( verts[ia], hull[hull_len] );
1864
1865 hull[hull_len ++][1] = 0.2f;
1866 }
1867 }
1868
1869 for( int i=0; i<vg_list_size(indices); i++ )
1870 {
1871 int ia = indices[i][0],
1872 ib = indices[i][1];
1873
1874 v3f p0, p1;
1875
1876 float ya = verts[ia][1],
1877 yb = verts[ib][1],
1878 d = 1.0f/(yb-ya),
1879 qa;
1880
1881 float planes[] = { 0.2f, kheight };
1882
1883 for( int k=0; k<vg_list_size(planes); k++ )
1884 {
1885 float clip = planes[k];
1886
1887 if( (ya-clip) * (yb-clip) < 0.0f )
1888 {
1889 v3_muls( verts[ia], (yb-clip)*d, p0 );
1890 v3_muladds( p0, verts[ib], -(ya-clip)*d, p0 );
1891
1892 int add_point = 1;
1893 for( int j=0; j<hull_len; j++ )
1894 {
1895 v2f delta = { p0[0]-hull[j][0], p0[2]-hull[j][2] };
1896 if( v2_length2( delta ) < 0.0004f )
1897 {
1898 add_point = 0;
1899 break;
1900 }
1901 }
1902
1903 if( add_point )
1904 v3_copy( p0, hull[hull_len ++] );
1905
1906 m4x3_mulv( player.to_world, p0, p0 );
1907 vg_line_pt3( p0, 0.1f, 0xffffff00 );
1908 }
1909 }
1910 }
1911
1912 if( hull_len < 3 )
1913 return;
1914
1915 int len = giftwrapXZ( hull, hull_indices, hull_len );
1916 for( int i=0; i<len; i++ )
1917 {
1918 v3f p0, p1, p2, p3;
1919 v3_copy( hull[hull_indices[i]], p0 );
1920 v3_copy( hull[hull_indices[(i+1)%len]], p1 );
1921 p0[1] = 0.2f;
1922 p1[1] = 0.2f;
1923 v3_add( p0, (v3f){0,kheight-0.2f,0}, p2 );
1924 v3_add( p1, (v3f){0,kheight-0.2f,0}, p3 );
1925
1926 m4x3_mulv( player.to_world, p0, p0 );
1927 m4x3_mulv( player.to_world, p1, p1 );
1928 m4x3_mulv( player.to_world, p2, p2 );
1929 m4x3_mulv( player.to_world, p3, p3 );
1930
1931 vg_line2( p0, p1, 0xff00ffff, 0xff000000 );
1932 vg_line( p2, p3, 0xff00ffff );
1933 vg_line( p0, p2, 0xff00ffa0 );
1934 }
1935
1936 v2f endpoints[] = {{ 0.0f, -1.0f },{ 0.0f, 1.0f }};
1937
1938 for( int j=0; j<vg_list_size(endpoints); j++ )
1939 {
1940 v2f point;
1941 v2_copy( endpoints[j], point );
1942
1943 int collide = 1;
1944 float min_dist = 99999.9f;
1945 v2f normal = {0.0f,0.0f};
1946 for( int i=0; i<len; i++ )
1947 {
1948 v2f p0, p1;
1949 p0[0] = hull[hull_indices[i]][0];
1950 p0[1] = hull[hull_indices[i]][2];
1951 p1[0] = hull[hull_indices[(i+1)%len]][0];
1952 p1[1] = hull[hull_indices[(i+1)%len]][2];
1953
1954 v2f t,n, rel;
1955 v2_sub( p1, p0, t );
1956 n[0] = -t[1];
1957 n[1] = t[0];
1958 v2_normalize(n);
1959
1960 v2_sub( point, p0, rel );
1961 float d = -v2_dot( n, rel ) + 0.5f;
1962
1963 if( d < 0.0f )
1964 {
1965 collide = 0;
1966 break;
1967 }
1968
1969 if( d < min_dist )
1970 {
1971 min_dist = d;
1972 v2_copy( n, normal );
1973 }
1974 }
1975
1976 if( collide )
1977 {
1978 v3f p0, p1;
1979 p0[0] = 0.0f;
1980 p0[1] = 0.2f;
1981 p0[2] = -1.0f;
1982
1983 p1[0] = p0[0] + normal[0]*min_dist;
1984 p1[1] = p0[1];
1985 p1[2] = p0[2] + normal[1]*min_dist;
1986
1987 m4x3_mulv( player.to_world, p0, p0 );
1988 m4x3_mulv( player.to_world, p1, p1 );
1989
1990 vg_line( p0, p1, 0xffffffff );
1991
1992 v3f vel;
1993 m3x3_mulv( player.to_local, player.v, vel );
1994 vel[1] = vel[2];
1995
1996 float vn = vg_maxf( -v2_dot( vel, normal ), 0.0f );
1997 vn += -0.2f * (1.0f/k_rb_delta) * vg_minf( 0.0f, -min_dist+0.04f );
1998
1999 v2f impulse;
2000 if( vn > 14.0f )
2001 {
2002 player.is_dead = 1;
2003 character_ragdoll_copypose( &player.mdl, player.v );
2004 return;
2005 }
2006
2007 if( vn > 0.0f )
2008 {
2009 v2_muls( normal, min_dist, impulse );
2010 float rotation = v2_cross( point, impulse )*0.08f;
2011 v4f rot;
2012 v3f up = {0.0f,1.0f,0.0f};
2013 m3x3_mulv( player.to_world, up, up );
2014 q_axis_angle( rot, up, -rotation );
2015 q_mul( rot, player.rot, player.rot );
2016 }
2017
2018 v2_muls( normal, vn*0.03f, impulse );
2019 v3f impulse_world = { impulse[0], 0.0f, impulse[1] };
2020
2021 m3x3_mulv( player.to_world, impulse_world, impulse_world );
2022 v3_add( impulse_world, player.v, player.v );
2023 }
2024 }
2025 }
2026
2027 static void player_audio(void)
2028 {
2029 float speed = vg_minf(v3_length( player.v )*0.1f,1.0f),
2030 attn = v3_dist( player.co, player.camera[3] )+1.0f;
2031 attn = (1.0f/(attn*attn)) * speed;
2032
2033 static float air = 0.0f;
2034 air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 0.7f);
2035
2036 v3f ears = { 1.0f,0.0f,0.0f };
2037 v3f delta;
2038
2039 v3_sub( player.co, player.camera[3], delta );
2040 v3_normalize( delta );
2041 m3x3_mulv( player.camera, ears, ears );
2042
2043 float pan = v3_dot( ears, delta );
2044 audio_player0.pan = pan;
2045 audio_player1.pan = pan;
2046 audio_player2.pan = pan;
2047
2048 if( freecam )
2049 {
2050 audio_player0.vol = 0.0f;
2051 audio_player1.vol = 0.0f;
2052 audio_player2.vol = 0.0f;
2053 }
2054 else
2055 {
2056 if( player.is_dead )
2057 {
2058 audio_player0.vol = 0.0f;
2059 audio_player1.vol = 0.0f;
2060 audio_player2.vol = 0.0f;
2061 }
2062 else
2063 {
2064 float slide = vg_clampf( fabsf(player.slip), 0.0f, 1.0f );
2065 audio_player0.vol = (1.0f-air)*attn*(1.0f-slide);
2066 audio_player1.vol = air *attn;
2067 audio_player2.vol = (1.0f-air)*attn*slide;
2068 }
2069 }
2070 }
2071
2072 static void player_update(void)
2073 {
2074 for( int i=0; i<player.land_log_count; i++ )
2075 draw_cross( player.land_target_log[i], player.land_target_colours[i], 1);
2076
2077 if( vg_get_axis("grabl")>0.0f)
2078 reset_player(0,NULL);
2079
2080 if( vg_get_button_down( "switchmode" ) )
2081 {
2082 player.on_board ^= 0x1;
2083 }
2084
2085 if( freecam )
2086 {
2087 player_freecam();
2088 }
2089 else
2090 {
2091 if( player.is_dead )
2092 {
2093 /*
2094 * Follow camera
2095 */
2096 character_ragdoll_iter( &player.mdl );
2097 character_debug_ragdoll( &player.mdl );
2098
2099 v3f delta;
2100 v3f head_pos;
2101 v3_copy( player.mdl.ragdoll[k_chpart_head].co, head_pos );
2102
2103 v3_sub( head_pos, player.camera_pos, delta );
2104 v3_normalize( delta );
2105
2106 v3f follow_pos;
2107 v3_muladds( head_pos, delta, -2.5f, follow_pos );
2108 v3_lerp( player.camera_pos, follow_pos, 0.1f, player.camera_pos );
2109
2110 /*
2111 * Make sure the camera stays above the ground
2112 */
2113 v3f min_height = {0.0f,1.0f,0.0f};
2114
2115 v3f sample;
2116 v3_add( player.camera_pos, min_height, sample );
2117 ray_hit hit;
2118 hit.dist = min_height[1]*2.0f;
2119
2120 if( ray_world( sample, (v3f){0.0f,-1.0f,0.0f}, &hit ))
2121 v3_add( hit.pos, min_height, player.camera_pos );
2122
2123 player.camera_pos[1] =
2124 vg_maxf( wrender.height + 2.0f, player.camera_pos[1] );
2125
2126 player.angles[0] = atan2f( delta[0], -delta[2] );
2127 player.angles[1] = -asinf( delta[1] );
2128 }
2129 else
2130 {
2131 if( player.on_board )
2132 {
2133 bh_debug_node(&world.bhcubes, 0,
2134 player.camera_pos, 0xff80ff00 );
2135
2136 u32 colliders[16];
2137 boxf wbox = {{ -2.0f, -2.0f, -2.0f },
2138 { 2.0f, 2.0f, 2.0f }};
2139 m4x3_transform_aabb( player.to_world, wbox );
2140 int len = bh_select( &world.bhcubes, wbox, colliders, 32 );
2141
2142 for( int i=0; i<len; i++ )
2143 player_do_collision( &world.temp_rbs[colliders[i]] );
2144
2145 player_do_motion();
2146 player_animate();
2147
2148 v3f offs = { -0.29f, 0.08f, 0.0f };
2149 m3x3_mulv( player.to_world, offs, offs );
2150 m4x3_mulv( player.to_world, player.mdl.ik_body.end, player.camera_pos );
2151 //m4x3_mulv( player.mdl.matrices[k_chpart_head], offs, player.camera_pos );
2152 // v3_copy( player.mdl.matrices[k_chpart_head][3], player.camera_pos );
2153 v3_add( offs, player.camera_pos, player.camera_pos );
2154 }
2155 else
2156 {
2157 player_walkgrid();
2158 }
2159 }
2160 }
2161
2162 player_audio();
2163
2164 /* Update camera matrices */
2165 m4x3_identity( player.camera );
2166 m4x3_rotate_y( player.camera, -player.angles[0] );
2167 m4x3_rotate_x( player.camera, -0.30f -player.angles[1] );
2168 v3_copy( player.camera_pos, player.camera[3] );
2169 m4x3_invert_affine( player.camera, player.camera_inverse );
2170 }
2171
2172 static void draw_player(void)
2173 {
2174 /* Draw */
2175 m4x3_copy( player.to_world, player.mdl.mroot );
2176
2177 if( player.is_dead )
2178 character_mimic_ragdoll( &player.mdl );
2179 else
2180 character_eval( &player.mdl );
2181
2182 float opacity = 1.0f-player.air_blend;
2183 if( player.is_dead )
2184 opacity = 0.0f;
2185
2186 character_draw( &player.mdl, opacity );
2187 }
2188
2189 #endif /* PLAYER_H */