Skip to content
This repository was archived by the owner on Nov 10, 2024. It is now read-only.

Commit 08ca42a

Browse files
committed
알려진 문제 모두 해결
근데 시간 지날수록 (점이 많아질수록) 속도 느려짐 (fps 작아짐) 그리고 충돌도 추가해야됨
1 parent 01fca57 commit 08ca42a

File tree

8 files changed

+254
-262
lines changed

8 files changed

+254
-262
lines changed

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::app::util::FrameHistory;
1212
mod audio;
1313
mod graphics;
1414
mod io;
15-
mod manager;
15+
pub mod manager;
1616
mod simulations;
1717
mod util;
1818

@@ -214,7 +214,7 @@ impl eframe::App for State {
214214
ui.horizontal(|ui| {
215215
ui.label("Time mul");
216216
let _slider =
217-
Slider::new(self.simulation_manager.time_multiplier(), 0.1..=5.0)
217+
Slider::new(self.simulation_manager.time_multiplier(), 1..=5)
218218
.ui(ui);
219219
});
220220

src/app/graphics/plot.rs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
use eframe::epaint::FontFamily;
2-
use std::cmp::min;
32

43
use egui::plot::{Line, PlotBounds, PlotPoint, PlotUi, Polygon, Text};
54
use egui::{Align2, InnerResponse, Pos2, RichText, TextStyle};
65

7-
use nalgebra::max;
8-
use std::fmt::Debug;
9-
use tracing::info;
10-
116
use crate::app::graphics::define::{PlotColor, PlotDrawHelper};
127
use crate::app::graphics::CSPlotObjects;
138
use crate::app::simulations::classic_simulation::object::drawing::get_object_mesh;
149

1510
use crate::app::simulations::classic_simulation::{CSimObject, Simulation};
1611
use crate::app::simulations::state::SimulationState;
17-
use crate::app::NVec2;
1812

1913
pub mod object;
2014

@@ -45,6 +39,8 @@ pub struct SimPlot {
4539
plot_data: PlotData,
4640
}
4741

42+
pub const PLOT_MAX_DISTANCE: f64 = 225.0;
43+
4844
impl SimPlot {
4945
// 그래프를 그린다.
5046
pub fn draw(
@@ -54,7 +50,7 @@ impl SimPlot {
5450
state: &mut SimulationState,
5551
) {
5652
self.plot_data.nearest_label = String::new();
57-
self.plot_data.near_value = ObjectTraceLine::MAX_DISTANCE;
53+
self.plot_data.near_value = PLOT_MAX_DISTANCE;
5854

5955
if !state.sim_started {
6056
plot_ui.set_plot_bounds(PlotBounds::from_min_max([-100.0, -100.0], [100.0, 100.0]));
@@ -67,7 +63,7 @@ impl SimPlot {
6763
if let Some(pointer_pos) = plot_ui.pointer_coordinate() {
6864
if self.plot_data.dragging_object {
6965
let pos = simulation_objects[self.plot_data.selected_index]
70-
.state
66+
.current_state()
7167
.position;
7268
Line::new(vec![[pos.x, pos.y], [pointer_pos.x, pointer_pos.y]]).draw(plot_ui);
7369
}
@@ -79,8 +75,13 @@ impl SimPlot {
7975
continue;
8076
}
8177

82-
plot_ui
83-
.polygon(Polygon::new(get_object_mesh(obj)).color(PlotColor::Object.get_color()));
78+
plot_ui.polygon(
79+
Polygon::new(get_object_mesh(
80+
obj.state_at_timestep(state.current_step),
81+
obj.shape,
82+
))
83+
.color(PlotColor::Object.get_color()),
84+
);
8485

8586
self.draw_object(obj, state, plot_ui, index);
8687
}
@@ -159,45 +160,3 @@ impl SimPlot {
159160
.for_each(|item| item.draw(plot_ui));
160161
}
161162
}
162-
163-
#[derive(Clone, Debug)]
164-
pub struct ObjectTraceLine {
165-
data: Vec<[f64; 2]>,
166-
last_pos: NVec2,
167-
start_timestep: usize,
168-
}
169-
170-
impl ObjectTraceLine {
171-
const MAX_DISTANCE: f64 = 225.0;
172-
const MAX_TRACE_LENGTH: usize = 500;
173-
174-
pub(crate) fn new() -> Self {
175-
Self {
176-
data: vec![],
177-
last_pos: NVec2::new(0.0, 0.0),
178-
start_timestep: 0,
179-
}
180-
}
181-
182-
pub(crate) fn update(&mut self, pos: NVec2) {
183-
self.data.push([pos.x, pos.y]);
184-
}
185-
186-
pub(crate) fn line(&self, current_timestep: usize, init_timestep: usize) -> Line {
187-
let line_len = current_timestep
188-
.saturating_sub(init_timestep)
189-
.clamp(0, Self::MAX_TRACE_LENGTH);
190-
191-
let data_len = self.data.len();
192-
193-
let index_end = current_timestep
194-
.saturating_sub(init_timestep)
195-
.clamp(0, data_len);
196-
197-
let index_start = index_end.saturating_sub(line_len);
198-
199-
Line::new(self.data.clone()[index_start..index_end].to_vec())
200-
.color(PlotColor::TraceLine.get_color())
201-
.name("trace line")
202-
}
203-
}

src/app/manager.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ use crate::app::graphics::plot::SimPlot;
22

33
use crate::app::simulations::classic_simulation::state::CSimSettings;
44
use crate::app::simulations::classic_simulation::{ClassicSimulation, Simulation};
5-
use crate::app::Float;
65
use egui::Ui;
76
use instant::Instant;
87

98
use crate::app::simulations::classic_simulation::template::init::SimulationInit;
109
use crate::app::simulations::classic_simulation::template::{CSPreset, CSTemplate};
1110
use crate::app::simulations::state::{SimulationSettings, SimulationState};
1211

12+
pub const SIMULATION_TICK: f64 = 1.0 / 240.0;
13+
1314
/// This is the main simulation manager. It is responsible for managing the simulation and the plot.
1415
pub struct SimulationManager {
1516
simulation: Option<Box<dyn Simulation>>,
1617
sim_state: SimulationState,
17-
timestep: Vec<f32>,
1818
simulation_plot: SimPlot,
1919
is_paused: bool,
2020
last_time_stamp: Instant,
@@ -27,7 +27,6 @@ impl Default for SimulationManager {
2727
Self {
2828
simulation: None,
2929
sim_state: SimulationState::default(),
30-
timestep: vec![],
3130
simulation_plot: SimPlot::default(),
3231
is_paused: true,
3332
last_time_stamp: Instant::now(),
@@ -39,7 +38,7 @@ impl Default for SimulationManager {
3938

4039
/// simple getter and setter
4140
impl SimulationManager {
42-
pub fn time_multiplier(&mut self) -> &mut f64 {
41+
pub fn time_multiplier(&mut self) -> &mut usize {
4342
&mut self.sim_state.time_mul
4443
}
4544

@@ -86,7 +85,6 @@ impl SimulationManager {
8685
self.simulation.replace(simulation);
8786

8887
self.sim_state.reset();
89-
self.timestep.clear();
9088
}
9189

9290
pub fn get_simulation(
@@ -131,14 +129,12 @@ impl SimulationManager {
131129

132130
pub fn timestep_changed(&mut self) {
133131
self.pause();
134-
if !self.timestep.is_empty() {
135-
self.sim_state.time = self.timestep[self.sim_state.current_step] as f64;
132+
self.sim_state.time = SIMULATION_TICK * self.sim_state.current_step as f64;
136133

137-
self.simulation
138-
.as_mut()
139-
.unwrap()
140-
.at_time_step(self.sim_state.current_step);
141-
}
134+
self.simulation
135+
.as_mut()
136+
.unwrap()
137+
.at_time_step(self.sim_state.current_step);
142138
}
143139
}
144140

@@ -169,24 +165,29 @@ impl SimulationManager {
169165
}
170166
}
171167
}
168+
172169
/// for simulation tick
173170
impl SimulationManager {
171+
pub fn simulation_step(&mut self) {
172+
if let Some(simulation) = &mut self.simulation {
173+
self.sim_state.max_step += 1;
174+
self.sim_state.current_step = self.sim_state.max_step;
175+
176+
simulation.step(&mut self.sim_state);
177+
}
178+
179+
self.sim_state.time += SIMULATION_TICK;
180+
}
181+
174182
pub fn step(&mut self) {
175183
if !self.is_paused && self.sim_state.sim_started {
176-
let mut dt = self.last_time_stamp.elapsed().as_secs_f64();
184+
//let mut dt = self.last_time_stamp.elapsed().as_secs_f64();
177185

178186
self.last_time_stamp = Instant::now();
179-
dt *= self.sim_state.time_mul;
180187

181-
if let Some(simulation) = &mut self.simulation {
182-
self.sim_state.max_step = self.timestep.len();
183-
self.sim_state.current_step = self.sim_state.max_step;
184-
self.timestep.push(self.sim_state.time as f32);
185-
186-
simulation.step(dt as Float, &mut self.sim_state);
188+
for _ in 0..self.sim_state.time_mul {
189+
self.simulation_step();
187190
}
188-
189-
self.sim_state.time += dt;
190191
} else {
191192
if self.is_sim_initializing {
192193
let CSPreset {

0 commit comments

Comments
 (0)