1 #ifndef PLAYER_COMMON_C
2 #define PLAYER_COMMON_C
4 #include "ent_skateshop.h"
8 #include "vg/vg_perlin.h"
10 VG_STATIC
void player_vector_angles( v3f angles
, v3f v
, float C
, float k
)
12 float yaw
= atan2f( v
[0], -v
[2] ),
27 VG_STATIC
float player_get_heading_yaw( player_instance
*player
)
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] );
35 VG_STATIC
void player_camera_portal_correction( player_instance
*player
)
37 if( player
->gate_waiting
){
38 /* construct plane equation for reciever gate */
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] );
43 f32 pol
= v3_dot( player
->cam
.pos
, plane
) - plane
[3];
45 /* check camera polarity */
46 if( (pol
< 0.0f
) || (pol
> 5.0f
) ) {
47 vg_success( "Plane cleared\n" );
48 player_apply_transport_to_cam( player
->gate_waiting
->transport
);
49 player
->gate_waiting
= NULL
;
50 player
->viewable_world
= world_current_instance();
53 /* de-transform camera and player back */
55 m4x3_invert_affine( player
->gate_waiting
->transport
, inverse
);
56 m4x3_mulv( inverse
, player
->cam
.pos
, player
->cam
.pos
);
58 struct skeleton
*sk
= &player
->playeravatar
->sk
;
59 skeleton_apply_transform( sk
, inverse
);
64 VG_STATIC
void player__cam_iterate( player_instance
*player
){
65 struct player_avatar
*av
= player
->playeravatar
;
66 struct player_cam_controller
*cc
= &player
->cam_control
;
68 if( player
->subsystem
== k_player_subsystem_walk
){
69 v3_copy( (v3f
){-0.1f
,1.8f
,0.0f
}, cc
->fpv_viewpoint
);
70 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
71 v3_copy( (v3f
){0.0f
,1.4f
,0.0f
}, cc
->tpv_offset
);
74 v3_copy( (v3f
){-0.15f
,1.75f
,0.0f
}, cc
->fpv_viewpoint
);
75 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, cc
->fpv_offset
);
77 f32 h
= vg_lerpf( 0.4f
, 1.4f
, k_cam_height
);
78 v3_copy( (v3f
){0.0f
,h
,0.0f
}, cc
->tpv_offset
);
79 v3_add( cc
->tpv_offset_extra
, cc
->tpv_offset
, cc
->tpv_offset
);
82 player
->cam_velocity_constant
= 0.25f
;
83 player
->cam_velocity_coefficient
= 0.7f
;
87 player
->cam_velocity_influence_smooth
= vg_lerpf(
88 player
->cam_velocity_influence_smooth
,
89 player
->cam_velocity_influence
,
90 vg
.time_frame_delta
* 8.0f
);
92 player
->cam_velocity_coefficient_smooth
= vg_lerpf(
93 player
->cam_velocity_coefficient_smooth
,
94 player
->cam_velocity_coefficient
,
95 vg
.time_frame_delta
* 8.0f
);
97 player
->cam_velocity_constant_smooth
= vg_lerpf(
98 player
->cam_velocity_constant_smooth
,
99 player
->cam_velocity_constant
,
100 vg
.time_frame_delta
* 8.0f
);
102 enum camera_mode target_mode
= cc
->camera_mode
;
104 if( player
->subsystem
== k_player_subsystem_dead
)
105 target_mode
= k_cam_thirdperson
;
107 cc
->camera_type_blend
=
108 vg_lerpf( cc
->camera_type_blend
,
109 (target_mode
== k_cam_firstperson
)? 1.0f
: 0.0f
,
110 5.0f
* vg
.time_frame_delta
);
112 v3_lerp( cc
->fpv_viewpoint_smooth
, cc
->fpv_viewpoint
,
113 vg
.time_frame_delta
* 8.0f
, cc
->fpv_viewpoint_smooth
);
115 v3_lerp( cc
->fpv_offset_smooth
, cc
->fpv_offset
,
116 vg
.time_frame_delta
* 8.0f
, cc
->fpv_offset_smooth
);
118 v3_lerp( cc
->tpv_offset_smooth
, cc
->tpv_offset
,
119 vg
.time_frame_delta
* 8.0f
, cc
->tpv_offset_smooth
);
121 /* fov -- simple blend */
122 float fov_skate
= vg_lerpf( 97.0f
, 135.0f
, k_fov
),
123 fov_walk
= vg_lerpf( 90.0f
, 110.0f
, k_fov
);
125 player
->cam
.fov
= vg_lerpf( fov_walk
, fov_skate
, cc
->camera_type_blend
);
128 * first person camera
132 v3f fpv_pos
, fpv_offset
;
133 m4x3_mulv( av
->sk
.final_mtx
[ av
->id_head
-1 ],
134 cc
->fpv_viewpoint_smooth
, fpv_pos
);
135 m3x3_mulv( player
->rb
.to_world
, cc
->fpv_offset_smooth
, fpv_offset
);
136 v3_add( fpv_offset
, fpv_pos
, fpv_pos
);
140 v3_lerp( cc
->cam_velocity_smooth
, player
->rb
.v
, 4.0f
*vg
.time_frame_delta
,
141 cc
->cam_velocity_smooth
);
144 m3x3_mulv( player
->invbasis
, cc
->cam_velocity_smooth
, velocity_local
);
145 player_vector_angles( velocity_angles
, velocity_local
,
146 player
->cam_velocity_coefficient_smooth
,
147 player
->cam_velocity_constant_smooth
);
149 float inf_fpv
= player
->cam_velocity_influence_smooth
* cc
->camera_type_blend
,
150 inf_tpv
= player
->cam_velocity_influence_smooth
*
151 (1.0f
-cc
->camera_type_blend
);
153 camera_lerp_angles( player
->angles
, velocity_angles
,
158 * Third person camera
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 ....
164 * it is done in the local basis then transformed back */
167 v3_muls( player
->rb
.v
, 0.4f
*vg
.time_frame_delta
, future
);
168 m3x3_mulv( player
->invbasis
, future
, future
);
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] ) };
176 v3_sub( camera_follow_dir
, future
, v0
);
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
;
183 float ya
= atan2f( -velocity_local
[1], 30.0f
);
185 follow_angles
[1] = 0.3f
+ ya
;
186 camera_lerp_angles( player
->angles
, follow_angles
,
192 rb_extrapolate( &player
->rb
, pco
, pq
);
193 v3_lerp( cc
->tpv_lpf
, pco
, 20.0f
*vg
.time_frame_delta
, cc
->tpv_lpf
);
195 /* now move into world */
196 v3f tpv_pos
, tpv_offset
, tpv_origin
;
199 q_mulv( pq
, cc
->tpv_offset_smooth
, tpv_origin
);
200 v3_add( tpv_origin
, cc
->tpv_lpf
, tpv_origin
);
203 m3x3_mulv( player
->basis
, camera_follow_dir
, camera_follow_dir
);
204 v3_muls( camera_follow_dir
, 1.8f
, tpv_offset
);
205 v3_muladds( tpv_offset
, cc
->cam_velocity_smooth
, -0.025f
, tpv_offset
);
207 v3_add( tpv_origin
, tpv_offset
, tpv_pos
);
210 if( spherecast_world( world_current_instance(), tpv_origin
, tpv_pos
,
211 0.2f
, &t
, n
) != -1 ){
212 v3_lerp( tpv_origin
, tpv_pos
, t
, tpv_pos
);
219 v3_lerp( tpv_pos
, fpv_pos
, cc
->camera_type_blend
, player
->cam
.pos
);
220 v3_copy( player
->angles
, player
->cam
.angles
);
223 f32 speed
= v3_length(player
->rb
.v
),
224 strength
= k_cam_shake_strength
* speed
;
225 player
->cam_trackshake
+= speed
*k_cam_shake_trackspeed
*vg
.time_frame_delta
;
227 v2f rnd
= {perlin1d( player
->cam_trackshake
, 1.0f
, 4, 20 ),
228 perlin1d( player
->cam_trackshake
, 1.0f
, 4, 63 ) };
229 v2_muladds( player
->cam
.angles
, rnd
, strength
, player
->cam
.angles
);
232 v3_muls( player
->cam_land_punch_v
, -k_cam_damp
, Fd
);
233 v3_muls( player
->cam_land_punch
, -k_cam_spring
, Fs
);
234 v3_muladds( player
->cam_land_punch
, player
->cam_land_punch_v
,
235 vg
.time_frame_delta
, player
->cam_land_punch
);
237 v3_muladds( player
->cam_land_punch_v
, F
, vg
.time_frame_delta
,
238 player
->cam_land_punch_v
);
239 v3_add( player
->cam_land_punch
, player
->cam
.pos
, player
->cam
.pos
);
241 if( k_cinema
>= 0.0001f
){
242 ent_camera
*cam
= NULL
;
243 f32 min_dist
= k_cinema
;
245 world_instance
*world
= player
->viewable_world
;
246 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_camera
); i
++ ){
247 ent_camera
*c
= mdl_arritm(&world
->ent_camera
,i
);
249 f32 dist
= v3_dist( c
->transform
.co
, player
->rb
.co
);
251 if( dist
< min_dist
){
258 player
->cam
.fov
= cam
->fov
;
259 v3_copy( cam
->transform
.co
, player
->cam
.pos
);
262 mdl_transform_vector( &cam
->transform
, (v3f
){0.0f
,-1.0f
,0.0f
}, v0
);
263 else v3_sub( player
->rb
.co
, cam
->transform
.co
, v0
);
264 m3x3_mulv( player
->invbasis
, v0
, v0
);
265 player_vector_angles( player
->cam
.angles
, v0
, 1.0f
, 0.0f
);
269 /* portal transitions */
270 player_camera_portal_correction( player
);
273 VG_STATIC
void player_look( v3f angles
, float speed
){
274 if( vg_ui
.wants_mouse
) return;
279 v2_copy( vg
.mouse_delta
, mouse_input
);
280 if( k_invert_y
) mouse_input
[1] *= -1.0f
;
281 v2_muladds( angles
, mouse_input
, 0.0025f
* speed
, angles
);
284 joystick_state( k_srjoystick_look
, jlook
);
286 angles
[0] += jlook
[0] * vg
.time_frame_delta
* 4.0f
* speed
;
287 float input_y
= jlook
[1] * vg
.time_frame_delta
* 4.0f
;
288 if( k_invert_y
) input_y
*= -1.0f
;
290 angles
[1] += input_y
* speed
;
291 angles
[1] = vg_clampf( angles
[1], -VG_PIf
*0.5f
, VG_PIf
*0.5f
);
294 #endif /* PLAYER_COMMON_C */