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