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