@@ -42,11 +42,93 @@ pub fn update_camera_aspect_ratios(world: &mut World) {
4242}
4343
4444pub fn fly_camera_system ( world : & mut World ) {
45+ touch_camera_system ( world) ;
4546 look_camera_system ( world) ;
4647 wasd_keyboard_controls_system ( world) ;
4748 gamepad_fly_camera_system ( world) ;
4849}
4950
51+ fn touch_camera_system ( world : & mut World ) {
52+ let Some ( camera_entity) = world. resources . active_camera else {
53+ return ;
54+ } ;
55+
56+ let delta_time = world. resources . window . timing . delta_time ;
57+
58+ let ( right, up) = {
59+ let Some ( local_transform) = world. get_local_transform ( camera_entity) else {
60+ return ;
61+ } ;
62+ ( local_transform. right_vector ( ) , local_transform. up_vector ( ) )
63+ } ;
64+
65+ let touch_count = world. resources . input . touch . touch_count ;
66+
67+ if touch_count == 1 {
68+ let raw_delta = world. resources . input . touch . single_finger_delta ;
69+
70+ if raw_delta. magnitude ( ) > 0.0 {
71+ let Some ( camera) = world. get_camera_mut ( camera_entity) else {
72+ return ;
73+ } ;
74+ let Some ( smoothing) = camera. smoothing . as_mut ( ) else {
75+ return ;
76+ } ;
77+
78+ let smoothing_factor = 1.0 - ( -delta_time * smoothing. mouse_responsiveness ) . exp ( ) ;
79+ smoothing. smoothed_mouse_delta = smoothing. smoothed_mouse_delta * ( 1.0 - smoothing_factor)
80+ + raw_delta * smoothing_factor;
81+
82+ let pixels_to_radians = ( std:: f32:: consts:: PI / 1000.0 ) * smoothing. mouse_dpi_scale ;
83+ let mut delta =
84+ smoothing. smoothed_mouse_delta * smoothing. mouse_sensitivity * pixels_to_radians;
85+ delta. x *= -1.0 ;
86+ delta. y *= -1.0 ;
87+
88+ let Some ( local_transform) = world. get_local_transform_mut ( camera_entity) else {
89+ return ;
90+ } ;
91+
92+ let yaw = nalgebra_glm:: quat_angle_axis ( delta. x , & Vec3 :: y ( ) ) ;
93+ local_transform. rotation = yaw * local_transform. rotation ;
94+
95+ let forward = local_transform. forward_vector ( ) ;
96+ let current_pitch = forward. y . asin ( ) ;
97+
98+ let new_pitch = current_pitch + delta. y ;
99+ if new_pitch. abs ( ) <= 89_f32 . to_radians ( ) {
100+ let pitch = nalgebra_glm:: quat_angle_axis ( delta. y , & Vec3 :: x ( ) ) ;
101+ local_transform. rotation *= pitch;
102+ }
103+
104+ mark_local_transform_dirty ( world, camera_entity) ;
105+ }
106+ } else if touch_count == 2 {
107+ let mut delta =
108+ world. resources . input . touch . two_finger_delta * world. resources . window . timing . delta_time ;
109+ delta. x *= -1.0 ;
110+ delta. y *= -1.0 ;
111+
112+ if delta. magnitude ( ) > 0.0 {
113+ let Some ( local_transform) = world. get_local_transform_mut ( camera_entity) else {
114+ return ;
115+ } ;
116+ let translation_right = right * delta. x ;
117+ let translation_up = up * delta. y ;
118+
119+ local_transform. translation += translation_right;
120+ local_transform. translation += translation_up;
121+
122+ let changed = translation_right. magnitude ( ) > 0.0 || translation_up. magnitude ( ) > 0.0 ;
123+ if changed {
124+ mark_local_transform_dirty ( world, camera_entity) ;
125+ }
126+ }
127+ }
128+
129+ world. resources . input . touch . reset ( ) ;
130+ }
131+
50132fn look_camera_system ( world : & mut World ) {
51133 let Some ( camera_entity) = world. resources . active_camera else {
52134 return ;
0 commit comments