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