2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
5 #ifndef PLAYER_PHYSICS_H
6 #define PLAYER_PHYSICS_H
10 static void apply_gravity( v3f vel
, float const timestep
)
12 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
13 v3_muladds( vel
, gravity
, timestep
, vel
);
17 * Called when launching into the air to predict and adjust trajectories
19 static void player_start_air(void)
21 struct player_phys
*phys
= &player
.phys
;
28 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
29 float best_velocity_delta
= -9999.9f
;
33 v3_cross( phys
->rb
.up
, phys
->rb
.v
, axis
);
35 player
.land_log_count
= 0;
37 m3x3_identity( phys
->vr
);
39 for( int m
=-3;m
<=12; m
++ )
41 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
44 v3_copy( phys
->rb
.co
, pco
);
45 v3_muls( phys
->rb
.v
, k_bias
, pv
);
48 * Try different 'rotations' of the velocity to find the best possible
49 * landing normal. This conserves magnitude at the expense of slightly
56 q_axis_angle( vr_q
, axis
, vmod
);
59 m3x3_mulv( vr
, pv
, pv
);
60 v3_muladds( pco
, pv
, pstep
, pco
);
62 for( int i
=0; i
<50; i
++ )
65 apply_gravity( pv
, pstep
);
67 m3x3_mulv( vr
, pv
, pv
);
68 v3_muladds( pco
, pv
, pstep
, pco
);
73 v3_sub( pco
, pco1
, vdir
);
74 contact
.dist
= v3_length( vdir
);
75 v3_divs( vdir
, contact
.dist
, vdir
);
77 if( ray_world( pco1
, vdir
, &contact
))
79 float land_delta
= v3_dot( pv
, contact
.normal
);
80 u32 scolour
= (u8
)(vg_minf(-land_delta
* 2.0f
, 255.0f
));
82 /* Bias prediction towords ramps */
83 if( ray_hit_is_ramp( &contact
) )
86 scolour
|= 0x0000a000;
89 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
91 best_velocity_delta
= land_delta
;
93 v3_copy( contact
.pos
, player
.land_target
);
95 m3x3_copy( vr
, phys
->vr_pstep
);
96 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
97 q_m3x3( vr_q
, phys
->vr
);
100 v3_copy( contact
.pos
,
101 player
.land_target_log
[player
.land_log_count
] );
102 player
.land_target_colours
[player
.land_log_count
] =
103 0xff000000 | scolour
;
105 player
.land_log_count
++;
114 * Main friction interface model
116 static void player_physics_control(void)
118 struct player_phys
*phys
= &player
.phys
;
121 * Computing localized friction forces for controlling the character
122 * Friction across X is significantly more than Z
126 m3x3_mulv( phys
->rb
.to_local
, phys
->rb
.v
, vel
);
129 if( fabsf(vel
[2]) > 0.01f
)
130 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
132 if( fabsf( slip
) > 1.2f
)
133 slip
= vg_signf( slip
) * 1.2f
;
135 phys
->reverse
= -vg_signf(vel
[2]);
137 float substep
= VG_TIMESTEP_FIXED
* 0.2f
;
140 float fwd_resistance
= vg_get_button( "break" )? 5.0f
: k_friction_resistance
;
142 float fwd_resistance
= k_friction_resistance
;
145 for( int i
=0; i
<5; i
++ )
147 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
148 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
151 if( vg_get_button( "jump" ) )
153 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
155 if( !phys
->jump_charge
)
156 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
158 phys
->jump_charge
= 1;
161 static int push_thresh_last
= 0;
162 float push_amt
= vg_get_axis( "walk/push" ) * 0.5f
+ 0.5f
;
163 int push_thresh
= push_amt
>0.15f
? 1: 0;
165 if( push_thresh
&& !push_thresh_last
)
166 player
.phys
.start_push
= vg
.time
;
168 push_thresh_last
= push_thresh
;
170 if( !vg_get_button("break") && push_thresh
)
172 player
.phys
.pushing
= 1.0f
;
173 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
175 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
176 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
177 current
= v3_length( vel
),
178 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
180 new_vel
-= vg_minf(current
, k_max_push_speed
);
181 vel
[2] -= new_vel
* phys
->reverse
;
185 static float previous
= 0.0f
;
186 float delta
= previous
- phys
->grab
,
187 pump
= delta
* k_pump_force
* VG_TIMESTEP_FIXED
;
188 previous
= phys
->grab
;
191 v3_muladds( phys
->rb
.co
, phys
->rb
.up
, pump
, p1
);
192 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
197 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
199 float steer
= vg_get_axis( "lookh" ),
200 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
202 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
206 * Air control, no real physics
208 static void player_physics_control_air(void)
210 struct player_phys
*phys
= &player
.phys
;
212 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
213 vg_line_cross( player
.land_target
, 0xff0000ff, 0.25f
);
220 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
223 v3_copy( phys
->rb
.co
, pco
);
224 v3_copy( phys
->rb
.v
, pv
);
226 float time_to_impact
= 0.0f
;
227 float limiter
= 1.0f
;
229 for( int i
=0; i
<50; i
++ )
231 v3_copy( pco
, pco1
);
232 m3x3_mulv( phys
->vr_pstep
, pv
, pv
);
233 apply_gravity( pv
, pstep
);
234 v3_muladds( pco
, pv
, pstep
, pco
);
239 v3_sub( pco
, pco1
, vdir
);
240 contact
.dist
= v3_length( vdir
);
241 v3_divs( vdir
, contact
.dist
, vdir
);
243 float orig_dist
= contact
.dist
;
244 if( ray_world( pco1
, vdir
, &contact
))
246 float angle
= v3_dot( phys
->rb
.up
, contact
.normal
);
248 v3_cross( phys
->rb
.up
, contact
.normal
, axis
);
250 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
251 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
252 limiter
= 1.0f
-limiter
;
254 limiter
= 1.0f
-limiter
;
259 q_axis_angle( correction
, axis
,
260 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
261 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
264 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
267 time_to_impact
+= pstep
;
270 phys
->iY
-= vg_get_axis( "lookh" ) * k_steer_air
* VG_TIMESTEP_FIXED
;
272 float iX
= vg_get_axis( "lookv" ) *
273 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
275 static float siX
= 0.0f
;
276 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
279 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
280 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
283 v2f target
= {0.0f
,0.0f
};
284 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
285 phys
->grab
, target
);
289 * Entire Walking physics model
290 * TODO: sleep when under certain velotiy
292 static void player_walk_physics(void)
294 struct player_phys
*phys
= &player
.phys
;
295 rigidbody
*rbf
= &player
.collide_front
,
296 *rbb
= &player
.collide_back
;
298 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
299 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
304 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
305 v3_copy( rbf
->co
, rbf
->to_world
[3] );
306 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
307 v3_copy( rbb
->co
, rbb
->to_world
[3] );
309 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
310 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
312 rb_update_bounds( rbf
);
313 rb_update_bounds( rbb
);
315 rb_debug( rbf
, 0xff0000ff );
316 rb_debug( rbb
, 0xff0000ff );
321 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
322 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
324 rb_presolve_contacts( manifold
, len
);
326 for( int j
=0; j
<5; j
++ )
328 for( int i
=0; i
<len
; i
++ )
330 struct contact
*ct
= &manifold
[i
];
333 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
336 float temp
= ct
->norm_impulse
;
337 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
338 vn
= ct
->norm_impulse
- temp
;
341 v3_muls( ct
->n
, vn
, impulse
);
343 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
346 for( int j
=0; j
<2; j
++ )
348 float f
= k_friction
* ct
->norm_impulse
,
349 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
352 float temp
= ct
->tangent_impulse
[j
];
353 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
354 lambda
= ct
->tangent_impulse
[j
] - temp
;
356 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
361 phys
->in_air
= len
==0?1:0;
365 float const DOWNFORCE
= -k_walk_downforce
*VG_TIMESTEP_FIXED
;
366 v3_muladds( phys
->rb
.v
, (v3f
){0.0f
,-1.0f
,0.0f
}, DOWNFORCE
, phys
->rb
.v
);
368 if( vg_get_button("jump") )
370 phys
->rb
.v
[1] = 5.0f
;
374 v3_zero( phys
->rb
.w
);
375 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
377 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
380 v3_muladds( phys
->rb
.co
, forward_dir
, 2.0f
, p1
);
381 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
383 player
.walk
= powf( vg_get_axis("walk/push")*0.5f
+ 0.5f
, 4.0f
);
385 if( player
.walk
> 0.025f
)
388 speed
= vg_lerpf( 0.025f
*k_runspeed
, k_runspeed
, player
.walk
),
389 amt
= k_walk_accel
* VG_TIMESTEP_FIXED
,
390 zvel
= v3_dot( phys
->rb
.v
, forward_dir
),
391 new_vel
= vg_minf( zvel
+ amt
, speed
),
392 diff
= new_vel
- vg_minf( zvel
, speed
);
396 v3_muladds( phys
->rb
.v
, forward_dir
, diff
, phys
->rb
.v
);
402 phys
->rb
.v
[0] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
403 phys
->rb
.v
[2] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
408 * Physics collision detection, and control
410 static void player_physics(void)
412 struct player_phys
*phys
= &player
.phys
;
414 * Update collision fronts
417 rigidbody
*rbf
= &player
.collide_front
,
418 *rbb
= &player
.collide_back
;
420 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
421 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
423 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
424 float h
= player
.air_blend
*0.2f
;
426 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
427 v3_copy( rbf
->co
, rbf
->to_world
[3] );
428 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
429 v3_copy( rbb
->co
, rbb
->to_world
[3] );
431 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
432 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
434 rb_update_bounds( rbf
);
435 rb_update_bounds( rbb
);
437 rb_debug( rbf
, 0xff00ffff );
438 rb_debug( rbb
, 0xffffff00 );
443 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
444 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
446 rb_presolve_contacts( manifold
, len
);
447 v3f surface_avg
= {0.0f
, 0.0f
, 0.0f
};
455 for( int i
=0; i
<len
; i
++ )
457 v3_add( manifold
[i
].n
, surface_avg
, surface_avg
);
460 v3_normalize( surface_avg
);
462 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.5f
)
470 for( int j
=0; j
<5; j
++ )
472 for( int i
=0; i
<len
; i
++ )
474 struct contact
*ct
= &manifold
[i
];
477 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
478 v3_cross( phys
->rb
.w
, delta
, dv
);
479 v3_add( phys
->rb
.v
, dv
, dv
);
481 float vn
= -v3_dot( dv
, ct
->n
);
484 float temp
= ct
->norm_impulse
;
485 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
486 vn
= ct
->norm_impulse
- temp
;
489 v3_muls( ct
->n
, vn
, impulse
);
491 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
492 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
498 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
499 v3_cross( delta
, impulse
, impulse
);
502 * W Impulses are limited to the Y and X axises, we don't really want
503 * roll angular velocities being included.
505 * Can also tweak the resistance of each axis here by scaling the wx,wy
509 float wy
= v3_dot( phys
->rb
.up
, impulse
),
510 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.5f
;
512 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
513 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
517 float grabt
= vg_get_axis( "grab" )*0.5f
+0.5f
;
518 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
519 player
.phys
.pushing
= 0.0f
;
524 float angle
= v3_dot( phys
->rb
.up
, surface_avg
);
525 v3_cross( phys
->rb
.up
, surface_avg
, axis
);
527 //float cz = v3_dot( player.rb.forward, axis );
528 //v3_muls( player.rb.forward, cz, axis );
533 q_axis_angle( correction
, axis
, acosf(angle
)*18.0f
*VG_TIMESTEP_FIXED
);
534 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
537 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
538 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
540 player_physics_control();
542 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
546 /* Launch more up if alignment is up else improve velocity */
547 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
549 dir
= mod
+ aup
*(1.0f
-mod
);
551 v3_copy( phys
->rb
.v
, jumpdir
);
552 v3_normalize( jumpdir
);
553 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
554 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
555 v3_normalize( jumpdir
);
557 float force
= k_jump_force
*phys
->jump
;
558 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
561 player
.jump_time
= vg
.time
;
563 /* TODO: Move to audio file */
565 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
566 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
567 audio_player_set_vol( &audio_player_extra
, 20.0f
);
568 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%4] );
574 player_physics_control_air();
577 if( !phys
->jump_charge
)
579 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
582 phys
->jump_charge
= 0;
583 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
586 static void player_save_frame(void)
588 player
.phys_gate_frame
= player
.phys
;
591 static void player_restore_frame(void)
593 player
.phys
= player
.phys_gate_frame
;
594 rb_update_transform( &player
.phys
.rb
);
597 static void player_do_motion(void)
599 struct player_phys
*phys
= &player
.phys
;
601 float horizontal
= vg_get_axis("horizontal"),
602 vertical
= vg_get_axis("vertical");
604 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
607 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
608 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
609 audio_player_set_vol( &audio_player_extra
, 20.0f
);
610 audio_player_playclip( &audio_player_extra
, &audio_splash
);
619 player_walk_physics();
621 /* Integrate velocity */
623 v3_copy( phys
->rb
.co
, prevco
);
625 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
626 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
628 /* Real angular velocity integration */
629 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, phys
->rb
.w
);
630 if( v3_length2( phys
->rb
.w
) > 0.0f
)
634 v3_copy( phys
->rb
.w
, axis
);
636 float mag
= v3_length( axis
);
637 v3_divs( axis
, mag
, axis
);
638 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
639 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
642 /* Faux angular velocity */
645 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
646 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
648 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
649 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
653 * Gate intersection, by tracing a line over the gate planes
655 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
657 struct route_gate
*rg
= &world
.routes
.gates
[i
];
658 teleport_gate
*gate
= &rg
->gate
;
660 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
662 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
663 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
664 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
665 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
666 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
667 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
669 v4f transport_rotation
;
670 m3x3_q( gate
->transport
, transport_rotation
);
671 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
673 world_routes_activate_gate( i
);
675 if( !phys
->on_board
)
677 v3f fwd_dir
= {cosf(player
.angles
[0]),
679 sinf(player
.angles
[0])};
680 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
682 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
688 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
694 rb_update_transform( &phys
->rb
);
698 * Free camera movement
700 static void player_mouseview(void)
702 if( gui_want_mouse() )
705 static v2f mouse_last
,
706 view_vel
= { 0.0f
, 0.0f
};
708 if( vg_get_button_down( "primary" ) )
709 v2_copy( vg
.mouse
, mouse_last
);
711 else if( vg_get_button( "primary" ) )
714 v2_sub( vg
.mouse
, mouse_last
, delta
);
715 v2_copy( vg
.mouse
, mouse_last
);
717 v2_muladds( view_vel
, delta
, 0.06f
*vg
.time_delta
, view_vel
);
720 v2_muls( view_vel
, 1.0f
-4.2f
*vg
.time_delta
, view_vel
);
721 v2_add( view_vel
, player
.angles
, player
.angles
);
722 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
725 static void player_freecam(void)
729 float movespeed
= fc_speed
;
730 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
731 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
733 m3x3_mulv( player
.camera
, lookdir
, lookdir
);
734 m3x3_mulv( player
.camera
, sidedir
, sidedir
);
736 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
737 if( vg_get_button( "forward" ) )
738 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
739 if( vg_get_button( "back" ) )
740 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
741 if( vg_get_button( "left" ) )
742 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
743 if( vg_get_button( "right" ) )
744 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
746 v3_muls( move_vel
, 0.7f
, move_vel
);
747 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
750 static void player_camera_update(void)
752 /* Update camera matrices */
753 v4f qyaw
, qpitch
, qcam
;
754 q_axis_angle( qyaw
, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, -player
.angles
[0] );
755 q_axis_angle( qpitch
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, -player
.angles
[1] );
757 q_mul( qyaw
, qpitch
, qcam
);
758 q_m3x3( qcam
, player
.camera
);
760 v3_copy( player
.camera_pos
, player
.camera
[3] );
761 m4x3_invert_affine( player
.camera
, player
.camera_inverse
);
764 static int reset_player( int argc
, char const *argv
[] )
766 struct player_phys
*phys
= &player
.phys
;
767 struct respawn_point
*rp
= NULL
, *r
;
771 for( int i
=0; i
<world
.spawn_count
; i
++ )
773 r
= &world
.spawns
[i
];
774 if( !strcmp( r
->name
, argv
[0] ) )
782 vg_warn( "No spawn named '%s'\n", argv
[0] );
787 float min_dist
= INFINITY
;
789 for( int i
=0; i
<world
.spawn_count
; i
++ )
791 r
= &world
.spawns
[i
];
792 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
794 vg_info( "Dist %s : %f\n", r
->name
, d
);
805 vg_error( "No spawn found\n" );
806 if( !world
.spawn_count
)
809 rp
= &world
.spawns
[0];
814 v4_copy( rp
->q
, phys
->rb
.q
);
815 v3_copy( rp
->co
, phys
->rb
.co
);
816 v3_zero( phys
->rb
.v
);
818 phys
->vswitch
= 1.0f
;
819 phys
->slip_last
= 0.0f
;
822 m3x3_identity( phys
->vr
);
824 player
.mdl
.shoes
[0] = 1;
825 player
.mdl
.shoes
[1] = 1;
827 rb_update_transform( &phys
->rb
);
832 #endif /* PLAYER_PHYSICS_H */