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