2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * TODO: Tilt camera down to face borde when its behind you or out of vision
12 #define PLAYER_REWIND_FRAMES 60*4
23 k_air_accelerate
= 20.0f
,
26 k_board_radius
= 0.3f
,
27 k_board_length
= 0.45f
,
28 k_board_allowance
= 0.04f
,
29 //k_friction_lat = 8.8f,
30 k_friction_lat
= 12.0f
,
31 k_friction_resistance
= 0.01f
,
32 k_max_push_speed
= 16.0f
,
34 k_push_cycle_rate
= 8.0f
,
35 k_steer_ground
= 2.5f
,
37 k_steer_air_lerp
= 0.3f
,
40 k_walk_downforce
= 8.0f
,
41 k_jump_charge_speed
= (1.0f
/1.0f
),
45 k_walk_accel
= 150.0f
,
46 k_walk_friction
= 8.0f
;
48 VG_STATIC
int freecam
= 0;
49 VG_STATIC
int walk_grid_iterations
= 1;
50 VG_STATIC
float fc_speed
= 10.0f
;
53 * -----------------------------------------------------------------------------
55 * -----------------------------------------------------------------------------
58 VG_STATIC
struct gplayer
61 rigidbody collide_front
, collide_back
;
65 rigidbody rb
, rb_gate_frame
;
66 float iY
, siY
; /* Yaw inertia */
68 v3f a
, v_last
, m
, bob
, vl
;
71 float vswitch
, slip
, slip_last
, reverse
;
73 float grab
, jump
, pushing
, push_time
;
79 int in_air
, on_board
, jump_charge
, jump_dir
, grind
;
86 m4x3f visual_transform
,
89 int is_dead
, death_tick_allowance
, rewinding
;
90 int rewind_sound_wait
;
103 v3f handl_target
, handr_target
,
107 struct input_binding
*input_js1h
,
124 v3f camera_pos
, smooth_localcam
;
133 u32 rewind_incrementer
,
136 float rewind_time
, rewind_total_length
, rewind_predicted_time
;
137 double diag_rewind_start
, diag_rewind_time
;
157 enum mdl_surface_prop surface_prop
;
162 glmesh player_meshes
[3];
166 struct skeleton_anim
*anim_stand
,
170 *anim_push
, *anim_push_reverse
,
171 *anim_ollie
, *anim_ollie_reverse
,
172 *anim_grabs
, *anim_stop
,
173 *anim_walk
, *anim_run
, *anim_idle
,
205 .collide_front
= { .type
= k_rb_shape_sphere
, .inf
.sphere
.radius
= 0.3f
},
206 .collide_back
= { .type
= k_rb_shape_sphere
, .inf
.sphere
.radius
= 0.3f
}
212 VG_STATIC
float *player_get_pos(void);
213 VG_STATIC
void player_kill(void);
214 VG_STATIC
float *player_cam_pos(void);
215 VG_STATIC
void player_save_frame(void);
216 VG_STATIC
void player_restore_frame(void);
217 VG_STATIC
void player_save_rewind_frame(void);
222 VG_STATIC
void player_mouseview(void);
224 #include "player_physics.h"
225 #include "player_ragdoll.h"
226 #include "player_model.h"
227 #include "player_animation.h"
228 #include "player_audio.h"
231 * -----------------------------------------------------------------------------
233 * -----------------------------------------------------------------------------
236 VG_STATIC
void player_init(void) /* 1 */
238 player
.input_js1h
= vg_create_named_input( "steer-h", k_input_type_axis
);
239 player
.input_js1v
= vg_create_named_input( "steer-v", k_input_type_axis
);
240 player
.input_grab
= vg_create_named_input( "grab", k_input_type_axis_norm
);
241 player
.input_js2h
= vg_create_named_input( "grab-h", k_input_type_axis
);
242 player
.input_js2v
= vg_create_named_input( "grab-v", k_input_type_axis
);
243 player
.input_jump
= vg_create_named_input( "jump", k_input_type_button
);
244 player
.input_push
= vg_create_named_input( "push", k_input_type_button
);
245 player
.input_walk
= vg_create_named_input( "walk", k_input_type_button
);
247 player
.input_walkh
= vg_create_named_input( "walk-h",
249 player
.input_walkv
= vg_create_named_input( "walk-v",
253 player
.input_switch_mode
= vg_create_named_input( "switch-mode",
254 k_input_type_button
);
255 player
.input_reset
= vg_create_named_input( "reset", k_input_type_button
);
257 const char *default_cfg
[] =
259 "bind steer-h gp-ls-h",
263 "bind steer-v gp-ls-v",
269 "bind grab-h gp-rs-h",
270 "bind grab-v gp-rs-v",
281 "bind walk-h gp-ls-h",
282 "bind walk-v -gp-ls-v",
291 "bind switch-mode gp-y",
292 "bind switch-mode e",
295 for( int i
=0; i
<vg_list_size(default_cfg
); i
++ )
296 vg_execute_console_input(default_cfg
[i
]);
298 rb_init( &player
.phys
.rb
);
299 rb_init( &player
.collide_front
);
300 rb_init( &player
.collide_back
);
302 vg_convar_push( (struct vg_convar
){
303 .name
= "gwalk_speed",
304 .data
= &k_walkspeed
,
305 .data_type
= k_convar_dtype_f32
,
306 .opt_f32
= { .clamp
= 0 },
310 vg_convar_push( (struct vg_convar
){
311 .name
= "air_accelerate",
312 .data
= &k_air_accelerate
,
313 .data_type
= k_convar_dtype_f32
,
314 .opt_f32
= { .clamp
= 0 },
318 vg_convar_push( (struct vg_convar
){
321 .data_type
= k_convar_dtype_f32
,
322 .opt_f32
= { .clamp
= 0 },
326 vg_convar_push( (struct vg_convar
){
327 .name
= "walk_accel",
328 .data
= &k_walk_accel
,
329 .data_type
= k_convar_dtype_f32
,
330 .opt_f32
= { .clamp
= 0 },
334 vg_convar_push( (struct vg_convar
){
337 .data_type
= k_convar_dtype_i32
,
338 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
342 vg_convar_push( (struct vg_convar
){
345 .data_type
= k_convar_dtype_f32
,
346 .opt_f32
= { .clamp
= 0 },
350 vg_function_push( (struct vg_cmd
){
352 .function
= reset_player
355 player
.rewind_length
= 0;
356 player
.rewind_buffer
=
357 vg_linear_alloc( vg_mem
.rtmemory
,
358 sizeof(struct rewind_frame
) * PLAYER_REWIND_FRAMES
);
366 VG_STATIC
void player_save_rewind_frame(void)
368 if( player
.rewind_length
< PLAYER_REWIND_FRAMES
)
370 struct rewind_frame
*fr
=
371 &player
.rewind_buffer
[ player
.rewind_length
++ ];
373 v2_copy( player
.angles
, fr
->ang
);
374 v3_copy( player
.camera_pos
, fr
->pos
);
376 player
.rewind_incrementer
= 0;
378 if( player
.rewind_length
> 1 )
380 player
.rewind_total_length
+=
381 v3_dist( player
.rewind_buffer
[player
.rewind_length
-1].pos
,
382 player
.rewind_buffer
[player
.rewind_length
-2].pos
);
389 VG_STATIC
int menu_enabled(void);
393 * Free camera movement
395 VG_STATIC
void player_mouseview(void)
400 v2_muladds( player
.angles
, vg
.mouse_delta
, 0.0025f
, player
.angles
);
402 if( vg_input
.controller_should_use_trackpad_look
)
404 static v2f last_input
;
406 static v2f vel_smooth
;
408 v2f input
= { player
.input_js2h
->axis
.value
,
409 player
.input_js2v
->axis
.value
};
411 if( (v2_length2(last_input
) > 0.001f
) && (v2_length2(input
) > 0.001f
) )
413 v2_sub( input
, last_input
, vel
);
414 v2_muls( vel
, 1.0f
/vg
.time_delta
, vel
);
421 v2_lerp( vel_smooth
, vel
, vg
.time_delta
*8.0f
, vel_smooth
);
423 v2_muladds( player
.angles
, vel_smooth
, vg
.time_delta
, player
.angles
);
424 v2_copy( input
, last_input
);
428 player
.angles
[0] += player
.input_js2h
->axis
.value
* vg
.time_delta
* 4.0f
;
429 player
.angles
[1] += player
.input_js2v
->axis
.value
* vg
.time_delta
* 4.0f
;
432 player
.angles
[1] = vg_clampf( player
.angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
435 /* Deal with input etc */
436 VG_STATIC
void player_update_pre(void)
438 struct player_phys
*phys
= &player
.phys
;
440 if( player
.rewinding
)
445 if( vg_input_button_down( player
.input_reset
) && !menu_enabled() )
449 reset_player( 0, NULL
);
451 audio_play_oneshot( &audio_ui
[0], 1.0f
);
456 double delta
= world
.time
- world
.last_use
;
458 if( (delta
<= RESET_MAX_TIME
) && (world
.last_use
!= 0.0) )
460 player
.rewinding
= 1;
461 player
.rewind_sound_wait
= 1;
462 player
.rewind_time
= (float)player
.rewind_length
- 0.0001f
;
463 player_save_rewind_frame();
465 audio_play_oneshot( &audio_rewind
[0], 1.0f
);
468 /* based on analytical testing. DONT CHANGE!
470 * time taken: y = (x^(4/5)) * 74.5
471 * inverse : x = (2/149)^(4/5) * y^(4/5)
474 float constant
= powf( 2.0f
/149.0f
, 4.0f
/5.0f
),
475 curve
= powf( player
.rewind_total_length
, 4.0f
/5.0f
);
477 player
.rewind_predicted_time
= constant
* curve
;
478 player
.diag_rewind_start
= vg
.time
;
479 player
.diag_rewind_time
= player
.rewind_time
;
482 player
.death_tick_allowance
= 30;
483 player_restore_frame();
485 if( !phys
->on_board
)
487 player
.angles
[0] = atan2f( -phys
->rb
.forward
[2],
488 -phys
->rb
.forward
[0] );
491 player
.mdl
.shoes
[0] = 1;
492 player
.mdl
.shoes
[1] = 1;
494 world_routes_notify_reset();
496 /* apply 1 frame of movement */
503 audio_play_oneshot( &audio_rewind
[4], 1.0f
);
509 if( vg_input_button_down( player
.input_switch_mode
) && !menu_enabled() )
511 phys
->on_board
^= 0x1;
516 v3_muladds( phys
->rb
.v
, phys
->rb
.forward
, 0.2f
, phys
->rb
.v
);
517 audio_play_oneshot( &audio_lands
[6], 1.0f
);
521 audio_play_oneshot( &audio_lands
[5], 1.0f
);
527 if( !phys
->on_board
)
531 VG_STATIC
void player_update_fixed(void) /* 2 */
533 if( player
.rewinding
)
536 if( player
.death_tick_allowance
)
537 player
.death_tick_allowance
--;
539 struct player_phys
*phys
= &player
.phys
;
543 player_ragdoll_iter();
547 player
.rewind_incrementer
++;
549 if( player
.rewind_incrementer
> (u32
)(0.25/VG_TIMESTEP_FIXED
) )
551 player_save_rewind_frame();
558 VG_STATIC
void player_update_post(void)
560 for( int i
=0; i
<player
.land_log_count
; i
++ )
562 struct land_log
*log
= &player
.land_log
[i
];
564 for( int j
=0; j
<log
->count
- 1; j
++ )
565 vg_line( log
->positions
[j
], log
->positions
[j
+1], log
->colour
);
567 vg_line_cross( log
->positions
[log
->count
-1], log
->colour
, 0.25f
);
572 player_debug_ragdoll();
575 player_animate_death_cam();
582 player_animate_camera();
589 /* CAMERA POSITIONING: LAYER 0 */
590 v2_copy( player
.angles
, main_camera
.angles
);
591 v3_copy( player
.camera_pos
, main_camera
.pos
);
593 if( player
.rewinding
)
595 if( player
.rewind_time
<= 0.0f
)
597 double taken
= vg
.time
- player
.diag_rewind_start
;
598 vg_success( "Rewind took (rt, pl, tl): %f, %f, %f\n",
599 taken
, player
.diag_rewind_time
,
600 player
.rewind_total_length
);
602 player
.rewinding
= 0;
603 player
.rewind_length
= 1;
604 player
.rewind_total_length
= 0.0f
;
605 player
.rewind_incrementer
= 0;
606 world
.sky_target_rate
= 1.0;
610 world
.sky_target_rate
= -100.0;
611 assert( player
.rewind_length
> 0 );
616 float budget
= vg
.time_delta
,
617 overall_length
= player
.rewind_length
;
619 world_routes_rollback_time( player
.rewind_time
/ overall_length
);
621 for( int i
=0; (i
<10)&&(player
.rewind_time
>0.0f
)&&(budget
>0.0f
); i
++ )
623 /* Interpolate frames */
624 int i0
= floorf( player
.rewind_time
),
625 i1
= VG_MIN( i0
+1, player
.rewind_length
-1 );
627 struct rewind_frame
*fr
= &player
.rewind_buffer
[i0
],
628 *fr1
= &player
.rewind_buffer
[i1
];
630 float dist
= vg_maxf( v3_dist( fr
->pos
, fr1
->pos
), 0.001f
),
631 subl
= vg_fractf( player
.rewind_time
) + 0.001f
,
633 sramp
= 3.0f
-(1.0f
/(0.4f
+0.4f
*player
.rewind_time
)),
634 speed
= sramp
*28.0f
+ 0.5f
*player
.rewind_time
,
635 mod
= speed
* (budget
/ dist
),
637 advl
= vg_minf( mod
, subl
),
638 advt
= (advl
/ mod
) * budget
;
640 player
.dist_accum
+= speed
* advt
;
641 player
.rewind_time
-= advl
;
645 player
.rewind_time
= vg_maxf( 0.0f
, player
.rewind_time
);
647 float current_time
= vg
.time
- player
.diag_rewind_start
,
648 remaining
= player
.rewind_predicted_time
- current_time
;
650 if( player
.rewind_sound_wait
)
652 if( player
.rewind_predicted_time
>= 6.5f
)
654 if( remaining
<= 6.5f
)
657 audio_play_oneshot( &audio_rewind
[3], 1.0f
);
659 player
.rewind_sound_wait
= 0;
662 else if( player
.rewind_predicted_time
>= 2.5f
)
664 if( remaining
<= 2.5f
)
667 audio_play_oneshot( &audio_rewind
[2], 1.0f
);
669 player
.rewind_sound_wait
= 0;
672 else if( player
.rewind_predicted_time
>= 1.5f
)
674 if( remaining
<= 1.5f
)
677 audio_play_oneshot( &audio_rewind
[1], 1.0f
);
679 player
.rewind_sound_wait
= 0;
684 int i0
= floorf( player
.rewind_time
),
685 i1
= VG_MIN( i0
+1, player
.rewind_length
-1 );
687 struct rewind_frame
*fr
= &player
.rewind_buffer
[i0
],
688 *fr1
= &player
.rewind_buffer
[i1
];
690 float sub
= vg_fractf(player
.rewind_time
);
692 v3_lerp( fr
->pos
, fr1
->pos
, sub
, override_pos
);
693 override_angles
[0] = vg_alerpf( fr
->ang
[0], fr1
->ang
[0], sub
);
694 override_angles
[1] = vg_lerpf ( fr
->ang
[1], fr1
->ang
[1], sub
);
696 /* CAMERA POSITIONING: LAYER 1 */
697 float blend
= (4.0f
-player
.rewind_time
) * 0.25f
,
698 c
= vg_clampf( blend
, 0.0f
, 1.0f
);
700 main_camera
.angles
[0] =
701 vg_alerpf(override_angles
[0], player
.angles
[0], c
);
702 main_camera
.angles
[1] =
703 vg_lerpf (override_angles
[1], player
.angles
[1], c
);
704 v3_lerp( override_pos
, player
.camera_pos
, c
, main_camera
.pos
);
708 camera_update_transform( &main_camera
);
712 VG_STATIC
void draw_player( camera
*cam
)
715 player_model_copy_ragdoll();
717 shader_viewchar_use();
718 vg_tex2d_bind( &tex_characters
, 0 );
719 shader_viewchar_uTexMain( 0 );
720 shader_viewchar_uCamera( cam
->transform
[3] );
721 shader_viewchar_uPv( cam
->mtx
.pv
);
722 shader_link_standard_ub( _shader_viewchar
.id
, 2 );
723 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms
,
724 player
.mdl
.sk
.bone_count
,
726 (float *)player
.mdl
.sk
.final_mtx
);
728 mesh_bind( &player
.mdl
.player_meshes
[cl_playermdl_id
] );
729 mesh_draw( &player
.mdl
.player_meshes
[cl_playermdl_id
] );
733 * -----------------------------------------------------------------------------
735 * -----------------------------------------------------------------------------
738 VG_STATIC
float *player_get_pos(void)
740 return player
.phys
.rb
.co
;
743 VG_STATIC
void player_kill(void)
745 if( player
.death_tick_allowance
== 0 )
748 player_ragdoll_copy_model( player
.phys
.rb
.v
);
749 world_routes_clear();
753 VG_STATIC
float *player_cam_pos(void)
755 return player
.camera_pos
;
759 #endif /* PLAYER_H */