6c170c6d9ae16f54272096c44c69b448c340c03c
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
5 #ifndef PLAYER_PHYSICS_H
6 #define PLAYER_PHYSICS_H
11 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 * Called when launching into the air to predict and adjust trajectories
20 static void player_start_air(void)
22 struct player_phys
*phys
= &player
.phys
;
29 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
30 float best_velocity_delta
= -9999.9f
;
34 v3_cross( phys
->rb
.up
, phys
->rb
.v
, axis
);
36 player
.land_log_count
= 0;
38 m3x3_identity( phys
->vr
);
40 for( int m
=-3;m
<=12; m
++ )
42 float vmod
= ((float)m
/ 15.0f
)*0.09f
;
45 v3_copy( phys
->rb
.co
, pco
);
46 v3_muls( phys
->rb
.v
, k_bias
, pv
);
49 * Try different 'rotations' of the velocity to find the best possible
50 * landing normal. This conserves magnitude at the expense of slightly
57 q_axis_angle( vr_q
, axis
, vmod
);
60 m3x3_mulv( vr
, pv
, pv
);
61 v3_muladds( pco
, pv
, pstep
, pco
);
63 for( int i
=0; i
<50; i
++ )
66 apply_gravity( pv
, pstep
);
68 m3x3_mulv( vr
, pv
, pv
);
69 v3_muladds( pco
, pv
, pstep
, pco
);
74 v3_sub( pco
, pco1
, vdir
);
75 contact
.dist
= v3_length( vdir
);
76 v3_divs( vdir
, contact
.dist
, vdir
);
78 if( ray_world( pco1
, vdir
, &contact
))
80 float land_delta
= v3_dot( pv
, contact
.normal
);
81 u32 scolour
= (u8
)(vg_minf(-land_delta
* 2.0f
, 255.0f
));
83 /* Bias prediction towords ramps */
84 if( ray_hit_is_ramp( &contact
) )
87 scolour
|= 0x0000a000;
90 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
92 best_velocity_delta
= land_delta
;
94 v3_copy( contact
.pos
, player
.land_target
);
96 m3x3_copy( vr
, phys
->vr_pstep
);
97 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
98 q_m3x3( vr_q
, phys
->vr
);
101 v3_copy( contact
.pos
,
102 player
.land_target_log
[player
.land_log_count
] );
103 player
.land_target_colours
[player
.land_log_count
] =
104 0xff000000 | scolour
;
106 player
.land_log_count
++;
115 * Main friction interface model
117 static void player_physics_control(void)
119 struct player_phys
*phys
= &player
.phys
;
122 * Computing localized friction forces for controlling the character
123 * Friction across X is significantly more than Z
127 m3x3_mulv( phys
->rb
.to_local
, phys
->rb
.v
, vel
);
130 if( fabsf(vel
[2]) > 0.01f
)
131 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
133 if( fabsf( slip
) > 1.2f
)
134 slip
= vg_signf( slip
) * 1.2f
;
136 phys
->reverse
= -vg_signf(vel
[2]);
138 float substep
= VG_TIMESTEP_FIXED
* 0.2f
;
141 float fwd_resistance
= vg_get_button( "break" )? 5.0f
: k_friction_resistance
;
143 float fwd_resistance
= k_friction_resistance
;
146 for( int i
=0; i
<5; i
++ )
148 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
149 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
152 if( vg_get_button( "jump" ) )
154 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
156 if( !phys
->jump_charge
)
157 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
159 phys
->jump_charge
= 1;
162 static int push_thresh_last
= 0;
163 float push_amt
= vg_get_axis( "walk/push" ) * 0.5f
+ 0.5f
;
164 int push_thresh
= push_amt
>0.15f
? 1: 0;
166 if( push_thresh
&& !push_thresh_last
)
167 player
.phys
.start_push
= vg
.time
;
169 push_thresh_last
= push_thresh
;
171 if( !vg_get_button("break") && push_thresh
)
173 player
.phys
.pushing
= 1.0f
;
174 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
176 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
177 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
178 current
= v3_length( vel
),
179 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
181 new_vel
-= vg_minf(current
, k_max_push_speed
);
182 vel
[2] -= new_vel
* phys
->reverse
;
186 static float previous
= 0.0f
;
187 float delta
= previous
- phys
->grab
,
188 pump
= delta
* k_pump_force
* VG_TIMESTEP_FIXED
;
189 previous
= phys
->grab
;
192 v3_muladds( phys
->rb
.co
, phys
->rb
.up
, pump
, p1
);
193 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
198 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
200 float steer
= vg_get_axis( "lookh" ),
201 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
203 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
207 * Air control, no real physics
209 static void player_physics_control_air(void)
211 struct player_phys
*phys
= &player
.phys
;
213 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
214 vg_line_cross( player
.land_target
, 0xff0000ff, 0.25f
);
221 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
224 v3_copy( phys
->rb
.co
, pco
);
225 v3_copy( phys
->rb
.v
, pv
);
227 float time_to_impact
= 0.0f
;
228 float limiter
= 1.0f
;
230 for( int i
=0; i
<50; i
++ )
232 v3_copy( pco
, pco1
);
233 m3x3_mulv( phys
->vr_pstep
, pv
, pv
);
234 apply_gravity( pv
, pstep
);
235 v3_muladds( pco
, pv
, pstep
, pco
);
240 v3_sub( pco
, pco1
, vdir
);
241 contact
.dist
= v3_length( vdir
);
242 v3_divs( vdir
, contact
.dist
, vdir
);
244 float orig_dist
= contact
.dist
;
245 if( ray_world( pco1
, vdir
, &contact
))
247 float angle
= v3_dot( phys
->rb
.up
, contact
.normal
);
249 v3_cross( phys
->rb
.up
, contact
.normal
, axis
);
251 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
252 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
253 limiter
= 1.0f
-limiter
;
255 limiter
= 1.0f
-limiter
;
260 q_axis_angle( correction
, axis
,
261 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
262 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
265 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
268 time_to_impact
+= pstep
;
271 phys
->iY
-= vg_get_axis( "lookh" ) * k_steer_air
* VG_TIMESTEP_FIXED
;
273 float iX
= vg_get_axis( "lookv" ) *
274 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
276 static float siX
= 0.0f
;
277 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
280 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
281 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
284 v2f target
= {0.0f
,0.0f
};
285 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
286 phys
->grab
, target
);
290 * Entire Walking physics model
291 * TODO: sleep when under certain velotiy
293 static void player_walk_physics(void)
295 struct player_phys
*phys
= &player
.phys
;
296 rigidbody
*rbf
= &player
.collide_front
,
297 *rbb
= &player
.collide_back
;
299 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
300 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
305 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
306 v3_copy( rbf
->co
, rbf
->to_world
[3] );
307 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
308 v3_copy( rbb
->co
, rbb
->to_world
[3] );
310 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
311 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
313 rb_update_bounds( rbf
);
314 rb_update_bounds( rbb
);
316 rb_debug( rbf
, 0xff0000ff );
317 rb_debug( rbb
, 0xff0000ff );
322 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
323 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
325 rb_presolve_contacts( manifold
, len
);
327 for( int j
=0; j
<5; j
++ )
329 for( int i
=0; i
<len
; i
++ )
331 struct contact
*ct
= &manifold
[i
];
334 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
337 float temp
= ct
->norm_impulse
;
338 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
339 vn
= ct
->norm_impulse
- temp
;
342 v3_muls( ct
->n
, vn
, impulse
);
344 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
347 for( int j
=0; j
<2; j
++ )
349 float f
= k_friction
* ct
->norm_impulse
,
350 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
353 float temp
= ct
->tangent_impulse
[j
];
354 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
355 lambda
= ct
->tangent_impulse
[j
] - temp
;
357 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
362 phys
->in_air
= len
==0?1:0;
366 float const DOWNFORCE
= -k_walk_downforce
*VG_TIMESTEP_FIXED
;
367 v3_muladds( phys
->rb
.v
, (v3f
){0.0f
,-1.0f
,0.0f
}, DOWNFORCE
, phys
->rb
.v
);
369 if( vg_get_button("jump") )
371 phys
->rb
.v
[1] = 5.0f
;
375 v3_zero( phys
->rb
.w
);
376 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
378 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
381 v3_muladds( phys
->rb
.co
, forward_dir
, 2.0f
, p1
);
382 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
384 player
.walk
= powf( vg_get_axis("walk/push")*0.5f
+ 0.5f
, 4.0f
);
386 if( player
.walk
> 0.025f
)
389 speed
= vg_lerpf( 0.025f
*k_runspeed
, k_runspeed
, player
.walk
),
390 amt
= k_walk_accel
* VG_TIMESTEP_FIXED
,
391 zvel
= v3_dot( phys
->rb
.v
, forward_dir
),
392 new_vel
= vg_minf( zvel
+ amt
, speed
),
393 diff
= new_vel
- vg_minf( zvel
, speed
);
397 v3_muladds( phys
->rb
.v
, forward_dir
, diff
, phys
->rb
.v
);
403 phys
->rb
.v
[0] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
404 phys
->rb
.v
[2] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
409 * Physics collision detection, and control
411 static void player_physics(void)
413 struct player_phys
*phys
= &player
.phys
;
415 * Update collision fronts
418 rigidbody
*rbf
= &player
.collide_front
,
419 *rbb
= &player
.collide_back
;
421 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
422 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
424 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
425 float h
= player
.air_blend
*0.2f
;
427 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
428 v3_copy( rbf
->co
, rbf
->to_world
[3] );
429 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
430 v3_copy( rbb
->co
, rbb
->to_world
[3] );
432 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
433 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
435 rb_update_bounds( rbf
);
436 rb_update_bounds( rbb
);
438 rb_debug( rbf
, 0xff00ffff );
439 rb_debug( rbb
, 0xffffff00 );
444 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
445 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
447 rb_presolve_contacts( manifold
, len
);
448 v3f surface_avg
= {0.0f
, 0.0f
, 0.0f
};
456 for( int i
=0; i
<len
; i
++ )
458 v3_add( manifold
[i
].n
, surface_avg
, surface_avg
);
461 v3_normalize( surface_avg
);
463 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.5f
)
473 for( int j
=0; j
<5; j
++ )
475 for( int i
=0; i
<len
; i
++ )
477 struct contact
*ct
= &manifold
[i
];
480 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
481 v3_cross( phys
->rb
.w
, delta
, dv
);
482 v3_add( phys
->rb
.v
, dv
, dv
);
484 float vn
= -v3_dot( dv
, ct
->n
);
487 float temp
= ct
->norm_impulse
;
488 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
489 vn
= ct
->norm_impulse
- temp
;
492 v3_muls( ct
->n
, vn
, impulse
);
494 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
495 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
501 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
502 v3_cross( delta
, impulse
, impulse
);
505 * W Impulses are limited to the Y and X axises, we don't really want
506 * roll angular velocities being included.
508 * Can also tweak the resistance of each axis here by scaling the wx,wy
512 float wy
= v3_dot( phys
->rb
.up
, impulse
),
513 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.5f
;
515 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
516 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
520 float grabt
= vg_get_axis( "grab" )*0.5f
+0.5f
;
521 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
522 player
.phys
.pushing
= 0.0f
;
528 float angle
= v3_dot( phys
->rb
.up
, surface_avg
);
529 v3_cross( phys
->rb
.up
, surface_avg
, axis
);
531 //float cz = v3_dot( player.rb.forward, axis );
532 //v3_muls( player.rb.forward, cz, axis );
537 q_axis_angle( correction
, axis
, acosf(angle
)*18.0f
*VG_TIMESTEP_FIXED
);
538 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
542 /* 20/10/22: make this only go axisways instead, may effect velocities. */
546 float d
= v3_dot( phys
->rb
.forward
, surface_avg
);
547 v3_muladds( surface_avg
, phys
->rb
.forward
, -d
, projected
);
548 v3_normalize( projected
);
550 float angle
= v3_dot( phys
->rb
.up
, projected
);
551 v3_cross( phys
->rb
.up
, projected
, axis
);
554 v3_add( phys
->rb
.co
, projected
, p0
);
555 v3_add( phys
->rb
.co
, phys
->rb
.up
, p1
);
556 vg_line( phys
->rb
.co
, p0
, 0xff00ff00 );
557 vg_line( phys
->rb
.co
, p1
, 0xff000fff );
559 if( fabsf(angle
) < 0.999f
)
562 q_axis_angle( correction
, axis
, acosf(angle
)*4.0f
*VG_TIMESTEP_FIXED
);
563 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
569 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
570 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
572 player_physics_control();
574 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
578 /* Launch more up if alignment is up else improve velocity */
579 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
581 dir
= mod
+ aup
*(1.0f
-mod
);
583 v3_copy( phys
->rb
.v
, jumpdir
);
584 v3_normalize( jumpdir
);
585 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
586 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
587 v3_normalize( jumpdir
);
589 float force
= k_jump_force
*phys
->jump
;
590 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
593 player
.jump_time
= vg
.time
;
595 /* TODO: Move to audio file */
597 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
598 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
599 audio_player_set_vol( &audio_player_extra
, 20.0f
);
600 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%2] );
606 player_physics_control_air();
609 if( !phys
->jump_charge
)
611 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
614 phys
->jump_charge
= 0;
615 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
618 static void player_save_frame(void)
620 player
.phys_gate_frame
= player
.phys
;
623 static void player_restore_frame(void)
625 player
.phys
= player
.phys_gate_frame
;
626 rb_update_transform( &player
.phys
.rb
);
629 static void player_do_motion(void)
631 struct player_phys
*phys
= &player
.phys
;
633 float horizontal
= vg_get_axis("horizontal"),
634 vertical
= vg_get_axis("vertical");
636 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
639 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
640 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
641 audio_player_set_vol( &audio_player_extra
, 20.0f
);
642 audio_player_playclip( &audio_player_extra
, &audio_splash
);
651 player_walk_physics();
653 /* Integrate velocity */
655 v3_copy( phys
->rb
.co
, prevco
);
657 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
658 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
660 /* Real angular velocity integration */
661 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, phys
->rb
.w
);
662 if( v3_length2( phys
->rb
.w
) > 0.0f
)
666 v3_copy( phys
->rb
.w
, axis
);
668 float mag
= v3_length( axis
);
669 v3_divs( axis
, mag
, axis
);
670 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
671 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
674 /* Faux angular velocity */
677 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
678 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
680 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
681 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
685 * Gate intersection, by tracing a line over the gate planes
687 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
689 struct route_gate
*rg
= &world
.routes
.gates
[i
];
690 teleport_gate
*gate
= &rg
->gate
;
692 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
694 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
695 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
696 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
697 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
698 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
699 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
701 v4f transport_rotation
;
702 m3x3_q( gate
->transport
, transport_rotation
);
703 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
705 world_routes_activate_gate( i
);
707 if( !phys
->on_board
)
709 v3f fwd_dir
= {cosf(player
.angles
[0]),
711 sinf(player
.angles
[0])};
712 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
714 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
717 player
.rewind_length
= 0;
718 player
.rewind_total_length
= 0.0f
;
719 player
.rewind_incrementer
= 10000;
723 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
729 rb_update_transform( &phys
->rb
);
733 * Free camera movement
735 static void player_mouseview(void)
737 if( gui_want_mouse() )
740 static v2f mouse_last
,
741 view_vel
= { 0.0f
, 0.0f
};
743 if( vg_get_button_down( "primary" ) )
744 v2_copy( vg
.mouse
, mouse_last
);
746 else if( vg_get_button( "primary" ) )
749 v2_sub( vg
.mouse
, mouse_last
, delta
);
750 v2_copy( vg
.mouse
, mouse_last
);
752 v2_muladds( view_vel
, delta
, 0.06f
*vg
.time_delta
, view_vel
);
755 v2_muls( view_vel
, 1.0f
-4.2f
*vg
.time_delta
, view_vel
);
756 v2_add( view_vel
, player
.angles
, player
.angles
);
757 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
760 static void player_freecam(void)
764 float movespeed
= fc_speed
;
765 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
766 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
768 m3x3_mulv( camera_mtx
, lookdir
, lookdir
);
769 m3x3_mulv( camera_mtx
, sidedir
, sidedir
);
771 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
772 if( vg_get_button( "forward" ) )
773 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
774 if( vg_get_button( "back" ) )
775 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
776 if( vg_get_button( "left" ) )
777 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
778 if( vg_get_button( "right" ) )
779 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
781 v3_muls( move_vel
, 0.7f
, move_vel
);
782 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
785 static int reset_player( int argc
, char const *argv
[] )
787 struct player_phys
*phys
= &player
.phys
;
788 struct respawn_point
*rp
= NULL
, *r
;
792 for( int i
=0; i
<world
.spawn_count
; i
++ )
794 r
= &world
.spawns
[i
];
795 if( !strcmp( r
->name
, argv
[0] ) )
803 vg_warn( "No spawn named '%s'\n", argv
[0] );
808 float min_dist
= INFINITY
;
810 for( int i
=0; i
<world
.spawn_count
; i
++ )
812 r
= &world
.spawns
[i
];
813 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
815 vg_info( "Dist %s : %f\n", r
->name
, d
);
826 vg_error( "No spawn found\n" );
827 if( !world
.spawn_count
)
830 rp
= &world
.spawns
[0];
835 v4_copy( rp
->q
, phys
->rb
.q
);
836 v3_copy( rp
->co
, phys
->rb
.co
);
837 v3_zero( phys
->rb
.v
);
839 phys
->vswitch
= 1.0f
;
840 phys
->slip_last
= 0.0f
;
843 m3x3_identity( phys
->vr
);
845 player
.mdl
.shoes
[0] = 1;
846 player
.mdl
.shoes
[1] = 1;
848 rb_update_transform( &phys
->rb
);
853 #endif /* PLAYER_PHYSICS_H */