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