wowwww
[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 void player_vector_angles( v3f angles, v3f v, float C, float k )
8 {
9 float yaw = atan2f( v[0], -v[2] ),
10 pitch = atan2f
11 (
12 -v[1],
13 sqrtf
14 (
15 v[0]*v[0] + v[2]*v[2]
16 )
17 ) * C + k;
18
19 angles[0] = yaw;
20 angles[1] = pitch;
21 }
22
23 VG_STATIC float player_get_heading_yaw( player_instance *player )
24 {
25 v3f xz;
26 q_mulv( player->rb.q, (v3f){ 0.0f,0.0f,1.0f }, xz );
27 m3x3_mulv( player->invbasis, xz, xz );
28 return atan2f( xz[0], xz[2] );
29 }
30
31 VG_STATIC void player_camera_portal_correction( player_instance *player )
32 {
33 if( player->gate_waiting ){
34 /* construct plane equation for reciever gate */
35 v4f plane;
36 q_mulv( player->gate_waiting->q[1], (v3f){0.0f,0.0f,1.0f}, plane );
37 plane[3] = v3_dot( plane, player->gate_waiting->co[1] );
38
39 /* check camera polarity */
40 if( v3_dot( player->cam.pos, plane ) < plane[3] ) {
41 vg_success( "Plane cleared\n" );
42 player_apply_transport_to_cam( player->gate_waiting->transport );
43 player->gate_waiting = NULL;
44 player->viewable_world = get_active_world();
45 }
46 else{
47 /* de-transform camera and player back */
48 m4x3f inverse;
49 m4x3_invert_affine( player->gate_waiting->transport, inverse );
50 m4x3_mulv( inverse, player->cam.pos, player->cam.pos );
51
52 struct skeleton *sk = &player->playeravatar->sk;
53 skeleton_apply_transform( sk, inverse );
54 }
55 }
56 }
57
58 static v3f TEMP_TPV_EXTRA;
59
60 VG_STATIC void player__cam_iterate( player_instance *player )
61 {
62 struct player_avatar *av = player->playeravatar;
63
64 if( player->subsystem == k_player_subsystem_walk ){
65 v3_copy( (v3f){-0.1f,1.8f,0.0f}, player->fpv_viewpoint );
66 v3_copy( (v3f){0.0f,0.0f,0.0f}, player->fpv_offset );
67 v3_copy( (v3f){0.0f,1.4f,0.0f}, player->tpv_offset );
68 }
69 else{
70 v3_copy( (v3f){-0.15f,1.75f,0.0f}, player->fpv_viewpoint );
71 #if 0
72 v3_copy( (v3f){-0.35f,0.0f,0.0f}, player->fpv_offset );
73 #endif
74 v3_copy( (v3f){0.0f,0.0f,0.0f}, player->fpv_offset );
75 v3_copy( (v3f){0.0f,1.4f,0.0f}, player->tpv_offset );
76 v3_add( TEMP_TPV_EXTRA, player->tpv_offset, player->tpv_offset );
77 }
78
79 player->cam_velocity_constant = 0.25f;
80 player->cam_velocity_coefficient = 0.7f;
81
82 /* lerping */
83
84 player->cam_velocity_influence_smooth = vg_lerpf(
85 player->cam_velocity_influence_smooth,
86 player->cam_velocity_influence,
87 vg.frame_delta * 8.0f );
88
89 player->cam_velocity_coefficient_smooth = vg_lerpf(
90 player->cam_velocity_coefficient_smooth,
91 player->cam_velocity_coefficient,
92 vg.frame_delta * 8.0f );
93
94 player->cam_velocity_constant_smooth = vg_lerpf(
95 player->cam_velocity_constant_smooth,
96 player->cam_velocity_constant,
97 vg.frame_delta * 8.0f );
98
99 enum camera_mode target_mode = player->camera_mode;
100
101 if( player->subsystem == k_player_subsystem_dead )
102 target_mode = k_cam_thirdperson;
103
104 player->camera_type_blend =
105 vg_lerpf( player->camera_type_blend,
106 (target_mode == k_cam_firstperson)? 1.0f: 0.0f,
107 5.0f * vg.frame_delta );
108
109 v3_lerp( player->fpv_viewpoint_smooth, player->fpv_viewpoint,
110 vg.frame_delta * 8.0f, player->fpv_viewpoint_smooth );
111
112 v3_lerp( player->fpv_offset_smooth, player->fpv_offset,
113 vg.frame_delta * 8.0f, player->fpv_offset_smooth );
114
115 v3_lerp( player->tpv_offset_smooth, player->tpv_offset,
116 vg.frame_delta * 8.0f, player->tpv_offset_smooth );
117
118 /* fov -- simple blend */
119 float fov_skate = vg_lerpf( 97.0f, 135.0f, cl_fov ),
120 fov_walk = vg_lerpf( 90.0f, 110.0f, cl_fov );
121
122 player->cam.fov = vg_lerpf( fov_walk, fov_skate, player->camera_type_blend );
123
124 /*
125 * first person camera
126 */
127
128 /* position */
129 v3f fpv_pos, fpv_offset;
130 m4x3_mulv( av->sk.final_mtx[ av->id_head-1 ],
131 player->fpv_viewpoint_smooth, fpv_pos );
132 m3x3_mulv( player->rb.to_world, player->fpv_offset_smooth, fpv_offset );
133 v3_add( fpv_offset, fpv_pos, fpv_pos );
134
135 /* angles */
136 v3f velocity_angles;
137 v3_lerp( player->cam_velocity_smooth, player->rb.v, 4.0f*vg.frame_delta,
138 player->cam_velocity_smooth );
139
140 v3f velocity_local;
141 m3x3_mulv( player->invbasis, player->cam_velocity_smooth, velocity_local );
142 player_vector_angles( velocity_angles, velocity_local,
143 player->cam_velocity_coefficient_smooth,
144 player->cam_velocity_constant_smooth );
145
146 float inf_fpv = player->cam_velocity_influence_smooth *
147 player->camera_type_blend,
148 inf_tpv = player->cam_velocity_influence_smooth *
149 (1.0f-player->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.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( player->tpv_lpf, pco, 20.0f*vg.frame_delta, player->tpv_lpf );
192
193 /* now move into world */
194
195 m3x3_mulv( player->basis, camera_follow_dir, camera_follow_dir );
196 v3f tpv_pos, tpv_offset;
197
198 v3_muladds( player->tpv_lpf, camera_follow_dir, 1.8f, tpv_pos );
199 q_mulv( pq, player->tpv_offset_smooth, tpv_offset );
200 v3_add( tpv_offset, tpv_pos, tpv_pos );
201 v3_muladds( tpv_pos, player->cam_velocity_smooth, -0.025f, tpv_pos );
202
203 /*
204 * Blend cameras
205 */
206 v3_lerp( tpv_pos, fpv_pos, player->camera_type_blend, player->cam.pos );
207 v3_copy( player->angles, player->cam.angles );
208
209 float Fd = -player->cam_land_punch_v * k_cam_damp,
210 Fs = -player->cam_land_punch * k_cam_spring;
211 player->cam_land_punch += player->cam_land_punch_v * vg.frame_delta;
212 player->cam_land_punch_v += ( Fd + Fs ) * vg.frame_delta;
213 player->cam.angles[1] += player->cam_land_punch;
214
215 /* override camera */
216 player->cam.angles[0] =
217 vg_alerpf( player->cam.angles[0], player->cam_override_angles[0],
218 player->cam_override_strength );
219 player->cam.angles[1] =
220 vg_lerpf ( player->cam.angles[1], player->cam_override_angles[1],
221 player->cam_override_strength );
222 v3_lerp( player->cam.pos, player->cam_override_pos,
223 player->cam_override_strength, player->cam.pos );
224
225 /* portal transitions */
226 player_camera_portal_correction( player );
227 }
228
229 VG_STATIC void player_look( player_instance *player, v3f angles )
230 {
231 angles[2] = 0.0f;
232
233 v2f mouse_input;
234 v2_copy( vg.mouse_delta, mouse_input );
235 if( cl_invert_y )
236 mouse_input[1] *= -1.0f;
237 v2_muladds( angles, mouse_input, 0.0025f, angles );
238
239 if( vg_input.controller_should_use_trackpad_look ){
240 static v2f last_input;
241 static v2f vel;
242 static v2f vel_smooth;
243
244 v2f input = { player->input_js2h->axis.value,
245 player->input_js2v->axis.value };
246
247 if( cl_invert_y )
248 input[1] *= -1.0f;
249
250 if( (v2_length2(last_input) > 0.001f) && (v2_length2(input) > 0.001f) ){
251 v2_sub( input, last_input, vel );
252 v2_muls( vel, 1.0f/vg.time_delta, vel );
253 }
254 else{
255 v2_zero( vel );
256 }
257
258 v2_lerp( vel_smooth, vel, vg.time_delta*8.0f, vel_smooth );
259
260 v2_muladds( angles, vel_smooth, vg.time_delta, angles );
261 v2_copy( input, last_input );
262 }
263 else{
264 angles[0] += player->input_js2h->axis.value * vg.time_delta * 4.0f;
265
266 float input_y = player->input_js2v->axis.value * vg.time_delta * 4.0f;
267 if( cl_invert_y )
268 input_y *= -1.0f;
269
270 angles[1] += input_y;
271 }
272
273 angles[1] = vg_clampf( angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
274 }
275
276 #endif /* PLAYER_COMMON_C */