1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
6 VG_STATIC
void player_vector_angles( v3f angles
, v3f v
, float C
, float k
)
8 float yaw
= atan2f( v
[0], -v
[2] ),
22 VG_STATIC
float player_get_heading_yaw( player_instance
*player
)
25 q_mulv( player
->rb
.q
, (v3f
){ 0.0f
,0.0f
,1.0f
}, xz
);
26 m3x3_mulv( player
->invbasis
, xz
, xz
);
27 return atan2f( xz
[0], xz
[2] );
30 VG_STATIC
void player_camera_portal_correction( player_instance
*player
)
32 if( player
->gate_waiting
)
34 /* construct plane equation for reciever gate */
36 q_mulv( player
->gate_waiting
->q
[1], (v3f
){0.0f
,0.0f
,1.0f
}, plane
);
37 plane
[3] = v3_dot( plane
, player
->gate_waiting
->co
[1] );
39 /* check camera polarity */
40 if( v3_dot( player
->cam
.pos
, plane
) < plane
[3] )
42 vg_success( "Plane cleared\n" );
43 player_apply_transport_to_cam( player
->gate_waiting
->transport
);
44 player
->gate_waiting
= NULL
;
45 player
->viewable_world
= get_active_world();
49 /* de-transform camera and player back */
51 m4x3_invert_affine( player
->gate_waiting
->transport
, inverse
);
52 m4x3_mulv( inverse
, player
->cam
.pos
, player
->cam
.pos
);
54 struct skeleton
*sk
= &player
->playeravatar
->sk
;
55 skeleton_apply_transform( sk
, inverse
);
60 static v3f TEMP_TPV_EXTRA
;
62 VG_STATIC
void player__cam_iterate( player_instance
*player
)
64 struct player_avatar
*av
= player
->playeravatar
;
66 if( player
->subsystem
== k_player_subsystem_walk
){
67 v3_copy( (v3f
){-0.1f
,1.8f
,0.0f
}, player
->fpv_viewpoint
);
68 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, player
->fpv_offset
);
69 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, player
->tpv_offset
);
72 v3_copy( (v3f
){-0.15f
,1.7f
,0.0f
}, player
->fpv_viewpoint
);
74 v3_copy( (v3f
){-0.35f
,0.0f
,0.0f
}, player
->fpv_offset
);
76 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, player
->fpv_offset
);
77 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, player
->tpv_offset
);
78 v3_add( TEMP_TPV_EXTRA
, player
->tpv_offset
, player
->tpv_offset
);
81 player
->cam_velocity_constant
= 0.25f
;
82 player
->cam_velocity_coefficient
= 0.7f
;
86 player
->cam_velocity_influence_smooth
= vg_lerpf(
87 player
->cam_velocity_influence_smooth
,
88 player
->cam_velocity_influence
,
89 vg
.frame_delta
* 8.0f
);
91 player
->cam_velocity_coefficient_smooth
= vg_lerpf(
92 player
->cam_velocity_coefficient_smooth
,
93 player
->cam_velocity_coefficient
,
94 vg
.frame_delta
* 8.0f
);
96 player
->cam_velocity_constant_smooth
= vg_lerpf(
97 player
->cam_velocity_constant_smooth
,
98 player
->cam_velocity_constant
,
99 vg
.frame_delta
* 8.0f
);
101 enum camera_mode target_mode
= player
->camera_mode
;
103 if( player
->subsystem
== k_player_subsystem_dead
)
104 target_mode
= k_cam_thirdperson
;
106 player
->camera_type_blend
=
107 vg_lerpf( player
->camera_type_blend
,
108 (target_mode
== k_cam_firstperson
)? 1.0f
: 0.0f
,
109 5.0f
* vg
.frame_delta
);
111 v3_lerp( player
->fpv_viewpoint_smooth
, player
->fpv_viewpoint
,
112 vg
.frame_delta
* 8.0f
, player
->fpv_viewpoint_smooth
);
114 v3_lerp( player
->fpv_offset_smooth
, player
->fpv_offset
,
115 vg
.frame_delta
* 8.0f
, player
->fpv_offset_smooth
);
117 v3_lerp( player
->tpv_offset_smooth
, player
->tpv_offset
,
118 vg
.frame_delta
* 8.0f
, player
->tpv_offset_smooth
);
120 /* fov -- simple blend */
122 player
->cam
.fov
= vg_lerpf( 97.0f
, 128.0f
, player
->camera_type_blend
);
125 * first person camera
129 v3f fpv_pos
, fpv_offset
;
130 m4x3_mulv( av
->sk
.final_mtx
[ av
->id_head
-1 ],
131 player
->fpv_viewpoint_smooth
, fpv_pos
);
132 m3x3_mulv( player
->rb
.to_world
, player
->fpv_offset_smooth
, fpv_offset
);
133 v3_add( fpv_offset
, fpv_pos
, fpv_pos
);
137 v3_lerp( player
->cam_velocity_smooth
, player
->rb
.v
, 4.0f
*vg
.frame_delta
,
138 player
->cam_velocity_smooth
);
141 m3x3_mulv( player
->invbasis
, player
->cam_velocity_smooth
, velocity_local
);
142 player_vector_angles( velocity_angles
, velocity_local
,
143 player
->cam_velocity_coefficient_smooth
,
144 player
->cam_velocity_constant_smooth
);
146 float inf_fpv
= player
->cam_velocity_influence_smooth
*
147 player
->camera_type_blend
,
148 inf_tpv
= player
->cam_velocity_influence_smooth
*
149 (1.0f
-player
->camera_type_blend
);
151 camera_lerp_angles( player
->angles
, velocity_angles
,
156 * Third person camera
159 /* no idea what this technique is called, it acts like clamped position based
160 * on some derivative of where the final camera would end up ....
162 * it is done in the local basis then transformed back */
165 v3_muls( player
->rb
.v
, 0.4f
*vg
.frame_delta
, future
);
166 m3x3_mulv( player
->invbasis
, future
, future
);
168 v3f camera_follow_dir
=
169 { -sinf( player
->angles
[0] ) * cosf( player
->angles
[1] ),
170 sinf( player
->angles
[1] ),
171 cosf( player
->angles
[0] ) * cosf( player
->angles
[1] ) };
174 v3_sub( camera_follow_dir
, future
, v0
);
177 v3_copy( player
->angles
, follow_angles
);
178 follow_angles
[0] = atan2f( -v0
[0], v0
[2] );
179 follow_angles
[1] = 0.3f
+ velocity_angles
[1] * 0.2f
;
181 float ya
= atan2f( -velocity_local
[1], 30.0f
);
183 follow_angles
[1] = 0.3f
+ ya
;
184 camera_lerp_angles( player
->angles
, follow_angles
,
190 rb_extrapolate( &player
->rb
, pco
, pq
);
191 v3_lerp( player
->tpv_lpf
, pco
, 20.0f
*vg
.frame_delta
, player
->tpv_lpf
);
193 /* now move into world */
195 m3x3_mulv( player
->basis
, camera_follow_dir
, camera_follow_dir
);
196 v3f tpv_pos
, tpv_offset
;
198 v3_muladds( player
->tpv_lpf
, camera_follow_dir
, 1.8f
, tpv_pos
);
199 q_mulv( pq
, player
->tpv_offset_smooth
, tpv_offset
);
200 v3_add( tpv_offset
, tpv_pos
, tpv_pos
);
201 v3_muladds( tpv_pos
, player
->cam_velocity_smooth
, -0.025f
, tpv_pos
);
207 v3_lerp( tpv_pos
, fpv_pos
, player
->camera_type_blend
, player
->cam
.pos
);
208 v3_copy( player
->angles
, player
->cam
.angles
);
211 float Fd
= -player
->cam_land_punch_v
* k_cam_damp
,
212 Fs
= -player
->cam_land_punch
* k_cam_spring
;
213 player
->cam_land_punch
+= player
->cam_land_punch_v
* vg
.frame_delta
;
214 player
->cam_land_punch_v
+= ( Fd
+ Fs
) * vg
.frame_delta
;
215 player
->cam
.angles
[1] += player
->cam_land_punch
;
217 /* portal transitions */
218 player_camera_portal_correction( player
);
221 VG_STATIC
void player_look( player_instance
*player
, v3f angles
)
224 v2_muladds( angles
, vg
.mouse_delta
, 0.0025f
, angles
);
226 if( vg_input
.controller_should_use_trackpad_look
)
228 static v2f last_input
;
230 static v2f vel_smooth
;
232 v2f input
= { player
->input_js2h
->axis
.value
,
233 player
->input_js2v
->axis
.value
};
235 if( (v2_length2(last_input
) > 0.001f
) && (v2_length2(input
) > 0.001f
) )
237 v2_sub( input
, last_input
, vel
);
238 v2_muls( vel
, 1.0f
/vg
.time_delta
, vel
);
245 v2_lerp( vel_smooth
, vel
, vg
.time_delta
*8.0f
, vel_smooth
);
247 v2_muladds( angles
, vel_smooth
, vg
.time_delta
, angles
);
248 v2_copy( input
, last_input
);
252 angles
[0] += player
->input_js2h
->axis
.value
* vg
.time_delta
* 4.0f
;
253 angles
[1] += player
->input_js2v
->axis
.value
* vg
.time_delta
* 4.0f
;
256 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
259 #endif /* PLAYER_COMMON_C */