2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
5 #ifndef PLAYER_PHYSICS_H
6 #define PLAYER_PHYSICS_H
11 VG_STATIC
void apply_gravity( v3f vel
, float const timestep
)
13 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
14 v3_muladds( vel
, gravity
, timestep
, vel
);
18 grind_edge
*player_grind_collect_edge( v3f p0
, v3f p1
,
19 v3f c0
, v3f c1
, float max_dist
)
21 struct player_phys
*phys
= &player
.phys
;
24 bh_iter_init( 0, &it
);
28 box_init_inf( region
);
29 box_addpt( region
, p0
);
30 box_addpt( region
, p1
);
33 v3_add( (v3f
){ k_r
, k_r
, k_r
}, region
[1], region
[1] );
34 v3_add( (v3f
){-k_r
,-k_r
,-k_r
}, region
[0], region
[0] );
36 float closest
= k_r
*k_r
;
37 struct grind_edge
*closest_edge
= NULL
;
40 while( bh_next( world
.grind_bh
, &it
, region
, &idx
) )
42 struct grind_edge
*edge
= &world
.grind_edges
[ idx
];
48 closest_segment_segment( p0
, p1
, edge
->p0
, edge
->p1
, &s
,&t
, pa
, pb
);
63 * Cast a sphere from a to b and see what time it hits
65 VG_STATIC
int spherecast_world( v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
67 struct player_phys
*phys
= &player
.phys
;
70 bh_iter_init( 0, &it
);
73 box_init_inf( region
);
74 box_addpt( region
, pa
);
75 box_addpt( region
, pb
);
77 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
78 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
81 v3_sub( pb
, pa
, dir
);
87 while( bh_next( world
.geo_bh
, &it
, region
, &idx
) )
89 u32
*ptri
= &world
.scene_geo
->arrindices
[ idx
*3 ];
95 for( int j
=0; j
<3; j
++ )
97 v3_copy( world
.scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
98 box_addpt( box
, tri
[j
] );
101 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
102 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
103 if( !ray_aabb( box
, pa
, dir
, 1.0f
) )
108 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
124 * Trace a path given a velocity rotation.
125 * Closest to 0 is best.
127 VG_STATIC
void player_predict_land( m3x3f vr
,
128 struct land_prediction
*prediction
)
130 struct player_phys
*phys
= &player
.phys
;
132 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
133 float k_bias
= 0.96f
;
136 v3_copy( phys
->rb
.co
, pco
);
137 v3_muls( phys
->rb
.v
, k_bias
, pv
);
139 m3x3_mulv( vr
, pv
, pv
);
140 v3_muladds( pco
, pv
, pstep
, pco
);
142 struct grind_edge
*best_grind
= NULL
;
143 float closest_grind
= INFINITY
;
145 float grind_score
= INFINITY
,
146 air_score
= INFINITY
;
148 prediction
->log_length
= 0;
150 for( int i
=0; i
<vg_list_size(prediction
->log
); i
++ )
152 v3_copy( pco
, pco1
);
153 apply_gravity( pv
, pstep
);
155 m3x3_mulv( vr
, pv
, pv
);
156 v3_muladds( pco
, pv
, pstep
, pco
);
160 v3_sub( pco
, pco1
, vdir
);
162 float l
= v3_length( vdir
);
163 v3_muls( vdir
, 1.0f
/l
, vdir
);
166 struct grind_edge
*ge
= player_grind_collect_edge( pco
, pco1
,
169 if( ge
&& (v3_dot((v3f
){0.0f
,1.0f
,0.0f
},vdir
) < -0.2f
) )
171 float d2
= v3_dist2( c0
, c1
);
172 if( d2
< closest_grind
)
176 grind_score
= closest_grind
* 0.05f
;
183 int idx
= spherecast_world( pco1
, pco
, 0.4f
, &t1
, n1
);
186 v3_copy( n1
, prediction
->n
);
187 air_score
= -v3_dot( pv
, n1
);
189 u32 vert_index
= world
.scene_geo
->arrindices
[ idx
*3 ];
190 struct world_material
*mat
= world_tri_index_material( vert_index
);
192 /* Bias prediction towords ramps */
193 if( mat
->info
.flags
& k_material_flag_skate_surface
)
196 v3_lerp( pco1
, pco
, t1
, prediction
->log
[ prediction
->log_length
++ ] );
200 v3_copy( pco
, prediction
->log
[ prediction
->log_length
++ ] );
203 if( grind_score
< air_score
)
205 prediction
->score
= grind_score
;
206 prediction
->type
= k_prediction_grind
;
208 else if( air_score
< INFINITY
)
210 prediction
->score
= air_score
;
211 prediction
->type
= k_prediction_land
;
215 prediction
->score
= INFINITY
;
216 prediction
->type
= k_prediction_none
;
221 * Called when launching into the air to predict and adjust trajectories
223 VG_STATIC
void player_start_air(void)
225 struct player_phys
*phys
= &player
.phys
;
227 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
228 float best_velocity_delta
= -9999.9f
;
231 v3_cross( phys
->rb
.up
, phys
->rb
.v
, axis
);
232 v3_normalize( axis
);
233 player
.prediction_count
= 0;
235 m3x3_identity( phys
->vr
);
239 min_score
= INFINITY
,
240 max_score
= -INFINITY
;
243 * Search a broad selection of futures
245 for( int m
=-3;m
<=12; m
++ )
247 struct land_prediction
*p
=
248 &player
.predictions
[ player
.prediction_count
++ ];
250 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
255 q_axis_angle( vr_q
, axis
, vmod
);
258 player_predict_land( vr
, p
);
260 if( p
->type
!= k_prediction_none
)
262 if( p
->score
< min_score
)
264 min_score
= p
->score
;
268 if( p
->score
> max_score
)
269 max_score
= p
->score
;
274 q_axis_angle( vr_q
, axis
, best_vmod
*0.1f
);
275 q_m3x3( vr_q
, phys
->vr
);
277 q_axis_angle( vr_q
, axis
, best_vmod
);
278 q_m3x3( vr_q
, phys
->vr_pstep
);
283 for( int i
=0; i
<player
.prediction_count
; i
++ )
285 struct land_prediction
*p
= &player
.predictions
[i
];
291 vg_error( "negative score! (%f)\n", l
);
295 l
/= (max_score
-min_score
);
301 p
->colour
|= 0xff000000;
306 VG_STATIC
void player_physics_control_passive(void)
308 struct player_phys
*phys
= &player
.phys
;
309 float grabt
= player
.input_grab
->axis
.value
;
313 v2_muladds( phys
->grab_mouse_delta
, vg
.mouse_delta
, 0.02f
,
314 phys
->grab_mouse_delta
);
315 v2_normalize_clamp( phys
->grab_mouse_delta
);
318 v2_zero( phys
->grab_mouse_delta
);
321 v2_zero( phys
->grab_mouse_delta
);
323 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
324 player
.phys
.pushing
= 0.0f
;
326 if( !phys
->jump_charge
|| phys
->in_air
)
328 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
331 phys
->jump_charge
= 0;
332 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
336 * Main friction interface model
338 VG_STATIC
void player_physics_control(void)
340 struct player_phys
*phys
= &player
.phys
;
343 * Computing localized friction forces for controlling the character
344 * Friction across X is significantly more than Z
348 m3x3_mulv( phys
->rb
.to_local
, phys
->rb
.v
, vel
);
351 if( fabsf(vel
[2]) > 0.01f
)
352 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
354 if( fabsf( slip
) > 1.2f
)
355 slip
= vg_signf( slip
) * 1.2f
;
357 phys
->reverse
= -vg_signf(vel
[2]);
359 float substep
= VG_TIMESTEP_FIXED
;
360 float fwd_resistance
= k_friction_resistance
;
362 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
363 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
365 if( player
.input_jump
->button
.value
)
367 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
369 if( !phys
->jump_charge
)
370 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
372 phys
->jump_charge
= 1;
375 static int push_thresh_last
= 0;
376 float push
= player
.input_push
->button
.value
;
377 int push_thresh
= push
>0.15f
? 1: 0;
379 if( push_thresh
&& !push_thresh_last
)
380 player
.phys
.start_push
= vg
.time
;
382 push_thresh_last
= push_thresh
;
384 if( !player
.input_jump
->button
.value
&& push_thresh
)
386 player
.phys
.pushing
= 1.0f
;
387 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
389 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
390 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
391 current
= v3_length( vel
),
392 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
394 new_vel
-= vg_minf(current
, k_max_push_speed
);
395 vel
[2] -= new_vel
* phys
->reverse
;
398 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
400 float input
= player
.input_js1h
->axis
.value
,
401 grab
= player
.input_grab
->axis
.value
,
402 steer
= input
* (1.0f
-(phys
->jump
+grab
)*0.4f
),
403 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
405 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
410 * ===============================================
415 v3_muladds( phys
->rb
.co
, phys
->rb
.up
, 1.0f
-grab
, cog_ideal
);
416 v3_sub( cog_ideal
, phys
->cog
, diff
);
419 if( v3_length2( diff
) > 20.0f
*20.0f
)
420 v3_copy( cog_ideal
, phys
->cog
);
423 float rate_lat
= k_cog_spring_lat
* VG_TIMESTEP_FIXED
,
424 rate_vert
= k_cog_spring_vert
* VG_TIMESTEP_FIXED
,
425 vert_amt
= v3_dot( diff
, phys
->rb
.up
);
427 /* Split into vert/lat springs */
428 v3f diff_vert
, diff_lat
;
429 v3_muladds( diff
, phys
->rb
.up
, -vert_amt
, diff_lat
);
430 v3_muls( phys
->rb
.up
, vert_amt
, diff_vert
);
434 rate_vert
*= k_cog_boost_multiplier
;
436 float ap_a
= k_cog_mass_ratio
,
437 ap_b
= -(1.0f
-k_cog_mass_ratio
);
439 v3_muladds( phys
->cog_v
, diff_lat
, rate_lat
* ap_a
, phys
->cog_v
);
440 v3_muladds( phys
->cog_v
, diff_vert
, rate_vert
* ap_a
, phys
->cog_v
);
442 //v3_muladds( phys->rb.v, diff_lat, rate_lat * ap_b, phys->rb.v );
443 v3_muladds( phys
->rb
.v
, diff_vert
, rate_vert
* ap_b
, phys
->rb
.v
);
446 v3_muls( phys
->cog_v
, 1.0f
-(VG_TIMESTEP_FIXED
*k_cog_damp
), phys
->cog_v
);
449 v3_muladds( phys
->cog
, phys
->cog_v
, VG_TIMESTEP_FIXED
, phys
->cog
);
455 * ===============================================
460 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
464 /* Launch more up if alignment is up else improve velocity */
465 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
467 dir
= mod
+ aup
*(1.0f
-mod
);
469 v3_copy( phys
->rb
.v
, jumpdir
);
470 v3_normalize( jumpdir
);
471 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
472 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
473 v3_normalize( jumpdir
);
475 float force
= k_jump_force
*phys
->jump
;
476 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
479 player
.jump_time
= vg
.time
;
481 /* TODO: Move to audio file */
483 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
484 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
485 audio_player_set_vol( &audio_player_extra
, 20.0f
);
486 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%2] );
491 VG_STATIC
void player_physics_control_grind(void)
493 struct player_phys
*phys
= &player
.phys
;
494 v2f steer
= { player
.input_js1h
->axis
.value
,
495 player
.input_js1v
->axis
.value
};
497 float l2
= v2_length2( steer
);
499 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
501 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
503 float iX
= steer
[1] * phys
->reverse
* k_steer_air
* VG_TIMESTEP_FIXED
;
505 static float siX
= 0.0f
;
506 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
509 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
510 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
516 * Air control, no real physics
518 VG_STATIC
void player_physics_control_air(void)
520 struct player_phys
*phys
= &player
.phys
;
522 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
523 //vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
530 float pstep
= VG_TIMESTEP_FIXED
* 1.0f
;
531 float k_bias
= 0.98f
;
534 v3_copy( phys
->rb
.co
, pco
);
535 v3_muls( phys
->rb
.v
, 1.0f
, pv
);
537 float time_to_impact
= 0.0f
;
538 float limiter
= 1.0f
;
540 struct grind_edge
*best_grind
= NULL
;
541 float closest_grind
= INFINITY
;
543 v3f target_normal
= { 0.0f
, 1.0f
, 0.0f
};
546 for( int i
=0; i
<250; i
++ )
548 v3_copy( pco
, pco1
);
549 m3x3_mulv( phys
->vr
, pv
, pv
);
550 apply_gravity( pv
, pstep
);
551 v3_muladds( pco
, pv
, pstep
, pco
);
556 v3_sub( pco
, pco1
, vdir
);
557 contact
.dist
= v3_length( vdir
);
558 v3_divs( vdir
, contact
.dist
, vdir
);
561 struct grind_edge
*ge
= player_grind_collect_edge( pco
, pco1
,
564 if( ge
&& (v3_dot((v3f
){0.0f
,1.0f
,0.0f
},vdir
) < -0.2f
) )
566 vg_line( ge
->p0
, ge
->p1
, 0xff0000ff );
567 vg_line_cross( pco
, 0xff0000ff, 0.25f
);
572 float orig_dist
= contact
.dist
;
573 if( ray_world( pco1
, vdir
, &contact
) )
575 v3_copy( contact
.normal
, target_normal
);
577 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
578 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
581 time_to_impact
+= pstep
;
586 float angle
= v3_dot( phys
->rb
.up
, target_normal
);
588 v3_cross( phys
->rb
.up
, target_normal
, axis
);
590 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
591 limiter
= 1.0f
-limiter
;
593 limiter
= 1.0f
-limiter
;
595 if( fabsf(angle
) < 0.99f
)
598 q_axis_angle( correction
, axis
,
599 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
600 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
604 v2f steer
= { player
.input_js1h
->axis
.value
,
605 player
.input_js1v
->axis
.value
};
607 float l2
= v2_length2( steer
);
609 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
611 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
613 float iX
= steer
[1] *
614 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
616 static float siX
= 0.0f
;
617 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
620 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
621 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
624 v2f target
= {0.0f
,0.0f
};
625 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
626 phys
->grab
, target
);
630 VG_STATIC
void player_walk_collider_configuration(void)
632 struct player_phys
*phys
= &player
.phys
;
636 rigidbody
*rbf
= &player
.collide_front
,
637 *rbb
= &player
.collide_back
;
639 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
640 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
641 v3_copy( rbf
->co
, rbf
->to_world
[3] );
642 v3_copy( rbb
->co
, rbb
->to_world
[3] );
643 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
644 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
646 rb_update_bounds( rbf
);
647 rb_update_bounds( rbb
);
650 VG_STATIC
void player_regular_collider_configuration(void)
652 struct player_phys
*phys
= &player
.phys
;
654 /* Standard ground configuration */
655 rigidbody
*rbf
= &player
.collide_front
,
656 *rbb
= &player
.collide_back
;
658 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
659 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
661 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
662 float h
= player
.air_blend
*0.0f
;
664 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
665 v3_copy( rbf
->co
, rbf
->to_world
[3] );
666 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
667 v3_copy( rbb
->co
, rbb
->to_world
[3] );
669 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
670 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
672 rb_update_bounds( rbf
);
673 rb_update_bounds( rbb
);
676 VG_STATIC
void player_integrate(void);
678 VG_STATIC
int player_walk_surface_standable( v3f n
)
680 return v3_dot( n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
;
683 VG_STATIC
void player_walk_stepdown(void)
685 struct player_phys
*phys
= &player
.phys
;
686 float max_dist
= 0.4f
;
689 v3_copy( phys
->rb
.co
, pa
);
692 v3_muladds( pa
, (v3f
){0.01f
,1.0f
,0.01f
}, -max_dist
, pb
);
693 vg_line( pa
, pb
, 0xff000000 );
695 /* TODO: Make #define */
700 if( spherecast_world( pa
, pb
, r
, &t
, n
) != -1 )
702 if( player_walk_surface_standable( n
) )
705 v3_lerp( pa
, pb
, t
+0.001f
, phys
->rb
.co
);
706 phys
->rb
.co
[1] -= 0.3f
;
711 VG_STATIC
int player_update_collision_manifold( rb_ct
*manifold
);
712 VG_STATIC
void player_walk_physics(void)
714 struct player_phys
*phys
= &player
.phys
;
715 rigidbody
*rbf
= &player
.collide_front
,
716 *rbb
= &player
.collide_back
;
718 m3x3_identity( player
.collide_front
.to_world
);
719 m3x3_identity( player
.collide_back
.to_world
);
721 v3_zero( phys
->rb
.w
);
722 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
727 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
728 v3f right_dir
= { -forward_dir
[2], 0.0f
, forward_dir
[0] };
730 v2f walk
= { player
.input_walkh
->axis
.value
,
731 player
.input_walkv
->axis
.value
};
736 if( v2_length2(walk
) > 0.001f
)
737 v2_normalize_clamp( walk
);
741 player_walk_collider_configuration();
743 /* allow player to accelerate a bit */
745 v3_muls( forward_dir
, walk
[1], walk_3d
);
746 v3_muladds( walk_3d
, right_dir
, walk
[0], walk_3d
);
748 float current_vel
= fabsf(v3_dot( walk_3d
, phys
->rb
.v
)),
749 new_vel
= current_vel
+ VG_TIMESTEP_FIXED
*k_air_accelerate
,
750 clamped_new
= vg_clampf( new_vel
, 0.0f
, k_walkspeed
),
751 vel_diff
= vg_maxf( 0.0f
, clamped_new
- current_vel
);
753 v3_muladds( phys
->rb
.v
, right_dir
, walk
[0] * vel_diff
, phys
->rb
.v
);
754 v3_muladds( phys
->rb
.v
, forward_dir
, walk
[1] * vel_diff
, phys
->rb
.v
);
757 len
= player_update_collision_manifold( manifold
);
758 rb_presolve_contacts( manifold
, len
);
760 for( int i
=0; i
<len
; i
++ )
762 struct contact
*ct
= &manifold
[i
];
763 if( v3_dot( ct
->n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
)
767 for( int j
=0; j
<5; j
++ )
769 for( int i
=0; i
<len
; i
++ )
771 struct contact
*ct
= &manifold
[i
];
774 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
777 float temp
= ct
->norm_impulse
;
778 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
779 vn
= ct
->norm_impulse
- temp
;
782 v3_muls( ct
->n
, vn
, impulse
);
784 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
787 for( int j
=0; j
<2; j
++ )
789 float f
= k_friction
* ct
->norm_impulse
,
790 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
793 float temp
= ct
->tangent_impulse
[j
];
794 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
795 lambda
= ct
->tangent_impulse
[j
] - temp
;
797 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
806 player
.walk
= v2_length( walk
);
808 if( player
.input_walk
->button
.value
)
809 v2_muls( walk
, 0.5f
, walk
);
811 v2_muls( walk
, k_walkspeed
* VG_TIMESTEP_FIXED
, walk
);
813 /* Do XY translation */
814 v3f walk_apply
, walk_measured
;
815 v3_zero( walk_apply
);
816 v3_muladds( walk_apply
, right_dir
, walk
[0], walk_apply
);
817 v3_muladds( walk_apply
, forward_dir
, walk
[1], walk_apply
);
818 v3_add( walk_apply
, phys
->rb
.co
, phys
->rb
.co
);
820 /* Directly resolve collisions */
821 player_walk_collider_configuration();
822 len
= player_update_collision_manifold( manifold
);
826 for( int j
=0; j
<8; j
++ )
828 for( int i
=0; i
<len
; i
++ )
830 struct contact
*ct
= &manifold
[i
];
832 float resolved_amt
= v3_dot( ct
->n
, dt
),
833 remaining
= (ct
->p
-k_penetration_slop
) - resolved_amt
,
834 apply
= vg_maxf( remaining
, 0.0f
) * 0.3f
;
836 v3_muladds( dt
, ct
->n
, apply
, dt
);
839 v3_add( dt
, phys
->rb
.co
, phys
->rb
.co
);
841 v3_add( dt
, walk_apply
, walk_measured
);
842 v3_divs( walk_measured
, VG_TIMESTEP_FIXED
, phys
->rb
.v
);
846 struct world_material
*surface_mat
= world_contact_material(manifold
);
847 player
.surface_prop
= surface_mat
->info
.surface_prop
;
851 if( player
.input_jump
->button
.value
)
853 phys
->rb
.v
[1] = 5.0f
;
858 /* Check if grounded by current manifold */
860 for( int i
=0; i
<len
; i
++ )
862 struct contact
*ct
= &manifold
[i
];
863 if( player_walk_surface_standable( ct
->n
) )
869 player_walk_stepdown();
873 VG_STATIC
void player_grind(void)
875 struct player_phys
*phys
= &player
.phys
;
878 int idx
= bh_closest_point( world
.grind_bh
, phys
->rb
.co
, closest
, INFINITY
);
882 struct grind_edge
*edge
= &world
.grind_edges
[ idx
];
884 vg_line( phys
->rb
.co
, closest
, 0xff000000 );
885 vg_line_cross( closest
, 0xff000000, 0.3f
);
886 vg_line( edge
->p0
, edge
->p1
, 0xff000000 );
889 v3_sub( closest
, phys
->rb
.co
, grind_delta
);
891 float p
= v3_dot( phys
->rb
.forward
, grind_delta
);
892 v3_muladds( grind_delta
, phys
->rb
.forward
, -p
, grind_delta
);
894 float a
= vg_maxf( 0.0f
, 4.0f
-v3_dist2( closest
, phys
->rb
.co
) );
895 v3_muladds( phys
->rb
.v
, grind_delta
, a
*0.2f
, phys
->rb
.v
);
898 VG_STATIC
int player_update_grind_collision( rb_ct
*contact
)
900 struct player_phys
*phys
= &player
.phys
;
903 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, 0.5f
, p0
);
904 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, -0.5f
, p1
);
905 v3_muladds( p0
, phys
->rb
.up
, 0.125f
-0.15f
, p0
);
906 v3_muladds( p1
, phys
->rb
.up
, 0.125f
-0.15f
, p1
);
908 float const k_r
= 0.25f
;
909 struct grind_edge
*closest_edge
= player_grind_collect_edge( p0
, p1
,
914 vg_line( p0
, p1
, 0xff0000ff );
920 vg_line_cross( c0
, 0xff000000, 0.1f
);
921 vg_line_cross( c1
, 0xff000000, 0.1f
);
922 vg_line( c0
, c1
, 0xff000000 );
926 v3_sub( c1
, c0
, delta
);
928 if( v3_dot( delta
, phys
->rb
.up
) > 0.0001f
)
930 contact
->p
= v3_length( delta
);
931 contact
->type
= k_contact_type_edge
;
932 contact
->element_id
= 0;
933 v3_copy( c1
, contact
->co
);
934 contact
->rba
= &player
.phys
.rb
;
935 contact
->rbb
= &world
.rb_geo
;
937 v3f edge_dir
, axis_dir
;
938 v3_sub( closest_edge
->p1
, closest_edge
->p0
, edge_dir
);
939 v3_normalize( edge_dir
);
940 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, edge_dir
, axis_dir
);
941 v3_cross( edge_dir
, axis_dir
, contact
->n
);
944 vg_info( "%f %f\n", v3_length( contact
->n
), contact
->p
);
956 /* Manifold must be able to hold at least 64 elements */
957 VG_STATIC
int player_update_collision_manifold( rb_ct
*manifold
)
959 struct player_phys
*phys
= &player
.phys
;
961 rigidbody
*rbf
= &player
.collide_front
,
962 *rbb
= &player
.collide_back
;
964 rb_debug( rbf
, 0xff00ffff );
965 rb_debug( rbb
, 0xffffff00 );
969 phys
->rise
= vg_lerpf( phys
->rise
, phys
->in_air
? -0.25f
: 0.0f
,
976 len_f
= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
);
977 rb_manifold_filter_coplanar( manifold
, len_f
, 0.05f
);
980 rb_manifold_filter_backface( manifold
, len_f
);
981 rb_manifold_filter_joint_edges( manifold
, len_f
, 0.05f
);
982 rb_manifold_filter_pairs( manifold
, len_f
, 0.05f
);
984 int new_len_f
= rb_manifold_apply_filtered( manifold
, len_f
);
985 if( len_f
&& !new_len_f
)
990 rb_ct
*man_b
= &manifold
[len_f
];
991 len_b
= rb_sphere_scene( rbb
, &world
.rb_geo
, man_b
);
992 rb_manifold_filter_coplanar( man_b
, len_b
, 0.05f
);
995 rb_manifold_filter_backface( man_b
, len_b
);
996 rb_manifold_filter_joint_edges( man_b
, len_b
, 0.05f
);
997 rb_manifold_filter_pairs( man_b
, len_b
, 0.05f
);
999 int new_len_b
= rb_manifold_apply_filtered( man_b
, len_b
);
1000 if( len_b
&& !new_len_b
)
1005 return len_f
+ len_b
;
1008 VG_STATIC
void player_adhere_ground( rb_ct
*manifold
, int len
)
1010 struct player_phys
*phys
= &player
.phys
;
1011 int was_in_air
= phys
->in_air
;
1014 v3_zero( surface_avg
);
1019 * ================================================================
1022 player
.normal_pressure
= 0.0f
;
1024 player
.normal_pressure
= v3_dot( phys
->rb
.up
, phys
->rb
.v
);
1031 float mod
= 0.7f
* player
.input_grab
->axis
.value
+ 0.3f
,
1032 spring_k
= mod
* k_spring_force
,
1033 damp_K
= mod
* k_spring_dampener
,
1036 v3_copy( player
.collide_front
.co
, p0_0
);
1037 v3_copy( player
.collide_back
.co
, p1_0
);
1039 v3_muladds( p0_0
, phys
->rb
.up
, -disp_k
, p0_1
);
1040 v3_muladds( p1_0
, phys
->rb
.up
, -disp_k
, p1_1
);
1042 int cast0
= spherecast_world( p0_0
, p0_1
, 0.2f
, &t0
, n0
),
1043 cast1
= spherecast_world( p1_0
, p1_1
, 0.2f
, &t1
, n1
);
1048 m3x3_copy( phys
->rb
.to_world
, temp
);
1051 v3_lerp( p0_0
, p0_1
, t0
, temp
[3] );
1052 v3_copy( temp
[3], animp0
);
1053 debug_sphere( temp
, 0.2f
, VG__PINK
);
1056 v3_sub( p0_0
, phys
->rb
.co
, delta
);
1058 float displacement
= vg_clampf( 1.0f
-t0
, 0.0f
, 1.0f
),
1059 damp
= vg_maxf( 0.0f
, v3_dot( phys
->rb
.up
, phys
->rb
.v
) );
1060 v3_muls( phys
->rb
.up
, displacement
*spring_k
*k_rb_delta
-
1061 damp
*damp_K
*k_rb_delta
, F
);
1063 v3_muladds( phys
->rb
.v
, F
, 1.0f
, phys
->rb
.v
);
1065 /* Angular velocity */
1067 v3_cross( delta
, F
, wa
);
1068 v3_muladds( phys
->rb
.w
, wa
, k_spring_angular
, phys
->rb
.w
);
1071 v3_copy( p0_1
, animp0
);
1075 v3_lerp( p1_0
, p1_1
, t1
, temp
[3] );
1076 v3_copy( temp
[3], animp1
);
1077 debug_sphere( temp
, 0.2f
, VG__PINK
);
1080 v3_sub( p1_0
, phys
->rb
.co
, delta
);
1082 float displacement
= vg_clampf( 1.0f
-t1
, 0.0f
, 1.0f
),
1083 damp
= vg_maxf( 0.0f
, v3_dot( phys
->rb
.up
, phys
->rb
.v
) );
1084 v3_muls( phys
->rb
.up
, displacement
*spring_k
*k_rb_delta
-
1085 damp
*damp_K
*k_rb_delta
, F
);
1087 v3_muladds( phys
->rb
.v
, F
, 1.0f
, phys
->rb
.v
);
1089 /* Angular velocity */
1091 v3_cross( delta
, F
, wa
);
1092 v3_muladds( phys
->rb
.w
, wa
, k_spring_angular
, phys
->rb
.w
);
1095 v3_copy( p1_1
, animp1
);
1097 v3f animavg
, animdelta
;
1098 v3_add( animp0
, animp1
, animavg
);
1099 v3_muls( animavg
, 0.5f
, animavg
);
1101 v3_sub( animp1
, animp0
, animdelta
);
1102 v3_normalize( animdelta
);
1104 m4x3_mulv( phys
->rb
.to_local
, animavg
, player
.board_offset
);
1106 float dx
= -v3_dot( animdelta
, phys
->rb
.forward
),
1107 dy
= v3_dot( animdelta
, phys
->rb
.up
);
1109 float angle
= -atan2f( dy
, dx
);
1110 q_axis_angle( player
.board_rotation
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, angle
);
1113 * ================================================================
1117 if( len
== 0 && !((cast0
!=-1)&&(cast1
!=-1)) )
1119 phys
->lift_frames
++;
1121 if( phys
->lift_frames
>= 8 )
1126 for( int i
=0; i
<len
; i
++ )
1127 v3_add( surface_avg
, manifold
[i
].n
, surface_avg
);
1128 v3_normalize( surface_avg
);
1130 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.7f
)
1132 phys
->lift_frames
++;
1134 if( phys
->lift_frames
>= 8 )
1140 phys
->lift_frames
= 0;
1141 v3f projected
, axis
;
1143 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
1144 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
1146 float d
= v3_dot( phys
->rb
.forward
, surface_avg
);
1147 v3_muladds( surface_avg
, phys
->rb
.forward
, -d
, projected
);
1148 v3_normalize( projected
);
1150 float angle
= v3_dot( phys
->rb
.up
, projected
);
1151 v3_cross( phys
->rb
.up
, projected
, axis
);
1155 v3_add( phys
->rb
.co
, projected
, p0
);
1156 v3_add( phys
->rb
.co
, phys
->rb
.up
, p1
);
1157 vg_line( phys
->rb
.co
, p0
, 0xff00ff00 );
1158 vg_line( phys
->rb
.co
, p1
, 0xff000fff );
1161 if( fabsf(angle
) < 0.999f
)
1164 q_axis_angle( correction
, axis
,
1165 acosf(angle
)*4.0f
*VG_TIMESTEP_FIXED
);
1166 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
1171 if( !was_in_air
&& phys
->in_air
)
1175 VG_STATIC
void player_collision_response( rb_ct
*manifold
, int len
)
1177 struct player_phys
*phys
= &player
.phys
;
1181 * ===============================================
1185 player
.normal_pressure
= v3_dot( phys
->rb
.up
, phys
->rb
.v
);
1188 float ideal
= 1.0f
-player
.input_grab
->axis
.value
,
1189 diff
= phys
->spring
- ideal
,
1190 Fspring
= -k_cog_spring_lat
* diff
,
1191 Fdamp
= -k_cog_damp
* phys
->springv
,
1192 F
= (Fspring
+ Fdamp
) * k_rb_delta
;
1195 phys
->spring
+= phys
->springv
* k_rb_delta
;
1197 if( phys
->springv
> 0.0f
)
1198 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, F
*k_cog_spring_vert
, phys
->rb
.v
);
1201 player
.normal_pressure
= 0.0f
;
1203 player
.normal_pressure
= v3_dot( phys
->rb
.up
, phys
->rb
.v
);
1207 if( player
.input_grab
->axis
.value
> 0.5f
)
1212 v3_muls( phys
->rb
.up
, k_mmthrow_scale
, phys
->throw_v
);
1218 float doty
= v3_dot( phys
->rb
.up
, phys
->throw_v
);
1221 v3_muladds( phys
->throw_v
, phys
->rb
.up
, -doty
, Fl
);
1225 v3_muladds( phys
->rb
.v
, Fl
, k_mmcollect_lat
, phys
->rb
.v
);
1226 v3_muladds( phys
->throw_v
, Fl
, -k_mmcollect_lat
, phys
->throw_v
);
1229 v3_muls( phys
->rb
.up
, -doty
, Fv
);
1230 v3_muladds( phys
->rb
.v
, Fv
, k_mmcollect_vert
, phys
->rb
.v
);
1231 v3_muladds( phys
->throw_v
, Fv
, k_mmcollect_vert
, phys
->throw_v
);
1233 v3_copy( Fl
, player
.debug_mmcollect_lat
);
1234 v3_copy( Fv
, player
.debug_mmcollect_vert
);
1238 if( v3_length2( phys
->throw_v
) > 0.0001f
)
1241 v3_copy( phys
->throw_v
, dir
);
1242 v3_normalize( dir
);
1244 float max
= v3_dot( dir
, phys
->throw_v
),
1245 amt
= vg_minf( k_mmdecay
* k_rb_delta
, max
);
1247 v3_muladds( phys
->throw_v
, dir
, -amt
, phys
->throw_v
);
1251 /* TODO: RElocate */
1254 v3f ideal_cog
, ideal_diff
;
1255 v3_muladds( phys
->rb
.co
, phys
->rb
.up
,
1256 1.0f
-player
.input_grab
->axis
.value
, ideal_cog
);
1257 v3_sub( ideal_cog
, phys
->cog
, ideal_diff
);
1259 /* Apply velocities */
1261 v3_sub( phys
->rb
.v
, phys
->cog_v
, rv
);
1264 v3_muls( ideal_diff
, -k_cog_spring
* k_rb_rate
, F
);
1265 v3_muladds( F
, rv
, -k_cog_damp
* k_rb_rate
, F
);
1267 float ra
= k_cog_mass_ratio
,
1268 rb
= 1.0f
-k_cog_mass_ratio
;
1270 v3_muladds( phys
->cog_v
, F
, -rb
, phys
->cog_v
);
1275 * ===============================================
1278 for( int j
=0; j
<10; j
++ )
1280 for( int i
=0; i
<len
; i
++ )
1282 struct contact
*ct
= &manifold
[i
];
1285 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
1286 v3_cross( phys
->rb
.w
, delta
, dv
);
1287 v3_add( phys
->rb
.v
, dv
, dv
);
1289 float vn
= -v3_dot( dv
, ct
->n
);
1292 float temp
= ct
->norm_impulse
;
1293 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
1294 vn
= ct
->norm_impulse
- temp
;
1297 v3_muls( ct
->n
, vn
, impulse
);
1299 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
1300 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
1306 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
1307 v3_cross( delta
, impulse
, impulse
);
1310 * W Impulses are limited to the Y and X axises, we don't really want
1311 * roll angular velocities being included.
1313 * Can also tweak the resistance of each axis here by scaling the wx,wy
1317 float wy
= v3_dot( phys
->rb
.up
, impulse
) * 0.8f
,
1318 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.0f
;
1320 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
1321 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
1325 /* early integrate this */
1326 phys
->cog_v
[1] += -9.8f
* k_rb_delta
;
1327 v3_muladds( phys
->cog
, phys
->cog_v
, k_rb_delta
, phys
->cog
);
1330 VG_STATIC
void player_save_frame(void)
1332 player
.phys_gate_frame
= player
.phys
;
1335 VG_STATIC
void player_restore_frame(void)
1337 player
.phys
= player
.phys_gate_frame
;
1338 rb_update_transform( &player
.phys
.rb
);
1341 VG_STATIC
void player_integrate(void)
1343 struct player_phys
*phys
= &player
.phys
;
1344 v3_sub( phys
->rb
.v
, phys
->v_last
, phys
->a
);
1345 v3_muls( phys
->a
, 1.0f
/VG_TIMESTEP_FIXED
, phys
->a
);
1346 v3_copy( phys
->rb
.v
, phys
->v_last
);
1348 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
1349 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
1352 VG_STATIC
void player_do_motion(void)
1354 struct player_phys
*phys
= &player
.phys
;
1356 if( world
.water
.enabled
)
1358 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
1361 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
1362 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1363 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1364 audio_player_playclip( &audio_player_extra
, &audio_splash
);
1372 v3_copy( phys
->rb
.co
, prevco
);
1374 if( phys
->on_board
)
1378 player_regular_collider_configuration();
1379 int len
= player_update_collision_manifold( manifold
);
1380 int grind_col
= player_update_grind_collision( &manifold
[len
] );
1382 static int _grind_col_pre
= 0;
1387 v3f up
= { 0.0f
, 1.0f
, 0.0f
};
1388 float angle
= v3_dot( phys
->rb
.up
, up
);
1390 if( fabsf(angle
) < 0.99f
)
1393 v3_cross( phys
->rb
.up
, up
, axis
);
1396 q_axis_angle( correction
, axis
,
1397 VG_TIMESTEP_FIXED
* 10.0f
* acosf(angle
) );
1398 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
1401 float const DOWNFORCE
= -k_downforce
*1.2f
*VG_TIMESTEP_FIXED
;
1402 v3_muladds( phys
->rb
.v
, manifold
[len
].n
, DOWNFORCE
, phys
->rb
.v
);
1403 m3x3_identity( phys
->vr
);
1404 m3x3_identity( phys
->vr_pstep
);
1406 if( !_grind_col_pre
)
1409 audio_player_set_flags( &audio_player_extra
,
1410 AUDIO_FLAG_SPACIAL_3D
);
1411 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1412 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1413 audio_player_playclip( &audio_player_extra
, &audio_board
[5] );
1420 player_adhere_ground( manifold
, len
);
1422 if( _grind_col_pre
)
1425 audio_player_set_flags( &audio_player_extra
,
1426 AUDIO_FLAG_SPACIAL_3D
);
1427 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1428 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1429 audio_player_playclip( &audio_player_extra
, &audio_board
[6] );
1434 _grind_col_pre
= grind_col
;
1436 rb_presolve_contacts( manifold
, len
+ VG_MAX(0,grind_col
) );
1437 player_collision_response( manifold
, len
+ VG_MAX(0,grind_col
) );
1439 player_physics_control_passive();
1444 player_physics_control_grind();
1449 player_physics_control_air();
1451 player_physics_control();
1457 player_walk_physics();
1460 /* Real angular velocity integration */
1461 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
*0.5f
, phys
->rb
.w
);
1462 if( v3_length2( phys
->rb
.w
) > 0.0f
)
1466 v3_copy( phys
->rb
.w
, axis
);
1468 float mag
= v3_length( axis
);
1469 v3_divs( axis
, mag
, axis
);
1470 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
1471 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
1474 /* Faux angular velocity */
1477 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
1478 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
1480 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
1481 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
1485 * Gate intersection, by tracing a line over the gate planes
1487 for( int i
=0; i
<world
.gate_count
; i
++ )
1489 struct route_gate
*rg
= &world
.gates
[i
];
1490 teleport_gate
*gate
= &rg
->gate
;
1492 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
1494 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
1495 m4x3_mulv( gate
->transport
, phys
->cog
, phys
->cog
);
1496 m3x3_mulv( gate
->transport
, phys
->cog_v
, phys
->cog_v
);
1497 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
1498 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
1499 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
1500 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
1501 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
1503 /* Pre-emptively edit the camera matrices so that the motion vectors
1507 m4x3_invert_affine( gate
->transport
, transport_i
);
1508 m4x3_expand( transport_i
, transport_4
);
1509 m4x4_mul( main_camera
.mtx
.pv
, transport_4
, main_camera
.mtx
.pv
);
1510 m4x4_mul( main_camera
.mtx
.v
, transport_4
, main_camera
.mtx
.v
);
1512 v4f transport_rotation
;
1513 m3x3_q( gate
->transport
, transport_rotation
);
1514 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
1516 world_routes_activate_gate( i
);
1518 if( !phys
->on_board
)
1520 v3f fwd_dir
= {cosf(player
.angles
[0]),
1522 sinf(player
.angles
[0])};
1523 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
1525 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
1528 player
.rewind_length
= 0;
1529 player
.rewind_total_length
= 0.0f
;
1530 player
.rewind_incrementer
= 10000;
1531 player_save_frame();
1534 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
1540 rb_update_transform( &phys
->rb
);
1543 VG_STATIC
void player_freecam(void)
1547 float movespeed
= fc_speed
* VG_TIMESTEP_FIXED
;
1548 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
1549 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
1551 m3x3_mulv( main_camera
.transform
, lookdir
, lookdir
);
1552 m3x3_mulv( main_camera
.transform
, sidedir
, sidedir
);
1554 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
1556 v2f steer
= { player
.input_js1h
->axis
.value
,
1557 player
.input_js1v
->axis
.value
};
1559 v3_muladds( move_vel
, sidedir
, movespeed
*steer
[0], move_vel
);
1560 v3_muladds( move_vel
, lookdir
, -movespeed
*steer
[1], move_vel
);
1562 v3_muls( move_vel
, 0.7f
, move_vel
);
1563 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
1566 VG_STATIC
int kill_player( int argc
, char const *argv
[] )
1572 VG_STATIC
int reset_player( int argc
, char const *argv
[] )
1574 struct player_phys
*phys
= &player
.phys
;
1575 struct respawn_point
*rp
= NULL
, *r
;
1579 for( int i
=0; i
<world
.spawn_count
; i
++ )
1581 r
= &world
.spawns
[i
];
1582 if( !strcmp( r
->name
, argv
[0] ) )
1590 vg_warn( "No spawn named '%s'\n", argv
[0] );
1595 float min_dist
= INFINITY
;
1597 for( int i
=0; i
<world
.spawn_count
; i
++ )
1599 r
= &world
.spawns
[i
];
1600 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
1602 vg_info( "Dist %s : %f\n", r
->name
, d
);
1613 vg_error( "No spawn found\n" );
1614 vg_info( "Player position: %f %f %f\n", player
.phys
.rb
.co
[0],
1615 player
.phys
.rb
.co
[1],
1616 player
.phys
.rb
.co
[2] );
1617 vg_info( "Player velocity: %f %f %f\n", player
.phys
.rb
.v
[0],
1618 player
.phys
.rb
.v
[1],
1619 player
.phys
.rb
.v
[2] );
1621 if( !world
.spawn_count
)
1624 rp
= &world
.spawns
[0];
1630 q_m3x3( rp
->q
, the_long_way
);
1632 v3f delta
= {1.0f
,0.0f
,0.0f
};
1633 m3x3_mulv( the_long_way
, delta
, delta
);
1637 player
.angles
[0] = atan2f( delta
[0], -delta
[2] );
1638 player
.angles
[1] = -asinf( delta
[1] );
1641 v4_copy( rp
->q
, phys
->rb
.q
);
1642 v3_copy( rp
->co
, phys
->rb
.co
);
1643 v3_zero( phys
->rb
.v
);
1645 phys
->vswitch
= 1.0f
;
1646 phys
->slip_last
= 0.0f
;
1649 m3x3_identity( phys
->vr
);
1651 player
.mdl
.shoes
[0] = 1;
1652 player
.mdl
.shoes
[1] = 1;
1654 rb_update_transform( &phys
->rb
);
1656 v3_add( phys
->rb
.up
, phys
->rb
.co
, phys
->cog
);
1657 v3_zero( phys
->cog_v
);
1659 player_save_frame();
1663 VG_STATIC
void reset_player_poll( int argc
, char const *argv
[] )
1667 for( int i
=0; i
<world
.spawn_count
; i
++ )
1669 struct respawn_point
*r
= &world
.spawns
[i
];
1671 console_suggest_score_text( r
->name
, argv
[argc
-1], 0 );
1676 VG_STATIC
void player_physics_gui(void)
1680 vg_uictx
.cursor
[0] = 0;
1681 vg_uictx
.cursor
[1] = vg
.window_y
- 128;
1682 vg_uictx
.cursor
[3] = 14;
1687 snprintf( buf
, 127, "v: %6.3f %6.3f %6.3f\n", player
.phys
.rb
.v
[0],
1688 player
.phys
.rb
.v
[1],
1689 player
.phys
.rb
.v
[2] );
1691 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
1692 vg_uictx
.cursor
[1] += 14;
1695 snprintf( buf
, 127, "a: %6.3f %6.3f %6.3f (%6.3f)\n", player
.phys
.a
[0],
1698 v3_length(player
.phys
.a
));
1699 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
1700 vg_uictx
.cursor
[1] += 14;
1702 float normal_acceleration
= v3_dot( player
.phys
.a
, player
.phys
.rb
.up
);
1703 snprintf( buf
, 127, "Normal acceleration: %6.3f\n", normal_acceleration
);
1705 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
1706 vg_uictx
.cursor
[1] += 14;
1708 snprintf( buf
, 127, "Normal Pressure: %6.3f\n", player
.normal_pressure
);
1709 ui_text( vg_uictx
.cursor
, buf
, 1, 0 );
1710 vg_uictx
.cursor
[1] += 14;
1715 #endif /* PLAYER_PHYSICS_H */