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