7aaa857f794f941e81338f9790989f0050d76be4
7 static int freecam
= 0;
8 static float k_walkspeed
= 2.0f
;
9 static int walk_grid_iterations
= 1;
14 v3f co
, v
, a
, v_last
, m
, bob
;
16 float vswitch
, slip
, slip_last
,
19 float iY
; /* Yaw inertia */
20 int in_air
, is_dead
, on_board
;
30 v3f land_target_log
[22];
31 u32 land_target_colours
[22];
35 m4x3f to_world
, to_local
;
39 v3f handl_target
, handr_target
,
45 v3f camera_pos
, smooth_localcam
;
47 m4x3f camera
, camera_inverse
;
51 static void player_transform_update(void)
53 q_normalize( player
.rot
);
54 q_m3x3( player
.rot
, player
.to_world
);
55 v3_copy( player
.co
, player
.to_world
[3] );
57 m4x3_invert_affine( player
.to_world
, player
.to_local
);
60 static int reset_player( int argc
, char const *argv
[] )
62 v3_copy( (v3f
){ 0.0f
, -2.0f
, 0.0f
}, player
.co
);
66 if( !strcmp( argv
[0], "tutorial" ))
67 v3_copy( world
.tutorial
, player
.co
);
70 v3_copy( (v3f
){ 0.0f
, 0.0f
, -0.2f
}, player
.v
);
71 q_identity( player
.rot
);
72 player
.vswitch
= 1.0f
;
73 player
.slip_last
= 0.0f
;
76 m3x3_identity( player
.vr
);
78 player
.mdl
.shoes
[0] = 1;
79 player
.mdl
.shoes
[1] = 1;
81 player_transform_update();
85 static void player_mouseview(void)
87 static v2f mouse_last
,
88 view_vel
= { 0.0f
, 0.0f
};
90 if( vg_get_button_down( "primary" ) )
91 v2_copy( vg_mouse
, mouse_last
);
92 else if( vg_get_button( "primary" ) )
95 v2_sub( vg_mouse
, mouse_last
, delta
);
96 v2_copy( vg_mouse
, mouse_last
);
98 v2_muladds( view_vel
, delta
, 0.005f
, view_vel
);
101 v2_muls( view_vel
, 0.7f
, view_vel
);
102 v2_add( view_vel
, player
.angles
, player
.angles
);
103 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
107 static void player_freecam(void)
111 float movespeed
= 25.0f
;
112 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
113 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
115 m3x3_mulv( player
.camera
, lookdir
, lookdir
);
116 m3x3_mulv( player
.camera
, sidedir
, sidedir
);
118 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
119 if( vg_get_button( "forward" ) )
120 v3_muladds( move_vel
, lookdir
, ktimestep
* movespeed
, move_vel
);
121 if( vg_get_button( "back" ) )
122 v3_muladds( move_vel
, lookdir
, ktimestep
*-movespeed
, move_vel
);
123 if( vg_get_button( "left" ) )
124 v3_muladds( move_vel
, sidedir
, ktimestep
*-movespeed
, move_vel
);
125 if( vg_get_button( "right" ) )
126 v3_muladds( move_vel
, sidedir
, ktimestep
* movespeed
, move_vel
);
128 v3_muls( move_vel
, 0.7f
, move_vel
);
129 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
132 static void apply_gravity( v3f vel
, float const timestep
)
134 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
135 v3_muladds( vel
, gravity
, timestep
, vel
);
138 static void player_start_air(void)
142 float pstep
= ktimestep
*10.0f
;
144 float best_velocity_mod
= 0.0f
,
145 best_velocity_delta
= -9999.9f
;
148 m3x3_mulv( player
.to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, vup
);
149 v3_cross( vup
, player
.v
, axis
);
150 v3_normalize( axis
);
151 player
.land_log_count
= 0;
153 m3x3_identity( player
.vr
);
155 for( int m
=-3;m
<=12; m
++ )
157 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
160 v3_copy( player
.co
, pco
);
161 v3_copy( player
.v
, pv
);
164 * Try different 'rotations' of the velocity to find the best possible
165 * landing normal. This conserves magnitude at the expense of slightly
166 * unrealistic results
172 q_axis_angle( vr_q
, axis
, vmod
);
175 m3x3_mulv( vr
, pv
, pv
);
176 v3_muladds( pco
, pv
, ktimestep
, pco
);
178 for( int i
=0; i
<50; i
++ )
180 v3_copy( pco
, pco1
);
181 apply_gravity( pv
, pstep
);
183 m3x3_mulv( vr
, pv
, pv
);
184 v3_muladds( pco
, pv
, pstep
, pco
);
189 v3_sub( pco
, pco1
, vdir
);
190 contact
.dist
= v3_length( vdir
);
191 v3_divs( vdir
, contact
.dist
, vdir
);
193 if( ray_world( pco1
, vdir
, &contact
))
195 float land_delta
= v3_dot( pv
, contact
.normal
);
196 u32 scolour
= (u8
)(vg_minf(-land_delta
* 2.0f
, 255.0f
));
198 /* Bias prediction towords ramps */
199 if( ray_hit_is_ramp( &contact
) )
202 scolour
|= 0x0000a000;
205 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
207 best_velocity_delta
= land_delta
;
208 best_velocity_mod
= vmod
;
210 v3_copy( contact
.pos
, player
.land_target
);
212 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
213 q_m3x3( vr_q
, player
.vr
);
216 v3_copy( contact
.pos
,
217 player
.land_target_log
[player
.land_log_count
] );
218 player
.land_target_colours
[player
.land_log_count
] =
219 0xff000000 | scolour
;
221 player
.land_log_count
++;
228 //v3_rotate( player.v, best_velocity_mod, axis, player.v );
231 v3_muls( player
.v
, best_velocity_mod
, player
.v
);
234 static int sample_if_resistant( v3f pos
)
237 v3_copy( pos
, ground
);
243 if( ray_world( ground
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
246 v3_copy( player
.v
, angle
);
247 v3_normalize( angle
);
248 float resistance
= v3_dot( hit
.normal
, angle
);
250 if( resistance
< 0.25f
)
252 v3_copy( hit
.pos
, pos
);
260 static float stable_force( float current
, float diff
)
262 float new = current
+ diff
;
264 if( new * current
< 0.0f
)
270 static void player_physics_ground(void)
273 * Getting surface collision points,
274 * the contact manifold is a triangle for simplicity.
276 v3f contact_front
, contact_back
, contact_norm
, vup
, vside
,
279 float klength
= 0.65f
;
280 m4x3_mulv( player
.to_world
, (v3f
){ 0.15f
,0.0f
,-klength
}, contact_norm
);
281 m4x3_mulv( player
.to_world
, (v3f
){-0.15f
,0.0f
,-klength
}, contact_front
);
282 m4x3_mulv( player
.to_world
, (v3f
){ 0.00f
,0.0f
, klength
}, contact_back
);
283 m3x3_mulv( player
.to_world
, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, vup
);
284 m3x3_mulv( player
.to_world
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, vside
);
289 sample_if_resistant( contact_front
) +
290 sample_if_resistant( contact_back
) +
291 sample_if_resistant( contact_norm
);
293 if( contact_count
< 3 )
301 v3_sub( contact_norm
, contact_front
, v0
);
302 v3_sub( contact_back
, contact_front
, v1
);
303 v3_cross( v1
, v0
, norm
);
304 v3_normalize( norm
);
306 vg_line( contact_norm
, contact_front
, 0xff00ff00 );
307 vg_line( contact_back
, contact_front
, 0xff0000ff );
309 /* Surface alignment */
310 float angle
= v3_dot( vup
, norm
);
311 v3_cross( vup
, norm
, axis
);
316 q_axis_angle( correction
, axis
, acosf(angle
) );
317 q_mul( correction
, player
.rot
, player
.rot
);
320 float resistance
= v3_dot( norm
, player
.v
);
321 if( resistance
>= 0.0f
)
328 v3_muladds( player
.v
, norm
, -resistance
, player
.v
);
331 /* This is where velocity integration used to be */
335 player
.co
[1] = (contact_front
[1]+contact_back
[1])*0.5f
;
338 m3x3_mulv( player
.to_local
, player
.v
, vel
);
340 /* Calculate local forces */
342 if( fabsf(vel
[2]) > 0.01f
)
343 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
345 if( fabsf( slip
) > 1.2f
)
346 slip
= vg_signf( slip
) * 1.2f
;
348 player
.reverse
= -vg_signf(vel
[2]);
350 float substep
= ktimestep
* 0.2f
;
351 float fwd_resistance
= (vg_get_button( "break" )? 5.0f
: 0.02f
) * -substep
;
353 for( int i
=0; i
<5; i
++ )
355 vel
[2] = stable_force( vel
[2], vg_signf( vel
[2] ) * fwd_resistance
);
356 vel
[0] = stable_force( vel
[0], vg_signf( vel
[0] ) * -7.0f
*substep
);
359 static double start_push
= 0.0;
360 if( vg_get_button_down( "push" ) )
361 start_push
= vg_time
;
363 if( !vg_get_button("break") && vg_get_button( "push" ) )
365 float const k_maxpush
= 16.0f
,
368 float cycle_time
= vg_time
-start_push
,
369 amt
= k_pushaccel
* (sinf( cycle_time
* 8.0f
)*0.5f
+0.5f
)*ktimestep
,
370 current
= v3_length( vel
),
371 new_vel
= vg_minf( current
+ amt
, k_maxpush
);
372 new_vel
-= vg_minf(current
, k_maxpush
);
373 vel
[2] -= new_vel
* player
.reverse
;
376 m3x3_mulv( player
.to_world
, vel
, player
.v
);
378 if( vg_get_button( "yawl" ) )
379 player
.iY
+= 3.6f
* ktimestep
;
380 if( vg_get_button( "yawr" ) )
381 player
.iY
-= 3.6f
* ktimestep
;
383 float steer
= vg_get_axis( "horizontal" );
384 player
.iY
-= vg_signf(steer
)*powf(steer
,2.0f
) * 1.5f
* ktimestep
;
386 /* Too much lean and it starts to look like a snowboard here */
387 v2_lerp( player
.board_xy
, (v2f
){ slip
*0.25f
, 0.0f
},
388 ktimestep
*5.0f
, player
.board_xy
);
391 static void draw_cross(v3f pos
,u32 colour
, float scale
)
394 v3_add( (v3f
){ scale
,0.0f
,0.0f
}, pos
, p0
);
395 v3_add( (v3f
){-scale
,0.0f
,0.0f
}, pos
, p1
);
396 vg_line( p0
, p1
, colour
);
397 v3_add( (v3f
){0.0f
, scale
,0.0f
}, pos
, p0
);
398 v3_add( (v3f
){0.0f
,-scale
,0.0f
}, pos
, p1
);
399 vg_line( p0
, p1
, colour
);
400 v3_add( (v3f
){0.0f
,0.0f
, scale
}, pos
, p0
);
401 v3_add( (v3f
){0.0f
,0.0f
,-scale
}, pos
, p1
);
402 vg_line( p0
, p1
, colour
);
405 static void player_physics_air(void)
407 m3x3_mulv( player
.vr
, player
.v
, player
.v
);
408 for( int i
=0; i
<player
.land_log_count
; i
++ )
409 draw_cross( player
.land_target_log
[i
], player
.land_target_colours
[i
], 1);
411 draw_cross( player
.land_target
, 0xff0000ff, 1 );
414 v3_copy( player
.co
, ground_pos
);
415 ground_pos
[1] += 4.0f
;
419 if( ray_world( ground_pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
421 if( hit
.pos
[1] > player
.co
[1] )
425 if( !ray_hit_is_ramp( &hit
) )
428 character_ragdoll_copypose( &player
.mdl
, player
.v
);
437 float pstep
= ktimestep
*10.0f
;
440 v3_copy( player
.co
, pco
);
441 v3_copy( player
.v
, pv
);
443 float time_to_impact
= 0.0f
;
444 float limiter
= 1.0f
;
446 for( int i
=0; i
<50; i
++ )
448 v3_copy( pco
, pco1
);
449 apply_gravity( pv
, pstep
);
450 v3_muladds( pco
, pv
, pstep
, pco
);
452 //vg_line( pco, pco1, i&0x1?0xff000000:0xffffffff );
457 v3_sub( pco
, pco1
, vdir
);
458 contact
.dist
= v3_length( vdir
);
459 v3_divs( vdir
, contact
.dist
, vdir
);
461 float orig_dist
= contact
.dist
;
462 if( ray_world( pco1
, vdir
, &contact
))
465 m3x3_mulv( player
.to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, localup
);
467 float angle
= v3_dot( localup
, contact
.normal
);
469 v3_cross( localup
, contact
.normal
, axis
);
471 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
472 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
473 limiter
= 1.0f
-limiter
;
475 limiter
= 1.0f
-limiter
;
480 q_axis_angle( correction
, axis
, acosf(angle
)*0.05f
*(1.0f
-limiter
) );
481 q_mul( correction
, player
.rot
, player
.rot
);
484 draw_cross( contact
.pos
, 0xffff0000, 1 );
487 time_to_impact
+= pstep
;
490 player
.iY
-= vg_get_axis( "horizontal" ) * 3.6f
* ktimestep
;
493 float iX
= vg_get_axis( "vertical" ) * 3.6f
* limiter
* ktimestep
;
494 static float siX
= 0.0f
;
495 siX
= vg_lerpf( siX
, iX
, 0.3f
);
500 m3x3_mulv( player
.to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, vside
);
502 q_axis_angle( rotate
, vside
, siX
);
503 q_mul( rotate
, player
.rot
, player
.rot
);
506 v2f target
= {0.0f
,0.0f
};
507 v2_muladds( target
, (v2f
){ vg_get_axis("h1"), vg_get_axis("v1") },
508 player
.grab
, target
);
509 v2_lerp( player
.board_xy
, target
, ktimestep
*3.0f
, player
.board_xy
);
512 static void player_do_motion(void)
514 float horizontal
= vg_get_axis("horizontal"),
515 vertical
= vg_get_axis("vertical");
517 player
.joy_l
[0] = vg_signf(horizontal
) * powf( horizontal
, 2.0f
);
518 player
.joy_l
[1] = vg_signf(vertical
) * powf( vertical
, 2.0f
);
521 player_physics_air();
524 player_physics_ground();
526 /* Integrate velocity */
528 v3_copy( player
.co
, prevco
);
530 apply_gravity( player
.v
, ktimestep
);
531 v3_muladds( player
.co
, player
.v
, ktimestep
, player
.co
);
533 /* Integrate inertia */
534 v4f rotate
; v3f vup
= {0.0f
,1.0f
,0.0f
};
535 m3x3_mulv( player
.to_world
, vup
, vup
);
537 static float siY
= 0.0f
;
539 float lerpq
= player
.in_air
? 0.04f
: 0.3f
;
540 siY
= vg_lerpf( siY
, player
.iY
, lerpq
);
542 q_axis_angle( rotate
, vup
, siY
);
543 q_mul( rotate
, player
.rot
, player
.rot
);
545 player
.iY
= 0.0f
; /* temp */
549 if( gate_intersect( &gate_a
, player
.co
, prevco
) )
551 teleport_gate
*gate
= &gate_a
;
554 m4x3_mul( gate
->other
->to_world
, gate
->to_local
, transport
);
555 m4x3_mulv( transport
, player
.co
, player
.co
);
556 m3x3_mulv( transport
, player
.v
, player
.v
);
557 m3x3_mulv( transport
, player
.v_last
, player
.v_last
);
558 m3x3_mulv( transport
, player
.m
, player
.m
);
559 m3x3_mulv( transport
, player
.bob
, player
.bob
);
561 v4f transport_rotation
;
562 m3x3_q( transport
, transport_rotation
);
563 q_mul( transport_rotation
, player
.rot
, player
.rot
);
567 /* Camera and character */
568 player_transform_update();
570 player
.angles
[0] = atan2f( player
.v
[0], -player
.v
[2] );
571 player
.angles
[1] = atan2f( -player
.v
[1], sqrtf(player
.v
[0]*player
.v
[0]+
572 player
.v
[2]*player
.v
[2]) ) * 0.3f
;
574 player
.air_blend
= vg_lerpf( player
.air_blend
, player
.in_air
, 0.04f
);
575 v3_muladds( player
.camera_pos
, player
.v
, -0.05f
*player
.air_blend
,
579 static int player_walkgrid_tri_walkable( u32 tri
[3] )
581 return tri
[0] < world
.sm_road
.vertex_count
;
584 #define WALKGRID_SIZE 16
591 k_sample_type_air
, /* Nothing was hit. */
592 k_sample_type_invalid
, /* The point is invalid, but there is a sample
593 underneath that can be used */
594 k_sample_type_valid
, /* This point is good */
603 k_traverse_none
= 0x00,
609 samples
[WALKGRID_SIZE
][WALKGRID_SIZE
];
617 float move
; /* Current amount of movement we have left to apply */
618 v2f dir
; /* The movement delta */
619 v2i cell_id
;/* Current cell */
620 v2f pos
; /* Local position (in cell) */
625 * Get a sample at this pole location, will return 1 if the sample is valid,
626 * and pos will be updated to be the intersection location.
628 static void player_walkgrid_samplepole( struct grid_sample
*s
)
630 boxf region
= {{ s
->pos
[0] -0.01f
, s
->pos
[1] - 4.0f
, s
->pos
[2] -0.01f
},
631 { s
->pos
[0] +0.01f
, s
->pos
[1] + 4.0f
, s
->pos
[2] +0.01f
}};
634 vg_line( region
[0],region
[1], 0x20ffffff );
639 int len
= bvh_select_triangles( &world
.geo
, region
, geo
, 256 );
641 const float k_minworld_y
= -2000.0f
;
643 float walk_height
= k_minworld_y
,
644 block_height
= k_minworld_y
;
646 s
->type
= k_sample_type_air
;
648 for( int i
=0; i
<len
; i
++ )
650 u32
*ptri
= &world
.geo
.indices
[ geo
[i
] ];
652 for( int j
=0; j
<3; j
++ )
653 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
655 v3f vdown
= {0.0f
,-1.0f
,0.0f
};
657 v3_copy( s
->pos
, sample_from
);
658 sample_from
[1] = region
[1][1];
661 if( ray_tri( tri
, sample_from
, vdown
, &dist
))
664 v3_muladds( sample_from
, vdown
, dist
, p0
);
666 if( player_walkgrid_tri_walkable(ptri
) )
668 if( p0
[1] > walk_height
)
674 draw_cross( p0
, 0xffffffff, 0.05f
);
679 if( p0
[1] > block_height
)
680 block_height
= p0
[1];
682 draw_cross( p0
, 0xff0000ff, 0.05f
);
688 s
->pos
[1] = walk_height
;
690 if( walk_height
> k_minworld_y
)
691 if( block_height
> walk_height
)
692 s
->type
= k_sample_type_invalid
;
694 s
->type
= k_sample_type_valid
;
696 s
->type
= k_sample_type_air
;
699 if( s
->type
== k_sample_type_valid
)
701 vg_line_pt3( s
->pos
, 0.01f
, 0xff00ff00 );
710 count
= bvh_raycast( &world
.geo
, sample_pos
, vdir
, &hit
);
714 v3_copy( hit
.pos
, s
->pos
);
716 if( !player_walkgrid_tri_walkable( hit
.tri
) )
718 draw_cross( pos
, 0xff0000ff, 0.05f
);
723 draw_cross( pos
, 0xff00ff00, 0.05f
);
732 float const k_gridscale
= 0.5f
;
740 static void player_walkgrid_clip_blocker( struct grid_sample
*sa
,
741 struct grid_sample
*sb
,
742 struct grid_sample
*st
,
746 int valid_a
= sa
->type
== k_sample_type_valid
,
747 valid_b
= sb
->type
== k_sample_type_valid
;
748 struct grid_sample
*target
= valid_a
? sa
: sb
,
749 *other
= valid_a
? sb
: sa
;
750 v3_copy( target
->pos
, pos
);
751 v3_sub( other
->pos
, target
->pos
, clipdir
);
754 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*2.1f
, cell_region
[0]);
755 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*2.1f
, cell_region
[1]);
759 int len
= bvh_select_triangles( &world
.geo
, cell_region
, geo
, 256 );
762 float start_time
= v3_length( clipdir
),
763 min_time
= start_time
;
764 v3_normalize( clipdir
);
765 v3_muls( clipdir
, 0.0001f
, st
->clip
[dir
] );
767 for( int i
=0; i
<len
; i
++ )
769 u32
*ptri
= &world
.geo
.indices
[ geo
[i
] ];
770 for( int j
=0; j
<3; j
++ )
771 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
773 if( player_walkgrid_tri_walkable(ptri
) )
777 if(ray_tri( tri
, pos
, clipdir
, &dist
))
779 if( dist
> 0.0f
&& dist
< min_time
)
782 sb
->type
= k_sample_type_air
;
787 if( !(min_time
< start_time
) )
788 min_time
= 0.5f
* k_gridscale
;
790 v3_muls( clipdir
, min_time
/k_gridscale
, st
->clip
[dir
] );
793 v3_muladds( target
->pos
, st
->clip
[dir
], k_gridscale
, p0
);
796 static void player_walkgrid_clip_edge( struct grid_sample
*sa
,
797 struct grid_sample
*sb
,
798 struct grid_sample
*st
, /* data store */
801 v3f clipdir
= { 0.0f
, 0.0f
, 0.0f
}, pos
;
802 int valid_a
= sa
->type
== k_sample_type_valid
,
803 valid_b
= sb
->type
== k_sample_type_valid
;
805 struct grid_sample
*target
= valid_a
? sa
: sb
,
806 *other
= valid_a
? sb
: sa
;
808 v3_sub( other
->pos
, target
->pos
, clipdir
);
811 v3_copy( target
->pos
, pos
);
814 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*1.1f
, cell_region
[0]);
815 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*1.1f
, cell_region
[1]);
818 int len
= bvh_select_triangles( &world
.geo
, cell_region
, geo
, 256 );
820 float max_dist
= 0.0f
;
823 v3_cross( clipdir
,(v3f
){0.0f
,1.0f
,0.0f
},perp
);
824 v3_muls( clipdir
, 0.001f
, st
->clip
[dir
] );
826 for( int i
=0; i
<len
; i
++ )
828 u32
*ptri
= &world
.geo
.indices
[ geo
[i
] ];
829 for( int j
=0; j
<3; j
++ )
830 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
832 if( !player_walkgrid_tri_walkable(ptri
) )
835 for( int k
=0; k
<3; k
++ )
841 v3_sub( tri
[ia
], pos
, v0
);
842 v3_sub( tri
[ib
], pos
, v1
);
844 if( (clipdir
[2]*v0
[0] - clipdir
[0]*v0
[2]) *
845 (clipdir
[2]*v1
[0] - clipdir
[0]*v1
[2]) < 0.0f
)
847 float da
= v3_dot(v0
,perp
),
848 db
= v3_dot(v1
,perp
),
853 v3_muls( v1
, qa
, p0
);
854 v3_muladds( p0
, v0
, 1.0f
-qa
, p0
);
856 float h
= v3_dot(p0
,clipdir
)/v3_dot(clipdir
,clipdir
);
858 if( h
>= max_dist
&& h
<= 1.0f
)
861 float l
= 1.0f
/v3_length(clipdir
);
862 v3_muls( p0
, l
, st
->clip
[dir
] );
869 static void player_walkgrid_clip( struct grid_sample
*sa
,
870 struct grid_sample
*sb
,
873 int mintype
= VG_MIN( sa
->type
, sb
->type
),
874 maxtype
= VG_MAX( sa
->type
, sb
->type
);
876 if( maxtype
== k_sample_type_valid
)
878 if( mintype
== k_sample_type_air
|| mintype
== k_sample_type_invalid
)
880 player_walkgrid_clip_edge( sa
, sb
, sa
, dir
);
884 else if( mintype
== k_sample_type_invalid
)
886 player_walkgrid_clip_blocker( sa
, sb
, dir
);
892 static const struct conf
899 * o: the 'other' point to do a A/B test with
900 * if its -1, all AB is done.
910 k_walkgrid_configs
[16] = {
912 {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
913 {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
914 {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
916 {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
917 {{{ 3,3, 3,0, 1,0, -1,-1 },
918 { 1,1, 0,1, 1,0, -1,-1 }}, 2},
919 {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
920 {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
922 {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
923 {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
924 {{{ 2,2, 1,3, 0,1, -1,-1 },
925 { 0,0, 0,0, 0,1, -1,-1 }}, 2},
926 {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
928 {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
929 {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
930 {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
935 * Get a buffer of edges from cell location
937 static const struct conf
*player_walkgrid_conf( struct walkgrid
*wg
,
939 struct grid_sample
*corners
[4] )
941 corners
[0] = &wg
->samples
[cell
[1] ][cell
[0] ];
942 corners
[1] = &wg
->samples
[cell
[1]+1][cell
[0] ];
943 corners
[2] = &wg
->samples
[cell
[1]+1][cell
[0]+1];
944 corners
[3] = &wg
->samples
[cell
[1] ][cell
[0]+1];
946 u32 vd0
= corners
[0]->type
== k_sample_type_valid
,
947 vd1
= corners
[1]->type
== k_sample_type_valid
,
948 vd2
= corners
[2]->type
== k_sample_type_valid
,
949 vd3
= corners
[3]->type
== k_sample_type_valid
,
950 config
= (vd0
<<3) | (vd1
<<2) | (vd2
<<1) | vd3
;
952 return &k_walkgrid_configs
[ config
];
955 static void player_walkgrid_floor(v3f pos
)
957 v3_muls( pos
, 1.0f
/k_gridscale
, pos
);
958 v3_floor( pos
, pos
);
959 v3_muls( pos
, k_gridscale
, pos
);
963 * Computes the barycentric coordinate of location on a triangle (vertical),
964 * then sets the Y position to the interpolation of the three points
966 static void player_walkgrid_stand_tri( v3f a
, v3f b
, v3f c
, v3f pos
)
971 v3_sub( pos
, a
, v2
);
973 float d
= v0
[0]*v1
[2] - v1
[0]*v0
[2],
974 v
= (v2
[0]*v1
[2] - v1
[0]*v2
[2]) / d
,
975 w
= (v0
[0]*v2
[2] - v2
[0]*v0
[2]) / d
,
978 vg_line( pos
, a
, 0xffff0000 );
979 vg_line( pos
, b
, 0xff00ff00 );
980 vg_line( pos
, c
, 0xff0000ff );
981 pos
[1] = u
*a
[1] + v
*b
[1] + w
*c
[1];
985 * Get the minimum time value of pos+dir until a cell edge
987 * t[0] -> t[3] are the individual time values
988 * t[5] & t[6] are the maximum axis values
989 * t[6] is the minimum value
992 static void player_walkgrid_min_cell( float t
[7], v2f pos
, v2f dir
)
994 v2f frac
= { 1.0f
/dir
[0], 1.0f
/dir
[1] };
1001 if( fabsf(dir
[0]) > 0.0001f
)
1003 t
[0] = (0.0f
-pos
[0]) * frac
[0];
1004 t
[1] = (1.0f
-pos
[0]) * frac
[0];
1006 if( fabsf(dir
[1]) > 0.0001f
)
1008 t
[2] = (0.0f
-pos
[1]) * frac
[1];
1009 t
[3] = (1.0f
-pos
[1]) * frac
[1];
1012 t
[4] = vg_maxf(t
[0],t
[1]);
1013 t
[5] = vg_maxf(t
[2],t
[3]);
1014 t
[6] = vg_minf(t
[4],t
[5]);
1017 static void player_walkgrid_iter(struct walkgrid
*wg
, int iter
)
1021 * For each walkgrid iteration we are stepping through cells and determining
1022 * the intersections with the grid, and any edges that are present
1026 if( wg
->cell_id
[0] < 0 || wg
->cell_id
[0] >= WALKGRID_SIZE
-1 ||
1027 wg
->cell_id
[1] < 0 || wg
->cell_id
[1] >= WALKGRID_SIZE
-1 )
1030 * This condition should never be reached if the grid size is big
1038 u32 icolours
[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
1040 v3f pa
, pb
, pc
, pd
, pl0
, pl1
;
1041 pa
[0] = wg
->region
[0][0] + (float)wg
->cell_id
[0] *k_gridscale
;
1042 pa
[1] = (wg
->region
[0][1] + wg
->region
[1][1]) * 0.5f
+ k_gridscale
;
1043 pa
[2] = wg
->region
[0][2] + (float)wg
->cell_id
[1] *k_gridscale
;
1046 pb
[2] = pa
[2] + k_gridscale
;
1047 pc
[0] = pa
[0] + k_gridscale
;
1049 pc
[2] = pa
[2] + k_gridscale
;
1050 pd
[0] = pa
[0] + k_gridscale
;
1054 vg_line( pa
, pb
, 0xff00ffff );
1055 vg_line( pb
, pc
, 0xff00ffff );
1056 vg_line( pc
, pd
, 0xff00ffff );
1057 vg_line( pd
, pa
, 0xff00ffff );
1059 pl0
[0] = pa
[0] + wg
->pos
[0]*k_gridscale
;
1061 pl0
[2] = pa
[2] + wg
->pos
[1]*k_gridscale
;
1064 * If there are edges present, we need to create a 'substep' event, where
1065 * we find the intersection point, find the fully resolved position,
1066 * then the new pos dir is the intersection->resolution
1068 * the resolution is applied in non-discretized space in order to create a
1069 * suitable vector for finding outflow, we want it to leave the cell so it
1070 * can be used by the quad
1074 v2_copy( wg
->pos
, pos
);
1075 v2_muls( wg
->dir
, wg
->move
, dir
);
1077 struct grid_sample
*corners
[4];
1078 v2f corners2d
[4] = {{0.0f
,0.0f
},{0.0f
,1.0f
},{1.0f
,1.0f
},{1.0f
,0.0f
}};
1079 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
1082 player_walkgrid_min_cell( t
, pos
, dir
);
1084 for( int i
=0; i
<conf
->edge_count
; i
++ )
1086 const struct confedge
*edge
= &conf
->edges
[i
];
1088 v2f e0
, e1
, n
, r
, target
, res
, tangent
;
1089 e0
[0] = corners2d
[edge
->i0
][0] + corners
[edge
->d0
]->clip
[edge
->a0
][0];
1090 e0
[1] = corners2d
[edge
->i0
][1] + corners
[edge
->d0
]->clip
[edge
->a0
][2];
1091 e1
[0] = corners2d
[edge
->i1
][0] + corners
[edge
->d1
]->clip
[edge
->a1
][0];
1092 e1
[1] = corners2d
[edge
->i1
][1] + corners
[edge
->d1
]->clip
[edge
->a1
][2];
1094 v3f pe0
= { pa
[0] + e0
[0]*k_gridscale
,
1096 pa
[2] + e0
[1]*k_gridscale
};
1097 v3f pe1
= { pa
[0] + e1
[0]*k_gridscale
,
1099 pa
[2] + e1
[1]*k_gridscale
};
1101 v2_sub( e1
, e0
, tangent
);
1107 * If we find ourselfs already penetrating the edge, move back out a
1110 v2_sub( e0
, pos
, r
);
1111 float p1
= v2_dot(r
,n
);
1115 v2_muladds( pos
, n
, p1
+0.0001f
, pos
);
1116 v2_copy( pos
, wg
->pos
);
1117 v3f p_new
= { pa
[0] + pos
[0]*k_gridscale
,
1119 pa
[2] + pos
[1]*k_gridscale
};
1120 v3_copy( p_new
, pl0
);
1123 v2_add( pos
, dir
, target
);
1126 v2_sub( e0
, pos
, v1
);
1127 v2_sub( target
, pos
, v2
);
1131 v2_sub( e0
, target
, r
);
1132 float p
= v2_dot(r
,n
),
1133 t1
= v2_dot(v1
,v3
)/v2_dot(v2
,v3
);
1135 if( t1
< t
[6] && t1
> 0.0f
&& -p
< 0.001f
)
1137 v2_muladds( target
, n
, p
+0.0001f
, res
);
1140 v2_muladds( pos
, dir
, t1
, intersect
);
1141 v2_copy( intersect
, pos
);
1142 v2_sub( res
, intersect
, dir
);
1144 v3f p_res
= { pa
[0] + res
[0]*k_gridscale
,
1146 pa
[2] + res
[1]*k_gridscale
};
1147 v3f p_int
= { pa
[0] + intersect
[0]*k_gridscale
,
1149 pa
[2] + intersect
[1]*k_gridscale
};
1151 vg_line( pl0
, p_int
, icolours
[iter
%3] );
1152 v3_copy( p_int
, pl0
);
1153 v2_copy( pos
, wg
->pos
);
1155 player_walkgrid_min_cell( t
, pos
, dir
);
1160 * Compute intersection with grid cell moving outwards
1162 t
[6] = vg_minf( t
[6], 1.0f
);
1164 pl1
[0] = pl0
[0] + dir
[0]*k_gridscale
*t
[6];
1166 pl1
[2] = pl0
[2] + dir
[1]*k_gridscale
*t
[6];
1167 vg_line( pl0
, pl1
, icolours
[iter
%3] );
1172 * To figure out what t value created the clip so we know which edge
1178 wg
->pos
[1] = pos
[1] + dir
[1]*t
[6];
1180 if( t
[0] > t
[1] ) /* left edge */
1182 wg
->pos
[0] = 0.9999f
;
1185 if( wg
->cell_id
[0] == 0 )
1188 else /* Right edge */
1190 wg
->pos
[0] = 0.0001f
;
1193 if( wg
->cell_id
[0] == WALKGRID_SIZE
-2 )
1199 wg
->pos
[0] = pos
[0] + dir
[0]*t
[6];
1201 if( t
[2] > t
[3] ) /* bottom edge */
1203 wg
->pos
[1] = 0.9999f
;
1206 if( wg
->cell_id
[1] == 0 )
1211 wg
->pos
[1] = 0.0001f
;
1214 if( wg
->cell_id
[1] == WALKGRID_SIZE
-2 )
1223 v2_muladds( wg
->pos
, dir
, wg
->move
, wg
->pos
);
1228 static void player_walkgrid_stand_cell(struct walkgrid
*wg
)
1231 * NOTE: as opposed to the other function which is done in discretized space
1232 * this use a combination of both.
1236 world
[0] = wg
->region
[0][0]+((float)wg
->cell_id
[0]+wg
->pos
[0])*k_gridscale
;
1237 world
[1] = player
.co
[1];
1238 world
[2] = wg
->region
[0][2]+((float)wg
->cell_id
[1]+wg
->pos
[1])*k_gridscale
;
1240 struct grid_sample
*corners
[4];
1241 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
1243 if( conf
!= k_walkgrid_configs
)
1245 if( conf
->edge_count
== 0 )
1249 /* Split the basic quad along the shortest diagonal */
1250 if( fabsf(corners
[2]->pos
[1] - corners
[0]->pos
[1]) <
1251 fabsf(corners
[3]->pos
[1] - corners
[1]->pos
[1]) )
1253 vg_line( corners
[2]->pos
, corners
[0]->pos
, 0xffaaaaaa );
1255 if( wg
->pos
[0] > wg
->pos
[1] )
1256 player_walkgrid_stand_tri( corners
[0]->pos
,
1258 corners
[2]->pos
, world
);
1260 player_walkgrid_stand_tri( corners
[0]->pos
,
1262 corners
[1]->pos
, world
);
1266 vg_line( corners
[3]->pos
, corners
[1]->pos
, 0xffaaaaaa );
1268 if( wg
->pos
[0] < 1.0f
-wg
->pos
[1] )
1269 player_walkgrid_stand_tri( corners
[0]->pos
,
1271 corners
[1]->pos
, world
);
1273 player_walkgrid_stand_tri( corners
[3]->pos
,
1275 corners
[1]->pos
, world
);
1280 for( int i
=0; i
<conf
->edge_count
; i
++ )
1282 const struct confedge
*edge
= &conf
->edges
[i
];
1285 v3_muladds( corners
[edge
->i0
]->pos
,
1286 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
1287 v3_muladds( corners
[edge
->i1
]->pos
,
1288 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
1291 * Find penetration distance between player position and the edge
1294 v2f normal
= { -(p1
[2]-p0
[2]), p1
[0]-p0
[0] },
1295 rel
= { world
[0]-p0
[0], world
[2]-p0
[2] };
1297 if( edge
->o0
== -1 )
1299 /* No subregions (default case), just use triangle created by
1301 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
1308 * Test if we are in the first region, which is
1309 * edge.i0, edge.e0, edge.o0,
1312 v3_sub( p0
, corners
[edge
->o0
]->pos
, ref
);
1313 v3_sub( world
, corners
[edge
->o0
]->pos
, v0
);
1315 vg_line( corners
[edge
->o0
]->pos
, p0
, 0xffffff00 );
1316 vg_line( corners
[edge
->o0
]->pos
, world
, 0xff000000 );
1318 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
1320 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
1322 corners
[edge
->o0
]->pos
, world
);
1326 if( edge
->o1
== -1 )
1329 * No other edges mean we just need to use the opposite
1331 * e0, e1, o0 (in our case, also i1)
1333 player_walkgrid_stand_tri( p0
,
1335 corners
[edge
->o0
]->pos
, world
);
1340 * Note: this v0 calculation can be ommited with the
1343 * the last two triangles we have are:
1348 v3_sub( p1
, corners
[edge
->o1
]->pos
, ref
);
1349 v3_sub( world
, corners
[edge
->o1
]->pos
, v0
);
1350 vg_line( corners
[edge
->o1
]->pos
, p1
, 0xff00ffff );
1352 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
1354 player_walkgrid_stand_tri( p0
,
1356 corners
[edge
->o1
]->pos
,
1361 player_walkgrid_stand_tri( p1
,
1362 corners
[edge
->i1
]->pos
,
1363 corners
[edge
->o1
]->pos
,
1373 v3_copy( world
, player
.co
);
1376 static void player_walkgrid_getsurface(void)
1378 float const k_stepheight
= 0.5f
;
1379 float const k_miny
= 0.6f
;
1380 float const k_height
= 1.78f
;
1381 float const k_region_size
= (float)WALKGRID_SIZE
/2.0f
* k_gridscale
;
1383 static struct walkgrid wg
;
1386 v3_copy( player
.co
, cell
);
1387 player_walkgrid_floor( cell
);
1389 v3_muladds( cell
, (v3f
){-1.0f
,-1.0f
,-1.0f
}, k_region_size
, wg
.region
[0] );
1390 v3_muladds( cell
, (v3f
){ 1.0f
, 1.0f
, 1.0f
}, k_region_size
, wg
.region
[1] );
1394 * Create player input vector
1396 v3f delta
= {0.0f
,0.0f
,0.0f
};
1397 v3f fwd
= { -sinf(-player
.angles
[0]), 0.0f
, -cosf(-player
.angles
[0]) },
1398 side
= { -fwd
[2], 0.0f
, fwd
[0] };
1401 if( !vg_console_enabled() )
1403 if( glfwGetKey( vg_window
, GLFW_KEY_W
) )
1404 v3_muladds( delta
, fwd
, ktimestep
*k_walkspeed
, delta
);
1405 if( glfwGetKey( vg_window
, GLFW_KEY_S
) )
1406 v3_muladds( delta
, fwd
, -ktimestep
*k_walkspeed
, delta
);
1408 if( glfwGetKey( vg_window
, GLFW_KEY_A
) )
1409 v3_muladds( delta
, side
, -ktimestep
*k_walkspeed
, delta
);
1410 if( glfwGetKey( vg_window
, GLFW_KEY_D
) )
1411 v3_muladds( delta
, side
, ktimestep
*k_walkspeed
, delta
);
1415 * Create our move in grid space
1417 wg
.dir
[0] = delta
[0] * (1.0f
/k_gridscale
);
1418 wg
.dir
[1] = delta
[2] * (1.0f
/k_gridscale
);
1423 (player
.co
[0] - wg
.region
[0][0]) * (1.0f
/k_gridscale
),
1424 (player
.co
[2] - wg
.region
[0][2]) * (1.0f
/k_gridscale
)
1426 v2f region_cell_pos
;
1427 v2_floor( region_pos
, region_cell_pos
);
1428 v2_sub( region_pos
, region_cell_pos
, wg
.pos
);
1430 wg
.cell_id
[0] = region_cell_pos
[0];
1431 wg
.cell_id
[1] = region_cell_pos
[1];
1435 /* Get surface samples
1437 * TODO: Replace this with a spiral starting from the player position
1439 for( int y
=0; y
<WALKGRID_SIZE
; y
++ )
1441 for( int x
=0; x
<WALKGRID_SIZE
; x
++ )
1443 struct grid_sample
*s
= &wg
.samples
[y
][x
];
1444 v3_muladds( wg
.region
[0], (v3f
){ x
, 0, y
}, k_gridscale
, s
->pos
);
1445 s
->pos
[1] = cell
[1];
1446 player_walkgrid_samplepole( s
);
1451 * Calculate h+v clipping distances.
1452 * Distances are stored in A always, so you know that if the sample is
1453 * invalid, this signifies the start of the manifold as opposed to the
1454 * extent or bounds of it.
1456 for( int i
=0; i
<2; i
++ )
1458 for( int x
=0; x
<WALKGRID_SIZE
; x
++ )
1460 for( int z
=0; z
<WALKGRID_SIZE
-1; z
++ )
1462 struct grid_sample
*sa
, *sb
;
1465 sa
= &wg
.samples
[z
][x
];
1466 sb
= &wg
.samples
[z
+1][x
];
1470 sa
= &wg
.samples
[x
][z
];
1471 sb
= &wg
.samples
[x
][z
+1];
1474 player_walkgrid_clip( sa
, sb
, i
);
1476 if( sa
->type
== k_sample_type_valid
&&
1477 sb
->type
== k_sample_type_valid
)
1478 vg_line( sa
->pos
, sb
->pos
, 0xffffffff );
1480 if( sa
->valid
!= sb
->valid
)
1482 clipdir
[i
*2] = (float)(sa
->valid
- sb
->valid
) * k_gridscale
;
1484 player_walkgrid_clip( sa
->valid
? sa
->pos
: sb
->pos
,
1485 clipdir
, sa
->clip
[i
] );
1491 vg_line( sa
->pos
, sb
->pos
, 0xffffffff );
1500 for(int y
=0; y
<WALKGRID_SIZE
; y
++ )
1502 for(int x
=0; x
<WALKGRID_SIZE
; x
++ )
1504 struct grid_sample
*s
= &wg
.samples
[y
][x
];
1505 v3_muladds( wg
.region
[0], (v3f
){ x
, 0, y
}, k_gridscale
, s
->pos
);
1506 s
->state
= k_traverse_none
;
1507 s
->type
= k_sample_type_air
;
1508 v3_zero( s
->clip
[0] );
1509 v3_zero( s
->clip
[1] );
1513 v2i border
[WALKGRID_SIZE
*WALKGRID_SIZE
];
1514 v2i
*cborder
= border
;
1515 u32 border_length
= 1;
1517 struct grid_sample
*base
= NULL
;
1519 v2i starters
[] = {{0,0},{1,1},{0,1},{1,0}};
1521 for( int i
=0;i
<4;i
++ )
1524 v2i_add( wg
.cell_id
, starters
[i
], test
);
1525 v2i_copy( test
, border
[0] );
1526 base
= &wg
.samples
[test
[1]][test
[0]];
1528 base
->pos
[1] = cell
[1];
1529 player_walkgrid_samplepole( base
);
1531 if( base
->type
== k_sample_type_valid
)
1534 base
->type
= k_sample_type_air
;
1537 vg_line_pt3( base
->pos
, 0.1f
, 0xffffffff );
1541 while( border_length
)
1543 v2i directions
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1545 v2i
*old_border
= cborder
;
1546 int len
= border_length
;
1549 cborder
= old_border
+len
;
1551 for( int i
=0; i
<len
; i
++ )
1554 v2i_copy( old_border
[i
], co
);
1555 struct grid_sample
*sa
= &wg
.samples
[co
[1]][co
[0]];
1557 for( int j
=0; j
<4; j
++ )
1560 v2i_add( co
, directions
[j
], newp
);
1562 if( newp
[0] < 0 || newp
[1] < 0 ||
1563 newp
[0] == WALKGRID_SIZE
|| newp
[1] == WALKGRID_SIZE
)
1566 struct grid_sample
*sb
= &wg
.samples
[newp
[1]][newp
[0]];
1567 enum traverse_state thismove
= j
%2==0? 1: 2;
1569 if( (sb
->state
& thismove
) == 0x00 ||
1570 sb
->type
== k_sample_type_air
)
1572 sb
->pos
[1] = sa
->pos
[1];
1574 player_walkgrid_samplepole( sb
);
1576 if( sb
->type
!= k_sample_type_air
)
1579 * Need to do a blocker pass
1582 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
1583 player_walkgrid_clip_blocker( sa
, sb
, store
, j
%2 );
1586 if( sb
->type
!= k_sample_type_air
)
1588 vg_line( sa
->pos
, sb
->pos
, 0xffffffff );
1590 if( sb
->state
== k_traverse_none
)
1591 v2i_copy( newp
, cborder
[ border_length
++ ] );
1596 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
1597 vg_line( sa
->pos
, p1
, 0xffffffff );
1603 * A clipping pass is now done on the edge of the walkable
1607 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
1608 player_walkgrid_clip_edge( sa
, sb
, store
, j
%2 );
1611 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
1612 vg_line( sa
->pos
, p1
, 0xffffffff );
1615 sb
->state
|= thismove
;
1619 sa
->state
= k_traverse_h
|k_traverse_v
;
1623 if( iter
== walk_grid_iterations
)
1628 player
.co
[0] += wg
.dir
[0];
1629 player
.co
[2] += wg
.dir
[1];
1634 /* Draw connections */
1635 struct grid_sample
*corners
[4];
1636 for( int x
=0; x
<WALKGRID_SIZE
-1; x
++ )
1638 for( int z
=0; z
<WALKGRID_SIZE
-1; z
++ )
1640 const struct conf
*conf
=
1641 player_walkgrid_conf( &wg
, (v2i
){x
,z
}, corners
);
1643 for( int i
=0; i
<conf
->edge_count
; i
++ )
1645 const struct confedge
*edge
= &conf
->edges
[i
];
1648 v3_muladds( corners
[edge
->i0
]->pos
,
1649 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
1650 v3_muladds( corners
[edge
->i1
]->pos
,
1651 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
1653 vg_line( p0
, p1
, 0xff0000ff );
1659 * Commit player movement into the grid
1662 if( v3_length2(delta
) <= 0.00001f
)
1666 for(; i
<8 && wg
.move
> 0.001f
; i
++ )
1667 player_walkgrid_iter( &wg
, i
);
1669 player_walkgrid_stand_cell( &wg
);
1672 static void player_walkgrid(void)
1674 player_walkgrid_getsurface();
1676 m4x3_mulv( player
.to_world
, (v3f
){0.0f
,1.8f
,0.0f
}, player
.camera_pos
);
1678 player_transform_update();
1681 static void player_animate(void)
1683 /* Camera position */
1684 v3_sub( player
.v
, player
.v_last
, player
.a
);
1685 v3_copy( player
.v
, player
.v_last
);
1687 v3_add( player
.m
, player
.a
, player
.m
);
1688 v3_lerp( player
.m
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.1f
, player
.m
);
1691 player
.m
[0] = vg_clampf( player
.m
[0], -2.0f
, 2.0f
);
1692 player
.m
[1] = vg_clampf( player
.m
[1], -0.2f
, 5.0f
);
1693 player
.m
[2] = vg_clampf( player
.m
[2], -2.0f
, 2.0f
);
1694 v3_copy( player
.m
, target
);
1695 v3_lerp( player
.bob
, target
, 0.2f
, player
.bob
);
1698 float lslip
= fabsf(player
.slip
); //vg_minf( 0.4f, slip );
1700 float grabt
= vg_get_axis( "grabr" )*0.5f
+0.5f
;
1701 player
.grab
= vg_lerpf( player
.grab
, grabt
, 0.04f
);
1703 float kheight
= 2.0f
,
1708 head
[1] = (0.3f
+cosf(lslip
)*0.5f
*(1.0f
-player
.grab
*0.7f
)) * kheight
;
1712 m3x3_mulv( player
.to_local
, player
.bob
, offset
);
1714 offset
[0] *= 0.3333f
;
1715 offset
[1] *= -0.25f
;
1717 v3_muladds( head
, offset
, 0.7f
, head
);
1718 head
[1] = vg_clampf( head
[1], 0.3f
, kheight
);
1723 v3_copy( head
, player
.view
);
1724 v3f camoffs
= {-0.2f
,-0.6f
,0.00f
};
1725 v3_add( player
.view
, camoffs
, player
.view
);
1730 * Animation blending
1731 * ===========================================
1734 static float fslide
= 0.0f
;
1735 static float fdirz
= 0.0f
;
1736 static float fdirx
= 0.0f
;
1737 static float fstand
= 0.0f
;
1738 static float ffly
= 0.0f
;
1740 float speed
= v3_length( player
.v
);
1742 fstand
= vg_lerpf(fstand
, 1.0f
-vg_clampf(speed
*0.03f
,0.0f
,1.0f
),0.1f
);
1743 fslide
= vg_lerpf(fslide
, vg_clampf(lslip
+fabsf(offset
[0])*0.2f
,
1745 fdirz
= vg_lerpf(fdirz
, player
.reverse
> 0.0f
? 1.0f
: 0.0f
, 0.04f
);
1746 fdirx
= vg_lerpf(fdirx
, player
.slip
< 0.0f
? 1.0f
: 0.0f
, 0.04f
);
1747 ffly
= vg_lerpf(ffly
, player
.in_air
? 1.0f
: 0.0f
, 0.04f
);
1749 character_pose_reset( &player
.mdl
);
1751 float amt_air
= ffly
*ffly
,
1752 amt_ground
= 1.0f
-amt_air
,
1753 amt_std
= (1.0f
-fslide
) * amt_ground
,
1754 amt_stand
= amt_std
* fstand
,
1755 amt_aero
= amt_std
* (1.0f
-fstand
),
1756 amt_slide
= amt_ground
* fslide
;
1758 character_final_pose( &player
.mdl
, offset
, &pose_stand
, amt_stand
);
1759 character_final_pose( &player
.mdl
, offset
, &pose_aero
, amt_aero
*fdirz
);
1760 character_final_pose( &player
.mdl
, offset
,
1761 &pose_aero_reverse
, amt_aero
* (1.0f
-fdirz
) );
1762 character_final_pose( &player
.mdl
, offset
, &pose_slide
, amt_slide
*fdirx
);
1763 character_final_pose( &player
.mdl
, offset
,
1764 &pose_slide1
, amt_slide
*(1.0f
-fdirx
) );
1766 character_final_pose( &player
.mdl
, (v3f
){0.0f
,0.0f
,0.0f
},
1767 &pose_fly
, amt_air
);
1769 /* Camera position */
1770 v3_lerp( player
.smooth_localcam
, player
.mdl
.cam_pos
, 0.08f
,
1771 player
.smooth_localcam
);
1772 v3_muladds( player
.smooth_localcam
, offset
, 0.7f
, player
.camera_pos
);
1773 player
.camera_pos
[1] = vg_clampf( player
.camera_pos
[1], 0.3f
, kheight
);
1774 m4x3_mulv( player
.to_world
, player
.camera_pos
, player
.camera_pos
);
1778 * ==========================
1780 struct ik_basic
*arm_l
= &player
.mdl
.ik_arm_l
,
1781 *arm_r
= &player
.mdl
.ik_arm_r
;
1784 m3x3_mulv( player
.to_local
, player
.v
, localv
);
1785 v3_muladds( arm_l
->end
, localv
, -0.01f
, arm_l
->end
);
1786 v3_muladds( arm_r
->end
, localv
, -0.01f
, arm_r
->end
);
1788 /* New board transformation */
1789 v4f board_rotation
; v3f board_location
;
1792 q_axis_angle( rz
, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, player
.board_xy
[0] );
1793 q_axis_angle( rx
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, player
.board_xy
[1] );
1794 q_mul( rx
, rz
, board_rotation
);
1796 v3f
*mboard
= player
.mdl
.matrices
[k_chpart_board
];// player.mboard;
1797 q_m3x3( board_rotation
, mboard
);
1798 m3x3_mulv( mboard
, (v3f
){ 0.0f
, -0.5f
, 0.0f
}, board_location
);
1799 v3_add( (v3f
){0.0f
,0.5f
,0.0f
}, board_location
, board_location
);
1800 v3_copy( board_location
, mboard
[3] );
1803 float wheel_r
= offset
[0]*-0.4f
;
1805 q_axis_angle( qwheel
, (v3f
){0.0f
,1.0f
,0.0f
}, wheel_r
);
1807 q_m3x3( qwheel
, player
.mdl
.matrices
[k_chpart_wb
] );
1809 m3x3_transpose( player
.mdl
.matrices
[k_chpart_wb
],
1810 player
.mdl
.matrices
[k_chpart_wf
] );
1811 v3_copy( player
.mdl
.offsets
[k_chpart_wb
],
1812 player
.mdl
.matrices
[k_chpart_wb
][3] );
1813 v3_copy( player
.mdl
.offsets
[k_chpart_wf
],
1814 player
.mdl
.matrices
[k_chpart_wf
][3] );
1816 m4x3_mul( mboard
, player
.mdl
.matrices
[k_chpart_wb
],
1817 player
.mdl
.matrices
[k_chpart_wb
] );
1818 m4x3_mul( mboard
, player
.mdl
.matrices
[k_chpart_wf
],
1819 player
.mdl
.matrices
[k_chpart_wf
] );
1821 m4x3_mulv( mboard
, player
.mdl
.ik_leg_l
.end
, player
.mdl
.ik_leg_l
.end
);
1822 m4x3_mulv( mboard
, player
.mdl
.ik_leg_r
.end
, player
.mdl
.ik_leg_r
.end
);
1825 v3_copy( player
.mdl
.ik_arm_l
.end
, player
.handl_target
);
1826 v3_copy( player
.mdl
.ik_arm_r
.end
, player
.handr_target
);
1828 if( 1||player
.in_air
)
1830 float tuck
= player
.board_xy
[1],
1831 tuck_amt
= fabsf( tuck
) * (1.0f
-fabsf(player
.board_xy
[0]));
1833 float crouch
= player
.grab
*0.3f
;
1834 v3_muladds( player
.mdl
.ik_body
.base
, (v3f
){0.0f
,-1.0f
,0.0f
},
1835 crouch
, player
.mdl
.ik_body
.base
);
1836 v3_muladds( player
.mdl
.ik_body
.end
, (v3f
){0.0f
,-1.0f
,0.0f
},
1837 crouch
*1.2f
, player
.mdl
.ik_body
.end
);
1841 //foot_l *= 1.0f-tuck_amt*1.5f;
1843 if( player
.grab
> 0.1f
)
1845 m4x3_mulv( mboard
, (v3f
){0.1f
,0.14f
,0.6f
},
1846 player
.handl_target
);
1851 //foot_r *= 1.0f-tuck_amt*1.4f;
1853 if( player
.grab
> 0.1f
)
1855 m4x3_mulv( mboard
, (v3f
){0.1f
,0.14f
,-0.6f
},
1856 player
.handr_target
);
1861 v3_lerp( player
.handl
, player
.handl_target
, 0.1f
, player
.handl
);
1862 v3_lerp( player
.handr
, player
.handr_target
, 0.1f
, player
.handr
);
1864 v3_copy( player
.handl
, player
.mdl
.ik_arm_l
.end
);
1865 v3_copy( player
.handr
, player
.mdl
.ik_arm_r
.end
);
1869 static float rhead
= 0.0f
;
1870 rhead
= vg_lerpf( rhead
,
1871 vg_clampf(atan2f( localv
[2], -localv
[0] ),-1.0f
,1.0f
), 0.04f
);
1872 player
.mdl
.rhead
= rhead
;
1875 static void player_update(void)
1877 if( vg_get_axis("grabl")>0.0f
)
1878 reset_player(0,NULL
);
1886 if( player
.is_dead
)
1888 character_ragdoll_iter( &player
.mdl
);
1889 character_debug_ragdoll( &player
.mdl
);
1893 if( player
.on_board
)
1905 /* Update camera matrices */
1906 m4x3_identity( player
.camera
);
1907 m4x3_rotate_y( player
.camera
, -player
.angles
[0] );
1908 m4x3_rotate_x( player
.camera
, -0.33f
-player
.angles
[1] );
1909 v3_copy( player
.camera_pos
, player
.camera
[3] );
1910 m4x3_invert_affine( player
.camera
, player
.camera_inverse
);
1913 static void draw_player(void)
1916 m4x3_copy( player
.to_world
, player
.mdl
.mroot
);
1918 if( player
.is_dead
)
1919 character_mimic_ragdoll( &player
.mdl
);
1921 character_eval( &player
.mdl
);
1923 character_draw( &player
.mdl
, (player
.is_dead
|player
.in_air
)? 0.0f
: 1.0f
);
1926 #endif /* PLAYER_H */