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 * Called when launching into the air to predict and adjust trajectories
20 VG_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_material( &contact
)->info
.flags
85 & k_material_flag_skate_surface
)
88 scolour
|= 0x0000a000;
91 if( (land_delta
< 0.0f
) && (land_delta
> best_velocity_delta
) )
93 best_velocity_delta
= land_delta
;
95 v3_copy( contact
.pos
, player
.land_target
);
97 m3x3_copy( vr
, phys
->vr_pstep
);
98 q_axis_angle( vr_q
, axis
, vmod
*0.1f
);
99 q_m3x3( vr_q
, phys
->vr
);
102 v3_copy( contact
.pos
,
103 player
.land_target_log
[player
.land_log_count
] );
104 player
.land_target_colours
[player
.land_log_count
] =
105 0xff000000 | scolour
;
107 player
.land_log_count
++;
116 * Main friction interface model
118 VG_STATIC
void player_physics_control(void)
120 struct player_phys
*phys
= &player
.phys
;
123 * Computing localized friction forces for controlling the character
124 * Friction across X is significantly more than Z
128 m3x3_mulv( phys
->rb
.to_local
, phys
->rb
.v
, vel
);
131 if( fabsf(vel
[2]) > 0.01f
)
132 slip
= fabsf(-vel
[0] / vel
[2]) * vg_signf(vel
[0]);
134 if( fabsf( slip
) > 1.2f
)
135 slip
= vg_signf( slip
) * 1.2f
;
137 phys
->reverse
= -vg_signf(vel
[2]);
139 float substep
= VG_TIMESTEP_FIXED
* 0.2f
;
142 float fwd_resistance
= vg_get_button( "break" )? 5.0f
: k_friction_resistance
;
144 float fwd_resistance
= k_friction_resistance
;
147 for( int i
=0; i
<5; i
++ )
149 vel
[2] = stable_force( vel
[2],vg_signf(vel
[2]) * -fwd_resistance
*substep
);
150 vel
[0] = stable_force( vel
[0],vg_signf(vel
[0]) * -k_friction_lat
*substep
);
153 if( player
.input_jump
->button
.value
)
155 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
157 if( !phys
->jump_charge
)
158 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
160 phys
->jump_charge
= 1;
163 static int push_thresh_last
= 0;
164 float push
= player
.input_push
->axis
.value
;
165 int push_thresh
= push
>0.15f
? 1: 0;
167 if( push_thresh
&& !push_thresh_last
)
168 player
.phys
.start_push
= vg
.time
;
170 push_thresh_last
= push_thresh
;
172 if( !player
.input_jump
->button
.value
&& push_thresh
)
174 player
.phys
.pushing
= 1.0f
;
175 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
177 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
178 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
179 current
= v3_length( vel
),
180 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
182 new_vel
-= vg_minf(current
, k_max_push_speed
);
183 vel
[2] -= new_vel
* phys
->reverse
;
187 static float previous
= 0.0f
;
188 float delta
= previous
- phys
->grab
,
189 pump
= delta
* k_pump_force
* VG_TIMESTEP_FIXED
;
190 previous
= phys
->grab
;
193 v3_muladds( phys
->rb
.co
, phys
->rb
.up
, pump
, p1
);
194 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
199 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
201 float steer
= player
.input_js1h
->axis
.value
,
202 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
204 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
208 * Air control, no real physics
210 VG_STATIC
void player_physics_control_air(void)
212 struct player_phys
*phys
= &player
.phys
;
214 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
215 vg_line_cross( player
.land_target
, 0xff0000ff, 0.25f
);
222 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
225 v3_copy( phys
->rb
.co
, pco
);
226 v3_copy( phys
->rb
.v
, pv
);
228 float time_to_impact
= 0.0f
;
229 float limiter
= 1.0f
;
231 for( int i
=0; i
<50; i
++ )
233 v3_copy( pco
, pco1
);
234 m3x3_mulv( phys
->vr_pstep
, pv
, pv
);
235 apply_gravity( pv
, pstep
);
236 v3_muladds( pco
, pv
, pstep
, pco
);
241 v3_sub( pco
, pco1
, vdir
);
242 contact
.dist
= v3_length( vdir
);
243 v3_divs( vdir
, contact
.dist
, vdir
);
245 float orig_dist
= contact
.dist
;
246 if( ray_world( pco1
, vdir
, &contact
))
248 float angle
= v3_dot( phys
->rb
.up
, contact
.normal
);
250 v3_cross( phys
->rb
.up
, contact
.normal
, axis
);
252 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
253 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
254 limiter
= 1.0f
-limiter
;
256 limiter
= 1.0f
-limiter
;
261 q_axis_angle( correction
, axis
,
262 acosf(angle
)*(1.0f
-limiter
)*3.0f
*VG_TIMESTEP_FIXED
);
263 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
266 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
269 time_to_impact
+= pstep
;
272 float steerh
= player
.input_js1h
->axis
.value
,
273 steerv
= player
.input_js1v
->axis
.value
;
275 phys
->iY
-= steerh
* k_steer_air
* VG_TIMESTEP_FIXED
;
279 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
281 static float siX
= 0.0f
;
282 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
285 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
286 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
290 v2f target
= {0.0f
,0.0f
};
291 v2_muladds( target
, (v2f
){ vg_get_axis("grabh"), vg_get_axis("grabv") },
292 phys
->grab
, target
);
297 * Entire Walking physics model
298 * TODO: sleep when under certain velotiy
300 VG_STATIC
void player_walk_physics(void)
302 struct player_phys
*phys
= &player
.phys
;
303 rigidbody
*rbf
= &player
.collide_front
,
304 *rbb
= &player
.collide_back
;
306 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
307 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
312 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
313 v3_copy( rbf
->co
, rbf
->to_world
[3] );
314 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
315 v3_copy( rbb
->co
, rbb
->to_world
[3] );
317 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
318 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
320 rb_update_bounds( rbf
);
321 rb_update_bounds( rbb
);
323 rb_debug( rbf
, 0xff0000ff );
324 rb_debug( rbb
, 0xff0000ff );
329 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
330 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
332 rb_presolve_contacts( manifold
, len
);
334 for( int j
=0; j
<5; j
++ )
336 for( int i
=0; i
<len
; i
++ )
338 struct contact
*ct
= &manifold
[i
];
341 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
344 float temp
= ct
->norm_impulse
;
345 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
346 vn
= ct
->norm_impulse
- temp
;
349 v3_muls( ct
->n
, vn
, impulse
);
351 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
354 for( int j
=0; j
<2; j
++ )
356 float f
= k_friction
* ct
->norm_impulse
,
357 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
360 float temp
= ct
->tangent_impulse
[j
];
361 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
362 lambda
= ct
->tangent_impulse
[j
] - temp
;
364 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
374 struct world_material
*surface_mat
= world_contact_material( manifold
);
375 player
.surface_prop
= surface_mat
->info
.surface_prop
;
380 float const DOWNFORCE
= -k_walk_downforce
*VG_TIMESTEP_FIXED
;
381 v3_muladds( phys
->rb
.v
, (v3f
){0.0f
,-1.0f
,0.0f
}, DOWNFORCE
, phys
->rb
.v
);
383 if( player
.input_jump
->button
.value
)
385 phys
->rb
.v
[1] = 5.0f
;
389 v3_zero( phys
->rb
.w
);
390 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
392 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
395 v3_muladds( phys
->rb
.co
, forward_dir
, 2.0f
, p1
);
396 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
398 float walk
= player
.input_walk
->axis
.value
;
399 player
.walk
= powf( walk
, 4.0f
);
401 if( player
.walk
> 0.025f
)
404 speed
= vg_lerpf( 0.025f
*k_runspeed
, k_runspeed
, player
.walk
),
405 amt
= k_walk_accel
* VG_TIMESTEP_FIXED
,
406 zvel
= v3_dot( phys
->rb
.v
, forward_dir
),
407 new_vel
= vg_minf( zvel
+ amt
, speed
),
408 diff
= new_vel
- vg_minf( zvel
, speed
);
412 v3_muladds( phys
->rb
.v
, forward_dir
, diff
, phys
->rb
.v
);
418 phys
->rb
.v
[0] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
419 phys
->rb
.v
[2] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
424 * Physics collision detection, and control
426 VG_STATIC
void player_physics(void)
428 struct player_phys
*phys
= &player
.phys
;
430 * Update collision fronts
433 rigidbody
*rbf
= &player
.collide_front
,
434 *rbb
= &player
.collide_back
;
436 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
437 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
439 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
440 float h
= player
.air_blend
*0.2f
;
442 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
443 v3_copy( rbf
->co
, rbf
->to_world
[3] );
444 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
445 v3_copy( rbb
->co
, rbb
->to_world
[3] );
447 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
448 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
450 rb_update_bounds( rbf
);
451 rb_update_bounds( rbb
);
453 rb_debug( rbf
, 0xff00ffff );
454 rb_debug( rbb
, 0xffffff00 );
459 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
460 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
462 rb_presolve_contacts( manifold
, len
);
463 v3f surface_avg
= {0.0f
, 0.0f
, 0.0f
};
471 for( int i
=0; i
<len
; i
++ )
473 v3_add( manifold
[i
].n
, surface_avg
, surface_avg
);
476 v3_normalize( surface_avg
);
478 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.5f
)
488 for( int j
=0; j
<5; j
++ )
490 for( int i
=0; i
<len
; i
++ )
492 struct contact
*ct
= &manifold
[i
];
495 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
496 v3_cross( phys
->rb
.w
, delta
, dv
);
497 v3_add( phys
->rb
.v
, dv
, dv
);
499 float vn
= -v3_dot( dv
, ct
->n
);
502 float temp
= ct
->norm_impulse
;
503 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
504 vn
= ct
->norm_impulse
- temp
;
507 v3_muls( ct
->n
, vn
, impulse
);
509 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
510 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
516 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
517 v3_cross( delta
, impulse
, impulse
);
520 * W Impulses are limited to the Y and X axises, we don't really want
521 * roll angular velocities being included.
523 * Can also tweak the resistance of each axis here by scaling the wx,wy
527 float wy
= v3_dot( phys
->rb
.up
, impulse
),
528 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.5f
;
530 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
531 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
535 float grabt
= vg_maxf( player
.input_grab
->axis
.value
,
536 vg_maxf( fabsf( player
.input_emjs2h
->axis
.value
),
537 fabsf( player
.input_emjs2v
->axis
.value
) )
540 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
541 player
.phys
.pushing
= 0.0f
;
547 float angle
= v3_dot( phys
->rb
.up
, surface_avg
);
548 v3_cross( phys
->rb
.up
, surface_avg
, axis
);
550 //float cz = v3_dot( player.rb.forward, axis );
551 //v3_muls( player.rb.forward, cz, axis );
556 q_axis_angle( correction
, axis
, acosf(angle
)*18.0f
*VG_TIMESTEP_FIXED
);
557 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
561 /* 20/10/22: make this only go axisways instead, may effect velocities. */
565 float d
= v3_dot( phys
->rb
.forward
, surface_avg
);
566 v3_muladds( surface_avg
, phys
->rb
.forward
, -d
, projected
);
567 v3_normalize( projected
);
569 float angle
= v3_dot( phys
->rb
.up
, projected
);
570 v3_cross( phys
->rb
.up
, projected
, axis
);
573 v3_add( phys
->rb
.co
, projected
, p0
);
574 v3_add( phys
->rb
.co
, phys
->rb
.up
, p1
);
575 vg_line( phys
->rb
.co
, p0
, 0xff00ff00 );
576 vg_line( phys
->rb
.co
, p1
, 0xff000fff );
578 if( fabsf(angle
) < 0.999f
)
581 q_axis_angle( correction
, axis
, acosf(angle
)*4.0f
*VG_TIMESTEP_FIXED
);
582 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
588 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
589 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
591 player_physics_control();
593 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
597 /* Launch more up if alignment is up else improve velocity */
598 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
600 dir
= mod
+ aup
*(1.0f
-mod
);
602 v3_copy( phys
->rb
.v
, jumpdir
);
603 v3_normalize( jumpdir
);
604 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
605 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
606 v3_normalize( jumpdir
);
608 float force
= k_jump_force
*phys
->jump
;
609 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
612 player
.jump_time
= vg
.time
;
614 /* TODO: Move to audio file */
616 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
617 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
618 audio_player_set_vol( &audio_player_extra
, 20.0f
);
619 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%2] );
625 player_physics_control_air();
628 if( !phys
->jump_charge
)
630 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
633 phys
->jump_charge
= 0;
634 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
637 VG_STATIC
void player_save_frame(void)
639 player
.phys_gate_frame
= player
.phys
;
642 VG_STATIC
void player_restore_frame(void)
644 player
.phys
= player
.phys_gate_frame
;
645 rb_update_transform( &player
.phys
.rb
);
648 VG_STATIC
void player_do_motion(void)
650 struct player_phys
*phys
= &player
.phys
;
652 if( world
.water
.enabled
)
654 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
657 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
658 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
659 audio_player_set_vol( &audio_player_extra
, 20.0f
);
660 audio_player_playclip( &audio_player_extra
, &audio_splash
);
670 player_walk_physics();
672 /* Integrate velocity */
674 v3_copy( phys
->rb
.co
, prevco
);
676 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
677 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
679 /* Real angular velocity integration */
680 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, phys
->rb
.w
);
681 if( v3_length2( phys
->rb
.w
) > 0.0f
)
685 v3_copy( phys
->rb
.w
, axis
);
687 float mag
= v3_length( axis
);
688 v3_divs( axis
, mag
, axis
);
689 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
690 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
693 /* Faux angular velocity */
696 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
697 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
699 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
700 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
704 * Gate intersection, by tracing a line over the gate planes
706 for( int i
=0; i
<world
.gate_count
; i
++ )
708 struct route_gate
*rg
= &world
.gates
[i
];
709 teleport_gate
*gate
= &rg
->gate
;
711 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
713 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
714 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
715 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
716 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
717 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
718 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
720 v4f transport_rotation
;
721 m3x3_q( gate
->transport
, transport_rotation
);
722 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
724 world_routes_activate_gate( i
);
726 if( !phys
->on_board
)
728 v3f fwd_dir
= {cosf(player
.angles
[0]),
730 sinf(player
.angles
[0])};
731 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
733 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
736 player
.rewind_length
= 0;
737 player
.rewind_total_length
= 0.0f
;
738 player
.rewind_incrementer
= 10000;
742 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
748 rb_update_transform( &phys
->rb
);
752 * Free camera movement
754 VG_STATIC
void player_mouseview(void)
756 if( ui_want_mouse() )
759 static v2f mouse_last
,
760 view_vel
= { 0.0f
, 0.0f
};
763 if( vg_get_button_down( "primary" ) )
764 v2_copy( vg
.mouse
, mouse_last
);
766 else if( vg_get_button( "primary" ) )
769 v2_sub( vg
.mouse
, mouse_last
, delta
);
770 v2_copy( vg
.mouse
, mouse_last
);
772 v2_muladds( view_vel
, delta
, 0.06f
*vg
.time_delta
, view_vel
);
776 v2_muls( view_vel
, 1.0f
-4.2f
*vg
.time_delta
, view_vel
);
777 v2_add( view_vel
, player
.angles
, player
.angles
);
778 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
781 VG_STATIC
void player_freecam(void)
785 float movespeed
= fc_speed
;
786 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
787 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
789 m3x3_mulv( camera_mtx
, lookdir
, lookdir
);
790 m3x3_mulv( camera_mtx
, sidedir
, sidedir
);
792 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
793 if( vg_get_button( "forward" ) )
794 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
795 if( vg_get_button( "back" ) )
796 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
797 if( vg_get_button( "left" ) )
798 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
799 if( vg_get_button( "right" ) )
800 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
802 v3_muls( move_vel
, 0.7f
, move_vel
);
803 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
806 VG_STATIC
int reset_player( int argc
, char const *argv
[] )
808 struct player_phys
*phys
= &player
.phys
;
809 struct respawn_point
*rp
= NULL
, *r
;
813 for( int i
=0; i
<world
.spawn_count
; i
++ )
815 r
= &world
.spawns
[i
];
816 if( !strcmp( r
->name
, argv
[0] ) )
824 vg_warn( "No spawn named '%s'\n", argv
[0] );
829 float min_dist
= INFINITY
;
831 for( int i
=0; i
<world
.spawn_count
; i
++ )
833 r
= &world
.spawns
[i
];
834 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
836 vg_info( "Dist %s : %f\n", r
->name
, d
);
847 vg_error( "No spawn found\n" );
848 if( !world
.spawn_count
)
851 rp
= &world
.spawns
[0];
857 q_m3x3( rp
->q
, the_long_way
);
859 v3f delta
= {1.0f
,0.0f
,0.0f
};
860 m3x3_mulv( the_long_way
, delta
, delta
);
862 player
.angles
[0] = atan2f( delta
[0], -delta
[2] );
863 player
.angles
[1] = -asinf( delta
[1] );
866 v4_copy( rp
->q
, phys
->rb
.q
);
867 v3_copy( rp
->co
, phys
->rb
.co
);
868 v3_zero( phys
->rb
.v
);
870 phys
->vswitch
= 1.0f
;
871 phys
->slip_last
= 0.0f
;
874 m3x3_identity( phys
->vr
);
876 player
.mdl
.shoes
[0] = 1;
877 player
.mdl
.shoes
[1] = 1;
879 rb_update_transform( &phys
->rb
);
884 #endif /* PLAYER_PHYSICS_H */