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