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
* 0.2f
;
360 float fwd_resistance
= k_friction_resistance
;
362 for( int i
=0; i
<5; i
++ )
364 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
365 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
368 if( player
.input_jump
->button
.value
)
370 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
372 if( !phys
->jump_charge
)
373 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
375 phys
->jump_charge
= 1;
378 static int push_thresh_last
= 0;
379 float push
= player
.input_push
->button
.value
;
380 int push_thresh
= push
>0.15f
? 1: 0;
382 if( push_thresh
&& !push_thresh_last
)
383 player
.phys
.start_push
= vg
.time
;
385 push_thresh_last
= push_thresh
;
387 if( !player
.input_jump
->button
.value
&& push_thresh
)
389 player
.phys
.pushing
= 1.0f
;
390 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
392 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
393 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
394 current
= v3_length( vel
),
395 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
397 new_vel
-= vg_minf(current
, k_max_push_speed
);
398 vel
[2] -= new_vel
* phys
->reverse
;
401 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
403 float input
= player
.input_js1h
->axis
.value
,
404 grab
= player
.input_grab
->axis
.value
,
405 steer
= input
* (1.0f
-(phys
->jump
+grab
)*0.4f
),
406 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
408 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
410 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
414 /* Launch more up if alignment is up else improve velocity */
415 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
417 dir
= mod
+ aup
*(1.0f
-mod
);
419 v3_copy( phys
->rb
.v
, jumpdir
);
420 v3_normalize( jumpdir
);
421 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
422 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
423 v3_normalize( jumpdir
);
425 float force
= k_jump_force
*phys
->jump
;
426 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
429 player
.jump_time
= vg
.time
;
431 /* TODO: Move to audio file */
433 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
434 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
435 audio_player_set_vol( &audio_player_extra
, 20.0f
);
436 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%2] );
441 VG_STATIC
void player_physics_control_grind(void)
443 struct player_phys
*phys
= &player
.phys
;
444 v2f steer
= { player
.input_js1h
->axis
.value
,
445 player
.input_js1v
->axis
.value
};
447 float l2
= v2_length2( steer
);
449 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
451 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
453 float iX
= steer
[1] * phys
->reverse
* k_steer_air
* VG_TIMESTEP_FIXED
;
455 static float siX
= 0.0f
;
456 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
459 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
460 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
466 * Air control, no real physics
468 VG_STATIC
void player_physics_control_air(void)
470 struct player_phys
*phys
= &player
.phys
;
472 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
473 //vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
480 float pstep
= VG_TIMESTEP_FIXED
* 1.0f
;
481 float k_bias
= 0.98f
;
484 v3_copy( phys
->rb
.co
, pco
);
485 v3_muls( phys
->rb
.v
, 1.0f
, pv
);
487 float time_to_impact
= 0.0f
;
488 float limiter
= 1.0f
;
490 struct grind_edge
*best_grind
= NULL
;
491 float closest_grind
= INFINITY
;
493 v3f target_normal
= { 0.0f
, 1.0f
, 0.0f
};
496 for( int i
=0; i
<250; i
++ )
498 v3_copy( pco
, pco1
);
499 m3x3_mulv( phys
->vr
, pv
, pv
);
500 apply_gravity( pv
, pstep
);
501 v3_muladds( pco
, pv
, pstep
, pco
);
506 v3_sub( pco
, pco1
, vdir
);
507 contact
.dist
= v3_length( vdir
);
508 v3_divs( vdir
, contact
.dist
, vdir
);
511 struct grind_edge
*ge
= player_grind_collect_edge( pco
, pco1
,
514 if( ge
&& (v3_dot((v3f
){0.0f
,1.0f
,0.0f
},vdir
) < -0.2f
) )
516 vg_line( ge
->p0
, ge
->p1
, 0xff0000ff );
517 vg_line_cross( pco
, 0xff0000ff, 0.25f
);
522 float orig_dist
= contact
.dist
;
523 if( ray_world( pco1
, vdir
, &contact
) )
525 v3_copy( contact
.normal
, target_normal
);
527 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
528 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
531 time_to_impact
+= pstep
;
536 float angle
= v3_dot( phys
->rb
.up
, target_normal
);
538 v3_cross( phys
->rb
.up
, target_normal
, axis
);
540 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
541 limiter
= 1.0f
-limiter
;
543 limiter
= 1.0f
-limiter
;
545 if( fabsf(angle
) < 0.99f
)
548 q_axis_angle( correction
, axis
,
549 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
550 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
554 v2f steer
= { player
.input_js1h
->axis
.value
,
555 player
.input_js1v
->axis
.value
};
557 float l2
= v2_length2( steer
);
559 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
561 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
563 float iX
= steer
[1] *
564 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
566 static float siX
= 0.0f
;
567 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
570 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
571 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
574 v2f target
= {0.0f
,0.0f
};
575 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
576 phys
->grab
, target
);
580 VG_STATIC
void player_walk_update_collision(void)
582 struct player_phys
*phys
= &player
.phys
;
586 rigidbody
*rbf
= &player
.collide_front
,
587 *rbb
= &player
.collide_back
;
589 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
590 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
591 v3_copy( rbf
->co
, rbf
->to_world
[3] );
592 v3_copy( rbb
->co
, rbb
->to_world
[3] );
593 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
594 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
596 rb_update_bounds( rbf
);
597 rb_update_bounds( rbb
);
600 VG_STATIC
void player_integrate(void);
602 VG_STATIC
int player_walk_surface_standable( v3f n
)
604 return v3_dot( n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
;
607 VG_STATIC
void player_walk_stepdown(void)
609 struct player_phys
*phys
= &player
.phys
;
610 float max_dist
= 0.4f
;
613 v3_copy( phys
->rb
.co
, pa
);
616 v3_muladds( pa
, (v3f
){0.01f
,1.0f
,0.01f
}, -max_dist
, pb
);
617 vg_line( pa
, pb
, 0xff000000 );
619 /* TODO: Make #define */
624 if( spherecast_world( pa
, pb
, r
, &t
, n
) != -1 )
626 if( player_walk_surface_standable( n
) )
629 v3_lerp( pa
, pb
, t
+0.001f
, phys
->rb
.co
);
630 phys
->rb
.co
[1] -= 0.3f
;
636 * Entire Walking physics model
637 * TODO: sleep when under certain velotiy
639 VG_STATIC
void player_walk_physics(void)
641 struct player_phys
*phys
= &player
.phys
;
642 rigidbody
*rbf
= &player
.collide_front
,
643 *rbb
= &player
.collide_back
;
645 m3x3_identity( player
.collide_front
.to_world
);
646 m3x3_identity( player
.collide_back
.to_world
);
648 v3_zero( phys
->rb
.w
);
649 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
654 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
655 v3f right_dir
= { -forward_dir
[2], 0.0f
, forward_dir
[0] };
657 v2f walk
= { player
.input_walkh
->axis
.value
,
658 player
.input_walkv
->axis
.value
};
663 if( v2_length2(walk
) > 0.001f
)
664 v2_normalize_clamp( walk
);
668 player_walk_update_collision();
669 rb_debug( rbf
, 0xff0000ff );
670 rb_debug( rbb
, 0xff0000ff );
672 /* allow player to accelerate a bit */
674 v3_muls( forward_dir
, walk
[1], walk_3d
);
675 v3_muladds( walk_3d
, right_dir
, walk
[0], walk_3d
);
677 float current_vel
= fabsf(v3_dot( walk_3d
, phys
->rb
.v
)),
678 new_vel
= current_vel
+ VG_TIMESTEP_FIXED
*k_air_accelerate
,
679 clamped_new
= vg_clampf( new_vel
, 0.0f
, k_walkspeed
),
680 vel_diff
= vg_maxf( 0.0f
, clamped_new
- current_vel
);
682 v3_muladds( phys
->rb
.v
, right_dir
, walk
[0] * vel_diff
, phys
->rb
.v
);
683 v3_muladds( phys
->rb
.v
, forward_dir
, walk
[1] * vel_diff
, phys
->rb
.v
);
687 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
688 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
689 rb_presolve_contacts( manifold
, len
);
691 for( int i
=0; i
<len
; i
++ )
693 struct contact
*ct
= &manifold
[i
];
694 if( v3_dot( ct
->n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
)
698 for( int j
=0; j
<5; j
++ )
700 for( int i
=0; i
<len
; i
++ )
702 struct contact
*ct
= &manifold
[i
];
705 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
708 float temp
= ct
->norm_impulse
;
709 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
710 vn
= ct
->norm_impulse
- temp
;
713 v3_muls( ct
->n
, vn
, impulse
);
715 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
718 for( int j
=0; j
<2; j
++ )
720 float f
= k_friction
* ct
->norm_impulse
,
721 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
724 float temp
= ct
->tangent_impulse
[j
];
725 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
726 lambda
= ct
->tangent_impulse
[j
] - temp
;
728 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
737 player
.walk
= v2_length( walk
);
739 if( player
.input_walk
->button
.value
)
740 v2_muls( walk
, 0.5f
, walk
);
742 v2_muls( walk
, k_walkspeed
* VG_TIMESTEP_FIXED
, walk
);
745 v3_zero( walk_apply
);
747 /* Do XY translation */
748 v3_muladds( walk_apply
, right_dir
, walk
[0], walk_apply
);
749 v3_muladds( walk_apply
, forward_dir
, walk
[1], walk_apply
);
750 v3_add( walk_apply
, phys
->rb
.co
, phys
->rb
.co
);
751 v3_divs( walk_apply
, VG_TIMESTEP_FIXED
, phys
->rb
.v
);
753 /* Directly resolve collisions */
754 player_walk_update_collision();
755 rb_debug( rbf
, 0xffffff00 );
756 rb_debug( rbb
, 0xffffff00 );
759 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
760 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
764 for( int j
=0; j
<3; j
++ )
766 for( int i
=0; i
<len
; i
++ )
768 struct contact
*ct
= &manifold
[i
];
770 float p
= vg_maxf( 0.0f
, ct
->p
- 0.00f
),
771 cur
= vg_clampf( v3_dot( ct
->n
, dt
), 0.0f
, p
);
772 v3_muladds( dt
, ct
->n
, (p
- cur
) * 0.333333333f
, dt
);
775 v3_add( dt
, phys
->rb
.co
, phys
->rb
.co
);
779 struct world_material
*surface_mat
= world_contact_material(manifold
);
780 player
.surface_prop
= surface_mat
->info
.surface_prop
;
784 if( player
.input_jump
->button
.value
)
786 phys
->rb
.v
[1] = 5.0f
;
791 /* Check if grounded by current manifold */
793 for( int i
=0; i
<len
; i
++ )
795 struct contact
*ct
= &manifold
[i
];
796 if( player_walk_surface_standable( ct
->n
) )
802 player_walk_stepdown();
805 /* if we've put us in the air, step down slowly */
807 float max_dist
= 0.3f
,
808 start_y
= phys
->rb
.co
[1];
811 v3_copy( phys
->rb
.co
, pa
);
812 v3_muladds( pa
, (v3f
){0.0f
,1.0f
,0.0f
}, -max_dist
, pb
);
815 for( int j
=0; j
<8; j
++ )
817 for( int i
=0; i
<len
; i
++ )
819 struct contact
*ct
= &manifold
[i
];
820 if( v3_dot( ct
->n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
)
828 for( int j
=0; j
<3; j
++ )
830 for( int i
=0; i
<len
; i
++ )
832 struct contact
*ct
= &manifold
[i
];
834 float p
= vg_maxf( 0.0f
, ct
->p
- 0.0025f
),
835 cur
= vg_clampf( v3_dot( ct
->n
, dt
), 0.0f
, p
);
836 v3_muladds( dt
, ct
->n
, (p
- cur
) * 0.333333333f
, dt
);
839 v3_add( dt
, phys
->rb
.co
, phys
->rb
.co
);
844 phys
->rb
.co
[1] -= max_dist
* 0.125f
;
846 player_walk_update_collision();
848 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
849 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
852 /* Transitioning into air mode */
853 phys
->rb
.co
[1] = start_y
;
858 VG_STATIC
void player_grind(void)
860 struct player_phys
*phys
= &player
.phys
;
863 int idx
= bh_closest_point( world
.grind_bh
, phys
->rb
.co
, closest
, INFINITY
);
867 struct grind_edge
*edge
= &world
.grind_edges
[ idx
];
869 vg_line( phys
->rb
.co
, closest
, 0xff000000 );
870 vg_line_cross( closest
, 0xff000000, 0.3f
);
871 vg_line( edge
->p0
, edge
->p1
, 0xff000000 );
874 v3_sub( closest
, phys
->rb
.co
, grind_delta
);
876 float p
= v3_dot( phys
->rb
.forward
, grind_delta
);
877 v3_muladds( grind_delta
, phys
->rb
.forward
, -p
, grind_delta
);
879 float a
= vg_maxf( 0.0f
, 4.0f
-v3_dist2( closest
, phys
->rb
.co
) );
880 v3_muladds( phys
->rb
.v
, grind_delta
, a
*0.2f
, phys
->rb
.v
);
883 VG_STATIC
int player_update_grind_collision( rb_ct
*contact
)
885 struct player_phys
*phys
= &player
.phys
;
888 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, 0.5f
, p0
);
889 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, -0.5f
, p1
);
890 v3_muladds( p0
, phys
->rb
.up
, 0.125f
-0.15f
, p0
);
891 v3_muladds( p1
, phys
->rb
.up
, 0.125f
-0.15f
, p1
);
893 float const k_r
= 0.25f
;
894 struct grind_edge
*closest_edge
= player_grind_collect_edge( p0
, p1
,
898 vg_line( p0
, p1
, 0xff0000ff );
902 vg_line_cross( c0
, 0xff000000, 0.1f
);
903 vg_line_cross( c1
, 0xff000000, 0.1f
);
904 vg_line( c0
, c1
, 0xff000000 );
907 v3_sub( c1
, c0
, delta
);
909 if( v3_dot( delta
, phys
->rb
.up
) > 0.0001f
)
911 contact
->p
= v3_length( delta
);
912 contact
->type
= k_contact_type_edge
;
913 contact
->element_id
= 0;
914 v3_copy( c1
, contact
->co
);
915 contact
->rba
= &player
.phys
.rb
;
916 contact
->rbb
= &world
.rb_geo
;
918 v3f edge_dir
, axis_dir
;
919 v3_sub( closest_edge
->p1
, closest_edge
->p0
, edge_dir
);
920 v3_normalize( edge_dir
);
921 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, edge_dir
, axis_dir
);
922 v3_cross( edge_dir
, axis_dir
, contact
->n
);
925 vg_info( "%f %f\n", v3_length( contact
->n
), contact
->p
);
937 /* Manifold must be able to hold at least 64 elements */
938 VG_STATIC
int player_update_collision_manifold( rb_ct
*manifold
)
940 struct player_phys
*phys
= &player
.phys
;
942 phys
->rise
= vg_lerpf( phys
->rise
, phys
->in_air
? -0.25f
: 0.0f
,
945 rigidbody
*rbf
= &player
.collide_front
,
946 *rbb
= &player
.collide_back
;
948 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
949 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
951 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
952 float h
= player
.air_blend
*0.0f
;
954 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
955 v3_copy( rbf
->co
, rbf
->to_world
[3] );
956 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
957 v3_copy( rbb
->co
, rbb
->to_world
[3] );
959 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
960 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
962 rb_update_bounds( rbf
);
963 rb_update_bounds( rbb
);
965 rb_debug( rbf
, 0xff00ffff );
966 rb_debug( rbb
, 0xffffff00 );
971 len_f
= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
);
972 rb_manifold_filter_coplanar( manifold
, len_f
, 0.05f
);
975 rb_manifold_filter_backface( manifold
, len_f
);
976 rb_manifold_filter_joint_edges( manifold
, len_f
, 0.05f
);
977 rb_manifold_filter_pairs( manifold
, len_f
, 0.05f
);
979 int new_len_f
= rb_manifold_apply_filtered( manifold
, len_f
);
980 if( len_f
&& !new_len_f
)
985 rb_ct
*man_b
= &manifold
[len_f
];
986 len_b
= rb_sphere_scene( rbb
, &world
.rb_geo
, man_b
);
987 rb_manifold_filter_coplanar( man_b
, len_b
, 0.05f
);
990 rb_manifold_filter_backface( man_b
, len_b
);
991 rb_manifold_filter_joint_edges( man_b
, len_b
, 0.05f
);
992 rb_manifold_filter_pairs( man_b
, len_b
, 0.05f
);
994 int new_len_b
= rb_manifold_apply_filtered( man_b
, len_b
);
995 if( len_b
&& !new_len_b
)
1001 * Preprocess collision points, and create a surface picture.
1002 * we want contacts that are within our 'capsule's internal line to be
1003 * clamped so that they face the line and do not oppose, to stop the
1004 * player hanging up on stuff
1006 for( int i
=0; i
<len
; i
++ )
1009 v3_sub( manifold
[i
].co
, rbf
->co
, dfront
);
1010 v3_sub( manifold
[i
].co
, rbb
->co
, dback
);
1012 if( (v3_dot( dfront
, phys
->rb
.forward
) < -0.02f
) &&
1013 (v3_dot( dback
, phys
->rb
.forward
) > 0.02f
))
1015 float p
= v3_dot( manifold
[i
].n
, phys
->rb
.forward
);
1016 v3_muladds( manifold
[i
].n
, phys
->rb
.forward
, -p
, manifold
[i
].n
);
1017 v3_normalize( manifold
[i
].n
);
1022 return len_f
+ len_b
;
1025 VG_STATIC
void player_adhere_ground( rb_ct
*manifold
, int len
)
1027 struct player_phys
*phys
= &player
.phys
;
1028 int was_in_air
= phys
->in_air
;
1031 v3_zero( surface_avg
);
1035 phys
->lift_frames
++;
1037 if( phys
->lift_frames
>= 8 )
1042 for( int i
=0; i
<len
; i
++ )
1043 v3_add( surface_avg
, manifold
[i
].n
, surface_avg
);
1044 v3_normalize( surface_avg
);
1046 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.7f
)
1048 phys
->lift_frames
++;
1050 if( phys
->lift_frames
>= 8 )
1056 phys
->lift_frames
= 0;
1057 v3f projected
, axis
;
1059 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
1060 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
1062 float d
= v3_dot( phys
->rb
.forward
, surface_avg
);
1063 v3_muladds( surface_avg
, phys
->rb
.forward
, -d
, projected
);
1064 v3_normalize( projected
);
1066 float angle
= v3_dot( phys
->rb
.up
, projected
);
1067 v3_cross( phys
->rb
.up
, projected
, axis
);
1070 v3_add( phys
->rb
.co
, projected
, p0
);
1071 v3_add( phys
->rb
.co
, phys
->rb
.up
, p1
);
1072 vg_line( phys
->rb
.co
, p0
, 0xff00ff00 );
1073 vg_line( phys
->rb
.co
, p1
, 0xff000fff );
1075 if( fabsf(angle
) < 0.999f
)
1078 q_axis_angle( correction
, axis
,
1079 acosf(angle
)*4.0f
*VG_TIMESTEP_FIXED
);
1080 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
1085 if( !was_in_air
&& phys
->in_air
)
1089 VG_STATIC
void player_collision_response( rb_ct
*manifold
, int len
)
1091 struct player_phys
*phys
= &player
.phys
;
1093 for( int j
=0; j
<5; j
++ )
1095 for( int i
=0; i
<len
; i
++ )
1097 struct contact
*ct
= &manifold
[i
];
1100 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
1101 v3_cross( phys
->rb
.w
, delta
, dv
);
1102 v3_add( phys
->rb
.v
, dv
, dv
);
1104 float vn
= -v3_dot( dv
, ct
->n
);
1107 float temp
= ct
->norm_impulse
;
1108 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
1109 vn
= ct
->norm_impulse
- temp
;
1112 v3_muls( ct
->n
, vn
, impulse
);
1114 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
1115 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
1121 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
1122 v3_cross( delta
, impulse
, impulse
);
1125 * W Impulses are limited to the Y and X axises, we don't really want
1126 * roll angular velocities being included.
1128 * Can also tweak the resistance of each axis here by scaling the wx,wy
1132 float wy
= v3_dot( phys
->rb
.up
, impulse
) * 0.8f
,
1133 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.0f
;
1135 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
1136 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
1141 VG_STATIC
void player_save_frame(void)
1143 player
.phys_gate_frame
= player
.phys
;
1146 VG_STATIC
void player_restore_frame(void)
1148 player
.phys
= player
.phys_gate_frame
;
1149 rb_update_transform( &player
.phys
.rb
);
1152 VG_STATIC
void player_integrate(void)
1154 struct player_phys
*phys
= &player
.phys
;
1155 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
1156 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
1159 VG_STATIC
void player_do_motion(void)
1161 struct player_phys
*phys
= &player
.phys
;
1163 if( world
.water
.enabled
)
1165 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
1168 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
1169 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1170 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1171 audio_player_playclip( &audio_player_extra
, &audio_splash
);
1179 v3_copy( phys
->rb
.co
, prevco
);
1181 if( phys
->on_board
)
1184 int len
= player_update_collision_manifold( manifold
);
1185 int grind_col
= player_update_grind_collision( &manifold
[len
] );
1187 static int _grind_col_pre
= 0;
1192 v3f up
= { 0.0f
, 1.0f
, 0.0f
};
1193 float angle
= v3_dot( phys
->rb
.up
, up
);
1195 if( fabsf(angle
) < 0.99f
)
1198 v3_cross( phys
->rb
.up
, up
, axis
);
1201 q_axis_angle( correction
, axis
,
1202 VG_TIMESTEP_FIXED
* 10.0f
* acosf(angle
) );
1203 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
1206 float const DOWNFORCE
= -k_downforce
*1.2f
*VG_TIMESTEP_FIXED
;
1207 v3_muladds( phys
->rb
.v
, manifold
[len
].n
, DOWNFORCE
, phys
->rb
.v
);
1208 m3x3_identity( phys
->vr
);
1209 m3x3_identity( phys
->vr_pstep
);
1211 if( !_grind_col_pre
)
1214 audio_player_set_flags( &audio_player_extra
,
1215 AUDIO_FLAG_SPACIAL_3D
);
1216 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1217 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1218 audio_player_playclip( &audio_player_extra
, &audio_board
[5] );
1225 player_adhere_ground( manifold
, len
);
1227 if( _grind_col_pre
)
1230 audio_player_set_flags( &audio_player_extra
,
1231 AUDIO_FLAG_SPACIAL_3D
);
1232 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1233 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1234 audio_player_playclip( &audio_player_extra
, &audio_board
[6] );
1239 _grind_col_pre
= grind_col
;
1241 rb_presolve_contacts( manifold
, len
+ VG_MAX(0,grind_col
) );
1242 player_collision_response( manifold
, len
+ VG_MAX(0,grind_col
) );
1244 player_physics_control_passive();
1249 player_physics_control_grind();
1254 player_physics_control_air();
1256 player_physics_control();
1262 player_walk_physics();
1265 /* Real angular velocity integration */
1266 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
*0.5f
, phys
->rb
.w
);
1267 if( v3_length2( phys
->rb
.w
) > 0.0f
)
1271 v3_copy( phys
->rb
.w
, axis
);
1273 float mag
= v3_length( axis
);
1274 v3_divs( axis
, mag
, axis
);
1275 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
1276 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
1279 /* Faux angular velocity */
1282 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
1283 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
1285 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
1286 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
1290 * Gate intersection, by tracing a line over the gate planes
1292 for( int i
=0; i
<world
.gate_count
; i
++ )
1294 struct route_gate
*rg
= &world
.gates
[i
];
1295 teleport_gate
*gate
= &rg
->gate
;
1297 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
1299 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
1300 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
1301 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
1302 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
1303 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
1304 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
1306 /* Pre-emptively edit the camera matrices so that the motion vectors
1310 m4x3_invert_affine( gate
->transport
, transport_i
);
1311 m4x3_expand( transport_i
, transport_4
);
1312 m4x4_mul( main_camera
.mtx
.pv
, transport_4
, main_camera
.mtx
.pv
);
1313 m4x4_mul( main_camera
.mtx
.v
, transport_4
, main_camera
.mtx
.v
);
1315 v4f transport_rotation
;
1316 m3x3_q( gate
->transport
, transport_rotation
);
1317 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
1319 world_routes_activate_gate( i
);
1321 if( !phys
->on_board
)
1323 v3f fwd_dir
= {cosf(player
.angles
[0]),
1325 sinf(player
.angles
[0])};
1326 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
1328 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
1331 player
.rewind_length
= 0;
1332 player
.rewind_total_length
= 0.0f
;
1333 player
.rewind_incrementer
= 10000;
1334 player_save_frame();
1337 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
1343 rb_update_transform( &phys
->rb
);
1346 VG_STATIC
void player_freecam(void)
1350 float movespeed
= fc_speed
* VG_TIMESTEP_FIXED
;
1351 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
1352 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
1354 m3x3_mulv( main_camera
.transform
, lookdir
, lookdir
);
1355 m3x3_mulv( main_camera
.transform
, sidedir
, sidedir
);
1357 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
1359 v2f steer
= { player
.input_js1h
->axis
.value
,
1360 player
.input_js1v
->axis
.value
};
1362 v3_muladds( move_vel
, sidedir
, movespeed
*steer
[0], move_vel
);
1363 v3_muladds( move_vel
, lookdir
, -movespeed
*steer
[1], move_vel
);
1365 v3_muls( move_vel
, 0.7f
, move_vel
);
1366 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
1369 VG_STATIC
int reset_player( int argc
, char const *argv
[] )
1371 struct player_phys
*phys
= &player
.phys
;
1372 struct respawn_point
*rp
= NULL
, *r
;
1376 for( int i
=0; i
<world
.spawn_count
; i
++ )
1378 r
= &world
.spawns
[i
];
1379 if( !strcmp( r
->name
, argv
[0] ) )
1387 vg_warn( "No spawn named '%s'\n", argv
[0] );
1392 float min_dist
= INFINITY
;
1394 for( int i
=0; i
<world
.spawn_count
; i
++ )
1396 r
= &world
.spawns
[i
];
1397 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
1399 vg_info( "Dist %s : %f\n", r
->name
, d
);
1410 vg_error( "No spawn found\n" );
1411 vg_info( "Player position: %f %f %f\n", player
.phys
.rb
.co
[0],
1412 player
.phys
.rb
.co
[1],
1413 player
.phys
.rb
.co
[2] );
1414 vg_info( "Player velocity: %f %f %f\n", player
.phys
.rb
.v
[0],
1415 player
.phys
.rb
.v
[1],
1416 player
.phys
.rb
.v
[2] );
1418 if( !world
.spawn_count
)
1421 rp
= &world
.spawns
[0];
1427 q_m3x3( rp
->q
, the_long_way
);
1429 v3f delta
= {1.0f
,0.0f
,0.0f
};
1430 m3x3_mulv( the_long_way
, delta
, delta
);
1432 player
.angles
[0] = atan2f( delta
[0], -delta
[2] );
1433 player
.angles
[1] = -asinf( delta
[1] );
1436 v4_copy( rp
->q
, phys
->rb
.q
);
1437 v3_copy( rp
->co
, phys
->rb
.co
);
1438 v3_zero( phys
->rb
.v
);
1440 phys
->vswitch
= 1.0f
;
1441 phys
->slip_last
= 0.0f
;
1444 m3x3_identity( phys
->vr
);
1446 player
.mdl
.shoes
[0] = 1;
1447 player
.mdl
.shoes
[1] = 1;
1449 rb_update_transform( &phys
->rb
);
1450 player_save_frame();
1454 #endif /* PLAYER_PHYSICS_H */