1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
12 VG_STATIC
void player_vector_angles( v3f angles
, v3f v
, float C
, float k
)
14 float yaw
= atan2f( v
[0], -v
[2] ),
29 VG_STATIC
float player_get_heading_yaw( player_instance
*player
)
32 q_mulv( player
->rb
.q
, (v3f
){ 0.0f
,0.0f
,1.0f
}, xz
);
33 m3x3_mulv( player
->invbasis
, xz
, xz
);
34 return atan2f( xz
[0], xz
[2] );
37 VG_STATIC
void player_camera_portal_correction( player_instance
*player
)
39 if( player
->gate_waiting
){
40 /* construct plane equation for reciever gate */
42 q_mulv( player
->gate_waiting
->q
[1], (v3f
){0.0f
,0.0f
,1.0f
}, plane
);
43 plane
[3] = v3_dot( plane
, player
->gate_waiting
->co
[1] );
45 /* check camera polarity */
46 if( v3_dot( player
->cam
.pos
, plane
) < plane
[3] ) {
47 vg_success( "Plane cleared\n" );
48 player_apply_transport_to_cam( player
->gate_waiting
->transport
);
49 player
->gate_waiting
= NULL
;
50 player
->viewable_world
= get_active_world();
53 /* de-transform camera and player back */
55 m4x3_invert_affine( player
->gate_waiting
->transport
, inverse
);
56 m4x3_mulv( inverse
, player
->cam
.pos
, player
->cam
.pos
);
58 struct skeleton
*sk
= &player
->playeravatar
->sk
;
59 skeleton_apply_transform( sk
, inverse
);
64 static v3f TEMP_TPV_EXTRA
;
66 VG_STATIC
void player__cam_iterate( player_instance
*player
)
68 struct player_avatar
*av
= player
->playeravatar
;
70 if( player
->subsystem
== k_player_subsystem_walk
){
71 v3_copy( (v3f
){-0.1f
,1.8f
,0.0f
}, player
->fpv_viewpoint
);
72 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, player
->fpv_offset
);
73 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, player
->tpv_offset
);
76 v3_copy( (v3f
){-0.15f
,1.75f
,0.0f
}, player
->fpv_viewpoint
);
78 v3_copy( (v3f
){-0.35f
,0.0f
,0.0f
}, player
->fpv_offset
);
80 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, player
->fpv_offset
);
81 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, player
->tpv_offset
);
82 v3_add( TEMP_TPV_EXTRA
, player
->tpv_offset
, player
->tpv_offset
);
85 player
->cam_velocity_constant
= 0.25f
;
86 player
->cam_velocity_coefficient
= 0.7f
;
90 player
->cam_velocity_influence_smooth
= vg_lerpf(
91 player
->cam_velocity_influence_smooth
,
92 player
->cam_velocity_influence
,
93 vg
.time_frame_delta
* 8.0f
);
95 player
->cam_velocity_coefficient_smooth
= vg_lerpf(
96 player
->cam_velocity_coefficient_smooth
,
97 player
->cam_velocity_coefficient
,
98 vg
.time_frame_delta
* 8.0f
);
100 player
->cam_velocity_constant_smooth
= vg_lerpf(
101 player
->cam_velocity_constant_smooth
,
102 player
->cam_velocity_constant
,
103 vg
.time_frame_delta
* 8.0f
);
105 enum camera_mode target_mode
= player
->camera_mode
;
107 if( player
->subsystem
== k_player_subsystem_dead
)
108 target_mode
= k_cam_thirdperson
;
110 player
->camera_type_blend
=
111 vg_lerpf( player
->camera_type_blend
,
112 (target_mode
== k_cam_firstperson
)? 1.0f
: 0.0f
,
113 5.0f
* vg
.time_frame_delta
);
115 v3_lerp( player
->fpv_viewpoint_smooth
, player
->fpv_viewpoint
,
116 vg
.time_frame_delta
* 8.0f
, player
->fpv_viewpoint_smooth
);
118 v3_lerp( player
->fpv_offset_smooth
, player
->fpv_offset
,
119 vg
.time_frame_delta
* 8.0f
, player
->fpv_offset_smooth
);
121 v3_lerp( player
->tpv_offset_smooth
, player
->tpv_offset
,
122 vg
.time_frame_delta
* 8.0f
, player
->tpv_offset_smooth
);
124 /* fov -- simple blend */
125 float fov_skate
= vg_lerpf( 97.0f
, 135.0f
, cl_fov
),
126 fov_walk
= vg_lerpf( 90.0f
, 110.0f
, cl_fov
);
128 player
->cam
.fov
= vg_lerpf( fov_walk
, fov_skate
, player
->camera_type_blend
);
131 * first person camera
135 v3f fpv_pos
, fpv_offset
;
136 m4x3_mulv( av
->sk
.final_mtx
[ av
->id_head
-1 ],
137 player
->fpv_viewpoint_smooth
, fpv_pos
);
138 m3x3_mulv( player
->rb
.to_world
, player
->fpv_offset_smooth
, fpv_offset
);
139 v3_add( fpv_offset
, fpv_pos
, fpv_pos
);
143 v3_lerp( player
->cam_velocity_smooth
, player
->rb
.v
, 4.0f
*vg
.time_frame_delta
,
144 player
->cam_velocity_smooth
);
147 m3x3_mulv( player
->invbasis
, player
->cam_velocity_smooth
, velocity_local
);
148 player_vector_angles( velocity_angles
, velocity_local
,
149 player
->cam_velocity_coefficient_smooth
,
150 player
->cam_velocity_constant_smooth
);
152 float inf_fpv
= player
->cam_velocity_influence_smooth
*
153 player
->camera_type_blend
,
154 inf_tpv
= player
->cam_velocity_influence_smooth
*
155 (1.0f
-player
->camera_type_blend
);
157 camera_lerp_angles( player
->angles
, velocity_angles
,
162 * Third person camera
165 /* no idea what this technique is called, it acts like clamped position based
166 * on some derivative of where the final camera would end up ....
168 * it is done in the local basis then transformed back */
171 v3_muls( player
->rb
.v
, 0.4f
*vg
.time_frame_delta
, future
);
172 m3x3_mulv( player
->invbasis
, future
, future
);
174 v3f camera_follow_dir
=
175 { -sinf( player
->angles
[0] ) * cosf( player
->angles
[1] ),
176 sinf( player
->angles
[1] ),
177 cosf( player
->angles
[0] ) * cosf( player
->angles
[1] ) };
180 v3_sub( camera_follow_dir
, future
, v0
);
183 v3_copy( player
->angles
, follow_angles
);
184 follow_angles
[0] = atan2f( -v0
[0], v0
[2] );
185 follow_angles
[1] = 0.3f
+ velocity_angles
[1] * 0.2f
;
187 float ya
= atan2f( -velocity_local
[1], 30.0f
);
189 follow_angles
[1] = 0.3f
+ ya
;
190 camera_lerp_angles( player
->angles
, follow_angles
,
196 rb_extrapolate( &player
->rb
, pco
, pq
);
197 v3_lerp( player
->tpv_lpf
, pco
, 20.0f
*vg
.time_frame_delta
, player
->tpv_lpf
);
199 /* now move into world */
201 m3x3_mulv( player
->basis
, camera_follow_dir
, camera_follow_dir
);
202 v3f tpv_pos
, tpv_offset
;
204 v3_muladds( player
->tpv_lpf
, camera_follow_dir
, 1.8f
, tpv_pos
);
205 q_mulv( pq
, player
->tpv_offset_smooth
, tpv_offset
);
206 v3_add( tpv_offset
, tpv_pos
, tpv_pos
);
207 v3_muladds( tpv_pos
, player
->cam_velocity_smooth
, -0.025f
, tpv_pos
);
212 v3_lerp( tpv_pos
, fpv_pos
, player
->camera_type_blend
, player
->cam
.pos
);
213 v3_copy( player
->angles
, player
->cam
.angles
);
215 float Fd
= -player
->cam_land_punch_v
* k_cam_damp
,
216 Fs
= -player
->cam_land_punch
* k_cam_spring
;
217 player
->cam_land_punch
+= player
->cam_land_punch_v
* vg
.time_frame_delta
;
218 player
->cam_land_punch_v
+= ( Fd
+ Fs
) * vg
.time_frame_delta
;
219 player
->cam
.angles
[1] += player
->cam_land_punch
;
221 /* override camera */
222 player
->cam
.angles
[0] =
223 vg_alerpf( player
->cam
.angles
[0], player
->cam_override_angles
[0],
224 player
->cam_override_strength
);
225 player
->cam
.angles
[1] =
226 vg_lerpf ( player
->cam
.angles
[1], player
->cam_override_angles
[1],
227 player
->cam_override_strength
);
228 v3_lerp( player
->cam
.pos
, player
->cam_override_pos
,
229 player
->cam_override_strength
, player
->cam
.pos
);
230 player
->cam
.fov
= vg_lerpf( player
->cam
.fov
, player
->cam_override_fov
,
231 player
->cam_override_strength
);
233 /* portal transitions */
234 player_camera_portal_correction( player
);
237 VG_STATIC
void player_look( player_instance
*player
, v3f angles
)
242 v2_copy( vg
.mouse_delta
, mouse_input
);
244 mouse_input
[1] *= -1.0f
;
245 v2_muladds( angles
, mouse_input
, 0.0025f
, angles
);
247 angles
[0] += srinput
.joy_look
[0] * vg
.time_delta
* 4.0f
;
248 float input_y
= srinput
.joy_look
[1] * vg
.time_delta
* 4.0f
;
252 angles
[1] += input_y
;
254 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
257 #endif /* PLAYER_COMMON_C */