46c7dd4de0d416836bbe1b05d3b358fe80d71216
16 k_board_radius
= 0.3f
,
17 k_board_length
= 0.45f
,
18 k_board_allowance
= 0.04f
,
19 k_friction_lat
= 8.8f
,
20 k_friction_resistance
= 0.01f
,
21 k_max_push_speed
= 16.0f
,
23 k_push_cycle_rate
= 8.0f
,
24 k_steer_ground
= 2.5f
,
26 k_steer_air_lerp
= 0.3f
,
27 k_pump_force
= 000.0f
,
30 static int freecam
= 0;
31 static int walk_grid_iterations
= 1;
32 static float fc_speed
= 10.0f
;
37 rigidbody rb
, collide_front
, collide_back
, rb_gate_frame
;
39 v3f a
, v_last
, m
, bob
, vl
;
42 float vswitch
, slip
, slip_last
,
45 float iY
; /* Yaw inertia */
46 int in_air
, is_dead
, on_board
;
53 v3f land_target_log
[22];
54 u32 land_target_colours
[22];
60 v3f handl_target
, handr_target
,
66 v3f camera_pos
, smooth_localcam
;
68 m4x3f camera
, camera_inverse
;
74 .collide_front
= { .type
= k_rb_shape_sphere
, .inf
.sphere
.radius
= 0.3f
},
75 .collide_back
= { .type
= k_rb_shape_sphere
, .inf
.sphere
.radius
= 0.3f
}
84 * Free camera movement
87 static void player_mouseview(void)
89 if( gui_want_mouse() )
92 static v2f mouse_last
,
93 view_vel
= { 0.0f
, 0.0f
};
95 if( vg_get_button_down( "primary" ) )
96 v2_copy( vg_mouse
, mouse_last
);
98 else if( vg_get_button( "primary" ) )
101 v2_sub( vg_mouse
, mouse_last
, delta
);
102 v2_copy( vg_mouse
, mouse_last
);
104 v2_muladds( view_vel
, delta
, 0.001f
, view_vel
);
107 v2_muladds( view_vel
,
108 (v2f
){ vg_get_axis("h1"), vg_get_axis("v1") },
110 v2_muls( view_vel
, 0.93f
, view_vel
);
111 v2_add( view_vel
, player
.angles
, player
.angles
);
112 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
115 static void player_freecam(void)
119 float movespeed
= fc_speed
;
120 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
121 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
123 m3x3_mulv( player
.camera
, lookdir
, lookdir
);
124 m3x3_mulv( player
.camera
, sidedir
, sidedir
);
126 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
127 if( vg_get_button( "forward" ) )
128 v3_muladds( move_vel
, lookdir
, ktimestep
* movespeed
, move_vel
);
129 if( vg_get_button( "back" ) )
130 v3_muladds( move_vel
, lookdir
, ktimestep
*-movespeed
, move_vel
);
131 if( vg_get_button( "left" ) )
132 v3_muladds( move_vel
, sidedir
, ktimestep
*-movespeed
, move_vel
);
133 if( vg_get_button( "right" ) )
134 v3_muladds( move_vel
, sidedir
, ktimestep
* movespeed
, move_vel
);
136 v3_muls( move_vel
, 0.7f
, move_vel
);
137 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
141 * Player Physics Implementation
144 static void apply_gravity( v3f vel
, float const timestep
)
146 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
147 v3_muladds( vel
, gravity
, timestep
, vel
);
151 * TODO: The angle bias should become greater when launching from a steeper
152 * angle and skewed towords more 'downwards' angles when launching from
153 * shallower trajectories
155 * it should also be tweaked by the controller left stick being pushed
158 static void player_start_air(void)
165 float pstep
= ktimestep
*10.0f
;
166 float best_velocity_delta
= -9999.9f
;
167 float k_bias
= 0.96f
;
170 v3_cross( player
.rb
.up
, player
.rb
.v
, axis
);
171 v3_normalize( axis
);
172 player
.land_log_count
= 0;
174 m3x3_identity( player
.vr
);
176 for( int m
=-3;m
<=12; m
++ )
178 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
181 v3_copy( player
.rb
.co
, pco
);
182 v3_muls( player
.rb
.v
, k_bias
, pv
);
185 * Try different 'rotations' of the velocity to find the best possible
186 * landing normal. This conserves magnitude at the expense of slightly
187 * unrealistic results
193 q_axis_angle( vr_q
, axis
, vmod
);
196 m3x3_mulv( vr
, pv
, pv
);
197 v3_muladds( pco
, pv
, pstep
, pco
);
199 for( int i
=0; i
<50; i
++ )
201 v3_copy( pco
, pco1
);
202 apply_gravity( pv
, pstep
);
204 m3x3_mulv( vr
, pv
, pv
);
205 v3_muladds( pco
, pv
, pstep
, pco
);
210 v3_sub( pco
, pco1
, vdir
);
211 contact
.dist
= v3_length( vdir
);
212 v3_divs( vdir
, contact
.dist
, vdir
);
214 if( ray_world( pco1
, vdir
, &contact
))
216 float land_delta
= v3_dot( pv
, contact
.normal
);
217 u32 scolour
= (u8
)(vg_minf(-land_delta
* 2.0f
, 255.0f
));
219 /* Bias prediction towords ramps */
220 if( ray_hit_is_ramp( &contact
) )
223 scolour
|= 0x0000a000;
226 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
228 best_velocity_delta
= land_delta
;
230 v3_copy( contact
.pos
, player
.land_target
);
232 m3x3_copy( vr
, player
.vr_pstep
);
233 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
234 q_m3x3( vr_q
, player
.vr
);
237 v3_copy( contact
.pos
,
238 player
.land_target_log
[player
.land_log_count
] );
239 player
.land_target_colours
[player
.land_log_count
] =
240 0xff000000 | scolour
;
242 player
.land_log_count
++;
250 static void draw_cross(v3f pos
,u32 colour
, float scale
)
253 v3_add( (v3f
){ scale
,0.0f
,0.0f
}, pos
, p0
);
254 v3_add( (v3f
){-scale
,0.0f
,0.0f
}, pos
, p1
);
255 vg_line( p0
, p1
, colour
);
256 v3_add( (v3f
){0.0f
, scale
,0.0f
}, pos
, p0
);
257 v3_add( (v3f
){0.0f
,-scale
,0.0f
}, pos
, p1
);
258 vg_line( p0
, p1
, colour
);
259 v3_add( (v3f
){0.0f
,0.0f
, scale
}, pos
, p0
);
260 v3_add( (v3f
){0.0f
,0.0f
,-scale
}, pos
, p1
);
261 vg_line( p0
, p1
, colour
);
264 static void player_physics_control(void)
267 * Computing localized friction forces for controlling the character
268 * Friction across X is significantly more than Z
272 m3x3_mulv( player
.rb
.to_local
, player
.rb
.v
, vel
);
275 if( fabsf(vel
[2]) > 0.01f
)
276 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
278 if( fabsf( slip
) > 1.2f
)
279 slip
= vg_signf( slip
) * 1.2f
;
281 player
.reverse
= -vg_signf(vel
[2]);
283 float substep
= ktimestep
* 0.2f
;
284 float fwd_resistance
= (vg_get_button( "break" )? 5.0f
: 0.02f
) * -substep
;
286 for( int i
=0; i
<5; i
++ )
288 vel
[2] = stable_force( vel
[2], vg_signf( vel
[2] ) * fwd_resistance
);
289 vel
[0] = stable_force( vel
[0],
290 vg_signf( vel
[0] ) * -k_friction_lat
*substep
);
293 static double start_push
= 0.0;
294 if( vg_get_button_down( "push" ) )
295 start_push
= vg_time
;
297 if( !vg_get_button("break") && vg_get_button( "push" ) )
299 float cycle_time
= (vg_time
-start_push
)*k_push_cycle_rate
,
300 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*ktimestep
,
301 current
= v3_length( vel
),
302 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
303 new_vel
-= vg_minf(current
, k_max_push_speed
);
304 vel
[2] -= new_vel
* player
.reverse
;
308 static float previous
= 0.0f
;
309 float delta
= previous
- player
.grab
,
310 pump
= delta
* k_pump_force
*ktimestep
;
311 previous
= player
.grab
;
314 v3_muladds( player
.rb
.co
, player
.rb
.up
, pump
, p1
);
315 vg_line( player
.rb
.co
, p1
, 0xff0000ff );
320 m3x3_mulv( player
.rb
.to_world
, vel
, player
.rb
.v
);
322 float steer
= vg_get_axis( "horizontal" );
323 player
.iY
-= vg_signf(steer
)*powf(steer
,2.0f
) * k_steer_ground
* ktimestep
;
325 v2_lerp( player
.board_xy
, (v2f
){ slip
*0.25f
, 0.0f
},
326 ktimestep
*5.0f
, player
.board_xy
);
329 static void player_physics_control_air(void)
331 m3x3_mulv( player
.vr
, player
.rb
.v
, player
.rb
.v
);
332 draw_cross( player
.land_target
, 0xff0000ff, 0.25f
);
339 float pstep
= ktimestep
*10.0f
;
342 v3_copy( player
.rb
.co
, pco
);
343 v3_copy( player
.rb
.v
, pv
);
345 float time_to_impact
= 0.0f
;
346 float limiter
= 1.0f
;
348 for( int i
=0; i
<50; i
++ )
350 v3_copy( pco
, pco1
);
351 m3x3_mulv( player
.vr_pstep
, pv
, pv
);
352 apply_gravity( pv
, pstep
);
353 v3_muladds( pco
, pv
, pstep
, pco
);
355 //vg_line( pco, pco1, i&0x1?0xff000000:0xffffffff );
360 v3_sub( pco
, pco1
, vdir
);
361 contact
.dist
= v3_length( vdir
);
362 v3_divs( vdir
, contact
.dist
, vdir
);
364 float orig_dist
= contact
.dist
;
365 if( ray_world( pco1
, vdir
, &contact
))
367 float angle
= v3_dot( player
.rb
.up
, contact
.normal
);
369 v3_cross( player
.rb
.up
, contact
.normal
, axis
);
371 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
372 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
373 limiter
= 1.0f
-limiter
;
375 limiter
= 1.0f
-limiter
;
380 q_axis_angle( correction
, axis
, acosf(angle
)*0.05f
*(1.0f
-limiter
) );
381 q_mul( correction
, player
.rb
.q
, player
.rb
.q
);
384 draw_cross( contact
.pos
, 0xffff0000, 0.25f
);
387 time_to_impact
+= pstep
;
390 player
.iY
-= vg_get_axis( "horizontal" ) * k_steer_air
* ktimestep
;
392 float iX
= vg_get_axis( "vertical" ) *
393 player
.reverse
* k_steer_air
* limiter
* ktimestep
;
395 static float siX
= 0.0f
;
396 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
399 q_axis_angle( rotate
, player
.rb
.right
, siX
);
400 q_mul( rotate
, player
.rb
.q
, player
.rb
.q
);
403 v2f target
= {0.0f
,0.0f
};
404 v2_muladds( target
, (v2f
){ vg_get_axis("h1"), vg_get_axis("v1") },
405 player
.grab
, target
);
406 v2_lerp( player
.board_xy
, target
, ktimestep
*3.0f
, player
.board_xy
);
409 static void player_init(void)
411 rb_init( &player
.collide_front
);
412 rb_init( &player
.collide_back
);
415 static void player_physics(void)
418 * Update collision fronts
421 rigidbody
*rbf
= &player
.collide_front
,
422 *rbb
= &player
.collide_back
;
424 m3x3_copy( player
.rb
.to_world
, player
.collide_front
.to_world
);
425 m3x3_copy( player
.rb
.to_world
, player
.collide_back
.to_world
);
427 player
.air_blend
= vg_lerpf( player
.air_blend
, player
.in_air
, 0.1f
);
428 float h
= player
.air_blend
*0.2f
;
430 m4x3_mulv( player
.rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
431 v3_copy( rbf
->co
, rbf
->to_world
[3] );
432 m4x3_mulv( player
.rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
433 v3_copy( rbb
->co
, rbb
->to_world
[3] );
435 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
436 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
438 rb_update_bounds( rbf
);
439 rb_update_bounds( rbb
);
441 rb_debug( rbf
, 0xff00ffff );
442 rb_debug( rbb
, 0xffffff00 );
447 len
+= rb_sphere_vs_scene( rbf
, &world
.rb_geo
, manifold
+len
);
448 len
+= rb_sphere_vs_scene( rbb
, &world
.rb_geo
, manifold
+len
);
450 rb_presolve_contacts( manifold
, len
);
451 v3f surface_avg
= {0.0f
, 0.0f
, 0.0f
};
459 for( int i
=0; i
<len
; i
++ )
461 v3_add( manifold
[i
].n
, surface_avg
, surface_avg
);
464 if( manifold
[i
].element_id
<= world
.sm_geo_std_oob
.vertex_count
)
467 character_ragdoll_copypose( &player
.mdl
, player
.rb
.v
);
473 v3_normalize( surface_avg
);
475 if( v3_dot( player
.rb
.v
, surface_avg
) > 0.5f
)
483 for( int j
=0; j
<5; j
++ )
485 for( int i
=0; i
<len
; i
++ )
487 struct contact
*ct
= &manifold
[i
];
490 v3_sub( ct
->co
, player
.rb
.co
, delta
);
491 v3_cross( player
.rb
.w
, delta
, dv
);
492 v3_add( player
.rb
.v
, dv
, dv
);
494 float vn
= -v3_dot( dv
, ct
->n
);
497 float temp
= ct
->norm_impulse
;
498 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
499 vn
= ct
->norm_impulse
- temp
;
502 v3_muls( ct
->n
, vn
, impulse
);
504 if( fabsf(v3_dot( impulse
, player
.rb
.forward
)) > 10.0f
||
505 fabsf(v3_dot( impulse
, player
.rb
.up
)) > 50.0f
)
508 character_ragdoll_copypose( &player
.mdl
, player
.rb
.v
);
512 v3_add( impulse
, player
.rb
.v
, player
.rb
.v
);
513 v3_cross( delta
, impulse
, impulse
);
516 * W Impulses are limited to the Y and X axises, we don't really want
517 * roll angular velocities being included.
519 * Can also tweak the resistance of each axis here by scaling the wx,wy
523 float wy
= v3_dot( player
.rb
.up
, impulse
),
524 wx
= v3_dot( player
.rb
.right
, impulse
)*1.5f
;
526 v3_muladds( player
.rb
.w
, player
.rb
.up
, wy
, player
.rb
.w
);
527 v3_muladds( player
.rb
.w
, player
.rb
.right
, wx
, player
.rb
.w
);
531 float grabt
= vg_get_axis( "grabr" )*0.5f
+0.5f
;
532 player
.grab
= vg_lerpf( player
.grab
, grabt
, 0.14f
);
537 float angle
= v3_dot( player
.rb
.up
, surface_avg
);
538 v3_cross( player
.rb
.up
, surface_avg
, axis
);
540 //float cz = v3_dot( player.rb.forward, axis );
541 //v3_muls( player.rb.forward, cz, axis );
546 q_axis_angle( correction
, axis
, acosf(angle
)*0.3f
);
547 q_mul( correction
, player
.rb
.q
, player
.rb
.q
);
550 v3_muladds( player
.rb
.v
, player
.rb
.up
,
551 -k_downforce
*ktimestep
, player
.rb
.v
);
552 player_physics_control();
556 player_physics_control_air();
560 static void player_do_motion(void)
562 float horizontal
= vg_get_axis("horizontal"),
563 vertical
= vg_get_axis("vertical");
567 /* Integrate velocity */
569 v3_copy( player
.rb
.co
, prevco
);
571 apply_gravity( player
.rb
.v
, ktimestep
);
572 v3_muladds( player
.rb
.co
, player
.rb
.v
, ktimestep
, player
.rb
.co
);
574 /* Real angular velocity integration */
575 v3_lerp( player
.rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, player
.rb
.w
);
576 if( v3_length2( player
.rb
.w
) > 0.0f
)
580 v3_copy( player
.rb
.w
, axis
);
582 float mag
= v3_length( axis
);
583 v3_divs( axis
, mag
, axis
);
584 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
585 q_mul( rotation
, player
.rb
.q
, player
.rb
.q
);
588 /* Faux angular velocity */
591 static float siY
= 0.0f
;
592 float lerpq
= player
.in_air
? 0.04f
: 0.3f
;
593 siY
= vg_lerpf( siY
, player
.iY
, lerpq
);
595 q_axis_angle( rotate
, player
.rb
.up
, siY
);
596 q_mul( rotate
, player
.rb
.q
, player
.rb
.q
);
600 * Gate intersection, by tracing a line over the gate planes
602 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
604 struct route_gate
*rg
= &world
.routes
.gates
[i
];
605 teleport_gate
*gate
= &rg
->gate
;
607 if( gate_intersect( gate
, player
.rb
.co
, prevco
) )
609 m4x3_mulv( gate
->transport
, player
.rb
.co
, player
.rb
.co
);
610 m3x3_mulv( gate
->transport
, player
.rb
.v
, player
.rb
.v
);
611 m3x3_mulv( gate
->transport
, player
.vl
, player
.vl
);
612 m3x3_mulv( gate
->transport
, player
.v_last
, player
.v_last
);
613 m3x3_mulv( gate
->transport
, player
.m
, player
.m
);
614 m3x3_mulv( gate
->transport
, player
.bob
, player
.bob
);
616 v4f transport_rotation
;
617 m3x3_q( gate
->transport
, transport_rotation
);
618 q_mul( transport_rotation
, player
.rb
.q
, player
.rb
.q
);
620 world_routes_activate_gate( i
);
621 player
.rb_gate_frame
= player
.rb
;
626 rb_update_transform( &player
.rb
);
630 * Walkgrid implementation,
631 * loosely based of cmuratoris youtube video 'Killing the Walkmonster'
634 #define WALKGRID_SIZE 16
641 k_sample_type_air
, /* Nothing was hit. */
642 k_sample_type_invalid
, /* The point is invalid, but there is a sample
643 underneath that can be used */
644 k_sample_type_valid
, /* This point is good */
653 k_traverse_none
= 0x00,
659 samples
[WALKGRID_SIZE
][WALKGRID_SIZE
];
663 float move
; /* Current amount of movement we have left to apply */
664 v2f dir
; /* The movement delta */
665 v2i cell_id
;/* Current cell */
666 v2f pos
; /* Local position (in cell) */
670 static int player_walkgrid_tri_walkable( u32 tri
[3] )
672 return tri
[0] > world
.sm_geo_std_oob
.vertex_count
;
676 * Get a sample at this pole location, will return 1 if the sample is valid,
677 * and pos will be updated to be the intersection location.
679 static void player_walkgrid_samplepole( struct grid_sample
*s
)
681 boxf region
= {{ s
->pos
[0] -0.01f
, s
->pos
[1] - 4.0f
, s
->pos
[2] -0.01f
},
682 { s
->pos
[0] +0.01f
, s
->pos
[1] + 4.0f
, s
->pos
[2] +0.01f
}};
686 int len
= bh_select( &world
.geo
.bhtris
, region
, geo
, 256 );
688 const float k_minworld_y
= -2000.0f
;
690 float walk_height
= k_minworld_y
,
691 block_height
= k_minworld_y
;
693 s
->type
= k_sample_type_air
;
695 for( int i
=0; i
<len
; i
++ )
697 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
699 for( int j
=0; j
<3; j
++ )
700 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
702 v3f vdown
= {0.0f
,-1.0f
,0.0f
};
704 v3_copy( s
->pos
, sample_from
);
705 sample_from
[1] = region
[1][1];
708 if( ray_tri( tri
, sample_from
, vdown
, &dist
))
711 v3_muladds( sample_from
, vdown
, dist
, p0
);
713 if( player_walkgrid_tri_walkable(ptri
) )
715 if( p0
[1] > walk_height
)
722 if( p0
[1] > block_height
)
723 block_height
= p0
[1];
728 s
->pos
[1] = walk_height
;
730 if( walk_height
> k_minworld_y
)
731 if( block_height
> walk_height
)
732 s
->type
= k_sample_type_invalid
;
734 s
->type
= k_sample_type_valid
;
736 s
->type
= k_sample_type_air
;
739 float const k_gridscale
= 0.5f
;
747 static void player_walkgrid_clip_blocker( struct grid_sample
*sa
,
748 struct grid_sample
*sb
,
749 struct grid_sample
*st
,
753 int valid_a
= sa
->type
== k_sample_type_valid
,
754 valid_b
= sb
->type
== k_sample_type_valid
;
755 struct grid_sample
*target
= valid_a
? sa
: sb
,
756 *other
= valid_a
? sb
: sa
;
757 v3_copy( target
->pos
, pos
);
758 v3_sub( other
->pos
, target
->pos
, clipdir
);
761 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*2.1f
, cell_region
[0]);
762 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*2.1f
, cell_region
[1]);
766 int len
= bh_select( &world
.geo
.bhtris
, cell_region
, geo
, 256 );
768 float start_time
= v3_length( clipdir
),
769 min_time
= start_time
;
770 v3_normalize( clipdir
);
771 v3_muls( clipdir
, 0.0001f
, st
->clip
[dir
] );
773 for( int i
=0; i
<len
; i
++ )
775 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
776 for( int j
=0; j
<3; j
++ )
777 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
779 if( player_walkgrid_tri_walkable(ptri
) )
783 if(ray_tri( tri
, pos
, clipdir
, &dist
))
785 if( dist
> 0.0f
&& dist
< min_time
)
788 sb
->type
= k_sample_type_air
;
793 if( !(min_time
< start_time
) )
794 min_time
= 0.5f
* k_gridscale
;
796 min_time
= vg_clampf( min_time
/k_gridscale
, 0.01f
, 0.99f
);
798 v3_muls( clipdir
, min_time
, st
->clip
[dir
] );
801 v3_muladds( target
->pos
, st
->clip
[dir
], k_gridscale
, p0
);
804 static void player_walkgrid_clip_edge( struct grid_sample
*sa
,
805 struct grid_sample
*sb
,
806 struct grid_sample
*st
, /* data store */
809 v3f clipdir
= { 0.0f
, 0.0f
, 0.0f
}, pos
;
810 int valid_a
= sa
->type
== k_sample_type_valid
,
811 valid_b
= sb
->type
== k_sample_type_valid
;
813 struct grid_sample
*target
= valid_a
? sa
: sb
,
814 *other
= valid_a
? sb
: sa
;
816 v3_sub( other
->pos
, target
->pos
, clipdir
);
819 v3_copy( target
->pos
, pos
);
822 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*1.1f
, cell_region
[0]);
823 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*1.1f
, cell_region
[1]);
826 int len
= bh_select( &world
.geo
.bhtris
, cell_region
, geo
, 256 );
828 float max_dist
= 0.0f
;
831 v3_cross( clipdir
,(v3f
){0.0f
,1.0f
,0.0f
},perp
);
832 v3_muls( clipdir
, 0.001f
, st
->clip
[dir
] );
834 for( int i
=0; i
<len
; i
++ )
836 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
837 for( int j
=0; j
<3; j
++ )
838 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
840 if( !player_walkgrid_tri_walkable(ptri
) )
843 for( int k
=0; k
<3; k
++ )
849 v3_sub( tri
[ia
], pos
, v0
);
850 v3_sub( tri
[ib
], pos
, v1
);
852 if( (clipdir
[2]*v0
[0] - clipdir
[0]*v0
[2]) *
853 (clipdir
[2]*v1
[0] - clipdir
[0]*v1
[2]) < 0.0f
)
855 float da
= v3_dot(v0
,perp
),
856 db
= v3_dot(v1
,perp
),
861 v3_muls( v1
, qa
, p0
);
862 v3_muladds( p0
, v0
, 1.0f
-qa
, p0
);
864 float h
= v3_dot(p0
,clipdir
)/v3_dot(clipdir
,clipdir
);
866 if( h
>= max_dist
&& h
<= 1.0f
)
869 float l
= 1.0f
/v3_length(clipdir
);
870 v3_muls( p0
, l
, st
->clip
[dir
] );
877 static const struct conf
884 * o: the 'other' point to do a A/B test with
885 * if its -1, all AB is done.
895 k_walkgrid_configs
[16] = {
897 {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
898 {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
899 {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
901 {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
902 {{{ 3,3, 3,0, 1,0, -1,-1 },
903 { 1,1, 0,1, 1,0, -1,-1 }}, 2},
904 {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
905 {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
907 {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
908 {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
909 {{{ 2,2, 1,3, 0,1, -1,-1 },
910 { 0,0, 0,0, 0,1, -1,-1 }}, 2},
911 {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
913 {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
914 {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
915 {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
920 * Get a buffer of edges from cell location
922 static const struct conf
*player_walkgrid_conf( struct walkgrid
*wg
,
924 struct grid_sample
*corners
[4] )
926 corners
[0] = &wg
->samples
[cell
[1] ][cell
[0] ];
927 corners
[1] = &wg
->samples
[cell
[1]+1][cell
[0] ];
928 corners
[2] = &wg
->samples
[cell
[1]+1][cell
[0]+1];
929 corners
[3] = &wg
->samples
[cell
[1] ][cell
[0]+1];
931 u32 vd0
= corners
[0]->type
== k_sample_type_valid
,
932 vd1
= corners
[1]->type
== k_sample_type_valid
,
933 vd2
= corners
[2]->type
== k_sample_type_valid
,
934 vd3
= corners
[3]->type
== k_sample_type_valid
,
935 config
= (vd0
<<3) | (vd1
<<2) | (vd2
<<1) | vd3
;
937 return &k_walkgrid_configs
[ config
];
940 static void player_walkgrid_floor(v3f pos
)
942 v3_muls( pos
, 1.0f
/k_gridscale
, pos
);
943 v3_floor( pos
, pos
);
944 v3_muls( pos
, k_gridscale
, pos
);
948 * Computes the barycentric coordinate of location on a triangle (vertical),
949 * then sets the Y position to the interpolation of the three points
951 static void player_walkgrid_stand_tri( v3f a
, v3f b
, v3f c
, v3f pos
)
956 v3_sub( pos
, a
, v2
);
958 float d
= v0
[0]*v1
[2] - v1
[0]*v0
[2],
959 v
= (v2
[0]*v1
[2] - v1
[0]*v2
[2]) / d
,
960 w
= (v0
[0]*v2
[2] - v2
[0]*v0
[2]) / d
,
963 vg_line( pos
, a
, 0xffff0000 );
964 vg_line( pos
, b
, 0xff00ff00 );
965 vg_line( pos
, c
, 0xff0000ff );
966 pos
[1] = u
*a
[1] + v
*b
[1] + w
*c
[1];
970 * Get the minimum time value of pos+dir until a cell edge
972 * t[0] -> t[3] are the individual time values
973 * t[5] & t[6] are the maximum axis values
974 * t[6] is the minimum value
977 static void player_walkgrid_min_cell( float t
[7], v2f pos
, v2f dir
)
979 v2f frac
= { 1.0f
/dir
[0], 1.0f
/dir
[1] };
986 if( fabsf(dir
[0]) > 0.0001f
)
988 t
[0] = (0.0f
-pos
[0]) * frac
[0];
989 t
[1] = (1.0f
-pos
[0]) * frac
[0];
991 if( fabsf(dir
[1]) > 0.0001f
)
993 t
[2] = (0.0f
-pos
[1]) * frac
[1];
994 t
[3] = (1.0f
-pos
[1]) * frac
[1];
997 t
[4] = vg_maxf(t
[0],t
[1]);
998 t
[5] = vg_maxf(t
[2],t
[3]);
999 t
[6] = vg_minf(t
[4],t
[5]);
1002 static void player_walkgrid_iter(struct walkgrid
*wg
, int iter
)
1006 * For each walkgrid iteration we are stepping through cells and determining
1007 * the intersections with the grid, and any edges that are present
1010 u32 icolours
[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
1012 v3f pa
, pb
, pc
, pd
, pl0
, pl1
;
1013 pa
[0] = wg
->region
[0][0] + (float)wg
->cell_id
[0] *k_gridscale
;
1014 pa
[1] = (wg
->region
[0][1] + wg
->region
[1][1]) * 0.5f
+ k_gridscale
;
1015 pa
[2] = wg
->region
[0][2] + (float)wg
->cell_id
[1] *k_gridscale
;
1019 pb
[2] = pa
[2] + k_gridscale
;
1020 pc
[0] = pa
[0] + k_gridscale
;
1022 pc
[2] = pa
[2] + k_gridscale
;
1023 pd
[0] = pa
[0] + k_gridscale
;
1026 /* if you want to draw the current cell */
1027 vg_line( pa
, pb
, 0xff00ffff );
1028 vg_line( pb
, pc
, 0xff00ffff );
1029 vg_line( pc
, pd
, 0xff00ffff );
1030 vg_line( pd
, pa
, 0xff00ffff );
1032 pl0
[0] = pa
[0] + wg
->pos
[0]*k_gridscale
;
1034 pl0
[2] = pa
[2] + wg
->pos
[1]*k_gridscale
;
1037 * If there are edges present, we need to create a 'substep' event, where
1038 * we find the intersection point, find the fully resolved position,
1039 * then the new pos dir is the intersection->resolution
1041 * the resolution is applied in non-discretized space in order to create a
1042 * suitable vector for finding outflow, we want it to leave the cell so it
1043 * can be used by the quad
1047 v2_copy( wg
->pos
, pos
);
1048 v2_muls( wg
->dir
, wg
->move
, dir
);
1050 struct grid_sample
*corners
[4];
1051 v2f corners2d
[4] = {{0.0f
,0.0f
},{0.0f
,1.0f
},{1.0f
,1.0f
},{1.0f
,0.0f
}};
1052 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
1055 player_walkgrid_min_cell( t
, pos
, dir
);
1057 for( int i
=0; i
<conf
->edge_count
; i
++ )
1059 const struct confedge
*edge
= &conf
->edges
[i
];
1061 v2f e0
, e1
, n
, r
, target
, res
, tangent
;
1062 e0
[0] = corners2d
[edge
->i0
][0] + corners
[edge
->d0
]->clip
[edge
->a0
][0];
1063 e0
[1] = corners2d
[edge
->i0
][1] + corners
[edge
->d0
]->clip
[edge
->a0
][2];
1064 e1
[0] = corners2d
[edge
->i1
][0] + corners
[edge
->d1
]->clip
[edge
->a1
][0];
1065 e1
[1] = corners2d
[edge
->i1
][1] + corners
[edge
->d1
]->clip
[edge
->a1
][2];
1067 v3f pe0
= { pa
[0] + e0
[0]*k_gridscale
,
1069 pa
[2] + e0
[1]*k_gridscale
};
1070 v3f pe1
= { pa
[0] + e1
[0]*k_gridscale
,
1072 pa
[2] + e1
[1]*k_gridscale
};
1074 v2_sub( e1
, e0
, tangent
);
1080 * If we find ourselfs already penetrating the edge, move back out a
1083 v2_sub( e0
, pos
, r
);
1084 float p1
= v2_dot(r
,n
);
1088 v2_muladds( pos
, n
, p1
+0.0001f
, pos
);
1089 v2_copy( pos
, wg
->pos
);
1090 v3f p_new
= { pa
[0] + pos
[0]*k_gridscale
,
1092 pa
[2] + pos
[1]*k_gridscale
};
1093 v3_copy( p_new
, pl0
);
1096 v2_add( pos
, dir
, target
);
1099 v2_sub( e0
, pos
, v1
);
1100 v2_sub( target
, pos
, v2
);
1104 v2_sub( e0
, target
, r
);
1105 float p
= v2_dot(r
,n
),
1106 t1
= v2_dot(v1
,v3
)/v2_dot(v2
,v3
);
1108 if( t1
< t
[6] && t1
> 0.0f
&& -p
< 0.001f
)
1110 v2_muladds( target
, n
, p
+0.0001f
, res
);
1113 v2_muladds( pos
, dir
, t1
, intersect
);
1114 v2_copy( intersect
, pos
);
1115 v2_sub( res
, intersect
, dir
);
1117 v3f p_res
= { pa
[0] + res
[0]*k_gridscale
,
1119 pa
[2] + res
[1]*k_gridscale
};
1120 v3f p_int
= { pa
[0] + intersect
[0]*k_gridscale
,
1122 pa
[2] + intersect
[1]*k_gridscale
};
1124 vg_line( pl0
, p_int
, icolours
[iter
%3] );
1125 v3_copy( p_int
, pl0
);
1126 v2_copy( pos
, wg
->pos
);
1128 player_walkgrid_min_cell( t
, pos
, dir
);
1133 * Compute intersection with grid cell moving outwards
1135 t
[6] = vg_minf( t
[6], 1.0f
);
1137 pl1
[0] = pl0
[0] + dir
[0]*k_gridscale
*t
[6];
1139 pl1
[2] = pl0
[2] + dir
[1]*k_gridscale
*t
[6];
1140 vg_line( pl0
, pl1
, icolours
[iter
%3] );
1145 * To figure out what t value created the clip so we know which edge
1151 wg
->pos
[1] = pos
[1] + dir
[1]*t
[6];
1153 if( t
[0] > t
[1] ) /* left edge */
1155 wg
->pos
[0] = 0.9999f
;
1158 if( wg
->cell_id
[0] == 0 )
1161 else /* Right edge */
1163 wg
->pos
[0] = 0.0001f
;
1166 if( wg
->cell_id
[0] == WALKGRID_SIZE
-2 )
1172 wg
->pos
[0] = pos
[0] + dir
[0]*t
[6];
1174 if( t
[2] > t
[3] ) /* bottom edge */
1176 wg
->pos
[1] = 0.9999f
;
1179 if( wg
->cell_id
[1] == 0 )
1184 wg
->pos
[1] = 0.0001f
;
1187 if( wg
->cell_id
[1] == WALKGRID_SIZE
-2 )
1196 v2_muladds( wg
->pos
, dir
, wg
->move
, wg
->pos
);
1201 static void player_walkgrid_stand_cell(struct walkgrid
*wg
)
1204 * NOTE: as opposed to the other function which is done in discretized space
1205 * this use a combination of both.
1209 world
[0] = wg
->region
[0][0]+((float)wg
->cell_id
[0]+wg
->pos
[0])*k_gridscale
;
1210 world
[1] = player
.rb
.co
[1];
1211 world
[2] = wg
->region
[0][2]+((float)wg
->cell_id
[1]+wg
->pos
[1])*k_gridscale
;
1213 struct grid_sample
*corners
[4];
1214 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
1216 if( conf
!= k_walkgrid_configs
)
1218 if( conf
->edge_count
== 0 )
1222 /* Split the basic quad along the shortest diagonal */
1223 if( fabsf(corners
[2]->pos
[1] - corners
[0]->pos
[1]) <
1224 fabsf(corners
[3]->pos
[1] - corners
[1]->pos
[1]) )
1226 vg_line( corners
[2]->pos
, corners
[0]->pos
, 0xffaaaaaa );
1228 if( wg
->pos
[0] > wg
->pos
[1] )
1229 player_walkgrid_stand_tri( corners
[0]->pos
,
1231 corners
[2]->pos
, world
);
1233 player_walkgrid_stand_tri( corners
[0]->pos
,
1235 corners
[1]->pos
, world
);
1239 vg_line( corners
[3]->pos
, corners
[1]->pos
, 0xffaaaaaa );
1241 if( wg
->pos
[0] < 1.0f
-wg
->pos
[1] )
1242 player_walkgrid_stand_tri( corners
[0]->pos
,
1244 corners
[1]->pos
, world
);
1246 player_walkgrid_stand_tri( corners
[3]->pos
,
1248 corners
[1]->pos
, world
);
1253 for( int i
=0; i
<conf
->edge_count
; i
++ )
1255 const struct confedge
*edge
= &conf
->edges
[i
];
1258 v3_muladds( corners
[edge
->i0
]->pos
,
1259 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
1260 v3_muladds( corners
[edge
->i1
]->pos
,
1261 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
1264 * Find penetration distance between player position and the edge
1267 v2f normal
= { -(p1
[2]-p0
[2]), p1
[0]-p0
[0] },
1268 rel
= { world
[0]-p0
[0], world
[2]-p0
[2] };
1270 if( edge
->o0
== -1 )
1272 /* No subregions (default case), just use triangle created by
1274 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
1281 * Test if we are in the first region, which is
1282 * edge.i0, edge.e0, edge.o0,
1285 v3_sub( p0
, corners
[edge
->o0
]->pos
, ref
);
1286 v3_sub( world
, corners
[edge
->o0
]->pos
, v0
);
1288 vg_line( corners
[edge
->o0
]->pos
, p0
, 0xffffff00 );
1289 vg_line( corners
[edge
->o0
]->pos
, world
, 0xff000000 );
1291 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
1293 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
1295 corners
[edge
->o0
]->pos
, world
);
1299 if( edge
->o1
== -1 )
1302 * No other edges mean we just need to use the opposite
1304 * e0, e1, o0 (in our case, also i1)
1306 player_walkgrid_stand_tri( p0
,
1308 corners
[edge
->o0
]->pos
, world
);
1313 * Note: this v0 calculation can be ommited with the
1316 * the last two triangles we have are:
1321 v3_sub( p1
, corners
[edge
->o1
]->pos
, ref
);
1322 v3_sub( world
, corners
[edge
->o1
]->pos
, v0
);
1323 vg_line( corners
[edge
->o1
]->pos
, p1
, 0xff00ffff );
1325 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
1327 player_walkgrid_stand_tri( p0
,
1329 corners
[edge
->o1
]->pos
,
1334 player_walkgrid_stand_tri( p1
,
1335 corners
[edge
->i1
]->pos
,
1336 corners
[edge
->o1
]->pos
,
1346 v3_copy( world
, player
.rb
.co
);
1349 static void player_walkgrid_getsurface(void)
1351 float const k_stepheight
= 0.5f
;
1352 float const k_miny
= 0.6f
;
1353 float const k_height
= 1.78f
;
1354 float const k_region_size
= (float)WALKGRID_SIZE
/2.0f
* k_gridscale
;
1356 static struct walkgrid wg
;
1359 v3_copy( player
.rb
.co
, cell
);
1360 player_walkgrid_floor( cell
);
1362 v3_muladds( cell
, (v3f
){-1.0f
,-1.0f
,-1.0f
}, k_region_size
, wg
.region
[0] );
1363 v3_muladds( cell
, (v3f
){ 1.0f
, 1.0f
, 1.0f
}, k_region_size
, wg
.region
[1] );
1367 * Create player input vector
1369 v3f delta
= {0.0f
,0.0f
,0.0f
};
1370 v3f fwd
= { -sinf(-player
.angles
[0]), 0.0f
, -cosf(-player
.angles
[0]) },
1371 side
= { -fwd
[2], 0.0f
, fwd
[0] };
1374 if( !vg_console_enabled() )
1376 if( glfwGetKey( vg_window
, GLFW_KEY_W
) )
1377 v3_muladds( delta
, fwd
, ktimestep
*k_walkspeed
, delta
);
1378 if( glfwGetKey( vg_window
, GLFW_KEY_S
) )
1379 v3_muladds( delta
, fwd
, -ktimestep
*k_walkspeed
, delta
);
1381 if( glfwGetKey( vg_window
, GLFW_KEY_A
) )
1382 v3_muladds( delta
, side
, -ktimestep
*k_walkspeed
, delta
);
1383 if( glfwGetKey( vg_window
, GLFW_KEY_D
) )
1384 v3_muladds( delta
, side
, ktimestep
*k_walkspeed
, delta
);
1386 v3_muladds( delta
, fwd
,
1387 vg_get_axis("vertical")*-ktimestep
*k_walkspeed
, delta
);
1388 v3_muladds( delta
, side
,
1389 vg_get_axis("horizontal")*ktimestep
*k_walkspeed
, delta
);
1393 * Create our move in grid space
1395 wg
.dir
[0] = delta
[0] * (1.0f
/k_gridscale
);
1396 wg
.dir
[1] = delta
[2] * (1.0f
/k_gridscale
);
1401 (player
.rb
.co
[0] - wg
.region
[0][0]) * (1.0f
/k_gridscale
),
1402 (player
.rb
.co
[2] - wg
.region
[0][2]) * (1.0f
/k_gridscale
)
1404 v2f region_cell_pos
;
1405 v2_floor( region_pos
, region_cell_pos
);
1406 v2_sub( region_pos
, region_cell_pos
, wg
.pos
);
1408 wg
.cell_id
[0] = region_cell_pos
[0];
1409 wg
.cell_id
[1] = region_cell_pos
[1];
1411 for(int y
=0; y
<WALKGRID_SIZE
; y
++ )
1413 for(int x
=0; x
<WALKGRID_SIZE
; x
++ )
1415 struct grid_sample
*s
= &wg
.samples
[y
][x
];
1416 v3_muladds( wg
.region
[0], (v3f
){ x
, 0, y
}, k_gridscale
, s
->pos
);
1417 s
->state
= k_traverse_none
;
1418 s
->type
= k_sample_type_air
;
1419 v3_zero( s
->clip
[0] );
1420 v3_zero( s
->clip
[1] );
1424 v2i border
[WALKGRID_SIZE
*WALKGRID_SIZE
];
1425 v2i
*cborder
= border
;
1426 u32 border_length
= 1;
1428 struct grid_sample
*base
= NULL
;
1430 v2i starters
[] = {{0,0},{1,1},{0,1},{1,0}};
1432 for( int i
=0;i
<4;i
++ )
1435 v2i_add( wg
.cell_id
, starters
[i
], test
);
1436 v2i_copy( test
, border
[0] );
1437 base
= &wg
.samples
[test
[1]][test
[0]];
1439 base
->pos
[1] = cell
[1];
1440 player_walkgrid_samplepole( base
);
1442 if( base
->type
== k_sample_type_valid
)
1445 base
->type
= k_sample_type_air
;
1448 vg_line_pt3( base
->pos
, 0.1f
, 0xffffffff );
1452 while( border_length
)
1454 v2i directions
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1456 v2i
*old_border
= cborder
;
1457 int len
= border_length
;
1460 cborder
= old_border
+len
;
1462 for( int i
=0; i
<len
; i
++ )
1465 v2i_copy( old_border
[i
], co
);
1466 struct grid_sample
*sa
= &wg
.samples
[co
[1]][co
[0]];
1468 for( int j
=0; j
<4; j
++ )
1471 v2i_add( co
, directions
[j
], newp
);
1473 if( newp
[0] < 0 || newp
[1] < 0 ||
1474 newp
[0] == WALKGRID_SIZE
|| newp
[1] == WALKGRID_SIZE
)
1477 struct grid_sample
*sb
= &wg
.samples
[newp
[1]][newp
[0]];
1478 enum traverse_state thismove
= j
%2==0? 1: 2;
1480 if( (sb
->state
& thismove
) == 0x00 ||
1481 sb
->type
== k_sample_type_air
)
1483 sb
->pos
[1] = sa
->pos
[1];
1485 player_walkgrid_samplepole( sb
);
1487 if( sb
->type
!= k_sample_type_air
)
1490 * Need to do a blocker pass
1493 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
1494 player_walkgrid_clip_blocker( sa
, sb
, store
, j
%2 );
1497 if( sb
->type
!= k_sample_type_air
)
1499 vg_line( sa
->pos
, sb
->pos
, 0xffffffff );
1501 if( sb
->state
== k_traverse_none
)
1502 v2i_copy( newp
, cborder
[ border_length
++ ] );
1507 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
1508 vg_line( sa
->pos
, p1
, 0xffffffff );
1514 * A clipping pass is now done on the edge of the walkable
1518 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
1519 player_walkgrid_clip_edge( sa
, sb
, store
, j
%2 );
1522 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
1523 vg_line( sa
->pos
, p1
, 0xffffffff );
1526 sb
->state
|= thismove
;
1530 sa
->state
= k_traverse_h
|k_traverse_v
;
1534 if( iter
== walk_grid_iterations
)
1538 /* Draw connections */
1539 struct grid_sample
*corners
[4];
1540 for( int x
=0; x
<WALKGRID_SIZE
-1; x
++ )
1542 for( int z
=0; z
<WALKGRID_SIZE
-1; z
++ )
1544 const struct conf
*conf
=
1545 player_walkgrid_conf( &wg
, (v2i
){x
,z
}, corners
);
1547 for( int i
=0; i
<conf
->edge_count
; i
++ )
1549 const struct confedge
*edge
= &conf
->edges
[i
];
1552 v3_muladds( corners
[edge
->i0
]->pos
,
1553 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
1554 v3_muladds( corners
[edge
->i1
]->pos
,
1555 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
1557 vg_line( p0
, p1
, 0xff0000ff );
1563 * Commit player movement into the grid
1566 if( v3_length2(delta
) <= 0.00001f
)
1570 for(; i
<8 && wg
.move
> 0.001f
; i
++ )
1571 player_walkgrid_iter( &wg
, i
);
1573 player_walkgrid_stand_cell( &wg
);
1576 static void player_walkgrid(void)
1578 player_walkgrid_getsurface();
1580 m4x3_mulv( player
.rb
.to_world
, (v3f
){0.0f
,1.8f
,0.0f
}, player
.camera_pos
);
1582 rb_update_transform( &player
.rb
);
1589 static void player_animate(void)
1591 /* Camera position */
1592 v3_sub( player
.rb
.v
, player
.v_last
, player
.a
);
1593 v3_copy( player
.rb
.v
, player
.v_last
);
1595 v3_add( player
.m
, player
.a
, player
.m
);
1596 v3_lerp( player
.m
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.1f
, player
.m
);
1598 player
.m
[0] = vg_clampf( player
.m
[0], -2.0f
, 2.0f
);
1599 player
.m
[1] = vg_clampf( player
.m
[1], -2.0f
, 2.0f
);
1600 player
.m
[2] = vg_clampf( player
.m
[2], -2.0f
, 2.0f
);
1601 v3_lerp( player
.bob
, player
.m
, 0.2f
, player
.bob
);
1604 float lslip
= fabsf(player
.slip
);
1606 float kheight
= 2.0f
,
1611 m3x3_mulv( player
.rb
.to_local
, player
.bob
, offset
);
1613 static float speed_wobble
= 0.0f
, speed_wobble_2
= 0.0f
;
1615 float kickspeed
= vg_clampf(v3_length(player
.rb
.v
)*(1.0f
/40.0f
), 0.0f
, 1.0f
);
1616 float kicks
= (vg_randf()-0.5f
)*2.0f
*kickspeed
;
1617 float sign
= vg_signf( kicks
);
1618 speed_wobble
= vg_lerpf( speed_wobble
, kicks
*kicks
*sign
, 0.1f
);
1619 speed_wobble_2
= vg_lerpf( speed_wobble_2
, speed_wobble
, 0.04f
);
1622 offset
[0] += speed_wobble_2
*3.0f
;
1627 offset
[0] = vg_clampf( offset
[0], -0.8f
, 0.8f
);
1628 offset
[1] = vg_clampf( offset
[1], -0.5f
, 0.0f
);
1634 float angle
= v3_dot( player
.rb
.up
, (v3f
){0.0f
,1.0f
,0.0f
} );
1636 v3_cross( player
.rb
.up
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
);
1639 if( angle
< 0.99f
&& 0 )
1641 m3x3_mulv( player
.rb
.to_local
, axis
, axis
);
1642 q_axis_angle( correction
, axis
, acosf(angle
) );
1646 q_identity( correction
);
1650 * Animation blending
1651 * ===========================================
1655 static float fslide
= 0.0f
;
1656 static float fdirz
= 0.0f
;
1657 static float fdirx
= 0.0f
;
1658 static float fstand
= 0.0f
;
1659 static float ffly
= 0.0f
;
1661 float speed
= v3_length( player
.rb
.v
);
1663 fstand
= vg_lerpf(fstand
, 1.0f
-vg_clampf(speed
*0.03f
,0.0f
,1.0f
),0.1f
);
1664 fslide
= vg_lerpf(fslide
, vg_clampf(lslip
,0.0f
,1.0f
), 0.04f
);
1665 fdirz
= vg_lerpf(fdirz
, player
.reverse
> 0.0f
? 1.0f
: 0.0f
, 0.04f
);
1666 fdirx
= vg_lerpf(fdirx
, player
.slip
< 0.0f
? 1.0f
: 0.0f
, 0.01f
);
1667 ffly
= vg_lerpf(ffly
, player
.in_air
? 1.0f
: 0.0f
, 0.04f
);
1669 character_pose_reset( &player
.mdl
);
1672 float fstand1
= 1.0f
-(1.0f
-fstand
)*0.0f
;
1674 float amt_air
= ffly
*ffly
,
1675 amt_ground
= 1.0f
-amt_air
,
1676 amt_std
= (1.0f
-fslide
) * amt_ground
,
1677 amt_stand
= amt_std
* fstand1
,
1678 amt_aero
= amt_std
* (1.0f
-fstand1
),
1679 amt_slide
= amt_ground
* fslide
;
1681 character_final_pose( &player
.mdl
, offset
, &pose_stand
, amt_stand
*fdirz
);
1682 character_final_pose( &player
.mdl
, offset
,
1683 &pose_stand_reverse
, amt_stand
* (1.0f
-fdirz
) );
1685 character_final_pose( &player
.mdl
, offset
, &pose_aero
, amt_aero
*fdirz
);
1686 character_final_pose( &player
.mdl
, offset
,
1687 &pose_aero_reverse
, amt_aero
* (1.0f
-fdirz
) );
1689 character_final_pose( &player
.mdl
, offset
, &pose_slide
, amt_slide
*fdirx
);
1690 character_final_pose( &player
.mdl
, offset
,
1691 &pose_slide1
, amt_slide
*(1.0f
-fdirx
) );
1693 character_final_pose( &player
.mdl
, (v4f
){0.0f
,0.0f
,0.0f
,1.0f
},
1694 &pose_fly
, amt_air
);
1698 * ==========================
1700 struct ik_basic
*arm_l
= &player
.mdl
.ik_arm_l
,
1701 *arm_r
= &player
.mdl
.ik_arm_r
;
1704 m3x3_mulv( player
.rb
.to_local
, player
.rb
.v
, localv
);
1706 /* New board transformation */
1707 v4f board_rotation
; v3f board_location
;
1710 q_axis_angle( rz
, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, player
.board_xy
[0] );
1711 q_axis_angle( rx
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, player
.board_xy
[1] );
1712 q_mul( rx
, rz
, board_rotation
);
1714 v3f
*mboard
= player
.mdl
.matrices
[k_chpart_board
];// player.mboard;
1715 q_m3x3( board_rotation
, mboard
);
1716 m3x3_mulv( mboard
, (v3f
){ 0.0f
, -0.5f
, 0.0f
}, board_location
);
1717 v3_add( (v3f
){0.0f
,0.5f
,0.0f
}, board_location
, board_location
);
1718 v3_copy( board_location
, mboard
[3] );
1721 float wheel_r
= offset
[0]*-0.4f
;
1723 q_axis_angle( qwheel
, (v3f
){0.0f
,1.0f
,0.0f
}, wheel_r
);
1725 q_m3x3( qwheel
, player
.mdl
.matrices
[k_chpart_wb
] );
1727 m3x3_transpose( player
.mdl
.matrices
[k_chpart_wb
],
1728 player
.mdl
.matrices
[k_chpart_wf
] );
1729 v3_copy( player
.mdl
.offsets
[k_chpart_wb
],
1730 player
.mdl
.matrices
[k_chpart_wb
][3] );
1731 v3_copy( player
.mdl
.offsets
[k_chpart_wf
],
1732 player
.mdl
.matrices
[k_chpart_wf
][3] );
1734 m4x3_mul( mboard
, player
.mdl
.matrices
[k_chpart_wb
],
1735 player
.mdl
.matrices
[k_chpart_wb
] );
1736 m4x3_mul( mboard
, player
.mdl
.matrices
[k_chpart_wf
],
1737 player
.mdl
.matrices
[k_chpart_wf
] );
1739 m4x3_mulv( mboard
, player
.mdl
.ik_leg_l
.end
, player
.mdl
.ik_leg_l
.end
);
1740 m4x3_mulv( mboard
, player
.mdl
.ik_leg_r
.end
, player
.mdl
.ik_leg_r
.end
);
1743 v3_copy( player
.mdl
.ik_arm_l
.end
, player
.handl_target
);
1744 v3_copy( player
.mdl
.ik_arm_r
.end
, player
.handr_target
);
1746 if( 1||player
.in_air
)
1748 float tuck
= player
.board_xy
[1],
1749 tuck_amt
= fabsf( tuck
) * (1.0f
-fabsf(player
.board_xy
[0]));
1751 float crouch
= player
.grab
*0.3f
;
1752 v3_muladds( player
.mdl
.ik_body
.base
, (v3f
){0.0f
,-1.0f
,0.0f
},
1753 crouch
, player
.mdl
.ik_body
.base
);
1754 v3_muladds( player
.mdl
.ik_body
.end
, (v3f
){0.0f
,-1.0f
,0.0f
},
1755 crouch
*1.2f
, player
.mdl
.ik_body
.end
);
1759 //foot_l *= 1.0f-tuck_amt*1.5f;
1761 if( player
.grab
> 0.1f
)
1763 m4x3_mulv( mboard
, (v3f
){0.1f
,0.14f
,0.6f
},
1764 player
.handl_target
);
1769 //foot_r *= 1.0f-tuck_amt*1.4f;
1771 if( player
.grab
> 0.1f
)
1773 m4x3_mulv( mboard
, (v3f
){0.1f
,0.14f
,-0.6f
},
1774 player
.handr_target
);
1779 v3_lerp( player
.handl
, player
.handl_target
, 1.0f
, player
.handl
);
1780 v3_lerp( player
.handr
, player
.handr_target
, 1.0f
, player
.handr
);
1782 v3_copy( player
.handl
, player
.mdl
.ik_arm_l
.end
);
1783 v3_copy( player
.handr
, player
.mdl
.ik_arm_r
.end
);
1787 static float rhead
= 0.0f
;
1788 static const float klook_max
= 0.8f
;
1789 rhead
= vg_lerpf( rhead
,
1790 vg_clampf( atan2f(localv
[2],-localv
[0]),-klook_max
,klook_max
), 0.04f
);
1791 player
.mdl
.rhead
= rhead
;
1794 static void player_camera_update(void)
1796 /* Update camera matrices */
1797 m4x3_identity( player
.camera
);
1798 m4x3_rotate_y( player
.camera
, -player
.angles
[0] );
1799 m4x3_rotate_x( player
.camera
, -player
.angles
[1] );
1800 v3_copy( player
.camera_pos
, player
.camera
[3] );
1801 m4x3_invert_affine( player
.camera
, player
.camera_inverse
);
1804 static void player_animate_death_cam(void)
1808 v3_copy( player
.mdl
.ragdoll
[k_chpart_head
].co
, head_pos
);
1810 v3_sub( head_pos
, player
.camera_pos
, delta
);
1811 v3_normalize( delta
);
1814 v3_muladds( head_pos
, delta
, -2.5f
, follow_pos
);
1815 v3_lerp( player
.camera_pos
, follow_pos
, 0.1f
, player
.camera_pos
);
1818 * Make sure the camera stays above the ground
1820 v3f min_height
= {0.0f
,1.0f
,0.0f
};
1823 v3_add( player
.camera_pos
, min_height
, sample
);
1825 hit
.dist
= min_height
[1]*2.0f
;
1827 if( ray_world( sample
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
1828 v3_add( hit
.pos
, min_height
, player
.camera_pos
);
1830 player
.camera_pos
[1] =
1831 vg_maxf( wrender
.height
+ 2.0f
, player
.camera_pos
[1] );
1833 player
.angles
[0] = atan2f( delta
[0], -delta
[2] );
1834 player
.angles
[1] = -asinf( delta
[1] );
1837 static void player_animate_camera(void)
1839 v3f offs
= { -0.29f
, 0.08f
, 0.0f
};
1840 m3x3_mulv( player
.rb
.to_world
, offs
, offs
);
1841 m4x3_mulv( player
.rb
.to_world
, player
.mdl
.ik_body
.end
, player
.camera_pos
);
1842 v3_add( offs
, player
.camera_pos
, player
.camera_pos
);
1845 v3_lerp( player
.vl
, player
.rb
.v
, 0.05f
, player
.vl
);
1847 float yaw
= atan2f( player
.vl
[0], -player
.vl
[2] ),
1848 pitch
= atan2f( -player
.vl
[1],
1850 player
.vl
[0]*player
.vl
[0] + player
.vl
[2]*player
.vl
[2]
1853 player
.angles
[0] = yaw
;
1854 player
.angles
[1] = pitch
+ 0.30f
;
1857 static v2f shake_damp
= {0.0f
,0.0f
};
1858 v2f shake
= { vg_randf()-0.5f
, vg_randf()-0.5f
};
1859 v2_muls( shake
, v3_length(player
.rb
.v
)*0.3f
1860 * (1.0f
+fabsf(player
.slip
)), shake
);
1862 v2_lerp( shake_damp
, shake
, 0.01f
, shake_damp
);
1863 shake_damp
[0] *= 0.2f
;
1865 v2_muladds( player
.angles
, shake_damp
, 0.1f
, player
.angles
);
1871 static void player_audio(void)
1873 float speed
= vg_minf(v3_length( player
.rb
.v
)*0.1f
,1.0f
),
1874 attn
= v3_dist( player
.rb
.co
, player
.camera
[3] )+1.0f
;
1875 attn
= (1.0f
/(attn
*attn
)) * speed
;
1877 static float air
= 0.0f
;
1878 air
= vg_lerpf(air
, player
.in_air
? 1.0f
: 0.0f
, 0.7f
);
1880 v3f ears
= { 1.0f
,0.0f
,0.0f
};
1883 v3_sub( player
.rb
.co
, player
.camera
[3], delta
);
1884 v3_normalize( delta
);
1885 m3x3_mulv( player
.camera
, ears
, ears
);
1887 float pan
= v3_dot( ears
, delta
);
1888 audio_player0
.pan
= pan
;
1889 audio_player1
.pan
= pan
;
1890 audio_player2
.pan
= pan
;
1894 audio_player0
.vol
= 0.0f
;
1895 audio_player1
.vol
= 0.0f
;
1896 audio_player2
.vol
= 0.0f
;
1900 if( player
.is_dead
)
1902 audio_player0
.vol
= 0.0f
;
1903 audio_player1
.vol
= 0.0f
;
1904 audio_player2
.vol
= 0.0f
;
1908 float slide
= vg_clampf( fabsf(player
.slip
), 0.0f
, 1.0f
);
1909 audio_player0
.vol
= (1.0f
-air
)*attn
*(1.0f
-slide
);
1910 audio_player1
.vol
= air
*attn
;
1911 audio_player2
.vol
= (1.0f
-air
)*attn
*slide
;
1919 static float *player_cam_pos(void)
1921 return player
.camera_pos
;
1924 static int reset_player( int argc
, char const *argv
[] )
1926 struct respawn_point
*rp
= NULL
, *r
;
1930 for( int i
=0; i
<world
.spawn_count
; i
++ )
1932 r
= &world
.spawns
[i
];
1933 if( !strcmp( r
->name
, argv
[0] ) )
1941 vg_warn( "No spawn named '%s'\n", argv
[0] );
1946 float min_dist
= INFINITY
;
1948 for( int i
=0; i
<world
.spawn_count
; i
++ )
1950 r
= &world
.spawns
[i
];
1951 float d
= v3_dist2( r
->co
, player
.rb
.co
);
1953 vg_info( "Dist %s : %f\n", r
->name
, d
);
1964 vg_error( "No spawn found\n" );
1965 if( !world
.spawn_count
)
1968 rp
= &world
.spawns
[0];
1971 v4_copy( rp
->q
, player
.rb
.q
);
1972 v3_copy( rp
->co
, player
.rb
.co
);
1974 player
.vswitch
= 1.0f
;
1975 player
.slip_last
= 0.0f
;
1978 m3x3_identity( player
.vr
);
1980 player
.mdl
.shoes
[0] = 1;
1981 player
.mdl
.shoes
[1] = 1;
1983 rb_update_transform( &player
.rb
);
1984 m3x3_mulv( player
.rb
.to_world
, (v3f
){ 0.0f
, 0.0f
, -1.2f
}, player
.rb
.v
);
1986 player
.rb_gate_frame
= player
.rb
;
1990 static void player_update(void)
1992 for( int i
=0; i
<player
.land_log_count
; i
++ )
1993 draw_cross( player
.land_target_log
[i
],
1994 player
.land_target_colours
[i
], 0.25f
);
1996 if( vg_get_axis("grabl")>0.0f
)
1998 player
.rb
= player
.rb_gate_frame
;
2001 m3x3_identity( player
.vr
);
2003 player
.mdl
.shoes
[0] = 1;
2004 player
.mdl
.shoes
[1] = 1;
2006 world_routes_notify_reset();
2009 if( vg_get_button_down( "switchmode" ) )
2011 player
.on_board
^= 0x1;
2014 if( player
.is_dead
)
2016 character_ragdoll_iter( &player
.mdl
);
2017 character_debug_ragdoll( &player
.mdl
);
2020 player_animate_death_cam();
2024 if( player
.on_board
)
2030 player_animate_camera();
2041 player_camera_update();
2045 static void draw_player(void)
2048 m4x3_copy( player
.rb
.to_world
, player
.mdl
.mroot
);
2050 if( player
.is_dead
)
2051 character_mimic_ragdoll( &player
.mdl
);
2053 character_eval( &player
.mdl
);
2055 float opacity
= 1.0f
-player
.air_blend
;
2056 if( player
.is_dead
)
2059 character_draw( &player
.mdl
, opacity
, player
.camera
);
2062 #endif /* PLAYER_H */