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