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