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