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