instance support
[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 #if 0
454 for( int i=0; i<len; i++ )
455 {
456 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
457
458 for( int j=0; j<3; j++ )
459 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
460
461 vg_line(tri[0],tri[1],0xff00ff00 );
462 vg_line(tri[1],tri[2],0xff00ff00 );
463 vg_line(tri[2],tri[0],0xff00ff00 );
464
465 v3f temp;
466 v3_copy( player.rb.co, temp );
467
468 for( int j=0; j<2; j++ )
469 {
470 if(manifold_count >= vg_list_size(manifold))
471 {
472 vg_error("Manifold overflow!\n");
473 break;
474 }
475
476 rb_ct *ct = &manifold[manifold_count];
477 v3_copy( poles[j], player.rb.co );
478
479 manifold_count += rb_sphere_vs_triangle( &player.rb, tri, ct );
480 }
481
482 v3_copy( temp, player.rb.co );
483 }
484 #endif
485
486 rb_presolve_contacts( manifold, len );
487 v3f surface_avg = {0.0f, 0.0f, 0.0f};
488
489 if( !len )
490 {
491 player_start_air();
492 }
493 else
494 {
495 for( int i=0; i<len; i++ )
496 v3_add( manifold[i].n, surface_avg, surface_avg );
497
498 v3_normalize( surface_avg );
499
500 if( v3_dot( player.rb.v, surface_avg ) > 0.5f )
501 {
502 player_start_air();
503 }
504 else
505 player.in_air = 0;
506 }
507
508 for( int j=0; j<5; j++ )
509 {
510 for( int i=0; i<len; i++ )
511 {
512 struct contact *ct = &manifold[i];
513
514 v3f dv, delta;
515 v3_sub( ct->co, player.rb.co, delta );
516 v3_cross( player.rb.w, delta, dv );
517 v3_add( player.rb.v, dv, dv );
518
519 float vn = -v3_dot( dv, ct->n );
520 vn += ct->bias;
521
522 float temp = ct->norm_impulse;
523 ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
524 vn = ct->norm_impulse - temp;
525
526 v3f impulse;
527 v3_muls( ct->n, vn, impulse );
528
529 if( fabsf(v3_dot( impulse, player.rb.forward )) > 10.0f ||
530 fabsf(v3_dot( impulse, player.rb.up )) > 50.0f )
531 {
532 player.is_dead = 1;
533 character_ragdoll_copypose( &player.mdl, player.rb.v );
534 return;
535 }
536
537 v3_add( impulse, player.rb.v, player.rb.v );
538 v3_cross( delta, impulse, impulse );
539
540 /*
541 * W Impulses are limited to the Y and X axises, we don't really want
542 * roll angular velocities being included.
543 *
544 * Can also tweak the resistance of each axis here by scaling the wx,wy
545 * components.
546 */
547
548 float wy = v3_dot( player.rb.up, impulse ),
549 wx = v3_dot( player.rb.right, impulse )*1.5f;
550
551 v3_muladds( player.rb.w, player.rb.up, wy, player.rb.w );
552 v3_muladds( player.rb.w, player.rb.right, wx, player.rb.w );
553 }
554 }
555
556 float grabt = vg_get_axis( "grabr" )*0.5f+0.5f;
557 player.grab = vg_lerpf( player.grab, grabt, 0.14f );
558
559 if( !player.in_air )
560 {
561 v3f axis;
562 float angle = v3_dot( player.rb.up, surface_avg );
563 v3_cross( player.rb.up, surface_avg, axis );
564
565 //float cz = v3_dot( player.rb.forward, axis );
566 //v3_muls( player.rb.forward, cz, axis );
567
568 if( angle < 0.999f )
569 {
570 v4f correction;
571 q_axis_angle( correction, axis, acosf(angle)*0.3f );
572 q_mul( correction, player.rb.q, player.rb.q );
573 }
574
575 v3_muladds( player.rb.v, player.rb.up,
576 -k_downforce*ktimestep, player.rb.v );
577 player_physics_control();
578 }
579 else
580 {
581 player_physics_control_air();
582 }
583 }
584
585 static void player_do_motion(void)
586 {
587 float horizontal = vg_get_axis("horizontal"),
588 vertical = vg_get_axis("vertical");
589
590 player_physics();
591
592 /* Integrate velocity */
593 v3f prevco;
594 v3_copy( player.rb.co, prevco );
595
596 apply_gravity( player.rb.v, ktimestep );
597 v3_muladds( player.rb.co, player.rb.v, ktimestep, player.rb.co );
598
599 /* Real angular velocity integration */
600 v3_lerp( player.rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, player.rb.w );
601 if( v3_length2( player.rb.w ) > 0.0f )
602 {
603 v4f rotation;
604 v3f axis;
605 v3_copy( player.rb.w, axis );
606
607 float mag = v3_length( axis );
608 v3_divs( axis, mag, axis );
609 q_axis_angle( rotation, axis, mag*k_rb_delta );
610 q_mul( rotation, player.rb.q, player.rb.q );
611 }
612
613 /* Faux angular velocity */
614 v4f rotate;
615
616 static float siY = 0.0f;
617 float lerpq = player.in_air? 0.04f: 0.3f;
618 siY = vg_lerpf( siY, player.iY, lerpq );
619
620 q_axis_angle( rotate, player.rb.up, siY );
621 q_mul( rotate, player.rb.q, player.rb.q );
622 player.iY = 0.0f;
623
624 /*
625 * Gate intersection, by tracing a line over the gate planes
626 */
627 for( int i=0; i<world.gate_count; i++ )
628 {
629 teleport_gate *gate = &world.gates[i];
630
631 if( gate_intersect( gate, player.rb.co, prevco ) )
632 {
633 m4x3_mulv( gate->transport, player.rb.co, player.rb.co );
634 m3x3_mulv( gate->transport, player.rb.v, player.rb.v );
635 m3x3_mulv( gate->transport, player.vl, player.vl );
636 m3x3_mulv( gate->transport, player.v_last, player.v_last );
637 m3x3_mulv( gate->transport, player.m, player.m );
638 m3x3_mulv( gate->transport, player.bob, player.bob );
639
640 v4f transport_rotation;
641 m3x3_q( gate->transport, transport_rotation );
642 q_mul( transport_rotation, player.rb.q, player.rb.q );
643
644 break;
645 }
646 }
647
648 rb_update_transform( &player.rb );
649 }
650
651 /*
652 * Walkgrid implementation,
653 * loosely based of cmuratoris youtube video 'Killing the Walkmonster'
654 */
655
656 #define WALKGRID_SIZE 16
657 struct walkgrid
658 {
659 struct grid_sample
660 {
661 enum sample_type
662 {
663 k_sample_type_air, /* Nothing was hit. */
664 k_sample_type_invalid, /* The point is invalid, but there is a sample
665 underneath that can be used */
666 k_sample_type_valid, /* This point is good */
667 }
668 type;
669
670 v3f clip[2];
671 v3f pos;
672
673 enum traverse_state
674 {
675 k_traverse_none = 0x00,
676 k_traverse_h = 0x01,
677 k_traverse_v = 0x02
678 }
679 state;
680 }
681 samples[WALKGRID_SIZE][WALKGRID_SIZE];
682
683 boxf region;
684
685 float move; /* Current amount of movement we have left to apply */
686 v2f dir; /* The movement delta */
687 v2i cell_id;/* Current cell */
688 v2f pos; /* Local position (in cell) */
689 float h;
690 };
691
692 static int player_walkgrid_tri_walkable( u32 tri[3] )
693 {
694 return tri[0] > world.sm_geo_std_oob.vertex_count;
695 }
696
697 /*
698 * Get a sample at this pole location, will return 1 if the sample is valid,
699 * and pos will be updated to be the intersection location.
700 */
701 static void player_walkgrid_samplepole( struct grid_sample *s )
702 {
703 boxf region = {{ s->pos[0] -0.01f, s->pos[1] - 4.0f, s->pos[2] -0.01f},
704 { s->pos[0] +0.01f, s->pos[1] + 4.0f, s->pos[2] +0.01f}};
705
706 u32 geo[256];
707 v3f tri[3];
708 int len = bh_select( &world.geo.bhtris, region, geo, 256 );
709
710 const float k_minworld_y = -2000.0f;
711
712 float walk_height = k_minworld_y,
713 block_height = k_minworld_y;
714
715 s->type = k_sample_type_air;
716
717 for( int i=0; i<len; i++ )
718 {
719 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
720
721 for( int j=0; j<3; j++ )
722 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
723
724 v3f vdown = {0.0f,-1.0f,0.0f};
725 v3f sample_from;
726 v3_copy( s->pos, sample_from );
727 sample_from[1] = region[1][1];
728
729 float dist;
730 if( ray_tri( tri, sample_from, vdown, &dist ))
731 {
732 v3f p0;
733 v3_muladds( sample_from, vdown, dist, p0 );
734
735 if( player_walkgrid_tri_walkable(ptri) )
736 {
737 if( p0[1] > walk_height )
738 {
739 walk_height = p0[1];
740 }
741 }
742 else
743 {
744 if( p0[1] > block_height )
745 block_height = p0[1];
746 }
747 }
748 }
749
750 s->pos[1] = walk_height;
751
752 if( walk_height > k_minworld_y )
753 if( block_height > walk_height )
754 s->type = k_sample_type_invalid;
755 else
756 s->type = k_sample_type_valid;
757 else
758 s->type = k_sample_type_air;
759 }
760
761 float const k_gridscale = 0.5f;
762
763 enum eclipdir
764 {
765 k_eclipdir_h = 0,
766 k_eclipdir_v = 1
767 };
768
769 static void player_walkgrid_clip_blocker( struct grid_sample *sa,
770 struct grid_sample *sb,
771 struct grid_sample *st,
772 enum eclipdir dir )
773 {
774 v3f clipdir, pos;
775 int valid_a = sa->type == k_sample_type_valid,
776 valid_b = sb->type == k_sample_type_valid;
777 struct grid_sample *target = valid_a? sa: sb,
778 *other = valid_a? sb: sa;
779 v3_copy( target->pos, pos );
780 v3_sub( other->pos, target->pos, clipdir );
781
782 boxf cell_region;
783 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*2.1f, cell_region[0]);
784 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*2.1f, cell_region[1]);
785
786 u32 geo[256];
787 v3f tri[3];
788 int len = bh_select( &world.geo.bhtris, cell_region, geo, 256 );
789
790 float start_time = v3_length( clipdir ),
791 min_time = start_time;
792 v3_normalize( clipdir );
793 v3_muls( clipdir, 0.0001f, st->clip[dir] );
794
795 for( int i=0; i<len; i++ )
796 {
797 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
798 for( int j=0; j<3; j++ )
799 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
800
801 if( player_walkgrid_tri_walkable(ptri) )
802 continue;
803
804 float dist;
805 if(ray_tri( tri, pos, clipdir, &dist ))
806 {
807 if( dist > 0.0f && dist < min_time )
808 {
809 min_time = dist;
810 sb->type = k_sample_type_air;
811 }
812 }
813 }
814
815 if( !(min_time < start_time) )
816 min_time = 0.5f * k_gridscale;
817
818 min_time = vg_clampf( min_time/k_gridscale, 0.01f, 0.99f );
819
820 v3_muls( clipdir, min_time, st->clip[dir] );
821
822 v3f p0;
823 v3_muladds( target->pos, st->clip[dir], k_gridscale, p0 );
824 }
825
826 static void player_walkgrid_clip_edge( struct grid_sample *sa,
827 struct grid_sample *sb,
828 struct grid_sample *st, /* data store */
829 enum eclipdir dir )
830 {
831 v3f clipdir = { 0.0f, 0.0f, 0.0f }, pos;
832 int valid_a = sa->type == k_sample_type_valid,
833 valid_b = sb->type == k_sample_type_valid;
834
835 struct grid_sample *target = valid_a? sa: sb,
836 *other = valid_a? sb: sa;
837
838 v3_sub( other->pos, target->pos, clipdir );
839 clipdir[1] = 0.0f;
840
841 v3_copy( target->pos, pos );
842
843 boxf cell_region;
844 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*1.1f, cell_region[0]);
845 v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*1.1f, cell_region[1]);
846
847 u32 geo[256];
848 int len = bh_select( &world.geo.bhtris, cell_region, geo, 256 );
849
850 float max_dist = 0.0f;
851 v3f tri[3];
852 v3f perp;
853 v3_cross( clipdir,(v3f){0.0f,1.0f,0.0f},perp );
854 v3_muls( clipdir, 0.001f, st->clip[dir] );
855
856 for( int i=0; i<len; i++ )
857 {
858 u32 *ptri = &world.geo.indices[ geo[i]*3 ];
859 for( int j=0; j<3; j++ )
860 v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
861
862 if( !player_walkgrid_tri_walkable(ptri) )
863 continue;
864
865 for( int k=0; k<3; k++ )
866 {
867 int ia = k,
868 ib = (k+1)%3;
869
870 v3f v0, v1;
871 v3_sub( tri[ia], pos, v0 );
872 v3_sub( tri[ib], pos, v1 );
873
874 if( (clipdir[2]*v0[0] - clipdir[0]*v0[2]) *
875 (clipdir[2]*v1[0] - clipdir[0]*v1[2]) < 0.0f )
876 {
877 float da = v3_dot(v0,perp),
878 db = v3_dot(v1,perp),
879 d = da-db,
880 qa = da/d;
881
882 v3f p0;
883 v3_muls( v1, qa, p0 );
884 v3_muladds( p0, v0, 1.0f-qa, p0 );
885
886 float h = v3_dot(p0,clipdir)/v3_dot(clipdir,clipdir);
887
888 if( h >= max_dist && h <= 1.0f )
889 {
890 max_dist = h;
891 float l = 1.0f/v3_length(clipdir);
892 v3_muls( p0, l, st->clip[dir] );
893 }
894 }
895 }
896 }
897 }
898
899 static const struct conf
900 {
901 struct confedge
902 {
903 /* i: sample index
904 * d: data index
905 * a: axis index
906 * o: the 'other' point to do a A/B test with
907 * if its -1, all AB is done.
908 */
909 int i0, i1,
910 d0, d1,
911 a0, a1,
912 o0, o1;
913 }
914 edges[2];
915 int edge_count;
916 }
917 k_walkgrid_configs[16] = {
918 {{},0},
919 {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
920 {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
921 {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
922
923 {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
924 {{{ 3,3, 3,0, 1,0, -1,-1 },
925 { 1,1, 0,1, 1,0, -1,-1 }}, 2},
926 {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
927 {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
928
929 {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
930 {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
931 {{{ 2,2, 1,3, 0,1, -1,-1 },
932 { 0,0, 0,0, 0,1, -1,-1 }}, 2},
933 {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
934
935 {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
936 {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
937 {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
938 {{},0},
939 };
940
941 /*
942 * Get a buffer of edges from cell location
943 */
944 static const struct conf *player_walkgrid_conf( struct walkgrid *wg,
945 v2i cell,
946 struct grid_sample *corners[4] )
947 {
948 corners[0] = &wg->samples[cell[1] ][cell[0] ];
949 corners[1] = &wg->samples[cell[1]+1][cell[0] ];
950 corners[2] = &wg->samples[cell[1]+1][cell[0]+1];
951 corners[3] = &wg->samples[cell[1] ][cell[0]+1];
952
953 u32 vd0 = corners[0]->type == k_sample_type_valid,
954 vd1 = corners[1]->type == k_sample_type_valid,
955 vd2 = corners[2]->type == k_sample_type_valid,
956 vd3 = corners[3]->type == k_sample_type_valid,
957 config = (vd0<<3) | (vd1<<2) | (vd2<<1) | vd3;
958
959 return &k_walkgrid_configs[ config ];
960 }
961
962 static void player_walkgrid_floor(v3f pos)
963 {
964 v3_muls( pos, 1.0f/k_gridscale, pos );
965 v3_floor( pos, pos );
966 v3_muls( pos, k_gridscale, pos );
967 }
968
969 /*
970 * Computes the barycentric coordinate of location on a triangle (vertical),
971 * then sets the Y position to the interpolation of the three points
972 */
973 static void player_walkgrid_stand_tri( v3f a, v3f b, v3f c, v3f pos )
974 {
975 v3f v0,v1,v2;
976 v3_sub( b, a, v0 );
977 v3_sub( c, a, v1 );
978 v3_sub( pos, a, v2 );
979
980 float d = v0[0]*v1[2] - v1[0]*v0[2],
981 v = (v2[0]*v1[2] - v1[0]*v2[2]) / d,
982 w = (v0[0]*v2[2] - v2[0]*v0[2]) / d,
983 u = 1.0f - v - w;
984
985 vg_line( pos, a, 0xffff0000 );
986 vg_line( pos, b, 0xff00ff00 );
987 vg_line( pos, c, 0xff0000ff );
988 pos[1] = u*a[1] + v*b[1] + w*c[1];
989 }
990
991 /*
992 * Get the minimum time value of pos+dir until a cell edge
993 *
994 * t[0] -> t[3] are the individual time values
995 * t[5] & t[6] are the maximum axis values
996 * t[6] is the minimum value
997 *
998 */
999 static void player_walkgrid_min_cell( float t[7], v2f pos, v2f dir )
1000 {
1001 v2f frac = { 1.0f/dir[0], 1.0f/dir[1] };
1002
1003 t[0] = 999.9f;
1004 t[1] = 999.9f;
1005 t[2] = 999.9f;
1006 t[3] = 999.9f;
1007
1008 if( fabsf(dir[0]) > 0.0001f )
1009 {
1010 t[0] = (0.0f-pos[0]) * frac[0];
1011 t[1] = (1.0f-pos[0]) * frac[0];
1012 }
1013 if( fabsf(dir[1]) > 0.0001f )
1014 {
1015 t[2] = (0.0f-pos[1]) * frac[1];
1016 t[3] = (1.0f-pos[1]) * frac[1];
1017 }
1018
1019 t[4] = vg_maxf(t[0],t[1]);
1020 t[5] = vg_maxf(t[2],t[3]);
1021 t[6] = vg_minf(t[4],t[5]);
1022 }
1023
1024 static void player_walkgrid_iter(struct walkgrid *wg, int iter)
1025 {
1026
1027 /*
1028 * For each walkgrid iteration we are stepping through cells and determining
1029 * the intersections with the grid, and any edges that are present
1030 */
1031
1032 u32 icolours[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
1033
1034 v3f pa, pb, pc, pd, pl0, pl1;
1035 pa[0] = wg->region[0][0] + (float)wg->cell_id[0] *k_gridscale;
1036 pa[1] = (wg->region[0][1] + wg->region[1][1]) * 0.5f + k_gridscale;
1037 pa[2] = wg->region[0][2] + (float)wg->cell_id[1] *k_gridscale;
1038 pb[0] = pa[0];
1039 pb[1] = pa[1];
1040 pb[2] = pa[2] + k_gridscale;
1041 pc[0] = pa[0] + k_gridscale;
1042 pc[1] = pa[1];
1043 pc[2] = pa[2] + k_gridscale;
1044 pd[0] = pa[0] + k_gridscale;
1045 pd[1] = pa[1];
1046 pd[2] = pa[2];
1047 #if 0
1048 /* if you want to draw the current cell */
1049 vg_line( pa, pb, 0xff00ffff );
1050 vg_line( pb, pc, 0xff00ffff );
1051 vg_line( pc, pd, 0xff00ffff );
1052 vg_line( pd, pa, 0xff00ffff );
1053 #endif
1054 pl0[0] = pa[0] + wg->pos[0]*k_gridscale;
1055 pl0[1] = pa[1];
1056 pl0[2] = pa[2] + wg->pos[1]*k_gridscale;
1057
1058 /*
1059 * If there are edges present, we need to create a 'substep' event, where
1060 * we find the intersection point, find the fully resolved position,
1061 * then the new pos dir is the intersection->resolution
1062 *
1063 * the resolution is applied in non-discretized space in order to create a
1064 * suitable vector for finding outflow, we want it to leave the cell so it
1065 * can be used by the quad
1066 */
1067
1068 v2f pos, dir;
1069 v2_copy( wg->pos, pos );
1070 v2_muls( wg->dir, wg->move, dir );
1071
1072 struct grid_sample *corners[4];
1073 v2f corners2d[4] = {{0.0f,0.0f},{0.0f,1.0f},{1.0f,1.0f},{1.0f,0.0f}};
1074 const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
1075
1076 float t[7];
1077 player_walkgrid_min_cell( t, pos, dir );
1078
1079 for( int i=0; i<conf->edge_count; i++ )
1080 {
1081 const struct confedge *edge = &conf->edges[i];
1082
1083 v2f e0, e1, n, r, target, res, tangent;
1084 e0[0] = corners2d[edge->i0][0] + corners[edge->d0]->clip[edge->a0][0];
1085 e0[1] = corners2d[edge->i0][1] + corners[edge->d0]->clip[edge->a0][2];
1086 e1[0] = corners2d[edge->i1][0] + corners[edge->d1]->clip[edge->a1][0];
1087 e1[1] = corners2d[edge->i1][1] + corners[edge->d1]->clip[edge->a1][2];
1088
1089 v3f pe0 = { pa[0] + e0[0]*k_gridscale,
1090 pa[1],
1091 pa[2] + e0[1]*k_gridscale };
1092 v3f pe1 = { pa[0] + e1[0]*k_gridscale,
1093 pa[1],
1094 pa[2] + e1[1]*k_gridscale };
1095
1096 v2_sub( e1, e0, tangent );
1097 n[0] = -tangent[1];
1098 n[1] = tangent[0];
1099 v2_normalize( n );
1100
1101 /*
1102 * If we find ourselfs already penetrating the edge, move back out a
1103 * little
1104 */
1105 v2_sub( e0, pos, r );
1106 float p1 = v2_dot(r,n);
1107
1108 if( -p1 < 0.0001f )
1109 {
1110 v2_muladds( pos, n, p1+0.0001f, pos );
1111 v2_copy( pos, wg->pos );
1112 v3f p_new = { pa[0] + pos[0]*k_gridscale,
1113 pa[1],
1114 pa[2] + pos[1]*k_gridscale };
1115 v3_copy( p_new, pl0 );
1116 }
1117
1118 v2_add( pos, dir, target );
1119
1120 v2f v1, v2, v3;
1121 v2_sub( e0, pos, v1 );
1122 v2_sub( target, pos, v2 );
1123
1124 v2_copy( n, v3 );
1125
1126 v2_sub( e0, target, r );
1127 float p = v2_dot(r,n),
1128 t1 = v2_dot(v1,v3)/v2_dot(v2,v3);
1129
1130 if( t1 < t[6] && t1 > 0.0f && -p < 0.001f )
1131 {
1132 v2_muladds( target, n, p+0.0001f, res );
1133
1134 v2f intersect;
1135 v2_muladds( pos, dir, t1, intersect );
1136 v2_copy( intersect, pos );
1137 v2_sub( res, intersect, dir );
1138
1139 v3f p_res = { pa[0] + res[0]*k_gridscale,
1140 pa[1],
1141 pa[2] + res[1]*k_gridscale };
1142 v3f p_int = { pa[0] + intersect[0]*k_gridscale,
1143 pa[1],
1144 pa[2] + intersect[1]*k_gridscale };
1145
1146 vg_line( pl0, p_int, icolours[iter%3] );
1147 v3_copy( p_int, pl0 );
1148 v2_copy( pos, wg->pos );
1149
1150 player_walkgrid_min_cell( t, pos, dir );
1151 }
1152 }
1153
1154 /*
1155 * Compute intersection with grid cell moving outwards
1156 */
1157 t[6] = vg_minf( t[6], 1.0f );
1158
1159 pl1[0] = pl0[0] + dir[0]*k_gridscale*t[6];
1160 pl1[1] = pl0[1];
1161 pl1[2] = pl0[2] + dir[1]*k_gridscale*t[6];
1162 vg_line( pl0, pl1, icolours[iter%3] );
1163
1164 if( t[6] < 1.0f )
1165 {
1166 /*
1167 * To figure out what t value created the clip so we know which edge
1168 * to wrap around
1169 */
1170
1171 if( t[4] < t[5] )
1172 {
1173 wg->pos[1] = pos[1] + dir[1]*t[6];
1174
1175 if( t[0] > t[1] ) /* left edge */
1176 {
1177 wg->pos[0] = 0.9999f;
1178 wg->cell_id[0] --;
1179
1180 if( wg->cell_id[0] == 0 )
1181 wg->move = -1.0f;
1182 }
1183 else /* Right edge */
1184 {
1185 wg->pos[0] = 0.0001f;
1186 wg->cell_id[0] ++;
1187
1188 if( wg->cell_id[0] == WALKGRID_SIZE-2 )
1189 wg->move = -1.0f;
1190 }
1191 }
1192 else
1193 {
1194 wg->pos[0] = pos[0] + dir[0]*t[6];
1195
1196 if( t[2] > t[3] ) /* bottom edge */
1197 {
1198 wg->pos[1] = 0.9999f;
1199 wg->cell_id[1] --;
1200
1201 if( wg->cell_id[1] == 0 )
1202 wg->move = -1.0f;
1203 }
1204 else /* top edge */
1205 {
1206 wg->pos[1] = 0.0001f;
1207 wg->cell_id[1] ++;
1208
1209 if( wg->cell_id[1] == WALKGRID_SIZE-2 )
1210 wg->move = -1.0f;
1211 }
1212 }
1213
1214 wg->move -= t[6];
1215 }
1216 else
1217 {
1218 v2_muladds( wg->pos, dir, wg->move, wg->pos );
1219 wg->move = 0.0f;
1220 }
1221 }
1222
1223 static void player_walkgrid_stand_cell(struct walkgrid *wg)
1224 {
1225 /*
1226 * NOTE: as opposed to the other function which is done in discretized space
1227 * this use a combination of both.
1228 */
1229
1230 v3f world;
1231 world[0] = wg->region[0][0]+((float)wg->cell_id[0]+wg->pos[0])*k_gridscale;
1232 world[1] = player.rb.co[1];
1233 world[2] = wg->region[0][2]+((float)wg->cell_id[1]+wg->pos[1])*k_gridscale;
1234
1235 struct grid_sample *corners[4];
1236 const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
1237
1238 if( conf != k_walkgrid_configs )
1239 {
1240 if( conf->edge_count == 0 )
1241 {
1242 v3f v0;
1243
1244 /* Split the basic quad along the shortest diagonal */
1245 if( fabsf(corners[2]->pos[1] - corners[0]->pos[1]) <
1246 fabsf(corners[3]->pos[1] - corners[1]->pos[1]) )
1247 {
1248 vg_line( corners[2]->pos, corners[0]->pos, 0xffaaaaaa );
1249
1250 if( wg->pos[0] > wg->pos[1] )
1251 player_walkgrid_stand_tri( corners[0]->pos,
1252 corners[3]->pos,
1253 corners[2]->pos, world );
1254 else
1255 player_walkgrid_stand_tri( corners[0]->pos,
1256 corners[2]->pos,
1257 corners[1]->pos, world );
1258 }
1259 else
1260 {
1261 vg_line( corners[3]->pos, corners[1]->pos, 0xffaaaaaa );
1262
1263 if( wg->pos[0] < 1.0f-wg->pos[1] )
1264 player_walkgrid_stand_tri( corners[0]->pos,
1265 corners[3]->pos,
1266 corners[1]->pos, world );
1267 else
1268 player_walkgrid_stand_tri( corners[3]->pos,
1269 corners[2]->pos,
1270 corners[1]->pos, world );
1271 }
1272 }
1273 else
1274 {
1275 for( int i=0; i<conf->edge_count; i++ )
1276 {
1277 const struct confedge *edge = &conf->edges[i];
1278
1279 v3f p0, p1;
1280 v3_muladds( corners[edge->i0]->pos,
1281 corners[edge->d0]->clip[edge->a0], k_gridscale, p0 );
1282 v3_muladds( corners[edge->i1]->pos,
1283 corners[edge->d1]->clip[edge->a1], k_gridscale, p1 );
1284
1285 /*
1286 * Find penetration distance between player position and the edge
1287 */
1288
1289 v2f normal = { -(p1[2]-p0[2]), p1[0]-p0[0] },
1290 rel = { world[0]-p0[0], world[2]-p0[2] };
1291
1292 if( edge->o0 == -1 )
1293 {
1294 /* No subregions (default case), just use triangle created by
1295 * i0, e0, e1 */
1296 player_walkgrid_stand_tri( corners[edge->i0]->pos,
1297 p0,
1298 p1, world );
1299 }
1300 else
1301 {
1302 /*
1303 * Test if we are in the first region, which is
1304 * edge.i0, edge.e0, edge.o0,
1305 */
1306 v3f v0, ref;
1307 v3_sub( p0, corners[edge->o0]->pos, ref );
1308 v3_sub( world, corners[edge->o0]->pos, v0 );
1309
1310 vg_line( corners[edge->o0]->pos, p0, 0xffffff00 );
1311 vg_line( corners[edge->o0]->pos, world, 0xff000000 );
1312
1313 if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
1314 {
1315 player_walkgrid_stand_tri( corners[edge->i0]->pos,
1316 p0,
1317 corners[edge->o0]->pos, world );
1318 }
1319 else
1320 {
1321 if( edge->o1 == -1 )
1322 {
1323 /*
1324 * No other edges mean we just need to use the opposite
1325 *
1326 * e0, e1, o0 (in our case, also i1)
1327 */
1328 player_walkgrid_stand_tri( p0,
1329 p1,
1330 corners[edge->o0]->pos, world );
1331 }
1332 else
1333 {
1334 /*
1335 * Note: this v0 calculation can be ommited with the
1336 * current tileset.
1337 *
1338 * the last two triangles we have are:
1339 * e0, e1, o1
1340 * and
1341 * e1, i1, o1
1342 */
1343 v3_sub( p1, corners[edge->o1]->pos, ref );
1344 v3_sub( world, corners[edge->o1]->pos, v0 );
1345 vg_line( corners[edge->o1]->pos, p1, 0xff00ffff );
1346
1347 if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
1348 {
1349 player_walkgrid_stand_tri( p0,
1350 p1,
1351 corners[edge->o1]->pos,
1352 world );
1353 }
1354 else
1355 {
1356 player_walkgrid_stand_tri( p1,
1357 corners[edge->i1]->pos,
1358 corners[edge->o1]->pos,
1359 world );
1360 }
1361 }
1362 }
1363 }
1364 }
1365 }
1366 }
1367
1368 v3_copy( world, player.rb.co );
1369 }
1370
1371 static void player_walkgrid_getsurface(void)
1372 {
1373 float const k_stepheight = 0.5f;
1374 float const k_miny = 0.6f;
1375 float const k_height = 1.78f;
1376 float const k_region_size = (float)WALKGRID_SIZE/2.0f * k_gridscale;
1377
1378 static struct walkgrid wg;
1379
1380 v3f cell;
1381 v3_copy( player.rb.co, cell );
1382 player_walkgrid_floor( cell );
1383
1384 v3_muladds( cell, (v3f){-1.0f,-1.0f,-1.0f}, k_region_size, wg.region[0] );
1385 v3_muladds( cell, (v3f){ 1.0f, 1.0f, 1.0f}, k_region_size, wg.region[1] );
1386
1387
1388 /*
1389 * Create player input vector
1390 */
1391 v3f delta = {0.0f,0.0f,0.0f};
1392 v3f fwd = { -sinf(-player.angles[0]), 0.0f, -cosf(-player.angles[0]) },
1393 side = { -fwd[2], 0.0f, fwd[0] };
1394
1395 /* Temp */
1396 if( !vg_console_enabled() )
1397 {
1398 if( glfwGetKey( vg_window, GLFW_KEY_W ) )
1399 v3_muladds( delta, fwd, ktimestep*k_walkspeed, delta );
1400 if( glfwGetKey( vg_window, GLFW_KEY_S ) )
1401 v3_muladds( delta, fwd, -ktimestep*k_walkspeed, delta );
1402
1403 if( glfwGetKey( vg_window, GLFW_KEY_A ) )
1404 v3_muladds( delta, side, -ktimestep*k_walkspeed, delta );
1405 if( glfwGetKey( vg_window, GLFW_KEY_D ) )
1406 v3_muladds( delta, side, ktimestep*k_walkspeed, delta );
1407
1408 v3_muladds( delta, fwd,
1409 vg_get_axis("vertical")*-ktimestep*k_walkspeed, delta );
1410 v3_muladds( delta, side,
1411 vg_get_axis("horizontal")*ktimestep*k_walkspeed, delta );
1412 }
1413
1414 /*
1415 * Create our move in grid space
1416 */
1417 wg.dir[0] = delta[0] * (1.0f/k_gridscale);
1418 wg.dir[1] = delta[2] * (1.0f/k_gridscale);
1419 wg.move = 1.0f;
1420
1421 v2f region_pos =
1422 {
1423 (player.rb.co[0] - wg.region[0][0]) * (1.0f/k_gridscale),
1424 (player.rb.co[2] - wg.region[0][2]) * (1.0f/k_gridscale)
1425 };
1426 v2f region_cell_pos;
1427 v2_floor( region_pos, region_cell_pos );
1428 v2_sub( region_pos, region_cell_pos, wg.pos );
1429
1430 wg.cell_id[0] = region_cell_pos[0];
1431 wg.cell_id[1] = region_cell_pos[1];
1432
1433 for(int y=0; y<WALKGRID_SIZE; y++ )
1434 {
1435 for(int x=0; x<WALKGRID_SIZE; x++ )
1436 {
1437 struct grid_sample *s = &wg.samples[y][x];
1438 v3_muladds( wg.region[0], (v3f){ x, 0, y }, k_gridscale, s->pos );
1439 s->state = k_traverse_none;
1440 s->type = k_sample_type_air;
1441 v3_zero( s->clip[0] );
1442 v3_zero( s->clip[1] );
1443 }
1444 }
1445
1446 v2i border[WALKGRID_SIZE*WALKGRID_SIZE];
1447 v2i *cborder = border;
1448 u32 border_length = 1;
1449
1450 struct grid_sample *base = NULL;
1451
1452 v2i starters[] = {{0,0},{1,1},{0,1},{1,0}};
1453
1454 for( int i=0;i<4;i++ )
1455 {
1456 v2i test;
1457 v2i_add( wg.cell_id, starters[i], test );
1458 v2i_copy( test, border[0] );
1459 base = &wg.samples[test[1]][test[0]];
1460
1461 base->pos[1] = cell[1];
1462 player_walkgrid_samplepole( base );
1463
1464 if( base->type == k_sample_type_valid )
1465 break;
1466 else
1467 base->type = k_sample_type_air;
1468 }
1469
1470 vg_line_pt3( base->pos, 0.1f, 0xffffffff );
1471
1472 int iter = 0;
1473
1474 while( border_length )
1475 {
1476 v2i directions[] = {{1,0},{0,1},{-1,0},{0,-1}};
1477
1478 v2i *old_border = cborder;
1479 int len = border_length;
1480
1481 border_length = 0;
1482 cborder = old_border+len;
1483
1484 for( int i=0; i<len; i++ )
1485 {
1486 v2i co;
1487 v2i_copy( old_border[i], co );
1488 struct grid_sample *sa = &wg.samples[co[1]][co[0]];
1489
1490 for( int j=0; j<4; j++ )
1491 {
1492 v2i newp;
1493 v2i_add( co, directions[j], newp );
1494
1495 if( newp[0] < 0 || newp[1] < 0 ||
1496 newp[0] == WALKGRID_SIZE || newp[1] == WALKGRID_SIZE )
1497 continue;
1498
1499 struct grid_sample *sb = &wg.samples[newp[1]][newp[0]];
1500 enum traverse_state thismove = j%2==0? 1: 2;
1501
1502 if( (sb->state & thismove) == 0x00 ||
1503 sb->type == k_sample_type_air )
1504 {
1505 sb->pos[1] = sa->pos[1];
1506
1507 player_walkgrid_samplepole( sb );
1508
1509 if( sb->type != k_sample_type_air )
1510 {
1511 /*
1512 * Need to do a blocker pass
1513 */
1514
1515 struct grid_sample *store = (j>>1 == 0)? sa: sb;
1516 player_walkgrid_clip_blocker( sa, sb, store, j%2 );
1517
1518
1519 if( sb->type != k_sample_type_air )
1520 {
1521 vg_line( sa->pos, sb->pos, 0xffffffff );
1522
1523 if( sb->state == k_traverse_none )
1524 v2i_copy( newp, cborder[ border_length ++ ] );
1525 }
1526 else
1527 {
1528 v3f p1;
1529 v3_muladds( sa->pos, store->clip[j%2], k_gridscale, p1 );
1530 vg_line( sa->pos, p1, 0xffffffff );
1531 }
1532 }
1533 else
1534 {
1535 /*
1536 * A clipping pass is now done on the edge of the walkable
1537 * surface
1538 */
1539
1540 struct grid_sample *store = (j>>1 == 0)? sa: sb;
1541 player_walkgrid_clip_edge( sa, sb, store, j%2 );
1542
1543 v3f p1;
1544 v3_muladds( sa->pos, store->clip[j%2], k_gridscale, p1 );
1545 vg_line( sa->pos, p1, 0xffffffff );
1546 }
1547
1548 sb->state |= thismove;
1549 }
1550 }
1551
1552 sa->state = k_traverse_h|k_traverse_v;
1553 }
1554
1555 iter ++;
1556 if( iter == walk_grid_iterations )
1557 break;
1558 }
1559
1560 /* Draw connections */
1561 struct grid_sample *corners[4];
1562 for( int x=0; x<WALKGRID_SIZE-1; x++ )
1563 {
1564 for( int z=0; z<WALKGRID_SIZE-1; z++ )
1565 {
1566 const struct conf *conf =
1567 player_walkgrid_conf( &wg, (v2i){x,z}, corners );
1568
1569 for( int i=0; i<conf->edge_count; i++ )
1570 {
1571 const struct confedge *edge = &conf->edges[i];
1572
1573 v3f p0, p1;
1574 v3_muladds( corners[edge->i0]->pos,
1575 corners[edge->d0]->clip[edge->a0], k_gridscale, p0 );
1576 v3_muladds( corners[edge->i1]->pos,
1577 corners[edge->d1]->clip[edge->a1], k_gridscale, p1 );
1578
1579 vg_line( p0, p1, 0xff0000ff );
1580 }
1581 }
1582 }
1583
1584 /*
1585 * Commit player movement into the grid
1586 */
1587
1588 if( v3_length2(delta) <= 0.00001f )
1589 return;
1590
1591 int i=0;
1592 for(; i<8 && wg.move > 0.001f; i++ )
1593 player_walkgrid_iter( &wg, i );
1594
1595 player_walkgrid_stand_cell( &wg );
1596 }
1597
1598 static void player_walkgrid(void)
1599 {
1600 player_walkgrid_getsurface();
1601
1602 m4x3_mulv( player.rb.to_world, (v3f){0.0f,1.8f,0.0f}, player.camera_pos );
1603 player_mouseview();
1604 rb_update_transform( &player.rb );
1605 }
1606
1607 /*
1608 * Animation
1609 */
1610
1611 static void player_animate(void)
1612 {
1613 /* Camera position */
1614 v3_sub( player.rb.v, player.v_last, player.a );
1615 v3_copy( player.rb.v, player.v_last );
1616
1617 v3_add( player.m, player.a, player.m );
1618 v3_lerp( player.m, (v3f){0.0f,0.0f,0.0f}, 0.1f, player.m );
1619
1620 player.m[0] = vg_clampf( player.m[0], -2.0f, 2.0f );
1621 player.m[1] = vg_clampf( player.m[1], -2.0f, 2.0f );
1622 player.m[2] = vg_clampf( player.m[2], -2.0f, 2.0f );
1623 v3_lerp( player.bob, player.m, 0.2f, player.bob );
1624
1625 /* Head */
1626 float lslip = fabsf(player.slip);
1627
1628 float kheight = 2.0f,
1629 kleg = 0.6f;
1630
1631 v3f offset;
1632 v3_zero( offset );
1633 m3x3_mulv( player.rb.to_local, player.bob, offset );
1634
1635 static float speed_wobble = 0.0f, speed_wobble_2 = 0.0f;
1636
1637 float kickspeed = vg_clampf(v3_length(player.rb.v)*(1.0f/40.0f), 0.0f, 1.0f);
1638 float kicks = (vg_randf()-0.5f)*2.0f*kickspeed;
1639 float sign = vg_signf( kicks );
1640 speed_wobble = vg_lerpf( speed_wobble, kicks*kicks*sign, 0.1f );
1641 speed_wobble_2 = vg_lerpf( speed_wobble_2, speed_wobble, 0.04f );
1642
1643 offset[0] *= 0.26f;
1644 offset[0] += speed_wobble_2*3.0f;
1645
1646 offset[1] *= -0.3f;
1647 offset[2] *= 0.01f;
1648
1649 offset[0] = vg_clampf( offset[0], -0.8f, 0.8f );
1650 offset[1] = vg_clampf( offset[1], -0.5f, 0.0f );
1651
1652 /*
1653 * Player rotation
1654 */
1655 #if 0
1656 float angle = v3_dot( player.rb.up, (v3f){0.0f,1.0f,0.0f} );
1657 v3f axis;
1658 v3_cross( player.rb.up, (v3f){0.0f,1.0f,0.0f}, axis );
1659
1660 v4f correction;
1661 if( angle < 0.99f && 0 )
1662 {
1663 m3x3_mulv( player.rb.to_local, axis, axis );
1664 q_axis_angle( correction, axis, acosf(angle) );
1665 }
1666 else
1667 {
1668 q_identity( correction );
1669 }
1670
1671 /*
1672 * Animation blending
1673 * ===========================================
1674 */
1675 #endif
1676
1677 static float fslide = 0.0f;
1678 static float fdirz = 0.0f;
1679 static float fdirx = 0.0f;
1680 static float fstand = 0.0f;
1681 static float ffly = 0.0f;
1682
1683 float speed = v3_length( player.rb.v );
1684
1685 fstand = vg_lerpf(fstand, 1.0f-vg_clampf(speed*0.03f,0.0f,1.0f),0.1f);
1686 fslide = vg_lerpf(fslide, vg_clampf(lslip,0.0f,1.0f), 0.04f);
1687 fdirz = vg_lerpf(fdirz, player.reverse > 0.0f? 1.0f: 0.0f, 0.04f );
1688 fdirx = vg_lerpf(fdirx, player.slip < 0.0f? 1.0f: 0.0f, 0.01f );
1689 ffly = vg_lerpf(ffly, player.in_air? 1.0f: 0.0f, 0.04f );
1690
1691 character_pose_reset( &player.mdl );
1692
1693 /* TODO */
1694 float fstand1 = 1.0f-(1.0f-fstand)*0.0f;
1695
1696 float amt_air = ffly*ffly,
1697 amt_ground = 1.0f-amt_air,
1698 amt_std = (1.0f-fslide) * amt_ground,
1699 amt_stand = amt_std * fstand1,
1700 amt_aero = amt_std * (1.0f-fstand1),
1701 amt_slide = amt_ground * fslide;
1702
1703 character_final_pose( &player.mdl, offset, &pose_stand, amt_stand*fdirz );
1704 character_final_pose( &player.mdl, offset,
1705 &pose_stand_reverse, amt_stand * (1.0f-fdirz) );
1706
1707 character_final_pose( &player.mdl, offset, &pose_aero, amt_aero*fdirz );
1708 character_final_pose( &player.mdl, offset,
1709 &pose_aero_reverse, amt_aero * (1.0f-fdirz) );
1710
1711 character_final_pose( &player.mdl, offset, &pose_slide, amt_slide*fdirx );
1712 character_final_pose( &player.mdl, offset,
1713 &pose_slide1, amt_slide*(1.0f-fdirx) );
1714
1715 character_final_pose( &player.mdl, (v4f){0.0f,0.0f,0.0f,1.0f},
1716 &pose_fly, amt_air );
1717
1718 /*
1719 * Additive effects
1720 * ==========================
1721 */
1722 struct ik_basic *arm_l = &player.mdl.ik_arm_l,
1723 *arm_r = &player.mdl.ik_arm_r;
1724
1725 v3f localv;
1726 m3x3_mulv( player.rb.to_local, player.rb.v, localv );
1727
1728 /* New board transformation */
1729 v4f board_rotation; v3f board_location;
1730
1731 v4f rz, rx;
1732 q_axis_angle( rz, (v3f){ 0.0f, 0.0f, 1.0f }, player.board_xy[0] );
1733 q_axis_angle( rx, (v3f){ 1.0f, 0.0f, 0.0f }, player.board_xy[1] );
1734 q_mul( rx, rz, board_rotation );
1735
1736 v3f *mboard = player.mdl.matrices[k_chpart_board];// player.mboard;
1737 q_m3x3( board_rotation, mboard );
1738 m3x3_mulv( mboard, (v3f){ 0.0f, -0.5f, 0.0f }, board_location );
1739 v3_add( (v3f){0.0f,0.5f,0.0f}, board_location, board_location );
1740 v3_copy( board_location, mboard[3] );
1741
1742
1743 float wheel_r = offset[0]*-0.4f;
1744 v4f qwheel;
1745 q_axis_angle( qwheel, (v3f){0.0f,1.0f,0.0f}, wheel_r );
1746
1747 q_m3x3( qwheel, player.mdl.matrices[k_chpart_wb] );
1748
1749 m3x3_transpose( player.mdl.matrices[k_chpart_wb],
1750 player.mdl.matrices[k_chpart_wf] );
1751 v3_copy( player.mdl.offsets[k_chpart_wb],
1752 player.mdl.matrices[k_chpart_wb][3] );
1753 v3_copy( player.mdl.offsets[k_chpart_wf],
1754 player.mdl.matrices[k_chpart_wf][3] );
1755
1756 m4x3_mul( mboard, player.mdl.matrices[k_chpart_wb],
1757 player.mdl.matrices[k_chpart_wb] );
1758 m4x3_mul( mboard, player.mdl.matrices[k_chpart_wf],
1759 player.mdl.matrices[k_chpart_wf] );
1760
1761 m4x3_mulv( mboard, player.mdl.ik_leg_l.end, player.mdl.ik_leg_l.end );
1762 m4x3_mulv( mboard, player.mdl.ik_leg_r.end, player.mdl.ik_leg_r.end );
1763
1764
1765 v3_copy( player.mdl.ik_arm_l.end, player.handl_target );
1766 v3_copy( player.mdl.ik_arm_r.end, player.handr_target );
1767
1768 if( 1||player.in_air )
1769 {
1770 float tuck = player.board_xy[1],
1771 tuck_amt = fabsf( tuck ) * (1.0f-fabsf(player.board_xy[0]));
1772
1773 float crouch = player.grab*0.3f;
1774 v3_muladds( player.mdl.ik_body.base, (v3f){0.0f,-1.0f,0.0f},
1775 crouch, player.mdl.ik_body.base );
1776 v3_muladds( player.mdl.ik_body.end, (v3f){0.0f,-1.0f,0.0f},
1777 crouch*1.2f, player.mdl.ik_body.end );
1778
1779 if( tuck < 0.0f )
1780 {
1781 //foot_l *= 1.0f-tuck_amt*1.5f;
1782
1783 if( player.grab > 0.1f )
1784 {
1785 m4x3_mulv( mboard, (v3f){0.1f,0.14f,0.6f},
1786 player.handl_target );
1787 }
1788 }
1789 else
1790 {
1791 //foot_r *= 1.0f-tuck_amt*1.4f;
1792
1793 if( player.grab > 0.1f )
1794 {
1795 m4x3_mulv( mboard, (v3f){0.1f,0.14f,-0.6f},
1796 player.handr_target );
1797 }
1798 }
1799 }
1800
1801 v3_lerp( player.handl, player.handl_target, 1.0f, player.handl );
1802 v3_lerp( player.handr, player.handr_target, 1.0f, player.handr );
1803
1804 v3_copy( player.handl, player.mdl.ik_arm_l.end );
1805 v3_copy( player.handr, player.mdl.ik_arm_r.end );
1806
1807 /* Head rotation */
1808
1809 static float rhead = 0.0f;
1810 static const float klook_max = 0.8f;
1811 rhead = vg_lerpf( rhead,
1812 vg_clampf( atan2f(localv[2],-localv[0]),-klook_max,klook_max), 0.04f );
1813 player.mdl.rhead = rhead;
1814 }
1815
1816 static void player_camera_update(void)
1817 {
1818 /* Update camera matrices */
1819 m4x3_identity( player.camera );
1820 m4x3_rotate_y( player.camera, -player.angles[0] );
1821 m4x3_rotate_x( player.camera, -player.angles[1] );
1822 v3_copy( player.camera_pos, player.camera[3] );
1823 m4x3_invert_affine( player.camera, player.camera_inverse );
1824 }
1825
1826 static void player_animate_death_cam(void)
1827 {
1828 v3f delta;
1829 v3f head_pos;
1830 v3_copy( player.mdl.ragdoll[k_chpart_head].co, head_pos );
1831
1832 v3_sub( head_pos, player.camera_pos, delta );
1833 v3_normalize( delta );
1834
1835 v3f follow_pos;
1836 v3_muladds( head_pos, delta, -2.5f, follow_pos );
1837 v3_lerp( player.camera_pos, follow_pos, 0.1f, player.camera_pos );
1838
1839 /*
1840 * Make sure the camera stays above the ground
1841 */
1842 v3f min_height = {0.0f,1.0f,0.0f};
1843
1844 v3f sample;
1845 v3_add( player.camera_pos, min_height, sample );
1846 ray_hit hit;
1847 hit.dist = min_height[1]*2.0f;
1848
1849 if( ray_world( sample, (v3f){0.0f,-1.0f,0.0f}, &hit ))
1850 v3_add( hit.pos, min_height, player.camera_pos );
1851
1852 player.camera_pos[1] =
1853 vg_maxf( wrender.height + 2.0f, player.camera_pos[1] );
1854
1855 player.angles[0] = atan2f( delta[0], -delta[2] );
1856 player.angles[1] = -asinf( delta[1] );
1857 }
1858
1859 static void player_animate_camera(void)
1860 {
1861 v3f offs = { -0.29f, 0.08f, 0.0f };
1862 m3x3_mulv( player.rb.to_world, offs, offs );
1863 m4x3_mulv( player.rb.to_world, player.mdl.ik_body.end, player.camera_pos );
1864 v3_add( offs, player.camera_pos, player.camera_pos );
1865
1866 /* Look angles */
1867 v3_lerp( player.vl, player.rb.v, 0.05f, player.vl );
1868
1869 float yaw = atan2f( player.vl[0], -player.vl[2] ),
1870 pitch = atan2f( -player.vl[1],
1871 sqrtf(
1872 player.vl[0]*player.vl[0] + player.vl[2]*player.vl[2]
1873 )) * 0.7f;
1874
1875 player.angles[0] = yaw;
1876 player.angles[1] = pitch + 0.30f;
1877
1878 /* Camera shake */
1879 static v2f shake_damp = {0.0f,0.0f};
1880 v2f shake = { vg_randf()-0.5f, vg_randf()-0.5f };
1881 v2_muls( shake, v3_length(player.rb.v)*0.3f
1882 * (1.0f+fabsf(player.slip)), shake);
1883
1884 v2_lerp( shake_damp, shake, 0.01f, shake_damp );
1885 shake_damp[0] *= 0.2f;
1886
1887 v2_muladds( player.angles, shake_damp, 0.1f, player.angles );
1888 }
1889
1890 /*
1891 * Audio
1892 */
1893 static void player_audio(void)
1894 {
1895 float speed = vg_minf(v3_length( player.rb.v )*0.1f,1.0f),
1896 attn = v3_dist( player.rb.co, player.camera[3] )+1.0f;
1897 attn = (1.0f/(attn*attn)) * speed;
1898
1899 static float air = 0.0f;
1900 air = vg_lerpf(air, player.in_air? 1.0f: 0.0f, 0.7f);
1901
1902 v3f ears = { 1.0f,0.0f,0.0f };
1903 v3f delta;
1904
1905 v3_sub( player.rb.co, player.camera[3], delta );
1906 v3_normalize( delta );
1907 m3x3_mulv( player.camera, ears, ears );
1908
1909 float pan = v3_dot( ears, delta );
1910 audio_player0.pan = pan;
1911 audio_player1.pan = pan;
1912 audio_player2.pan = pan;
1913
1914 if( freecam )
1915 {
1916 audio_player0.vol = 0.0f;
1917 audio_player1.vol = 0.0f;
1918 audio_player2.vol = 0.0f;
1919 }
1920 else
1921 {
1922 if( player.is_dead )
1923 {
1924 audio_player0.vol = 0.0f;
1925 audio_player1.vol = 0.0f;
1926 audio_player2.vol = 0.0f;
1927 }
1928 else
1929 {
1930 float slide = vg_clampf( fabsf(player.slip), 0.0f, 1.0f );
1931 audio_player0.vol = (1.0f-air)*attn*(1.0f-slide);
1932 audio_player1.vol = air *attn;
1933 audio_player2.vol = (1.0f-air)*attn*slide;
1934 }
1935 }
1936 }
1937
1938 /*
1939 * Public Endpoints
1940 */
1941 static float *player_cam_pos(void)
1942 {
1943 return player.camera_pos;
1944 }
1945
1946 static int reset_player( int argc, char const *argv[] )
1947 {
1948 struct respawn_point *rp = NULL, *r;
1949
1950 if( argc == 1 )
1951 {
1952 for( int i=0; i<world.spawn_count; i++ )
1953 {
1954 r = &world.spawns[i];
1955 if( !strcmp( r->name, argv[0] ) )
1956 {
1957 rp = r;
1958 break;
1959 }
1960 }
1961
1962 if( !rp )
1963 vg_warn( "No spawn named '%s'\n", argv[0] );
1964 }
1965
1966 if( !rp )
1967 {
1968 float min_dist = INFINITY;
1969
1970 for( int i=0; i<world.spawn_count; i++ )
1971 {
1972 r = &world.spawns[i];
1973 float d = v3_dist2( r->co, player.rb.co );
1974
1975 vg_info( "Dist %s : %f\n", r->name, d );
1976 if( d < min_dist )
1977 {
1978 min_dist = d;
1979 rp = r;
1980 }
1981 }
1982 }
1983
1984 if( !rp )
1985 {
1986 vg_error( "No spawn found\n" );
1987 if( !world.spawn_count )
1988 return 0;
1989
1990 rp = &world.spawns[0];
1991 }
1992
1993 v4_copy( rp->q, player.rb.q );
1994 v3_copy( rp->co, player.rb.co );
1995
1996 player.vswitch = 1.0f;
1997 player.slip_last = 0.0f;
1998 player.is_dead = 0;
1999 player.in_air = 1;
2000 m3x3_identity( player.vr );
2001
2002 player.mdl.shoes[0] = 1;
2003 player.mdl.shoes[1] = 1;
2004
2005 rb_update_transform( &player.rb );
2006 m3x3_mulv( player.rb.to_world, (v3f){ 0.0f, 0.0f, -1.2f }, player.rb.v );
2007 return 1;
2008 }
2009
2010 static void player_update(void)
2011 {
2012 for( int i=0; i<player.land_log_count; i++ )
2013 draw_cross( player.land_target_log[i],
2014 player.land_target_colours[i], 0.25f);
2015
2016 if( vg_get_axis("grabl")>0.0f)
2017 reset_player(0,NULL);
2018
2019 if( vg_get_button_down( "switchmode" ) )
2020 {
2021 player.on_board ^= 0x1;
2022 }
2023
2024 if( player.is_dead )
2025 {
2026 character_ragdoll_iter( &player.mdl );
2027 character_debug_ragdoll( &player.mdl );
2028
2029 if( !freecam )
2030 player_animate_death_cam();
2031 }
2032 else
2033 {
2034 if( player.on_board )
2035 {
2036 player_do_motion();
2037 player_animate();
2038
2039 if( !freecam )
2040 player_animate_camera();
2041 }
2042 else
2043 {
2044 player_walkgrid();
2045 }
2046 }
2047
2048 if( freecam )
2049 player_freecam();
2050
2051 player_camera_update();
2052 player_audio();
2053 }
2054
2055 static void draw_player(void)
2056 {
2057 /* Draw */
2058 m4x3_copy( player.rb.to_world, player.mdl.mroot );
2059
2060 if( player.is_dead )
2061 character_mimic_ragdoll( &player.mdl );
2062 else
2063 character_eval( &player.mdl );
2064
2065 float opacity = 1.0f-player.air_blend;
2066 if( player.is_dead )
2067 opacity = 0.0f;
2068
2069 character_draw( &player.mdl, opacity, player.camera );
2070 }
2071
2072 #endif /* PLAYER_H */