plish
[carveJwlIkooP6JGAAIwe30JlM.git] / player_common.c
1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
3
4 #include "player.h"
5
6 VG_STATIC void player_vector_angles( v3f angles, v3f v, float C, float k )
7 {
8 float yaw = atan2f( v[0], -v[2] ),
9 pitch = atan2f
10 (
11 -v[1],
12 sqrtf
13 (
14 v[0]*v[0] + v[2]*v[2]
15 )
16 ) * C + k;
17
18 angles[0] = yaw;
19 angles[1] = pitch;
20 }
21
22 VG_STATIC float player_get_heading_yaw( player_instance *player )
23 {
24 v3f xz;
25 q_mulv( player->rb.q, (v3f){ 0.0f,0.0f,1.0f }, xz );
26 return atan2f( xz[0], xz[2] );
27 }
28
29 VG_STATIC void player_camera_portal_correction( player_instance *player )
30 {
31 if( player->gate_waiting )
32 {
33 /* construct plane equation for reciever gate */
34 v4f plane;
35 v3_copy( player->gate_waiting->recv_to_world[2], plane );
36 plane[3] = v3_dot( plane, player->gate_waiting->recv_to_world[3] );
37
38 /* check camera polarity */
39 if( v3_dot( player->cam.pos, plane ) < plane[3] )
40 {
41 vg_success( "Plane cleared\n" );
42 player_apply_transport_to_cam( player->gate_waiting->transport );
43 player->gate_waiting = NULL;
44 }
45 else
46 {
47 /* de-transform camera and player back */
48 m4x3f inverse;
49 m4x3_invert_affine( player->gate_waiting->transport, inverse );
50 m4x3_mulv( inverse, player->cam.pos, player->cam.pos );
51
52 /* TODO: Find robust method for this */
53 v3f fwd_dir = { cosf(player->cam.angles[0]),
54 0.0f,
55 sinf(player->cam.angles[0])};
56 m3x3_mulv( inverse, fwd_dir, fwd_dir );
57 player->cam.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
58
59 struct skeleton *sk = &player->playeravatar->sk;
60 skeleton_apply_transform( sk, inverse );
61 }
62 }
63 }
64
65 VG_STATIC void player__cam_iterate( player_instance *player )
66 {
67 struct player_avatar *av = player->playeravatar;
68
69 if( player->subsystem == k_player_subsystem_walk )
70 {
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 );
74 }
75 else
76 {
77 v3_copy( (v3f){0.0f,1.8f,0.0f}, player->fpv_viewpoint );
78 v3_copy( (v3f){-0.35f,0.0f,0.0f}, player->fpv_offset );
79 v3_copy( (v3f){0.0f,1.4f,0.0f}, player->tpv_offset );
80 }
81
82 player->cam_velocity_constant = 0.25f;
83 player->cam_velocity_coefficient = 0.7f;
84
85 /* lerping */
86
87 player->cam_velocity_influence_smooth = vg_lerpf(
88 player->cam_velocity_influence_smooth,
89 player->cam_velocity_influence,
90 vg.frame_delta * 8.0f );
91
92 player->cam_velocity_coefficient_smooth = vg_lerpf(
93 player->cam_velocity_coefficient_smooth,
94 player->cam_velocity_coefficient,
95 vg.frame_delta * 8.0f );
96
97 player->cam_velocity_constant_smooth = vg_lerpf(
98 player->cam_velocity_constant_smooth,
99 player->cam_velocity_constant,
100 vg.frame_delta * 8.0f );
101
102 player->camera_type_blend =
103 vg_lerpf( player->camera_type_blend,
104 (player->camera_mode == k_cam_firstperson)? 1.0f: 0.0f,
105 5.0f * vg.frame_delta );
106
107 v3_lerp( player->fpv_viewpoint_smooth, player->fpv_viewpoint,
108 vg.frame_delta * 8.0f, player->fpv_viewpoint_smooth );
109
110 v3_lerp( player->fpv_offset_smooth, player->fpv_offset,
111 vg.frame_delta * 8.0f, player->fpv_offset_smooth );
112
113 v3_lerp( player->tpv_offset_smooth, player->tpv_offset,
114 vg.frame_delta * 8.0f, player->tpv_offset_smooth );
115
116 /* fov -- simple blend */
117 player->cam.fov = vg_lerpf( 97.0f, 118.0f, player->camera_type_blend );
118
119 /*
120 * first person camera
121 */
122
123 /* position */
124 v3f fpv_pos, fpv_offset;
125 m4x3_mulv( av->sk.final_mtx[ av->id_head-1 ],
126 player->fpv_viewpoint_smooth, fpv_pos );
127 m3x3_mulv( player->rb.to_world, player->fpv_offset_smooth, fpv_offset );
128 v3_add( fpv_offset, fpv_pos, fpv_pos );
129
130 /* angles */
131 v3f velocity_angles;
132 v3_lerp( player->cam_velocity_smooth, player->rb.v, 4.0f*vg.frame_delta,
133 player->cam_velocity_smooth );
134 player_vector_angles( velocity_angles, player->cam_velocity_smooth,
135 player->cam_velocity_coefficient_smooth,
136 player->cam_velocity_constant_smooth );
137
138 float inf_fpv = player->cam_velocity_influence_smooth *
139 player->camera_type_blend,
140 inf_tpv = player->cam_velocity_influence_smooth *
141 (1.0f-player->camera_type_blend);
142
143 camera_lerp_angles( player->angles, velocity_angles,
144 inf_fpv,
145 player->angles );
146
147 /*
148 * Third person camera
149 */
150
151 /* no idea what this technique is called, it acts like clamped position based
152 * on some derivative of where the final camera would end up .... */
153
154 v3f future;
155 v3_muls( player->rb.v, 0.4f*vg.frame_delta, future );
156
157 v3f camera_follow_dir =
158 { -sinf( player->angles[0] ) * cosf( player->angles[1] ),
159 sinf( player->angles[1] ),
160 cosf( player->angles[0] ) * cosf( player->angles[1] ) };
161
162 v3f v0;
163 v3_sub( camera_follow_dir, future, v0 );
164
165 v3f follow_angles;
166 v3_copy( player->angles, follow_angles );
167 follow_angles[0] = atan2f( -v0[0], v0[2] );
168 follow_angles[1] = 0.3f + velocity_angles[1] * 0.2f;
169
170 float ya = atan2f
171 (
172 -player->cam_velocity_smooth[1],
173 30.0f
174 );
175
176 follow_angles[1] = 0.3f + ya;
177
178 camera_lerp_angles( player->angles, follow_angles,
179 inf_tpv,
180 player->angles );
181
182 v3f pco;
183 v4f pq;
184 rb_extrapolate( &player->rb, pco, pq );
185 v3_lerp( player->tpv_lpf, pco, 20.0f*vg.frame_delta, player->tpv_lpf );
186
187 v3f tpv_pos, tpv_offset;
188 v3_muladds( player->tpv_lpf, camera_follow_dir, 1.8f, tpv_pos );
189 q_mulv( pq, player->tpv_offset_smooth, tpv_offset );
190 v3_add( tpv_offset, tpv_pos, tpv_pos );
191 v3_muladds( tpv_pos, player->cam_velocity_smooth, -0.025f, tpv_pos );
192
193
194 /*
195 * Blend cameras
196 */
197
198 v3_lerp( tpv_pos, fpv_pos, player->camera_type_blend, player->cam.pos );
199 v3_copy( player->angles, player->cam.angles );
200
201 #if 0
202 /*
203 * Follow camera
204 */
205 v3_lerp( player->follow_pos, player->follow_pos_target,
206 vg.frame_delta * 15.0f, player->follow_pos );
207 camera_lerp_angles( player->follow_angles, player->follow_angles_target,
208 vg.frame_delta * 18.0f, player->follow_angles );
209
210 /*
211 * Fpv camera
212 */
213 v3_lerp( player->fpv_offset, player->fpv_offset_target,
214 vg.frame_delta * 6.0f, player->fpv_offset );
215
216 /*
217 * Mix follow / override cameras
218 */
219 player->camera_type_blend =
220 vg_lerpf( player->camera_type_blend,
221 (player->camera_mode == k_cam_firstperson)? 1.0f: 0.0f,
222 5.0f * vg.frame_delta );
223 float t = player->camera_type_blend;
224
225 v3f p0, p1, a0, a1;
226 v3_lerp( player->follow_pos, player->override_pos,
227 player->cam_position_override_strength, p1 );
228 camera_lerp_angles( player->follow_angles, player->override_angles,
229 player->cam_angles_override_strength, a1 );
230
231 v3_copy( player->fpv_pos, p0 );
232 v3_copy( player->fpv_angles, a0 );
233
234 v3_lerp( p0, p1, player->camera_type_blend, player->cam.pos );
235 camera_lerp_angles( a0, a1, player->camera_type_blend, player->cam.angles );
236
237
238 #endif
239 /* FIXME: cl_fov */
240
241 #if 0
242 float Fd = -player->cam_land_punch_v * k_cam_damp,
243 Fs = -player->cam_land_punch * k_cam_spring;
244 player->cam_land_punch += player->cam_land_punch_v * vg.frame_delta;
245 player->cam_land_punch_v += ( Fd + Fs ) * vg.frame_delta;
246 player->cam.angles[1] += player->cam_land_punch;
247 #endif
248
249 /* portal transitions */
250 player_camera_portal_correction( player );
251 }
252
253 #if 0
254 VG_STATIC void player_set_follower_subject( player_instance *player,
255 v3f subj )
256 {
257 v3f dir;
258 v3_sub( subj, player->follow_pos, dir );
259
260 if( v3_length2( dir ) < 0.1f*0.1f )
261 v3_copy( (v3f){ 0.0f, 0.0f, 1.0f }, dir ); /* FIXME */
262 else
263 v3_normalize( dir );
264
265 dir[1] *= 0.0f;
266
267 v3_muladds( subj, dir, -2.0f, player->follow_pos_target );
268 player_vector_angles( player->follow_angles_target, dir, 1.0f, 0.2f );
269 }
270 #endif
271
272 VG_STATIC void player_look( player_instance *player, v3f angles )
273 {
274 angles[2] = 0.0f;
275 v2_muladds( angles, vg.mouse_delta, 0.0025f, angles );
276
277 if( vg_input.controller_should_use_trackpad_look )
278 {
279 static v2f last_input;
280 static v2f vel;
281 static v2f vel_smooth;
282
283 v2f input = { player->input_js2h->axis.value,
284 player->input_js2v->axis.value };
285
286 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) )
287 {
288 v2_sub( input, last_input, vel );
289 v2_muls( vel, 1.0f/vg.time_delta, vel );
290 }
291 else
292 {
293 v2_zero( vel );
294 }
295
296 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
297
298 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
299 v2_copy( input, last_input );
300 }
301 else
302 {
303 angles[0] += player->input_js2h->axis.value * vg.time_delta * 4.0f;
304 angles[1] += player->input_js2v->axis.value * vg.time_delta * 4.0f;
305 }
306
307 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
308 }
309
310 #endif /* PLAYER_COMMON_C */