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
;
30 localplayer
.viewable_world
= world_current_instance();
33 /* de-transform camera and player back */
35 m4x3_invert_affine( localplayer
.gate_waiting
->transport
, inverse
);
36 m4x3_mulv( inverse
, localplayer
.cam
.pos
, localplayer
.cam
.pos
);
39 v3_angles_vector( localplayer
.cam
.angles
, v0
);
40 m3x3_mulv( inverse
, v0
, v0
);
41 v3_angles( v0
, localplayer
.cam
.angles
);
43 skeleton_apply_transform( &localplayer
.skeleton
, inverse
,
44 localplayer
.final_mtx
);
49 static void player__cam_iterate(void){
50 struct player_cam_controller
*cc
= &localplayer
.cam_control
;
52 if( localplayer
.subsystem
== k_player_subsystem_walk
){
53 v3_copy( (v3f
){-0.1f
,1.8f
,0.0f
}, cc
->fpv_viewpoint
);
54 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
55 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, cc
->tpv_offset
);
58 v3_copy( (v3f
){-0.15f
,1.75f
,0.0f
}, cc
->fpv_viewpoint
);
59 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
61 f32 h
= vg_lerpf( 0.4f
, 1.4f
, k_cam_height
);
62 v3_copy( (v3f
){0.0f
,h
,0.0f
}, cc
->tpv_offset
);
63 v3_add( cc
->tpv_offset_extra
, cc
->tpv_offset
, cc
->tpv_offset
);
66 localplayer
.cam_velocity_constant
= 0.25f
;
67 localplayer
.cam_velocity_coefficient
= 0.7f
;
71 localplayer
.cam_velocity_influence_smooth
= vg_lerpf(
72 localplayer
.cam_velocity_influence_smooth
,
73 localplayer
.cam_velocity_influence
,
74 vg
.time_frame_delta
* 8.0f
);
76 localplayer
.cam_velocity_coefficient_smooth
= vg_lerpf(
77 localplayer
.cam_velocity_coefficient_smooth
,
78 localplayer
.cam_velocity_coefficient
,
79 vg
.time_frame_delta
* 8.0f
);
81 localplayer
.cam_velocity_constant_smooth
= vg_lerpf(
82 localplayer
.cam_velocity_constant_smooth
,
83 localplayer
.cam_velocity_constant
,
84 vg
.time_frame_delta
* 8.0f
);
86 enum camera_mode target_mode
= cc
->camera_mode
;
88 if( localplayer
.subsystem
== k_player_subsystem_dead
)
89 target_mode
= k_cam_thirdperson
;
91 cc
->camera_type_blend
=
92 vg_lerpf( cc
->camera_type_blend
,
93 (target_mode
== k_cam_firstperson
)? 1.0f
: 0.0f
,
94 5.0f
* vg
.time_frame_delta
);
96 v3_lerp( cc
->fpv_viewpoint_smooth
, cc
->fpv_viewpoint
,
97 vg
.time_frame_delta
* 8.0f
, cc
->fpv_viewpoint_smooth
);
99 v3_lerp( cc
->fpv_offset_smooth
, cc
->fpv_offset
,
100 vg
.time_frame_delta
* 8.0f
, cc
->fpv_offset_smooth
);
102 v3_lerp( cc
->tpv_offset_smooth
, cc
->tpv_offset
,
103 vg
.time_frame_delta
* 8.0f
, cc
->tpv_offset_smooth
);
105 /* fov -- simple blend */
106 float fov_skate
= vg_lerpf( 97.0f
, 135.0f
, k_fov
),
107 fov_walk
= vg_lerpf( 90.0f
, 110.0f
, k_fov
);
109 localplayer
.cam
.fov
= vg_lerpf( fov_walk
, fov_skate
, cc
->camera_type_blend
);
112 * first person camera
116 v3f fpv_pos
, fpv_offset
;
117 m4x3_mulv( localplayer
.final_mtx
[ localplayer
.id_head
-1 ],
118 cc
->fpv_viewpoint_smooth
, fpv_pos
);
119 m3x3_mulv( localplayer
.rb
.to_world
, cc
->fpv_offset_smooth
, fpv_offset
);
120 v3_add( fpv_offset
, fpv_pos
, fpv_pos
);
124 v3_lerp( cc
->cam_velocity_smooth
, localplayer
.rb
.v
, 4.0f
*vg
.time_frame_delta
,
125 cc
->cam_velocity_smooth
);
127 v3_angles( cc
->cam_velocity_smooth
, velocity_angles
);
128 velocity_angles
[1] *= localplayer
.cam_velocity_coefficient_smooth
;
129 velocity_angles
[1] += localplayer
.cam_velocity_constant_smooth
;
131 float inf_fpv
= localplayer
.cam_velocity_influence_smooth
*
132 cc
->camera_type_blend
,
133 inf_tpv
= localplayer
.cam_velocity_influence_smooth
*
134 (1.0f
-cc
->camera_type_blend
);
136 camera_lerp_angles( localplayer
.angles
, velocity_angles
,
138 localplayer
.angles
);
141 * Third person camera
144 /* no idea what this technique is called, it acts like clamped position based
145 * on some derivative of where the final camera would end up ....
147 * it is done in the local basis then transformed back */
150 v3_muls( localplayer
.rb
.v
, 0.4f
*vg
.time_frame_delta
, future
);
152 v3f camera_follow_dir
=
153 { -sinf( localplayer
.angles
[0] ) * cosf( localplayer
.angles
[1] ),
154 sinf( localplayer
.angles
[1] ),
155 cosf( localplayer
.angles
[0] ) * cosf( localplayer
.angles
[1] ) };
158 v3_sub( camera_follow_dir
, future
, v0
);
161 v3_copy( localplayer
.angles
, follow_angles
);
162 follow_angles
[0] = atan2f( -v0
[0], v0
[2] );
163 follow_angles
[1] = 0.3f
+ velocity_angles
[1] * 0.2f
;
165 float ya
= atan2f( -cc
->cam_velocity_smooth
[1], 30.0f
);
167 follow_angles
[1] = 0.3f
+ ya
;
168 camera_lerp_angles( localplayer
.angles
, follow_angles
,
170 localplayer
.angles
);
174 rb_extrapolate( &localplayer
.rb
, pco
, pq
);
175 v3_muladds( pco
, localplayer
.holdout_pose
.root_co
,
176 localplayer
.holdout_time
, pco
);
177 v3_lerp( cc
->tpv_lpf
, pco
, 20.0f
*vg
.time_frame_delta
, cc
->tpv_lpf
);
179 /* now move into world */
180 v3f tpv_pos
, tpv_offset
, tpv_origin
;
183 q_mulv( pq
, cc
->tpv_offset_smooth
, tpv_origin
);
184 v3_add( tpv_origin
, cc
->tpv_lpf
, tpv_origin
);
187 v3_muls( camera_follow_dir
, 1.8f
, tpv_offset
);
188 v3_muladds( tpv_offset
, cc
->cam_velocity_smooth
, -0.025f
, tpv_offset
);
190 v3_add( tpv_origin
, tpv_offset
, tpv_pos
);
195 v3_lerp( tpv_pos
, fpv_pos
, cc
->camera_type_blend
, localplayer
.cam
.pos
);
196 v3_copy( localplayer
.angles
, localplayer
.cam
.angles
);
199 f32 speed
= v3_length(localplayer
.rb
.v
),
200 strength
= k_cam_shake_strength
* speed
;
201 localplayer
.cam_trackshake
+=
202 speed
*k_cam_shake_trackspeed
*vg
.time_frame_delta
;
204 v2f rnd
= {perlin1d( localplayer
.cam_trackshake
, 1.0f
, 4, 20 ),
205 perlin1d( localplayer
.cam_trackshake
, 1.0f
, 4, 63 ) };
206 v2_muladds( localplayer
.cam
.angles
, rnd
, strength
, localplayer
.cam
.angles
);
209 v3_muls( localplayer
.cam_land_punch_v
, -k_cam_damp
, Fd
);
210 v3_muls( localplayer
.cam_land_punch
, -k_cam_spring
, Fs
);
211 v3_muladds( localplayer
.cam_land_punch
, localplayer
.cam_land_punch_v
,
212 vg
.time_frame_delta
, localplayer
.cam_land_punch
);
214 v3_muladds( localplayer
.cam_land_punch_v
, F
, vg
.time_frame_delta
,
215 localplayer
.cam_land_punch_v
);
216 v3_add( localplayer
.cam_land_punch
, localplayer
.cam
.pos
,
217 localplayer
.cam
.pos
);
219 if( k_cinema
>= 0.0001f
){
220 ent_camera
*cam
= NULL
;
221 f32 min_dist
= k_cinema
;
223 world_instance
*world
= localplayer
.viewable_world
;
224 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_camera
); i
++ ){
225 ent_camera
*c
= mdl_arritm(&world
->ent_camera
,i
);
227 f32 dist
= v3_dist( c
->transform
.co
, localplayer
.rb
.co
);
229 if( dist
< min_dist
){
236 localplayer
.cam
.fov
= cam
->fov
;
237 v3_copy( cam
->transform
.co
, localplayer
.cam
.pos
);
240 mdl_transform_vector( &cam
->transform
, (v3f
){0.0f
,-1.0f
,0.0f
}, v0
);
242 v3_sub( localplayer
.rb
.co
, cam
->transform
.co
, v0
);
244 v3_angles( v0
, localplayer
.cam
.angles
);
248 /* portal transitions */
249 player_camera_portal_correction();
252 static void player_look( v3f angles
, float speed
){
253 if( vg_ui
.wants_mouse
) return;
258 v2_copy( vg
.mouse_delta
, mouse_input
);
259 if( k_invert_y
) mouse_input
[1] *= -1.0f
;
260 v2_muladds( angles
, mouse_input
, 0.0025f
* speed
, angles
);
263 joystick_state( k_srjoystick_look
, jlook
);
265 angles
[0] += jlook
[0] * vg
.time_frame_delta
* 4.0f
* speed
;
266 float input_y
= jlook
[1] * vg
.time_frame_delta
* 4.0f
;
267 if( k_invert_y
) input_y
*= -1.0f
;
269 angles
[1] += input_y
* speed
;
270 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
273 #endif /* PLAYER_COMMON_C */