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 * Called when launching into the air to predict and adjust trajectories
65 VG_STATIC
void player_start_air(void)
67 struct player_phys
*phys
= &player
.phys
;
69 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
70 float best_velocity_delta
= -9999.9f
;
74 v3_cross( phys
->rb
.up
, phys
->rb
.v
, axis
);
76 player
.land_log_count
= 0;
78 m3x3_identity( phys
->vr
);
80 for( int m
=-3;m
<=12; m
++ )
82 struct land_log
*log
= &player
.land_log
[ player
.land_log_count
++ ];
84 log
->colour
= 0xff000000;
86 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
89 v3_copy( phys
->rb
.co
, pco
);
90 v3_muls( phys
->rb
.v
, k_bias
, pv
);
93 * Try different 'rotations' of the velocity to find the best possible
94 * landing normal. This conserves magnitude at the expense of slightly
101 q_axis_angle( vr_q
, axis
, vmod
);
104 m3x3_mulv( vr
, pv
, pv
);
105 v3_muladds( pco
, pv
, pstep
, pco
);
107 struct grind_edge
*best_grind
= NULL
;
108 float closest_grind
= INFINITY
;
110 for( int i
=0; i
<50; i
++ )
112 v3_copy( pco
, pco1
);
113 apply_gravity( pv
, pstep
);
115 m3x3_mulv( vr
, pv
, pv
);
116 v3_muladds( pco
, pv
, pstep
, pco
);
121 v3_sub( pco
, pco1
, vdir
);
122 contact
.dist
= v3_length( vdir
);
123 v3_divs( vdir
, contact
.dist
, vdir
);
126 struct grind_edge
*ge
= player_grind_collect_edge( pco
, pco1
,
129 if( ge
&& (v3_dot((v3f
){0.0f
,1.0f
,0.0f
},vdir
) < -0.2f
) )
131 float d2
= v3_dist2( c0
, c1
);
132 if( d2
< closest_grind
)
139 if( ray_world( pco1
, vdir
, &contact
))
141 float land_delta
= v3_dot( pv
, contact
.normal
);
142 u32 scolour
= (u8
)(vg_minf(-land_delta
* 2.0f
, 255.0f
));
144 /* Bias prediction towords ramps */
145 if( ray_hit_material( &contact
)->info
.flags
146 & k_material_flag_skate_surface
)
149 scolour
|= 0x0000a000;
152 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
154 best_velocity_delta
= land_delta
;
156 v3_copy( contact
.pos
, player
.land_target
);
158 m3x3_copy( vr
, phys
->vr_pstep
);
159 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
160 q_m3x3( vr_q
, phys
->vr
);
163 v3_copy( contact
.pos
, log
->positions
[ log
->count
++ ] );
164 log
->colour
= 0xff000000 | scolour
;
168 v3_copy( pco
, log
->positions
[ log
->count
++ ] );
173 log
->colour
= 0xff0000ff;
175 float score
= -closest_grind
* 0.05f
;
177 if( score
> best_velocity_delta
)
179 best_velocity_delta
= score
;
181 m3x3_copy( vr
, phys
->vr_pstep
);
182 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
183 q_m3x3( vr_q
, phys
->vr
);
190 VG_STATIC
void player_physics_control_passive(void)
192 struct player_phys
*phys
= &player
.phys
;
193 float grabt
= player
.input_grab
->axis
.value
;
197 v2_muladds( phys
->grab_mouse_delta
, vg
.mouse_delta
, 0.02f
,
198 phys
->grab_mouse_delta
);
199 v2_normalize_clamp( phys
->grab_mouse_delta
);
202 v2_zero( phys
->grab_mouse_delta
);
204 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
205 player
.phys
.pushing
= 0.0f
;
207 if( !phys
->jump_charge
|| phys
->in_air
)
209 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
212 phys
->jump_charge
= 0;
213 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
217 * Main friction interface model
219 VG_STATIC
void player_physics_control(void)
221 struct player_phys
*phys
= &player
.phys
;
224 * Computing localized friction forces for controlling the character
225 * Friction across X is significantly more than Z
229 m3x3_mulv( phys
->rb
.to_local
, phys
->rb
.v
, vel
);
232 if( fabsf(vel
[2]) > 0.01f
)
233 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
235 if( fabsf( slip
) > 1.2f
)
236 slip
= vg_signf( slip
) * 1.2f
;
238 phys
->reverse
= -vg_signf(vel
[2]);
240 float substep
= VG_TIMESTEP_FIXED
* 0.2f
;
241 float fwd_resistance
= k_friction_resistance
;
243 for( int i
=0; i
<5; i
++ )
245 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
246 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
249 if( player
.input_jump
->button
.value
)
251 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
253 if( !phys
->jump_charge
)
254 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
256 phys
->jump_charge
= 1;
259 static int push_thresh_last
= 0;
260 float push
= player
.input_push
->button
.value
;
261 int push_thresh
= push
>0.15f
? 1: 0;
263 if( push_thresh
&& !push_thresh_last
)
264 player
.phys
.start_push
= vg
.time
;
266 push_thresh_last
= push_thresh
;
268 if( !player
.input_jump
->button
.value
&& push_thresh
)
270 player
.phys
.pushing
= 1.0f
;
271 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
273 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
274 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
275 current
= v3_length( vel
),
276 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
278 new_vel
-= vg_minf(current
, k_max_push_speed
);
279 vel
[2] -= new_vel
* phys
->reverse
;
282 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
284 float input
= player
.input_js1h
->axis
.value
,
285 grab
= player
.input_grab
->axis
.value
,
286 steer
= input
* (1.0f
-(phys
->jump
+grab
)*0.4f
),
287 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
289 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
291 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
295 /* Launch more up if alignment is up else improve velocity */
296 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
298 dir
= mod
+ aup
*(1.0f
-mod
);
300 v3_copy( phys
->rb
.v
, jumpdir
);
301 v3_normalize( jumpdir
);
302 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
303 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
304 v3_normalize( jumpdir
);
306 float force
= k_jump_force
*phys
->jump
;
307 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
310 player
.jump_time
= vg
.time
;
312 /* TODO: Move to audio file */
314 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
315 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
316 audio_player_set_vol( &audio_player_extra
, 20.0f
);
317 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%2] );
322 VG_STATIC
void player_physics_control_grind(void)
324 struct player_phys
*phys
= &player
.phys
;
325 v2f steer
= { player
.input_js1h
->axis
.value
,
326 player
.input_js1v
->axis
.value
};
328 float l2
= v2_length2( steer
);
330 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
332 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
334 float iX
= steer
[1] * phys
->reverse
* k_steer_air
* VG_TIMESTEP_FIXED
;
336 static float siX
= 0.0f
;
337 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
340 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
341 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
347 * Air control, no real physics
349 VG_STATIC
void player_physics_control_air(void)
351 struct player_phys
*phys
= &player
.phys
;
353 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
354 vg_line_cross( player
.land_target
, 0xff0000ff, 0.25f
);
361 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
364 v3_copy( phys
->rb
.co
, pco
);
365 v3_copy( phys
->rb
.v
, pv
);
367 float time_to_impact
= 0.0f
;
368 float limiter
= 1.0f
;
370 struct grind_edge
*best_grind
= NULL
;
371 float closest_grind
= INFINITY
;
373 v3f target_normal
= { 0.0f
, 1.0f
, 0.0f
};
376 for( int i
=0; i
<50; i
++ )
378 v3_copy( pco
, pco1
);
379 m3x3_mulv( phys
->vr_pstep
, pv
, pv
);
380 apply_gravity( pv
, pstep
);
381 v3_muladds( pco
, pv
, pstep
, pco
);
386 v3_sub( pco
, pco1
, vdir
);
387 contact
.dist
= v3_length( vdir
);
388 v3_divs( vdir
, contact
.dist
, vdir
);
391 struct grind_edge
*ge
= player_grind_collect_edge( pco
, pco1
,
394 if( ge
&& (v3_dot((v3f
){0.0f
,1.0f
,0.0f
},vdir
) < -0.2f
) )
396 vg_line( ge
->p0
, ge
->p1
, 0xff0000ff );
397 vg_line_cross( pco
, 0xff0000ff, 0.25f
);
402 float orig_dist
= contact
.dist
;
403 if( ray_world( pco1
, vdir
, &contact
) )
405 v3_copy( contact
.normal
, target_normal
);
407 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
408 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
411 time_to_impact
+= pstep
;
416 float angle
= v3_dot( phys
->rb
.up
, target_normal
);
418 v3_cross( phys
->rb
.up
, target_normal
, axis
);
420 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
421 limiter
= 1.0f
-limiter
;
423 limiter
= 1.0f
-limiter
;
425 if( fabsf(angle
) < 0.99f
)
428 q_axis_angle( correction
, axis
,
429 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
430 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
434 v2f steer
= { player
.input_js1h
->axis
.value
,
435 player
.input_js1v
->axis
.value
};
437 float l2
= v2_length2( steer
);
439 v2_muls( steer
, 1.0f
/sqrtf(l2
), steer
);
441 phys
->iY
-= steer
[0] * k_steer_air
* VG_TIMESTEP_FIXED
;
443 float iX
= steer
[1] *
444 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
446 static float siX
= 0.0f
;
447 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
450 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
451 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
454 v2f target
= {0.0f
,0.0f
};
455 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
456 phys
->grab
, target
);
460 VG_STATIC
void player_walk_update_collision(void)
462 struct player_phys
*phys
= &player
.phys
;
466 rigidbody
*rbf
= &player
.collide_front
,
467 *rbb
= &player
.collide_back
;
469 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
470 v3_add( phys
->rb
.co
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
471 v3_copy( rbf
->co
, rbf
->to_world
[3] );
472 v3_copy( rbb
->co
, rbb
->to_world
[3] );
473 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
474 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
476 rb_update_bounds( rbf
);
477 rb_update_bounds( rbb
);
480 VG_STATIC
void player_integrate(void);
482 * Entire Walking physics model
483 * TODO: sleep when under certain velotiy
485 VG_STATIC
void player_walk_physics(void)
487 struct player_phys
*phys
= &player
.phys
;
488 rigidbody
*rbf
= &player
.collide_front
,
489 *rbb
= &player
.collide_back
;
491 m3x3_identity( player
.collide_front
.to_world
);
492 m3x3_identity( player
.collide_back
.to_world
);
494 v3_zero( phys
->rb
.w
);
495 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
500 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
501 v3f right_dir
= { -forward_dir
[2], 0.0f
, forward_dir
[0] };
503 v2f walk
= { player
.input_walkh
->axis
.value
,
504 player
.input_walkv
->axis
.value
};
506 if( v2_length2(walk
) > 0.001f
)
507 v2_normalize_clamp( walk
);
511 player_walk_update_collision();
512 rb_debug( rbf
, 0xff0000ff );
513 rb_debug( rbb
, 0xff0000ff );
515 /* allow player to accelerate a bit */
517 v3_muls( forward_dir
, walk
[1], walk_3d
);
518 v3_muladds( walk_3d
, right_dir
, walk
[0], walk_3d
);
520 float current_vel
= fabsf(v3_dot( walk_3d
, phys
->rb
.v
)),
521 new_vel
= current_vel
+ VG_TIMESTEP_FIXED
*k_air_accelerate
,
522 clamped_new
= vg_clampf( new_vel
, 0.0f
, k_walkspeed
),
523 vel_diff
= vg_maxf( 0.0f
, clamped_new
- current_vel
);
525 v3_muladds( phys
->rb
.v
, right_dir
, walk
[0] * vel_diff
, phys
->rb
.v
);
526 v3_muladds( phys
->rb
.v
, forward_dir
, walk
[1] * vel_diff
, phys
->rb
.v
);
530 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
531 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
532 rb_presolve_contacts( manifold
, len
);
534 for( int i
=0; i
<len
; i
++ )
536 struct contact
*ct
= &manifold
[i
];
537 if( v3_dot( ct
->n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
)
541 for( int j
=0; j
<5; j
++ )
543 for( int i
=0; i
<len
; i
++ )
545 struct contact
*ct
= &manifold
[i
];
548 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
551 float temp
= ct
->norm_impulse
;
552 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
553 vn
= ct
->norm_impulse
- temp
;
556 v3_muls( ct
->n
, vn
, impulse
);
558 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
561 for( int j
=0; j
<2; j
++ )
563 float f
= k_friction
* ct
->norm_impulse
,
564 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
567 float temp
= ct
->tangent_impulse
[j
];
568 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
569 lambda
= ct
->tangent_impulse
[j
] - temp
;
571 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
580 player
.walk
= v2_length( walk
);
582 if( player
.input_walk
->button
.value
)
583 v2_muls( walk
, 0.5f
, walk
);
585 v2_muls( walk
, k_walkspeed
* VG_TIMESTEP_FIXED
, walk
);
588 v3_zero( walk_apply
);
590 /* Do XY translation */
591 v3_muladds( walk_apply
, right_dir
, walk
[0], walk_apply
);
592 v3_muladds( walk_apply
, forward_dir
, walk
[1], walk_apply
);
593 v3_add( walk_apply
, phys
->rb
.co
, phys
->rb
.co
);
594 v3_divs( walk_apply
, VG_TIMESTEP_FIXED
, phys
->rb
.v
);
596 /* Directly resolve collisions */
597 player_walk_update_collision();
598 rb_debug( rbf
, 0xffffff00 );
599 rb_debug( rbb
, 0xffffff00 );
602 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
603 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
607 for( int j
=0; j
<3; j
++ )
609 for( int i
=0; i
<len
; i
++ )
611 struct contact
*ct
= &manifold
[i
];
613 float p
= vg_maxf( 0.0f
, ct
->p
- 0.00f
),
614 cur
= vg_clampf( v3_dot( ct
->n
, dt
), 0.0f
, p
);
615 v3_muladds( dt
, ct
->n
, (p
- cur
) * 0.333333333f
, dt
);
618 v3_add( dt
, phys
->rb
.co
, phys
->rb
.co
);
622 struct world_material
*surface_mat
= world_contact_material(manifold
);
623 player
.surface_prop
= surface_mat
->info
.surface_prop
;
627 if( player
.input_jump
->button
.value
)
629 phys
->rb
.v
[1] = 5.0f
;
634 /* if we've put us in the air, step down slowly */
636 float max_dist
= 0.3f
,
637 start_y
= phys
->rb
.co
[1];
639 for( int j
=0; j
<8; j
++ )
641 for( int i
=0; i
<len
; i
++ )
643 struct contact
*ct
= &manifold
[i
];
644 if( v3_dot( ct
->n
, (v3f
){0.0f
,1.0f
,0.0f
} ) > 0.5f
)
652 for( int j
=0; j
<3; j
++ )
654 for( int i
=0; i
<len
; i
++ )
656 struct contact
*ct
= &manifold
[i
];
658 float p
= vg_maxf( 0.0f
, ct
->p
- 0.0025f
),
659 cur
= vg_clampf( v3_dot( ct
->n
, dt
), 0.0f
, p
);
660 v3_muladds( dt
, ct
->n
, (p
- cur
) * 0.333333333f
, dt
);
663 v3_add( dt
, phys
->rb
.co
, phys
->rb
.co
);
668 phys
->rb
.co
[1] -= max_dist
* 0.125f
;
670 player_walk_update_collision();
672 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
673 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
676 /* Transitioning into air mode */
677 phys
->rb
.co
[1] = start_y
;
681 VG_STATIC
void player_grind(void)
683 struct player_phys
*phys
= &player
.phys
;
686 int idx
= bh_closest_point( world
.grind_bh
, phys
->rb
.co
, closest
, INFINITY
);
690 struct grind_edge
*edge
= &world
.grind_edges
[ idx
];
692 vg_line( phys
->rb
.co
, closest
, 0xff000000 );
693 vg_line_cross( closest
, 0xff000000, 0.3f
);
694 vg_line( edge
->p0
, edge
->p1
, 0xff000000 );
697 v3_sub( closest
, phys
->rb
.co
, grind_delta
);
699 float p
= v3_dot( phys
->rb
.forward
, grind_delta
);
700 v3_muladds( grind_delta
, phys
->rb
.forward
, -p
, grind_delta
);
702 float a
= vg_maxf( 0.0f
, 4.0f
-v3_dist2( closest
, phys
->rb
.co
) );
703 v3_muladds( phys
->rb
.v
, grind_delta
, a
*0.2f
, phys
->rb
.v
);
706 VG_STATIC
int player_update_grind_collision( rb_ct
*contact
)
708 struct player_phys
*phys
= &player
.phys
;
711 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, 0.5f
, p0
);
712 v3_muladds( phys
->rb
.co
, phys
->rb
.forward
, -0.5f
, p1
);
713 v3_muladds( p0
, phys
->rb
.up
, 0.125f
, p0
);
714 v3_muladds( p1
, phys
->rb
.up
, 0.125f
, p1
);
716 float const k_r
= 0.25f
;
717 struct grind_edge
*closest_edge
= player_grind_collect_edge( p0
, p1
,
721 vg_line( p0
, p1
, 0xff0000ff );
725 vg_line_cross( c0
, 0xff000000, 0.1f
);
726 vg_line_cross( c1
, 0xff000000, 0.1f
);
727 vg_line( c0
, c1
, 0xff000000 );
730 v3_sub( c1
, c0
, delta
);
732 if( v3_dot( delta
, phys
->rb
.up
) > 0.0001f
)
734 contact
->p
= v3_length( delta
);
735 contact
->type
= k_contact_type_edge
;
736 contact
->element_id
= 0;
737 v3_copy( c1
, contact
->co
);
738 contact
->rba
= &player
.phys
.rb
;
739 contact
->rbb
= &world
.rb_geo
;
741 v3f edge_dir
, axis_dir
;
742 v3_sub( closest_edge
->p1
, closest_edge
->p0
, edge_dir
);
743 v3_normalize( edge_dir
);
744 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, edge_dir
, axis_dir
);
745 v3_cross( edge_dir
, axis_dir
, contact
->n
);
747 vg_info( "%f %f\n", v3_length( contact
->n
), contact
->p
);
758 /* Manifold must be able to hold at least 64 elements */
759 VG_STATIC
int player_update_collision_manifold( rb_ct
*manifold
)
761 struct player_phys
*phys
= &player
.phys
;
763 rigidbody
*rbf
= &player
.collide_front
,
764 *rbb
= &player
.collide_back
;
766 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
767 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
769 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
770 float h
= player
.air_blend
*0.2f
;
772 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
773 v3_copy( rbf
->co
, rbf
->to_world
[3] );
774 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
775 v3_copy( rbb
->co
, rbb
->to_world
[3] );
777 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
778 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
780 rb_update_bounds( rbf
);
781 rb_update_bounds( rbb
);
783 rb_debug( rbf
, 0xff00ffff );
784 rb_debug( rbb
, 0xffffff00 );
789 len_f
= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
);
790 rb_manifold_filter_coplanar( manifold
, len_f
, 0.05f
);
793 rb_manifold_filter_backface( manifold
, len_f
);
794 rb_manifold_filter_joint_edges( manifold
, len_f
, 0.05f
);
795 rb_manifold_filter_pairs( manifold
, len_f
, 0.05f
);
797 len_f
= rb_manifold_apply_filtered( manifold
, len_f
);
799 rb_ct
*man_b
= &manifold
[len_f
];
800 len_b
= rb_sphere_scene( rbb
, &world
.rb_geo
, man_b
);
801 rb_manifold_filter_coplanar( man_b
, len_b
, 0.05f
);
804 rb_manifold_filter_backface( man_b
, len_b
);
805 rb_manifold_filter_joint_edges( man_b
, len_b
, 0.05f
);
806 rb_manifold_filter_pairs( man_b
, len_b
, 0.05f
);
808 len_b
= rb_manifold_apply_filtered( man_b
, len_b
);
812 * Preprocess collision points, and create a surface picture.
813 * we want contacts that are within our 'capsule's internal line to be
814 * clamped so that they face the line and do not oppose, to stop the
815 * player hanging up on stuff
817 for( int i
=0; i
<len
; i
++ )
820 v3_sub( manifold
[i
].co
, rbf
->co
, dfront
);
821 v3_sub( manifold
[i
].co
, rbb
->co
, dback
);
823 if( (v3_dot( dfront
, phys
->rb
.forward
) < -0.02f
) &&
824 (v3_dot( dback
, phys
->rb
.forward
) > 0.02f
))
826 float p
= v3_dot( manifold
[i
].n
, phys
->rb
.forward
);
827 v3_muladds( manifold
[i
].n
, phys
->rb
.forward
, -p
, manifold
[i
].n
);
828 v3_normalize( manifold
[i
].n
);
833 return len_f
+ len_b
;
836 VG_STATIC
void player_adhere_ground( rb_ct
*manifold
, int len
)
838 struct player_phys
*phys
= &player
.phys
;
839 int was_in_air
= phys
->in_air
;
842 v3_zero( surface_avg
);
846 phys
->lift_frames
++;
848 if( phys
->lift_frames
>= 8 )
853 for( int i
=0; i
<len
; i
++ )
854 v3_add( surface_avg
, manifold
[i
].n
, surface_avg
);
855 v3_normalize( surface_avg
);
857 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.7f
)
859 phys
->lift_frames
++;
861 if( phys
->lift_frames
>= 8 )
867 phys
->lift_frames
= 0;
870 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
871 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
873 float d
= v3_dot( phys
->rb
.forward
, surface_avg
);
874 v3_muladds( surface_avg
, phys
->rb
.forward
, -d
, projected
);
875 v3_normalize( projected
);
877 float angle
= v3_dot( phys
->rb
.up
, projected
);
878 v3_cross( phys
->rb
.up
, projected
, axis
);
881 v3_add( phys
->rb
.co
, projected
, p0
);
882 v3_add( phys
->rb
.co
, phys
->rb
.up
, p1
);
883 vg_line( phys
->rb
.co
, p0
, 0xff00ff00 );
884 vg_line( phys
->rb
.co
, p1
, 0xff000fff );
886 if( fabsf(angle
) < 0.999f
)
889 q_axis_angle( correction
, axis
,
890 acosf(angle
)*4.0f
*VG_TIMESTEP_FIXED
);
891 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
896 if( !was_in_air
&& phys
->in_air
)
900 VG_STATIC
void player_collision_response( rb_ct
*manifold
, int len
)
902 struct player_phys
*phys
= &player
.phys
;
904 for( int j
=0; j
<5; j
++ )
906 for( int i
=0; i
<len
; i
++ )
908 struct contact
*ct
= &manifold
[i
];
911 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
912 v3_cross( phys
->rb
.w
, delta
, dv
);
913 v3_add( phys
->rb
.v
, dv
, dv
);
915 float vn
= -v3_dot( dv
, ct
->n
);
918 float temp
= ct
->norm_impulse
;
919 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
920 vn
= ct
->norm_impulse
- temp
;
923 v3_muls( ct
->n
, vn
, impulse
);
925 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
926 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
932 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
933 v3_cross( delta
, impulse
, impulse
);
936 * W Impulses are limited to the Y and X axises, we don't really want
937 * roll angular velocities being included.
939 * Can also tweak the resistance of each axis here by scaling the wx,wy
943 float wy
= v3_dot( phys
->rb
.up
, impulse
),
944 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.8f
;
946 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
947 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
952 VG_STATIC
void player_save_frame(void)
954 player
.phys_gate_frame
= player
.phys
;
957 VG_STATIC
void player_restore_frame(void)
959 player
.phys
= player
.phys_gate_frame
;
960 rb_update_transform( &player
.phys
.rb
);
963 VG_STATIC
void player_integrate(void)
965 struct player_phys
*phys
= &player
.phys
;
966 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
967 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
970 VG_STATIC
void player_do_motion(void)
972 struct player_phys
*phys
= &player
.phys
;
974 if( world
.water
.enabled
)
976 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
979 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
980 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
981 audio_player_set_vol( &audio_player_extra
, 20.0f
);
982 audio_player_playclip( &audio_player_extra
, &audio_splash
);
990 v3_copy( phys
->rb
.co
, prevco
);
995 int len
= player_update_collision_manifold( manifold
);
996 int grind_col
= player_update_grind_collision( &manifold
[len
] );
998 static int _grind_col_pre
= 0;
1003 v3f up
= { 0.0f
, 1.0f
, 0.0f
};
1004 float angle
= v3_dot( phys
->rb
.up
, up
);
1006 if( fabsf(angle
) < 0.99f
)
1009 v3_cross( phys
->rb
.up
, up
, axis
);
1012 q_axis_angle( correction
, axis
,
1013 VG_TIMESTEP_FIXED
* 10.0f
* acosf(angle
) );
1014 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
1017 float const DOWNFORCE
= -k_downforce
*1.2f
*VG_TIMESTEP_FIXED
;
1018 v3_muladds( phys
->rb
.v
, manifold
[len
].n
, DOWNFORCE
, phys
->rb
.v
);
1019 m3x3_identity( phys
->vr
);
1020 m3x3_identity( phys
->vr_pstep
);
1022 if( !_grind_col_pre
)
1025 audio_player_set_flags( &audio_player_extra
,
1026 AUDIO_FLAG_SPACIAL_3D
);
1027 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1028 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1029 audio_player_playclip( &audio_player_extra
, &audio_board
[5] );
1036 player_adhere_ground( manifold
, len
);
1038 if( _grind_col_pre
)
1041 audio_player_set_flags( &audio_player_extra
,
1042 AUDIO_FLAG_SPACIAL_3D
);
1043 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
1044 audio_player_set_vol( &audio_player_extra
, 20.0f
);
1045 audio_player_playclip( &audio_player_extra
, &audio_board
[6] );
1050 _grind_col_pre
= grind_col
;
1052 rb_presolve_contacts( manifold
, len
+ VG_MAX(0,grind_col
) );
1053 player_collision_response( manifold
, len
+ VG_MAX(0,grind_col
) );
1055 player_physics_control_passive();
1060 player_physics_control_grind();
1065 player_physics_control_air();
1067 player_physics_control();
1073 player_walk_physics();
1076 /* Real angular velocity integration */
1077 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, phys
->rb
.w
);
1078 if( v3_length2( phys
->rb
.w
) > 0.0f
)
1082 v3_copy( phys
->rb
.w
, axis
);
1084 float mag
= v3_length( axis
);
1085 v3_divs( axis
, mag
, axis
);
1086 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
1087 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
1090 /* Faux angular velocity */
1093 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
1094 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
1096 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
1097 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
1101 * Gate intersection, by tracing a line over the gate planes
1103 for( int i
=0; i
<world
.gate_count
; i
++ )
1105 struct route_gate
*rg
= &world
.gates
[i
];
1106 teleport_gate
*gate
= &rg
->gate
;
1108 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
1110 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
1111 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
1112 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
1113 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
1114 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
1115 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
1117 /* Pre-emptively edit the camera matrices so that the motion vectors
1121 m4x3_invert_affine( gate
->transport
, transport_i
);
1122 m4x3_expand( transport_i
, transport_4
);
1123 m4x4_mul( main_camera
.mtx
.pv
, transport_4
, main_camera
.mtx
.pv
);
1124 m4x4_mul( main_camera
.mtx
.v
, transport_4
, main_camera
.mtx
.v
);
1126 v4f transport_rotation
;
1127 m3x3_q( gate
->transport
, transport_rotation
);
1128 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
1130 world_routes_activate_gate( i
);
1132 if( !phys
->on_board
)
1134 v3f fwd_dir
= {cosf(player
.angles
[0]),
1136 sinf(player
.angles
[0])};
1137 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
1139 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
1142 player
.rewind_length
= 0;
1143 player
.rewind_total_length
= 0.0f
;
1144 player
.rewind_incrementer
= 10000;
1145 player_save_frame();
1148 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
1154 rb_update_transform( &phys
->rb
);
1157 VG_STATIC
void player_freecam(void)
1161 float movespeed
= fc_speed
;
1162 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
1163 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
1165 m3x3_mulv( main_camera
.transform
, lookdir
, lookdir
);
1166 m3x3_mulv( main_camera
.transform
, sidedir
, sidedir
);
1168 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
1172 if( vg_get_button( "forward" ) )
1173 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
1174 if( vg_get_button( "back" ) )
1175 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
1176 if( vg_get_button( "left" ) )
1177 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
1178 if( vg_get_button( "right" ) )
1179 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
1182 v3_muls( move_vel
, 0.7f
, move_vel
);
1183 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
1186 VG_STATIC
int reset_player( int argc
, char const *argv
[] )
1188 struct player_phys
*phys
= &player
.phys
;
1189 struct respawn_point
*rp
= NULL
, *r
;
1193 for( int i
=0; i
<world
.spawn_count
; i
++ )
1195 r
= &world
.spawns
[i
];
1196 if( !strcmp( r
->name
, argv
[0] ) )
1204 vg_warn( "No spawn named '%s'\n", argv
[0] );
1209 float min_dist
= INFINITY
;
1211 for( int i
=0; i
<world
.spawn_count
; i
++ )
1213 r
= &world
.spawns
[i
];
1214 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
1216 vg_info( "Dist %s : %f\n", r
->name
, d
);
1227 vg_error( "No spawn found\n" );
1228 vg_info( "Player position: %f %f %f\n", player
.phys
.rb
.co
[0],
1229 player
.phys
.rb
.co
[1],
1230 player
.phys
.rb
.co
[2] );
1231 vg_info( "Player velocity: %f %f %f\n", player
.phys
.rb
.v
[0],
1232 player
.phys
.rb
.v
[1],
1233 player
.phys
.rb
.v
[2] );
1235 if( !world
.spawn_count
)
1238 rp
= &world
.spawns
[0];
1244 q_m3x3( rp
->q
, the_long_way
);
1246 v3f delta
= {1.0f
,0.0f
,0.0f
};
1247 m3x3_mulv( the_long_way
, delta
, delta
);
1249 player
.angles
[0] = atan2f( delta
[0], -delta
[2] );
1250 player
.angles
[1] = -asinf( delta
[1] );
1253 v4_copy( rp
->q
, phys
->rb
.q
);
1254 v3_copy( rp
->co
, phys
->rb
.co
);
1255 v3_zero( phys
->rb
.v
);
1257 phys
->vswitch
= 1.0f
;
1258 phys
->slip_last
= 0.0f
;
1261 m3x3_identity( phys
->vr
);
1263 player
.mdl
.shoes
[0] = 1;
1264 player
.mdl
.shoes
[1] = 1;
1266 rb_update_transform( &phys
->rb
);
1267 player_save_frame();
1271 #endif /* PLAYER_PHYSICS_H */