1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::b2_draw::*;
use crate::b2_math::*;

use crate::private::rope::b2_rope as private;

#[derive(Clone, Copy, PartialEq)]
pub enum B2stretchingModel {
	B2PbdStretchingModel=0,
	B2XpbdStretchingModel,
}

#[derive(Clone, Copy, PartialEq)]
pub enum B2bendingModel {
	B2SpringAngleBendingModel = 0,
	B2PbdAngleBendingModel,
	B2XpbdAngleBendingModel,
	B2PbdDistanceBendingModel,
	B2PbdHeightBendingModel,
}

#[derive(Clone, Copy)]
pub struct B2ropeTuning {
	pub stretching_model: B2stretchingModel,
	pub bending_model: B2bendingModel,
	pub damping: f32,
	pub stretch_stiffness: f32,
	pub stretch_hertz: f32,
	pub stretch_damping: f32,
	pub bend_stiffness: f32,
	pub bend_hertz: f32,
	pub bend_damping: f32,
	pub isometric: bool,
	pub fixed_effective_mass: bool,
	pub warm_start: bool,
}

impl Default for B2ropeTuning {
	fn default() -> B2ropeTuning {
		Self {
			stretching_model: B2stretchingModel::B2PbdStretchingModel,
			bending_model: B2bendingModel::B2PbdAngleBendingModel,
			damping: 0.0,
			stretch_stiffness: 1.0,
			stretch_hertz: 0.0,
			stretch_damping: 0.0,
			bend_stiffness: 0.5,
			bend_hertz: 1.0,
			bend_damping: 0.0,
			isometric: false,
			fixed_effective_mass: false,
			warm_start: false,
		}
	}
}

#[derive(Default, Clone, Copy)]
pub struct B2ropeDefVertices {
	pub position: B2vec2,
	pub mass: f32,
}

///
pub struct B2ropeDef {
	pub position: B2vec2,
	pub vertices: Vec<B2ropeDefVertices>,
	pub gravity: B2vec2,
	pub tuning: B2ropeTuning,
}

impl Default for B2ropeDef {
	fn default() -> B2ropeDef {
		Self {
			position: B2vec2::zero(),
			vertices: Vec::default(),
			gravity: B2vec2::zero(),
			tuning: Default::default(),
		}
	}
}

#[derive(Default, Clone, Copy)]
pub(crate) struct B2ropeStretch {
	pub i1: i32,
	pub i2: i32,
	pub inv_mass1: f32,
	pub inv_mass2: f32,
	pub l: f32,
	pub lambda: f32,
	pub spring: f32,
	pub damper: f32,
}

#[derive(Default, Clone, Copy)]
pub(crate) struct B2ropeBend {
	pub i1: i32,
	pub i2: i32,
	pub i3: i32,
	pub inv_mass1: f32,
	pub inv_mass2: f32,
	pub inv_mass3: f32,
	pub inv_effective_mass: f32,
	pub lambda: f32,
	pub l1: f32,
	pub l2: f32,
	pub alpha1: f32,
	pub alpha2: f32,
	pub spring: f32,
	pub damper: f32,
}

#[derive(Default, Clone, Copy)]
pub(crate) struct B2ropePositions {
	pub(crate) m_bind_positions: B2vec2,
	pub(crate) m_ps: B2vec2,
	pub(crate) m_p0s: B2vec2,
	pub(crate) m_vs: B2vec2,
	pub(crate) m_inv_masses: f32,
}

pub struct B2rope {
	pub(crate) m_position: B2vec2,

	pub(crate) m_positions: Vec<B2ropePositions>,

	pub(crate) m_stretch_count: usize,
	pub(crate) m_bend_count: usize,

	pub(crate) m_stretch_constraints: Vec<B2ropeStretch>,
	pub(crate) m_bend_constraints: Vec<B2ropeBend>,

	pub(crate) m_gravity: B2vec2,

	pub(crate) m_tuning: B2ropeTuning,
}

impl Default for B2rope {
	fn default() -> Self {
		private::default()
	}
}

///
impl B2rope {
	///
	pub fn create(&mut self, def: &B2ropeDef) {
		private::create(self, def);
	}

	///
	pub fn set_tuning(&mut self, tuning: &B2ropeTuning) {
		private::set_tuning(self, tuning);
	}

	///
	pub fn step(&mut self, time_step: f32, iterations: i32, position: B2vec2) {
		private::step(self, time_step, iterations, position);
	}

	///
	pub fn reset(&mut self, position: B2vec2) {
		private::reset(self, position);
	}

	pub fn draw(&self, draw: &mut dyn B2drawTrait) {
		private::draw(self, draw);
	}
}