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