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

Commit 45b78b9

Browse files
committed
충돌 디버깅 수정시도
1 parent da89328 commit 45b78b9

File tree

5 files changed

+62
-101
lines changed

5 files changed

+62
-101
lines changed

src/app/graphics/plot.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
21
use eframe::epaint::FontFamily;
32

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

7-
use crate::app::graphics::define::{PlotColor};
6+
use crate::app::graphics::define::PlotColor;
87
use crate::app::graphics::CSPlotObjects;
98

109
use crate::app::simulations::classic_simulation::{CSimObject, Simulation};
1110
use crate::app::simulations::state::SimulationState;
1211

1312
pub mod object;
1413

15-
16-
1714
pub struct PlotData {
1815
pub near_value: f64,
1916
pub nearest_label: String,
@@ -61,9 +58,11 @@ impl SimPlot {
6158
state.sim_started = true;
6259
}
6360

64-
simulation.get_events(state.current_step).get_shapes().into_iter().for_each(|shape|{
65-
shape.draw(plot_ui);
66-
});
61+
if let Some(x) = simulation.get_events(state.current_step) {
62+
x.get_shapes().into_iter().for_each(|shape| {
63+
shape.draw(plot_ui);
64+
})
65+
}
6766

6867
let simulation_objects = simulation.get_children();
6968

@@ -74,7 +73,10 @@ impl SimPlot {
7473
.current_state()
7574
.position;
7675

77-
plot_ui.line(Line::new(vec![[pos.x, pos.y], [pointer_pos.x, pointer_pos.y]]));
76+
plot_ui.line(Line::new(vec![
77+
[pos.x, pos.y],
78+
[pointer_pos.x, pointer_pos.y],
79+
]));
7880
}
7981
}
8082

src/app/simulations/classic_simulation.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1+
pub mod event;
12
pub mod object;
23
pub mod sim_state;
34
pub mod template;
4-
pub mod event;
5-
65

76
use crate::app::NVec2;
87

98
use egui::plot::PlotPoint;
109
use egui::{Response, Ui};
1110
use nalgebra::{vector, SMatrix};
1211

13-
14-
1512
use crate::app::graphics::plot::{InputMessage, PlotData};
1613
use crate::app::manager::SIMULATION_TICK;
1714
use crate::app::simulations::polygon::is_inside;
@@ -22,7 +19,9 @@ use self::object::state::{CSObjectState, ForceIndex};
2219
use crate::app::simulations::classic_simulation::object::state::Collision;
2320
pub use object::CSimObject;
2421

25-
use crate::app::simulations::classic_simulation::event::{CollisionEvent, SimulationEvent, SimulationEvents};
22+
use crate::app::simulations::classic_simulation::event::{
23+
CollisionEvent, SimulationEvent, SimulationEvents,
24+
};
2625

2726
pub const GRAVITY: SMatrix<f64, 2, 1> = vector![0.0, -9.8];
2827
pub const ZERO_FORCE: SMatrix<f64, 2, 1> = vector![0.0, 0.0];
@@ -57,7 +56,7 @@ pub trait Simulation: Send + Sync {
5756

5857
fn get_children(&self) -> &Vec<CSimObject>;
5958

60-
fn get_events(&self, idx: usize) -> &SimulationEvents;
59+
fn get_events(&self, idx: usize) -> Option<&SimulationEvents>;
6160
}
6261

6362
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
@@ -78,7 +77,6 @@ const OPERATION_ITER: [Operation; 5] = [
7877
Operation::EditObject,
7978
];
8079

81-
8280
#[derive()]
8381
pub struct ClassicSimulation {
8482
pub objects: Vec<CSimObject>,
@@ -93,7 +91,7 @@ impl From<Vec<CSimObject>> for ClassicSimulation {
9391
ClassicSimulation {
9492
objects: object,
9593
global_acc_list: vec![GRAVITY],
96-
events: vec![SimulationEvents::default()],
94+
events: vec![],
9795
operation: Operation::default(),
9896
}
9997
}
@@ -224,8 +222,6 @@ impl Simulation for ClassicSimulation {
224222
}
225223
}
226224

227-
228-
229225
fn step(&mut self, state: &mut SimulationState) {
230226
self.update();
231227
puffin::profile_scope!("ClassicSimulation::step");
@@ -250,9 +246,9 @@ impl Simulation for ClassicSimulation {
250246
let Some((obj, rest)) = end.split_first_mut() else {panic!("Cannot Reach")};
251247

252248
for obj2 in rest {
253-
Self::collision(obj, obj2).map(|x|{
254-
self.events[state.current_step].add_event(x);
255-
});
249+
if let Some(x) = Self::collision(obj, obj2) {
250+
self.events[state.current_step.saturating_sub(1)].add_event(x);
251+
}
256252
}
257253
}
258254

@@ -265,8 +261,6 @@ impl Simulation for ClassicSimulation {
265261
Self::physics(obj, &self.global_acc_list);
266262
obj.save_state();
267263
}
268-
269-
270264
}
271265

