1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
4 #include "ent_skateshop.h"
8 #include "vg/vg_perlin.h"
10 static float player_get_heading_yaw(void){
12 q_mulv( localplayer
.rb
.q
, (v3f
){ 0.0f
,0.0f
,1.0f
}, xz
);
13 return atan2f( xz
[0], xz
[2] );
16 static void player_camera_portal_correction(void){
17 if( localplayer
.gate_waiting
){
18 /* construct plane equation for reciever gate */
20 q_mulv( localplayer
.gate_waiting
->q
[1], (v3f
){0.0f
,0.0f
,1.0f
}, plane
);
21 plane
[3] = v3_dot( plane
, localplayer
.gate_waiting
->co
[1] );
23 f32 pol
= v3_dot( localplayer
.cam
.pos
, plane
) - plane
[3];
25 /* check camera polarity */
26 if( (pol
< 0.0f
) || (pol
> 5.0f
) ) {
27 vg_success( "Plane cleared\n" );
28 player_apply_transport_to_cam( localplayer
.gate_waiting
->transport
);
29 localplayer
.gate_waiting
= NULL
;
32 /* de-transform camera and player back */
34 m4x3_invert_affine( localplayer
.gate_waiting
->transport
, inverse
);
35 m4x3_mulv( inverse
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
38 v3_angles_vector( localplayer
.cam
.angles
, v0
);
39 m3x3_mulv( inverse
, v0
, v0
);
40 v3_angles( v0
, localplayer
.cam
.angles
);
42 skeleton_apply_transform( &localplayer
.skeleton
, inverse
,
43 localplayer
.final_mtx
);
48 static void player__cam_iterate(void){
49 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
51 if( localplayer
.subsystem
== k_player_subsystem_walk
){
52 v3_copy( (v3f
){-0.1f
,1.8f
,0.0f
}, cc
->fpv_viewpoint
);
53 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
54 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, cc
->tpv_offset
);
57 v3_copy( (v3f
){-0.15f
,1.75f
,0.0f
}, cc
->fpv_viewpoint
);
58 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
60 f32 h
= vg_lerpf( 0.4f
, 1.4f
, k_cam_height
);
61 v3_copy( (v3f
){0.0f
,h
,0.0f
}, cc
->tpv_offset
);
62 v3_add( cc
->tpv_offset_extra
, cc
->tpv_offset
, cc
->tpv_offset
);
65 localplayer
.cam_velocity_constant
= 0.25f
;
66 localplayer
.cam_velocity_coefficient
= 0.7f
;
70 localplayer
.cam_velocity_influence_smooth
= vg_lerpf(
71 localplayer
.cam_velocity_influence_smooth
,
72 localplayer
.cam_velocity_influence
,
73 vg
.time_frame_delta
* 8.0f
);
75 localplayer
.cam_velocity_coefficient_smooth
= vg_lerpf(
76 localplayer
.cam_velocity_coefficient_smooth
,
77 localplayer
.cam_velocity_coefficient
,
78 vg
.time_frame_delta
* 8.0f
);
80 localplayer
.cam_velocity_constant_smooth
= vg_lerpf(
81 localplayer
.cam_velocity_constant_smooth
,
82 localplayer
.cam_velocity_constant
,
83 vg
.time_frame_delta
* 8.0f
);
85 enum camera_mode target_mode
= cc
->camera_mode
;
87 if( localplayer
.subsystem
== k_player_subsystem_dead
)
88 target_mode
= k_cam_thirdperson
;
90 cc
->camera_type_blend
=
91 vg_lerpf( cc
->camera_type_blend
,
92 (target_mode
== k_cam_firstperson
)? 1.0f
: 0.0f
,
93 5.0f
* vg
.time_frame_delta
);
95 v3_lerp( cc
->fpv_viewpoint_smooth
, cc
->fpv_viewpoint
,
96 vg
.time_frame_delta
* 8.0f
, cc
->fpv_viewpoint_smooth
);
98 v3_lerp( cc
->fpv_offset_smooth
, cc
->fpv_offset
,
99 vg
.time_frame_delta
* 8.0f
, cc
->fpv_offset_smooth
);
101 v3_lerp( cc
->tpv_offset_smooth
, cc
->tpv_offset
,
102 vg
.time_frame_delta
* 8.0f
, cc
->tpv_offset_smooth
);
104 /* fov -- simple blend */
105 float fov_skate
= vg_lerpf( 97.0f
, 135.0f
, k_fov
),
106 fov_walk
= vg_lerpf( 90.0f
, 110.0f
, k_fov
);
108 localplayer
.cam
.fov
= vg_lerpf( fov_walk
, fov_skate
, cc
->camera_type_blend
);
111 * first person camera
115 v3f fpv_pos
, fpv_offset
;
116 m4x3_mulv( localplayer
.final_mtx
[ localplayer
.id_head
-1 ],
117 cc
->fpv_viewpoint_smooth
, fpv_pos
);
118 m3x3_mulv( localplayer
.rb
.to_world
, cc
->fpv_offset_smooth
, fpv_offset
);
119 v3_add( fpv_offset
, fpv_pos
, fpv_pos
);
123 v3_lerp( cc
->cam_velocity_smooth
, localplayer
.rb
.v
, 4.0f
*vg
.time_frame_delta
,
124 cc
->cam_velocity_smooth
);
126 v3_angles( cc
->cam_velocity_smooth
, velocity_angles
);
127 velocity_angles
[1] *= localplayer
.cam_velocity_coefficient_smooth
;
128 velocity_angles
[1] += localplayer
.cam_velocity_constant_smooth
;
130 float inf_fpv
= localplayer
.cam_velocity_influence_smooth
*
131 cc
->camera_type_blend
,
132 inf_tpv
= localplayer
.cam_velocity_influence_smooth
*
133 (1.0f
-cc
->camera_type_blend
);
135 camera_lerp_angles( localplayer
.angles
, velocity_angles
,
137 localplayer
.angles
);
140 * Third person camera
143 /* no idea what this technique is called, it acts like clamped position based
144 * on some derivative of where the final camera would end up ....
146 * it is done in the local basis then transformed back */
149 v3_muls( localplayer
.rb
.v
, 0.4f
*vg
.time_frame_delta
, future
);
151 v3f camera_follow_dir
=
152 { -sinf( localplayer
.angles
[0] ) * cosf( localplayer
.angles
[1] ),
153 sinf( localplayer
.angles
[1] ),
154 cosf( localplayer
.angles
[0] ) * cosf( localplayer
.angles
[1] ) };
157 v3_sub( camera_follow_dir
, future
, v0
);
160 v3_copy( localplayer
.angles
, follow_angles
);
161 follow_angles
[0] = atan2f( -v0
[0], v0
[2] );
162 follow_angles
[1] = 0.3f
+ velocity_angles
[1] * 0.2f
;
164 float ya
= atan2f( -cc
->cam_velocity_smooth
[1], 30.0f
);
166 follow_angles
[1] = 0.3f
+ ya
;
167 camera_lerp_angles( localplayer
.angles
, follow_angles
,
169 localplayer
.angles
);
173 rb_extrapolate( &localplayer
.rb
, pco
, pq
);
174 v3_muladds( pco
, localplayer
.holdout_pose
.root_co
,
175 localplayer
.holdout_time
, pco
);
176 v3_lerp( cc
->tpv_lpf
, pco
, 20.0f
*vg
.time_frame_delta
, cc
->tpv_lpf
);
178 /* now move into world */
179 v3f tpv_pos
, tpv_offset
, tpv_origin
;
182 q_mulv( pq
, cc
->tpv_offset_smooth
, tpv_origin
);
183 v3_add( tpv_origin
, cc
->tpv_lpf
, tpv_origin
);
186 v3_muls( camera_follow_dir
, 1.8f
, tpv_offset
);
187 v3_muladds( tpv_offset
, cc
->cam_velocity_smooth
, -0.025f
, tpv_offset
);
189 v3_add( tpv_origin
, tpv_offset
, tpv_pos
);
194 v3_lerp( tpv_pos
, fpv_pos
, cc
->camera_type_blend
, localplayer
.cam
.pos
);
195 v3_copy( localplayer
.angles
, localplayer
.cam
.angles
);
198 f32 speed
= v3_length(localplayer
.rb
.v
),
199 strength
= k_cam_shake_strength
* speed
;
200 localplayer
.cam_trackshake
+=
201 speed
*k_cam_shake_trackspeed
*vg
.time_frame_delta
;
203 v2f rnd
= {perlin1d( localplayer
.cam_trackshake
, 1.0f
, 4, 20 ),
204 perlin1d( localplayer
.cam_trackshake
, 1.0f
, 4, 63 ) };
205 v2_muladds( localplayer
.cam
.angles
, rnd
, strength
, localplayer
.cam
.angles
);
208 v3_muls( localplayer
.cam_land_punch_v
, -k_cam_damp
, Fd
);
209 v3_muls( localplayer
.cam_land_punch
, -k_cam_spring
, Fs
);
210 v3_muladds( localplayer
.cam_land_punch
, localplayer
.cam_land_punch_v
,
211 vg
.time_frame_delta
, localplayer
.cam_land_punch
);
213 v3_muladds( localplayer
.cam_land_punch_v
, F
, vg
.time_frame_delta
,
214 localplayer
.cam_land_punch_v
);
215 v3_add( localplayer
.cam_land_punch
, localplayer
.cam
.pos
,
216 localplayer
.cam
.pos
);
218 if( k_cinema
>= 0.0001f
){
219 ent_camera
*cam
= NULL
;
220 f32 min_dist
= k_cinema
;
222 world_instance
*world
= world_current_instance();
223 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_camera
); i
++ ){
224 ent_camera
*c
= mdl_arritm(&world
->ent_camera
,i
);
226 f32 dist
= v3_dist( c
->transform
.co
, localplayer
.rb
.co
);
228 if( dist
< min_dist
){
235 localplayer
.cam
.fov
= cam
->fov
;
236 v3_copy( cam
->transform
.co
, localplayer
.cam
.pos
);
239 mdl_transform_vector( &cam
->transform
, (v3f
){0.0f
,-1.0f
,0.0f
}, v0
);
241 v3_sub( localplayer
.rb
.co
, cam
->transform
.co
, v0
);
243 v3_angles( v0
, localplayer
.cam
.angles
);
247 /* portal transitions */
248 player_camera_portal_correction();
251 static void player_look( v3f angles
, float speed
){
252 if( vg_ui
.wants_mouse
) return;
257 v2_copy( vg
.mouse_delta
, mouse_input
);
258 if( k_invert_y
) mouse_input
[1] *= -1.0f
;
259 v2_muladds( angles
, mouse_input
, 0.0025f
* speed
, angles
);
262 joystick_state( k_srjoystick_look
, jlook
);
264 angles
[0] += jlook
[0] * vg
.time_frame_delta
* 4.0f
* speed
;
265 float input_y
= jlook
[1] * vg
.time_frame_delta
* 4.0f
;
266 if( k_invert_y
) input_y
*= -1.0f
;
268 angles
[1] += input_y
* speed
;
269 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
272 #endif /* PLAYER_COMMON_C */