Skip to main content

box2d_rs/
b2_rope.rs

1use crate::b2_draw::*;
2use crate::b2_math::*;
3
4use crate::private::rope::b2_rope as private;
5
6#[derive(Clone, Copy, PartialEq)]
7pub enum B2stretchingModel {
8	B2PbdStretchingModel=0,
9	B2XpbdStretchingModel,
10}
11
12#[derive(Clone, Copy, PartialEq)]
13pub enum B2bendingModel {
14	B2SpringAngleBendingModel = 0,
15	B2PbdAngleBendingModel,
16	B2XpbdAngleBendingModel,
17	B2PbdDistanceBendingModel,
18	B2PbdHeightBendingModel,
19	B2PbdTriangleBendingModel,
20}
21
22#[derive(Clone, Copy)]
23pub struct B2ropeTuning {
24	pub stretching_model: B2stretchingModel,
25	pub bending_model: B2bendingModel,
26	pub damping: f32,
27	pub stretch_stiffness: f32,
28	pub stretch_hertz: f32,
29	pub stretch_damping: f32,
30	pub bend_stiffness: f32,
31	pub bend_hertz: f32,
32	pub bend_damping: f32,
33	pub isometric: bool,
34	pub fixed_effective_mass: bool,
35	pub warm_start: bool,
36}
37
38impl Default for B2ropeTuning {
39	fn default() -> B2ropeTuning {
40		Self {
41			stretching_model: B2stretchingModel::B2PbdStretchingModel,
42			bending_model: B2bendingModel::B2PbdAngleBendingModel,
43			damping: 0.0,
44			stretch_stiffness: 1.0,
45			stretch_hertz: 0.0,
46			stretch_damping: 0.0,
47			bend_stiffness: 0.5,
48			bend_hertz: 1.0,
49			bend_damping: 0.0,
50			isometric: false,
51			fixed_effective_mass: false,
52			warm_start: false,
53		}
54	}
55}
56
57#[derive(Default, Clone, Copy)]
58pub struct B2ropeDefVertices {
59	pub position: B2vec2,
60	pub mass: f32,
61}
62
63///
64pub struct B2ropeDef {
65	pub position: B2vec2,
66	pub vertices: Vec<B2ropeDefVertices>,
67	pub gravity: B2vec2,
68	pub tuning: B2ropeTuning,
69}
70
71impl Default for B2ropeDef {
72	fn default() -> B2ropeDef {
73		Self {
74			position: B2vec2::zero(),
75			vertices: Vec::default(),
76			gravity: B2vec2::zero(),
77			tuning: Default::default(),
78		}
79	}
80}
81
82#[derive(Default, Clone, Copy)]
83pub(crate) struct B2ropeStretch {
84	pub i1: i32,
85	pub i2: i32,
86	pub inv_mass1: f32,
87	pub inv_mass2: f32,
88	pub l: f32,
89	pub lambda: f32,
90	pub spring: f32,
91	pub damper: f32,
92}
93
94#[derive(Default, Clone, Copy)]
95pub(crate) struct B2ropeBend {
96	pub i1: i32,
97	pub i2: i32,
98	pub i3: i32,
99	pub inv_mass1: f32,
100	pub inv_mass2: f32,
101	pub inv_mass3: f32,
102	pub inv_effective_mass: f32,
103	pub lambda: f32,
104	pub l1: f32,
105	pub l2: f32,
106	pub alpha1: f32,
107	pub alpha2: f32,
108	pub spring: f32,
109	pub damper: f32,
110}
111
112#[derive(Default, Clone, Copy)]
113pub(crate) struct B2ropePositions {
114	pub(crate) m_bind_positions: B2vec2,
115	pub(crate) m_ps: B2vec2,
116	pub(crate) m_p0s: B2vec2,
117	pub(crate) m_vs: B2vec2,
118	pub(crate) m_inv_masses: f32,
119}
120
121pub struct B2rope {
122	pub(crate) m_position: B2vec2,
123
124	pub(crate) m_positions: Vec<B2ropePositions>,
125
126	pub(crate) m_stretch_count: usize,
127	pub(crate) m_bend_count: usize,
128
129	pub(crate) m_stretch_constraints: Vec<B2ropeStretch>,
130	pub(crate) m_bend_constraints: Vec<B2ropeBend>,
131
132	pub(crate) m_gravity: B2vec2,
133
134	pub(crate) m_tuning: B2ropeTuning,
135}
136
137impl Default for B2rope {
138	fn default() -> Self {
139		private::default()
140	}
141}
142
143///
144impl B2rope {
145	///
146	pub fn create(&mut self, def: &B2ropeDef) {
147		private::create(self, def);
148	}
149
150	///
151	pub fn set_tuning(&mut self, tuning: &B2ropeTuning) {
152		private::set_tuning(self, tuning);
153	}
154
155	///
156	pub fn step(&mut self, time_step: f32, iterations: i32, position: B2vec2) {
157		private::step(self, time_step, iterations, position);
158	}
159
160	///
161	pub fn reset(&mut self, position: B2vec2) {
162		private::reset(self, position);
163	}
164
165	pub fn draw(&self, draw: &mut dyn B2drawTrait) {
166		private::draw(self, draw);
167	}
168}