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