272266
fn at_time_step(&mut self, step: usize) {
@@ -279,13 +273,16 @@ impl Simulation for ClassicSimulation {
279273
&self.objects
280274
}
281275

282-
fn get_events(&self, idx: usize) -> &SimulationEvents {
283-
&self.events[idx]
276+
fn get_events(&self, idx: usize) -> Option<&SimulationEvents> {
277+
if idx == 0 {
278+
None
279+
} else {
280+
Some(&self.events[idx.saturating_sub(1)])
281+
}
284282
}
285283
}
286284

287-
288-
impl ClassicSimulation{
285+
impl ClassicSimulation {
289286
fn update(&mut self) {
290287
self.events.push(SimulationEvents::default());
291288
}
@@ -301,12 +298,12 @@ impl ClassicSimulation{
301298
// obj2_state.position += contact.penetration * -contact.contact_normal;
302299

303300
Some(contact)
304-
}else {
301+
} else {
305302
None
306303
}
307304
}
308305

309-
fn physics(obj: &mut CSimObject, global_acc_list: &Vec<NVec2>) {
306+
fn physics(obj: &mut CSimObject, global_acc_list: &[NVec2]) {
310307
// Physics
311308
let global_acc: NVec2 = global_acc_list.iter().sum();
312309
let state = obj.current_state_mut();
@@ -323,11 +320,11 @@ impl ClassicSimulation{
323320
let current_acc = state.acceleration();
324321

325322
let sum_acc = current_acc + global_acc; // Σa
326-
// let Δa = current_acc - last_state.acceleration();
323+
// let Δa = current_acc - last_state.acceleration();
327324

328325
let delta_v = sum_acc * dt; // 등가속도 운동에서의 보정.
329-
// let Δv_error = (Δa * dt) / 2.0;
330-
// let Δv = Δv + Δv_error;
326+
// let Δv_error = (Δa * dt) / 2.0;
327+
// let Δv = Δv + Δv_error;
331328

332329
let v = state.velocity;
333330

@@ -341,9 +338,6 @@ impl ClassicSimulation{
341338
state.position += delta_pos
342339
}
343340
}
344-
345341
}
346342

347-
348-
349343
//
Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1-
use egui::plot::Arrows;
21
use crate::app::graphics::define::PlotItem;
2+
use crate::app::simulations::classic_simulation::object::state::{CSObjectState, ListAdd};
33
use crate::app::NVec2;
4-
use crate::app::simulations::classic_simulation::object::state::CSObjectState;
4+
use egui::plot::Arrows;
55

66
pub struct SimulationEvents(Vec<SimulationEvent>);
77

8-
impl Default for SimulationEvents{
8+
impl Default for SimulationEvents {
99
fn default() -> Self {
1010
Self(vec![])
1111
}
1212
}
1313

14-
impl SimulationEvents{
15-
16-
pub fn add_events(&mut self, events: Vec<impl Into<SimulationEvent>>){
14+
impl SimulationEvents {
15+
pub fn add_events(&mut self, events: Vec<impl Into<SimulationEvent>>) {
1716
self.0.extend(events.into_iter().map(|x| x.into()));
1817
}
1918

20-
pub fn add_event(&mut self, event: impl Into<SimulationEvent>){
19+
pub fn add_event(&mut self, event: impl Into<SimulationEvent>) {
2120
self.0.push(event.into());
2221
}
2322

24-
pub fn get_shapes(&self) -> Vec<PlotItem>{
25-
self.0.iter().fold(vec![],|mut acc,x|{
23+
pub fn get_shapes(&self) -> Vec<PlotItem> {
24+
self.0.iter().fold(vec![], |mut acc, x| {
2625
acc.extend(x.get_shapes());
2726
acc
2827
})
2928
}
3029
}
3130

32-
impl Into<SimulationEvent> for CollisionEvent{
31+
impl Into<SimulationEvent> for CollisionEvent {
3332
fn into(self) -> SimulationEvent {
3433
SimulationEvent::Collision(self)
3534
}
3635
}
3736

38-
pub enum SimulationEvent{
37+
pub enum SimulationEvent {
3938
Collision(CollisionEvent),
4039
}
4140

42-
impl SimulationEvent{
43-
pub fn get_shapes(&self) -> Vec<PlotItem>{
44-
match self{
41+
impl SimulationEvent {
42+
pub fn get_shapes(&self) -> Vec<PlotItem> {
43+
match self {
4544
Self::Collision(event) => event.get_shapes().into_iter().map(|x| x.into()).collect(),
4645
}
4746
}
4847
}
4948

50-
pub struct CollisionEvent{
49+
pub struct CollisionEvent {
5150
pub contact_point: NVec2,
5251
pub contact_normal: NVec2,
5352
pub obj1_velocity: NVec2,
@@ -57,12 +56,9 @@ pub struct CollisionEvent{
5756
pub penetration: f64,
5857
}
5958

60-
61-
62-
impl CollisionEvent{
63-
pub fn get_shapes(&self) -> Vec<impl Into<PlotItem>>{
59+
impl CollisionEvent {
60+
pub fn get_shapes(&self) -> Vec<impl Into<PlotItem>> {
6461
let obj1_pos = self.obj1_state.position.data.0[0];
65-
6662
let obj2_pos = self.obj2_state.position.data.0[0];
6763

6864
let obj2_velocity = self.obj2_state.velocity.data.0[0];
@@ -74,10 +70,13 @@ impl CollisionEvent{
7470
let obj2_momentum = self.obj2_state.momentum().data.0[0];
7571

7672
vec![
77-
Arrows::new(vec![obj1_pos], vec![contact_normal]).name("contact_normal"),
78-
Arrows::new(vec![obj1_pos], vec![obj1_velocity]).name("obj1_velocity_diff"),
79-
Arrows::new(vec![obj2_pos], vec![obj2_velocity]).name("obj2_velocity_diff"),
80-
Arrows::new(vec![obj1_pos, obj2_pos], vec![obj1_momentum, obj2_momentum]).name("momentum"),
73+
Arrows::new(vec![obj2_pos], vec![contact_normal]).name("contact_normal"),
74+
Arrows::new(vec![obj1_pos], vec![obj1_velocity.add(obj1_pos)])
75+
.name("obj1_velocity_diff"),
76+
Arrows::new(vec![obj2_pos], vec![obj2_velocity.add(obj2_pos)])
77+
.name("obj2_velocity_diff"),
78+
Arrows::new(vec![obj1_pos, obj2_pos], vec![obj1_momentum, obj2_momentum])
79+
.name("momentum"),
8180
]
8281
}
8382
}

src/app/simulations/classic_simulation/object/shape.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::app::NVec2;
22
use egui::plot::PlotPoints;
33
use std::f64::consts::TAU;
44

5-
65
pub trait Shape {
76
fn get_points(&self) -> Vec<[f64; 2]>;
87
fn get_plot_points(&self, pos: NVec2) -> PlotPoints;

src/app/simulations/classic_simulation/object/state.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
2-
use egui::plot::{Arrows};
3-
use crate::app::simulations::classic_simulation::object::shape::{ObjectShape};
4-
5-
use crate::app::NVec2;
6-
use nalgebra::{vector};
7-
use crate::app::graphics::define::{ PlotItem};
81
use crate::app::simulations::classic_simulation::event::CollisionEvent;
2+
use crate::app::simulations::classic_simulation::object::shape::ObjectShape;
3+
use crate::app::NVec2;
4+
use nalgebra::vector;
95

10-
trait ListAdd<Rhs = Self> {
6+
pub trait ListAdd<Rhs = Self> {
117
type Output;
128
fn add(self, rhs: Rhs) -> Self::Output;
139
}
1410

15-
impl ListAdd for [f64; 2]{
11+
impl ListAdd for [f64; 2] {
1612
type Output = [f64; 2];
1713

1814
fn add(self, rhs: [f64; 2]) -> Self {
@@ -43,8 +39,7 @@ pub struct CSObjectState {
4339

4440
impl Collision for CSObjectState {
4541
fn contact(&self, ops: &CSObjectState) -> Option<CollisionEvent> {
46-
let mut shape = None;
47-
let info = match (self.shape, ops.shape) {
42+
match (self.shape, ops.shape) {
4843
(ObjectShape::Circle(circle), ObjectShape::Circle(circle2)) => {
4944
let dist = (self.position - ops.position).magnitude();
5045
let penetration = circle.radius + circle2.radius - dist;
@@ -72,32 +67,6 @@ impl Collision for CSObjectState {
7267
let obj1_velocity = contact_normal * obj1_scale / self.mass;
7368
let obj2_velocity = contact_normal * -obj2_scale / ops.mass;
7469

75-
let _raw_contact_point = contact_point.data.0[0];
76-
77-
let raw_self_position = self.position.data.0[0];
78-
let raw_ops_position = ops.position.data.0[0];
79-
80-
shape = Some(Box::new(move ||{
81-
vec![
82-
PlotItem::Arrows(Arrows::new(vec![raw_self_position], vec![
83-
raw_ops_position
84-
]).name("contact_normal")),
85-
PlotItem::Arrows(Arrows::new(vec![
86-
raw_self_position
87-
88-
], vec![
89-
90-
raw_self_position.add(obj1_velocity.data.0[0]),
91-
]).name("obj1_velocity")),
92-
PlotItem::Arrows(Arrows::new(vec![
93-
raw_ops_position
94-
95-
], vec![
96-
raw_ops_position.add(obj2_velocity.data.0[0]),
97-
]).name("obj2_velocity")),
98-
]
99-
}));
100-
10170
Some(CollisionEvent {
10271
contact_point,
10372
contact_normal,
@@ -107,16 +76,14 @@ impl Collision for CSObjectState {
10776
obj2_state: ops.clone(),
10877

10978
obj1_velocity,
110-
obj2_velocity
79+
obj2_velocity,
11180
})
11281
} else {
11382
None
11483
}
11584
}
11685
_ => None,
117-
};
118-
119-
info
86+
}
12087
}
12188
}
12289

0 commit comments

Comments
 (0)