Skip to content

Commit 882d75e

Browse files
remove useless depth variable
1 parent 15523d7 commit 882d75e

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/app.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl AppState {
187187
if self.particle_system.is_mouse_dragging {
188188
let (x, y) = self.input_manager.mouse_position();
189189

190-
// Convert screen coordinates to normalized device coordinates (-1 to 1)
190+
// Convert screen coordinates to normalized device coordinates
191191
let ndc_x = (2.0 * x / self.surface_config.width as f32) - 1.0;
192192
let ndc_y = 1.0 - (2.0 * y / self.surface_config.height as f32);
193193

@@ -201,9 +201,16 @@ impl AppState {
201201
let camera_right = camera_forward.cross(Vec3::Y).normalize();
202202
let camera_up = camera_right.cross(camera_forward).normalize();
203203

204-
// Use mouse_depth to determine distance from camera
205-
let distance = 20.0 + self.particle_system.mouse_depth;
204+
let current_pos = Vec3::new(
205+
self.particle_system.mouse_position[0],
206+
self.particle_system.mouse_position[1],
207+
self.particle_system.mouse_position[2],
208+
);
209+
let camera_pos = self.camera.position;
210+
let to_cursor = current_pos - camera_pos;
211+
let distance = to_cursor.dot(camera_forward);
206212

213+
// Calculate the plane at the specified distance from camera
207214
let plane_center = self.camera.position + camera_forward * distance;
208215

209216
// Scale the NDC coordinates based on the field of view and distance
@@ -303,7 +310,10 @@ impl AppState {
303310
"Dragging: {}",
304311
self.particle_system.is_mouse_dragging
305312
));
306-
ui.label(format!("Depth: {:.2}", self.particle_system.mouse_depth));
313+
ui.label(format!(
314+
"Depth: {:.2}",
315+
self.particle_system.mouse_position[2]
316+
));
307317

308318
ui.add(
309319
egui::Slider::new(&mut self.particle_system.mouse_radius, 1.0..=50.0)
@@ -508,9 +518,24 @@ impl ApplicationHandler for App {
508518
state.camera.fov = fov_degrees * std::f32::consts::PI / 180.0;
509519
state.camera.update_view_proj();
510520
} else {
511-
// Otherwise adjust mouse depth
512-
state.particle_system.mouse_depth =
513-
state.particle_system.mouse_depth - delta_value * 5.0;
521+
let camera_forward = Vec3::new(
522+
state.camera.yaw.cos() * state.camera.pitch.cos(),
523+
state.camera.pitch.sin(),
524+
state.camera.yaw.sin() * state.camera.pitch.cos(),
525+
)
526+
.normalize();
527+
528+
let cursor_pos = Vec3::new(
529+
state.particle_system.mouse_position[0],
530+
state.particle_system.mouse_position[1],
531+
state.particle_system.mouse_position[2],
532+
);
533+
534+
// Move cursor position along camera forward vector
535+
let move_distance = delta_value * 2.0;
536+
let new_pos = cursor_pos + camera_forward * move_distance;
537+
538+
state.particle_system.mouse_position = [new_pos.x, new_pos.y, new_pos.z];
514539
}
515540
}
516541
WindowEvent::MouseInput {

src/particle_system.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub struct ParticleSystem {
3838
pub mouse_force: f32,
3939
pub mouse_radius: f32,
4040
pub mouse_position: [f32; 3],
41-
pub mouse_depth: f32,
4241
pub is_mouse_dragging: bool,
4342
pub sim_param_buffer: egui_wgpu::wgpu::Buffer,
4443
}
@@ -195,7 +194,6 @@ impl ParticleSystem {
195194
mouse_force: 5.0,
196195
mouse_radius: 10.0,
197196
mouse_position: [0.0, 0.0, 0.0],
198-
mouse_depth: 0.0,
199197
is_mouse_dragging: false,
200198
sim_param_buffer,
201199
}

0 commit comments

Comments
 (0)