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