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