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