basic replayable replays
[carveJwlIkooP6JGAAIwe30JlM.git] / player_common.c
1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
3
4 #include "ent_skateshop.h"
5 #include "player.h"
6 #include "input.h"
7 #include "menu.h"
8 #include "vg/vg_perlin.h"
9
10 VG_STATIC void player_vector_angles( v3f angles, v3f v, float C, float k )
11 {
12 float yaw = atan2f( v[0], -v[2] ),
13 pitch = atan2f
14 (
15 -v[1],
16 sqrtf
17 (
18 v[0]*v[0] + v[2]*v[2]
19 )
20 ) * C + k;
21
22 angles[0] = yaw;
23 angles[1] = pitch;
24 angles[2] = 0.0f;
25 }
26
27 VG_STATIC float player_get_heading_yaw( player_instance *player )
28 {
29 v3f xz;
30 q_mulv( player->rb.q, (v3f){ 0.0f,0.0f,1.0f }, xz );
31 m3x3_mulv( player->invbasis, xz, xz );
32 return atan2f( xz[0], xz[2] );
33 }
34
35 VG_STATIC void player_camera_portal_correction( player_instance *player )
36 {
37 if( player->gate_waiting ){
38 /* construct plane equation for reciever gate */
39 v4f plane;
40 q_mulv( player->gate_waiting->q[1], (v3f){0.0f,0.0f,1.0f}, plane );
41 plane[3] = v3_dot( plane, player->gate_waiting->co[1] );
42
43 /* check camera polarity */
44 if( v3_dot( player->cam.pos, plane ) < plane[3] ) {
45 vg_success( "Plane cleared\n" );
46 player_apply_transport_to_cam( player->gate_waiting->transport );
47 player->gate_waiting = NULL;
48 player->viewable_world = world_current_instance();
49 }
50 else{
51 /* de-transform camera and player back */
52 m4x3f inverse;
53 m4x3_invert_affine( player->gate_waiting->transport, inverse );
54 m4x3_mulv( inverse, player->cam.pos, player->cam.pos );
55
56 struct skeleton *sk = &player->playeravatar->sk;
57 skeleton_apply_transform( sk, inverse );
58 }
59 }
60 }
61
62 VG_STATIC void player__cam_iterate( player_instance *player ){
63 struct player_avatar *av = player->playeravatar;
64 struct player_cam_controller *cc = &player->cam_control;
65
66 if( player->subsystem == k_player_subsystem_walk ){
67 v3_copy( (v3f){-0.1f,1.8f,0.0f}, cc->fpv_viewpoint );
68 v3_copy( (v3f){0.0f,0.0f,0.0f}, cc->fpv_offset );
69 v3_copy( (v3f){0.0f,1.4f,0.0f}, cc->tpv_offset );
70 }
71 else{
72 v3_copy( (v3f){-0.15f,1.75f,0.0f}, cc->fpv_viewpoint );
73 v3_copy( (v3f){0.0f,0.0f,0.0f}, cc->fpv_offset );
74 v3_copy( (v3f){0.0f,1.4f,0.0f}, cc->tpv_offset );
75 v3_add( cc->tpv_offset_extra, cc->tpv_offset, cc->tpv_offset );
76 }
77
78 player->cam_velocity_constant = 0.25f;
79 player->cam_velocity_coefficient = 0.7f;
80
81 /* lerping */
82
83 player->cam_velocity_influence_smooth = vg_lerpf(
84 player->cam_velocity_influence_smooth,
85 player->cam_velocity_influence,
86 vg.time_frame_delta * 8.0f );
87
88 player->cam_velocity_coefficient_smooth = vg_lerpf(
89 player->cam_velocity_coefficient_smooth,
90 player->cam_velocity_coefficient,
91 vg.time_frame_delta * 8.0f );
92
93 player->cam_velocity_constant_smooth = vg_lerpf(
94 player->cam_velocity_constant_smooth,
95 player->cam_velocity_constant,
96 vg.time_frame_delta * 8.0f );
97
98 enum camera_mode target_mode = cc->camera_mode;
99
100 if( player->subsystem == k_player_subsystem_dead )
101 target_mode = k_cam_thirdperson;
102
103 cc->camera_type_blend =
104 vg_lerpf( cc->camera_type_blend,
105 (target_mode == k_cam_firstperson)? 1.0f: 0.0f,
106 5.0f * vg.time_frame_delta );
107
108 v3_lerp( cc->fpv_viewpoint_smooth, cc->fpv_viewpoint,
109 vg.time_frame_delta * 8.0f, cc->fpv_viewpoint_smooth );
110
111 v3_lerp( cc->fpv_offset_smooth, cc->fpv_offset,
112 vg.time_frame_delta * 8.0f, cc->fpv_offset_smooth );
113
114 v3_lerp( cc->tpv_offset_smooth, cc->tpv_offset,
115 vg.time_frame_delta * 8.0f, cc->tpv_offset_smooth );
116
117 /* fov -- simple blend */
118 float fov_skate = vg_lerpf( 97.0f, 135.0f, k_fov ),
119 fov_walk = vg_lerpf( 90.0f, 110.0f, k_fov );
120
121 player->cam.fov = vg_lerpf( fov_walk, fov_skate, cc->camera_type_blend );
122
123 /*
124 * first person camera
125 */
126
127 /* position */
128 v3f fpv_pos, fpv_offset;
129 m4x3_mulv( av->sk.final_mtx[ av->id_head-1 ],
130 cc->fpv_viewpoint_smooth, fpv_pos );
131 m3x3_mulv( player->rb.to_world, cc->fpv_offset_smooth, fpv_offset );
132 v3_add( fpv_offset, fpv_pos, fpv_pos );
133
134 /* angles */
135 v3f velocity_angles;
136 v3_lerp( cc->cam_velocity_smooth, player->rb.v, 4.0f*vg.time_frame_delta,
137 cc->cam_velocity_smooth );
138
139 v3f velocity_local;
140 m3x3_mulv( player->invbasis, cc->cam_velocity_smooth, velocity_local );
141 player_vector_angles( velocity_angles, velocity_local,
142 player->cam_velocity_coefficient_smooth,
143 player->cam_velocity_constant_smooth );
144
145 float inf_fpv = player->cam_velocity_influence_smooth * cc->camera_type_blend,
146 inf_tpv = player->cam_velocity_influence_smooth *
147 (1.0f-cc->camera_type_blend);
148
149 camera_lerp_angles( player->angles, velocity_angles,
150 inf_fpv,
151 player->angles );
152
153 /*
154 * Third person camera
155 */
156
157 /* no idea what this technique is called, it acts like clamped position based
158 * on some derivative of where the final camera would end up ....
159 *
160 * it is done in the local basis then transformed back */
161
162 v3f future;
163 v3_muls( player->rb.v, 0.4f*vg.time_frame_delta, future );
164 m3x3_mulv( player->invbasis, future, future );
165
166 v3f camera_follow_dir =
167 { -sinf( player->angles[0] ) * cosf( player->angles[1] ),
168 sinf( player->angles[1] ),
169 cosf( player->angles[0] ) * cosf( player->angles[1] ) };
170
171 v3f v0;
172 v3_sub( camera_follow_dir, future, v0 );
173
174 v3f follow_angles;
175 v3_copy( player->angles, follow_angles );
176 follow_angles[0] = atan2f( -v0[0], v0[2] );
177 follow_angles[1] = 0.3f + velocity_angles[1] * 0.2f;
178
179 float ya = atan2f( -velocity_local[1], 30.0f );
180
181 follow_angles[1] = 0.3f + ya;
182 camera_lerp_angles( player->angles, follow_angles,
183 inf_tpv,
184 player->angles );
185
186 v3f pco;
187 v4f pq;
188 rb_extrapolate( &player->rb, pco, pq );
189 v3_lerp( cc->tpv_lpf, pco, 20.0f*vg.time_frame_delta, cc->tpv_lpf );
190
191 /* now move into world */
192 v3f tpv_pos, tpv_offset, tpv_origin;
193
194 /* origin */
195 q_mulv( pq, cc->tpv_offset_smooth, tpv_origin );
196 v3_add( tpv_origin, cc->tpv_lpf, tpv_origin );
197
198 /* offset */
199 m3x3_mulv( player->basis, camera_follow_dir, camera_follow_dir );
200 v3_muls( camera_follow_dir, 1.8f, tpv_offset );
201 v3_muladds( tpv_offset, cc->cam_velocity_smooth, -0.025f, tpv_offset );
202
203 v3_add( tpv_origin, tpv_offset, tpv_pos );
204 f32 t; v3f n;
205 if( spherecast_world( world_current_instance(), tpv_origin, tpv_pos,
206 0.2f, &t, n ) != -1 ){
207 v3_lerp( tpv_origin, tpv_pos, t, tpv_pos );
208 }
209
210 /*
211 * Blend cameras
212 */
213 v3_lerp( tpv_pos, fpv_pos, cc->camera_type_blend, player->cam.pos );
214 v3_copy( player->angles, player->cam.angles );
215
216 /* Camera shake */
217 f32 speed = v3_length(player->rb.v),
218 strength = k_cam_shake_strength * speed;
219 player->cam_trackshake += speed*k_cam_shake_trackspeed*vg.time_frame_delta;
220
221 v2f rnd = {perlin1d( player->cam_trackshake, 1.0f, 4, 20 ),
222 perlin1d( player->cam_trackshake, 1.0f, 4, 63 ) };
223 v2_muladds( player->cam.angles, rnd, strength, player->cam.angles );
224
225 v3f Fd, Fs, F;
226 v3_muls( player->cam_land_punch_v, -k_cam_damp, Fd );
227 v3_muls( player->cam_land_punch, -k_cam_spring, Fs );
228 v3_muladds( player->cam_land_punch, player->cam_land_punch_v,
229 vg.time_frame_delta, player->cam_land_punch );
230 v3_add( Fd, Fs, F );
231 v3_muladds( player->cam_land_punch_v, F, vg.time_frame_delta,
232 player->cam_land_punch_v );
233 v3_add( player->cam_land_punch, player->cam.pos, player->cam.pos );
234
235 /* override camera */
236 player->cam.angles[0] =
237 vg_alerpf( player->cam.angles[0], player->cam_override_angles[0],
238 player->cam_override_strength );
239 player->cam.angles[1] =
240 vg_lerpf ( player->cam.angles[1], player->cam_override_angles[1],
241 player->cam_override_strength );
242 v3_lerp( player->cam.pos, player->cam_override_pos,
243 player->cam_override_strength, player->cam.pos );
244 player->cam.fov = vg_lerpf( player->cam.fov, player->cam_override_fov,
245 player->cam_override_strength );
246
247
248 if( k_cinema >= 0.0001f ){
249 ent_camera *cam = NULL;
250 f32 min_dist = k_cinema;
251
252 world_instance *world = player->viewable_world;
253 for( u32 i=0; i<mdl_arrcount(&world->ent_camera); i++ ){
254 ent_camera *c = mdl_arritm(&world->ent_camera,i);
255
256 f32 dist = v3_dist( c->transform.co, player->rb.co );
257
258 if( dist < min_dist ){
259 min_dist = dist;
260 cam = c;
261 }
262 }
263
264 if( cam ){
265 player->cam.fov = cam->fov;
266 v3_copy( cam->transform.co, player->cam.pos );
267 v3f v0;
268 if( k_cinema_fixed )
269 mdl_transform_vector( &cam->transform, (v3f){0.0f,-1.0f,0.0f}, v0 );
270 else v3_sub( player->rb.co, cam->transform.co, v0 );
271 m3x3_mulv( player->invbasis, v0, v0 );
272 player_vector_angles( player->cam.angles, v0, 1.0f, 0.0f );
273 }
274 }
275
276 /* portal transitions */
277 player_camera_portal_correction( player );
278 }
279
280 VG_STATIC void player_look( player_instance *player, v3f angles )
281 {
282 if( vg_ui.wants_mouse ) return;
283
284 float sensitivity = skaterift.time_rate;
285
286 angles[2] = 0.0f;
287
288 v2f mouse_input;
289 v2_copy( vg.mouse_delta, mouse_input );
290 if( k_invert_y ) mouse_input[1] *= -1.0f;
291 v2_muladds( angles, mouse_input, 0.0025f * sensitivity, angles );
292
293 v2f jlook;
294 joystick_state( k_srjoystick_look, jlook );
295
296 angles[0] += jlook[0] * vg.time_delta * 4.0f * sensitivity;
297 float input_y = jlook[1] * vg.time_delta * 4.0f;
298 if( k_invert_y ) input_y *= -1.0f;
299
300 angles[1] += input_y * sensitivity;
301 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
302 }
303
304 #endif /* PLAYER_COMMON_C */