553e5d7abb8df868a8556272eee1fc59211e6f0f
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
;
138 float fwd_resistance
= (vg_get_button( "break" )? 5.0f
: 0.02f
) * -substep
;
140 for( int i
=0; i
<5; i
++ )
142 vel
[2] = stable_force( vel
[2], vg_signf( vel
[2] ) * fwd_resistance
);
143 vel
[0] = stable_force( vel
[0],
144 vg_signf( vel
[0] ) * -k_friction_lat
*substep
);
147 if( vg_get_button( "jump" ) )
149 phys
->jump
+= VG_TIMESTEP_FIXED
* k_jump_charge_speed
;
151 if( !phys
->jump_charge
)
152 phys
->jump_dir
= phys
->reverse
> 0.0f
? 1: 0;
154 phys
->jump_charge
= 1;
157 static int push_button_last
= 0;
158 int push_button
= vg_get_button( "push" );
159 push_button_last
= push_button
;
161 if( push_button
&& !push_button_last
)
162 player
.phys
.start_push
= vg
.time
;
164 if( !vg_get_button("break") && vg_get_button( "push" ) )
166 player
.phys
.pushing
= 1.0f
;
167 player
.phys
.push_time
= vg
.time
- player
.phys
.start_push
;
169 float cycle_time
= player
.phys
.push_time
*k_push_cycle_rate
,
170 amt
= k_push_accel
* (sinf(cycle_time
)*0.5f
+0.5f
)*VG_TIMESTEP_FIXED
,
171 current
= v3_length( vel
),
172 new_vel
= vg_minf( current
+ amt
, k_max_push_speed
);
174 new_vel
-= vg_minf(current
, k_max_push_speed
);
175 vel
[2] -= new_vel
* phys
->reverse
;
179 static float previous
= 0.0f
;
180 float delta
= previous
- phys
->grab
,
181 pump
= delta
* k_pump_force
* VG_TIMESTEP_FIXED
;
182 previous
= phys
->grab
;
185 v3_muladds( phys
->rb
.co
, phys
->rb
.up
, pump
, p1
);
186 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
191 m3x3_mulv( phys
->rb
.to_world
, vel
, phys
->rb
.v
);
193 float steer
= vg_get_axis( "horizontal" ),
194 steer_scaled
= vg_signf(steer
) * powf(steer
,2.0f
) * k_steer_ground
;
196 phys
->iY
-= steer_scaled
* VG_TIMESTEP_FIXED
;
200 * Air control, no real physics
202 static void player_physics_control_air(void)
204 struct player_phys
*phys
= &player
.phys
;
206 m3x3_mulv( phys
->vr
, phys
->rb
.v
, phys
->rb
.v
);
207 vg_line_cross( player
.land_target
, 0xff0000ff, 0.25f
);
214 float pstep
= VG_TIMESTEP_FIXED
* 10.0f
;
217 v3_copy( phys
->rb
.co
, pco
);
218 v3_copy( phys
->rb
.v
, pv
);
220 float time_to_impact
= 0.0f
;
221 float limiter
= 1.0f
;
223 for( int i
=0; i
<50; i
++ )
225 v3_copy( pco
, pco1
);
226 m3x3_mulv( phys
->vr_pstep
, pv
, pv
);
227 apply_gravity( pv
, pstep
);
228 v3_muladds( pco
, pv
, pstep
, pco
);
233 v3_sub( pco
, pco1
, vdir
);
234 contact
.dist
= v3_length( vdir
);
235 v3_divs( vdir
, contact
.dist
, vdir
);
237 float orig_dist
= contact
.dist
;
238 if( ray_world( pco1
, vdir
, &contact
))
240 float angle
= v3_dot( phys
->rb
.up
, contact
.normal
);
242 v3_cross( phys
->rb
.up
, contact
.normal
, axis
);
244 time_to_impact
+= (contact
.dist
/orig_dist
)*pstep
;
245 limiter
= vg_minf( 5.0f
, time_to_impact
)/5.0f
;
246 limiter
= 1.0f
-limiter
;
248 limiter
= 1.0f
-limiter
;
253 q_axis_angle( correction
, axis
, acosf(angle
)*0.05f
*(1.0f
-limiter
) );
254 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
257 vg_line_cross( contact
.pos
, 0xffff0000, 0.25f
);
260 time_to_impact
+= pstep
;
263 phys
->iY
-= vg_get_axis( "horizontal" ) * k_steer_air
* VG_TIMESTEP_FIXED
;
265 float iX
= vg_get_axis( "vertical" ) *
266 phys
->reverse
* k_steer_air
* limiter
* VG_TIMESTEP_FIXED
;
268 static float siX
= 0.0f
;
269 siX
= vg_lerpf( siX
, iX
, k_steer_air_lerp
);
272 q_axis_angle( rotate
, phys
->rb
.right
, siX
);
273 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
276 v2f target
= {0.0f
,0.0f
};
277 v2_muladds( target
, (v2f
){ vg_get_axis("h1"), vg_get_axis("v1") },
278 phys
->grab
, target
);
282 * Entire Walking physics model
283 * TODO: sleep when under certain velotiy
285 static void player_walk_physics(void)
287 struct player_phys
*phys
= &player
.phys
;
288 rigidbody
*rbf
= &player
.collide_front
,
289 *rbb
= &player
.collide_back
;
291 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
292 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
297 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h0
,0.0f
}, rbf
->co
);
298 v3_copy( rbf
->co
, rbf
->to_world
[3] );
299 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h1
,0.0f
}, rbb
->co
);
300 v3_copy( rbb
->co
, rbb
->to_world
[3] );
302 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
303 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
305 rb_update_bounds( rbf
);
306 rb_update_bounds( rbb
);
308 rb_debug( rbf
, 0xff0000ff );
309 rb_debug( rbb
, 0xff0000ff );
314 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
315 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
317 rb_presolve_contacts( manifold
, len
);
319 for( int j
=0; j
<5; j
++ )
321 for( int i
=0; i
<len
; i
++ )
323 struct contact
*ct
= &manifold
[i
];
326 float vn
= -v3_dot( phys
->rb
.v
, ct
->n
);
329 float temp
= ct
->norm_impulse
;
330 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
331 vn
= ct
->norm_impulse
- temp
;
334 v3_muls( ct
->n
, vn
, impulse
);
336 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
339 for( int j
=0; j
<2; j
++ )
341 float f
= k_friction
* ct
->norm_impulse
,
342 vt
= v3_dot( phys
->rb
.v
, ct
->t
[j
] ),
345 float temp
= ct
->tangent_impulse
[j
];
346 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
347 lambda
= ct
->tangent_impulse
[j
] - temp
;
349 v3_muladds( phys
->rb
.v
, ct
->t
[j
], lambda
, phys
->rb
.v
);
354 phys
->in_air
= len
==0?1:0;
356 v3_zero( phys
->rb
.w
);
357 q_axis_angle( phys
->rb
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -player
.angles
[0] );
359 v3f forward_dir
= { sinf(player
.angles
[0]),0.0f
,-cosf(player
.angles
[0]) };
362 v3_muladds( phys
->rb
.co
, forward_dir
, 2.0f
, p1
);
363 vg_line( phys
->rb
.co
, p1
, 0xff0000ff );
365 float move_dead
= 0.1f
,
366 move
= vg_get_axis("grabr")*0.5f
+ 0.5f
- move_dead
;
370 float move_norm
= move
* (1.0f
/(1.0f
-move_dead
)),
371 speed
= vg_lerpf( 0.1f
*k_runspeed
, k_runspeed
, move_norm
),
372 amt
= k_walk_accel
* VG_TIMESTEP_FIXED
,
373 zvel
= v3_dot( phys
->rb
.v
, forward_dir
),
374 new_vel
= vg_minf( zvel
+ amt
, speed
),
375 diff
= new_vel
- vg_minf( zvel
, speed
);
377 v3_muladds( phys
->rb
.v
, forward_dir
, diff
, phys
->rb
.v
);
380 float walk_norm
= 30.0f
/(float)player
.mdl
.anim_walk
->length
,
381 run_norm
= 30.0f
/(float)player
.mdl
.anim_run
->length
,
382 walk_adv
= vg_lerpf( walk_norm
,run_norm
,move_norm
);
384 player
.walk_timer
+= walk_adv
* VG_TIMESTEP_FIXED
;
388 player
.walk_timer
= 0.0f
;
391 phys
->rb
.v
[0] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
392 phys
->rb
.v
[2] *= 1.0f
- (VG_TIMESTEP_FIXED
* k_walk_friction
);
396 * Physics collision detection, and control
398 static void player_physics(void)
400 struct player_phys
*phys
= &player
.phys
;
402 * Update collision fronts
405 rigidbody
*rbf
= &player
.collide_front
,
406 *rbb
= &player
.collide_back
;
408 m3x3_copy( phys
->rb
.to_world
, player
.collide_front
.to_world
);
409 m3x3_copy( phys
->rb
.to_world
, player
.collide_back
.to_world
);
411 player
.air_blend
= vg_lerpf( player
.air_blend
, phys
->in_air
, 0.1f
);
412 float h
= player
.air_blend
*0.2f
;
414 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
,-k_board_length
}, rbf
->co
);
415 v3_copy( rbf
->co
, rbf
->to_world
[3] );
416 m4x3_mulv( phys
->rb
.to_world
, (v3f
){0.0f
,h
, k_board_length
}, rbb
->co
);
417 v3_copy( rbb
->co
, rbb
->to_world
[3] );
419 m4x3_invert_affine( rbf
->to_world
, rbf
->to_local
);
420 m4x3_invert_affine( rbb
->to_world
, rbb
->to_local
);
422 rb_update_bounds( rbf
);
423 rb_update_bounds( rbb
);
425 rb_debug( rbf
, 0xff00ffff );
426 rb_debug( rbb
, 0xffffff00 );
431 len
+= rb_sphere_scene( rbf
, &world
.rb_geo
, manifold
+len
);
432 len
+= rb_sphere_scene( rbb
, &world
.rb_geo
, manifold
+len
);
434 rb_presolve_contacts( manifold
, len
);
435 v3f surface_avg
= {0.0f
, 0.0f
, 0.0f
};
443 for( int i
=0; i
<len
; i
++ )
445 v3_add( manifold
[i
].n
, surface_avg
, surface_avg
);
448 v3_normalize( surface_avg
);
450 if( v3_dot( phys
->rb
.v
, surface_avg
) > 0.5f
)
458 for( int j
=0; j
<5; j
++ )
460 for( int i
=0; i
<len
; i
++ )
462 struct contact
*ct
= &manifold
[i
];
465 v3_sub( ct
->co
, phys
->rb
.co
, delta
);
466 v3_cross( phys
->rb
.w
, delta
, dv
);
467 v3_add( phys
->rb
.v
, dv
, dv
);
469 float vn
= -v3_dot( dv
, ct
->n
);
472 float temp
= ct
->norm_impulse
;
473 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
474 vn
= ct
->norm_impulse
- temp
;
477 v3_muls( ct
->n
, vn
, impulse
);
479 if( fabsf(v3_dot( impulse
, phys
->rb
.forward
)) > 10.0f
||
480 fabsf(v3_dot( impulse
, phys
->rb
.up
)) > 50.0f
)
486 v3_add( impulse
, phys
->rb
.v
, phys
->rb
.v
);
487 v3_cross( delta
, impulse
, impulse
);
490 * W Impulses are limited to the Y and X axises, we don't really want
491 * roll angular velocities being included.
493 * Can also tweak the resistance of each axis here by scaling the wx,wy
497 float wy
= v3_dot( phys
->rb
.up
, impulse
),
498 wx
= v3_dot( phys
->rb
.right
, impulse
)*1.5f
;
500 v3_muladds( phys
->rb
.w
, phys
->rb
.up
, wy
, phys
->rb
.w
);
501 v3_muladds( phys
->rb
.w
, phys
->rb
.right
, wx
, phys
->rb
.w
);
505 float grabt
= vg_get_axis( "grabr" )*0.5f
+0.5f
;
506 phys
->grab
= vg_lerpf( phys
->grab
, grabt
, 0.14f
);
507 player
.phys
.pushing
= 0.0f
;
512 float angle
= v3_dot( phys
->rb
.up
, surface_avg
);
513 v3_cross( phys
->rb
.up
, surface_avg
, axis
);
515 //float cz = v3_dot( player.rb.forward, axis );
516 //v3_muls( player.rb.forward, cz, axis );
521 q_axis_angle( correction
, axis
, acosf(angle
)*0.3f
);
522 q_mul( correction
, phys
->rb
.q
, phys
->rb
.q
);
525 float const DOWNFORCE
= -k_downforce
*VG_TIMESTEP_FIXED
;
526 v3_muladds( phys
->rb
.v
, phys
->rb
.up
, DOWNFORCE
, phys
->rb
.v
);
528 player_physics_control();
530 if( !phys
->jump_charge
&& phys
->jump
> 0.2f
)
534 /* Launch more up if alignment is up else improve velocity */
535 float aup
= fabsf(v3_dot( (v3f
){0.0f
,1.0f
,0.0f
}, phys
->rb
.up
)),
537 dir
= mod
+ aup
*(1.0f
-mod
);
539 v3_copy( phys
->rb
.v
, jumpdir
);
540 v3_normalize( jumpdir
);
541 v3_muls( jumpdir
, 1.0f
-dir
, jumpdir
);
542 v3_muladds( jumpdir
, phys
->rb
.up
, dir
, jumpdir
);
543 v3_normalize( jumpdir
);
545 float force
= k_jump_force
*phys
->jump
;
546 v3_muladds( phys
->rb
.v
, jumpdir
, force
, phys
->rb
.v
);
549 player
.jump_time
= vg
.time
;
551 /* TODO: Move to audio file */
553 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
554 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
555 audio_player_set_vol( &audio_player_extra
, 20.0f
);
556 audio_player_playclip( &audio_player_extra
, &audio_jumps
[rand()%4] );
562 player_physics_control_air();
565 if( !phys
->jump_charge
)
567 phys
->jump
-= k_jump_charge_speed
* VG_TIMESTEP_FIXED
;
570 phys
->jump_charge
= 0;
571 phys
->jump
= vg_clampf( phys
->jump
, 0.0f
, 1.0f
);
574 static void player_save_frame(void)
576 player
.phys_gate_frame
= player
.phys
;
579 static void player_restore_frame(void)
581 player
.phys
= player
.phys_gate_frame
;
582 rb_update_transform( &player
.phys
.rb
);
585 static void player_do_motion(void)
587 struct player_phys
*phys
= &player
.phys
;
589 float horizontal
= vg_get_axis("horizontal"),
590 vertical
= vg_get_axis("vertical");
592 if( (phys
->rb
.co
[1] < 0.0f
) && !player
.is_dead
)
595 audio_player_set_flags( &audio_player_extra
, AUDIO_FLAG_SPACIAL_3D
);
596 audio_player_set_position( &audio_player_extra
, phys
->rb
.co
);
597 audio_player_set_vol( &audio_player_extra
, 20.0f
);
598 audio_player_playclip( &audio_player_extra
, &audio_splash
);
607 player_walk_physics();
609 /* Integrate velocity */
611 v3_copy( phys
->rb
.co
, prevco
);
613 apply_gravity( phys
->rb
.v
, VG_TIMESTEP_FIXED
);
614 v3_muladds( phys
->rb
.co
, phys
->rb
.v
, VG_TIMESTEP_FIXED
, phys
->rb
.co
);
616 /* Real angular velocity integration */
617 v3_lerp( phys
->rb
.w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.125f
, phys
->rb
.w
);
618 if( v3_length2( phys
->rb
.w
) > 0.0f
)
622 v3_copy( phys
->rb
.w
, axis
);
624 float mag
= v3_length( axis
);
625 v3_divs( axis
, mag
, axis
);
626 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
627 q_mul( rotation
, phys
->rb
.q
, phys
->rb
.q
);
630 /* Faux angular velocity */
633 float lerpq
= phys
->in_air
? 0.04f
: 0.3f
;
634 phys
->siY
= vg_lerpf( phys
->siY
, phys
->iY
, lerpq
);
636 q_axis_angle( rotate
, phys
->rb
.up
, phys
->siY
);
637 q_mul( rotate
, phys
->rb
.q
, phys
->rb
.q
);
641 * Gate intersection, by tracing a line over the gate planes
643 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
645 struct route_gate
*rg
= &world
.routes
.gates
[i
];
646 teleport_gate
*gate
= &rg
->gate
;
648 if( gate_intersect( gate
, phys
->rb
.co
, prevco
) )
650 m4x3_mulv( gate
->transport
, phys
->rb
.co
, phys
->rb
.co
);
651 m3x3_mulv( gate
->transport
, phys
->rb
.v
, phys
->rb
.v
);
652 m3x3_mulv( gate
->transport
, phys
->vl
, phys
->vl
);
653 m3x3_mulv( gate
->transport
, phys
->v_last
, phys
->v_last
);
654 m3x3_mulv( gate
->transport
, phys
->m
, phys
->m
);
655 m3x3_mulv( gate
->transport
, phys
->bob
, phys
->bob
);
657 v4f transport_rotation
;
658 m3x3_q( gate
->transport
, transport_rotation
);
659 q_mul( transport_rotation
, phys
->rb
.q
, phys
->rb
.q
);
661 world_routes_activate_gate( i
);
663 if( !phys
->on_board
)
665 v3f fwd_dir
= {cosf(player
.angles
[0]),
667 sinf(player
.angles
[0])};
668 m3x3_mulv( gate
->transport
, fwd_dir
, fwd_dir
);
670 player
.angles
[0] = atan2f( fwd_dir
[2], fwd_dir
[0] );
676 audio_play_oneshot( &audio_gate_pass
, 1.0f
);
682 rb_update_transform( &phys
->rb
);
686 * Free camera movement
688 static void player_mouseview(void)
690 if( gui_want_mouse() )
693 static v2f mouse_last
,
694 view_vel
= { 0.0f
, 0.0f
};
696 if( vg_get_button_down( "primary" ) )
697 v2_copy( vg
.mouse
, mouse_last
);
699 else if( vg_get_button( "primary" ) )
702 v2_sub( vg
.mouse
, mouse_last
, delta
);
703 v2_copy( vg
.mouse
, mouse_last
);
705 v2_muladds( view_vel
, delta
, 0.06f
*vg
.time_delta
, view_vel
);
708 v2_muladds( view_vel
, (v2f
){ vg_get_axis("h1"), vg_get_axis("v1") },
709 3.0f
* vg
.time_delta
, view_vel
);
711 v2_muls( view_vel
, 1.0f
-4.2f
*vg
.time_delta
, view_vel
);
712 v2_add( view_vel
, player
.angles
, player
.angles
);
713 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
716 static void player_freecam(void)
720 float movespeed
= fc_speed
;
721 v3f lookdir
= { 0.0f
, 0.0f
, -1.0f
},
722 sidedir
= { 1.0f
, 0.0f
, 0.0f
};
724 m3x3_mulv( player
.camera
, lookdir
, lookdir
);
725 m3x3_mulv( player
.camera
, sidedir
, sidedir
);
727 static v3f move_vel
= { 0.0f
, 0.0f
, 0.0f
};
728 if( vg_get_button( "forward" ) )
729 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
730 if( vg_get_button( "back" ) )
731 v3_muladds( move_vel
, lookdir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
732 if( vg_get_button( "left" ) )
733 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
*-movespeed
, move_vel
);
734 if( vg_get_button( "right" ) )
735 v3_muladds( move_vel
, sidedir
, VG_TIMESTEP_FIXED
* movespeed
, move_vel
);
737 v3_muls( move_vel
, 0.7f
, move_vel
);
738 v3_add( move_vel
, player
.camera_pos
, player
.camera_pos
);
741 static void player_camera_update(void)
743 /* Update camera matrices */
744 v4f qyaw
, qpitch
, qcam
;
745 q_axis_angle( qyaw
, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, -player
.angles
[0] );
746 q_axis_angle( qpitch
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, -player
.angles
[1] );
748 q_mul( qyaw
, qpitch
, qcam
);
749 q_m3x3( qcam
, player
.camera
);
751 v3_copy( player
.camera_pos
, player
.camera
[3] );
752 m4x3_invert_affine( player
.camera
, player
.camera_inverse
);
755 static int reset_player( int argc
, char const *argv
[] )
757 struct player_phys
*phys
= &player
.phys
;
758 struct respawn_point
*rp
= NULL
, *r
;
762 for( int i
=0; i
<world
.spawn_count
; i
++ )
764 r
= &world
.spawns
[i
];
765 if( !strcmp( r
->name
, argv
[0] ) )
773 vg_warn( "No spawn named '%s'\n", argv
[0] );
778 float min_dist
= INFINITY
;
780 for( int i
=0; i
<world
.spawn_count
; i
++ )
782 r
= &world
.spawns
[i
];
783 float d
= v3_dist2( r
->co
, phys
->rb
.co
);
785 vg_info( "Dist %s : %f\n", r
->name
, d
);
796 vg_error( "No spawn found\n" );
797 if( !world
.spawn_count
)
800 rp
= &world
.spawns
[0];
805 v4_copy( rp
->q
, phys
->rb
.q
);
806 v3_copy( rp
->co
, phys
->rb
.co
);
807 v3_zero( phys
->rb
.v
);
809 phys
->vswitch
= 1.0f
;
810 phys
->slip_last
= 0.0f
;
813 m3x3_identity( phys
->vr
);
815 player
.mdl
.shoes
[0] = 1;
816 player
.mdl
.shoes
[1] = 1;
818 rb_update_transform( &phys
->rb
);
823 #endif /* PLAYER_PHYSICS_H */