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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! `WindZone` — bounded aerodynamic force field applied to rigid bodies
//! and cloth / hair particles.
//!
//! Companion of [`crate::buoyancy_zone`]: reuses the same
//! [`crate::buoyancy_zone::ZoneShape`] volume primitive but replaces
//! the buoyancy + drag model with a directional wind + turbulent gust
//! model tailored for cloth simulation, hair strand advection, and
//! kinematic character wind effects.
//!
//! Aerodynamic force acting on a particle / body inside the zone
//!
//! ```text
//! F = ½ · ρ_air · C_d · A · |v_rel| · v_rel
//! ```
//!
//! where `v_rel = wind_velocity(t) − body_velocity` and
//! `wind_velocity(t) = base_direction · base_speed
//! + turbulence · sin(2π · gust_frequency · t)`.
//!
// LIMITATION(COV-SOFT-072): The MVP evaluates the aerodynamic force in the body's centre and ignores surface-orientation dependence
// LIMITATION(COV-ENV-103): The MVP evaluates the aerodynamic force in the body's centre and ignores surface-orientation dependence
//! The MVP evaluates the aerodynamic force in the body's centre and
//! ignores surface-orientation dependence; full projected-area
//! integration and streamwise-turbulence spectral models are future
//! work.
use crate::buoyancy_zone::ZoneShape;
use crate::math::{Fix128, Vec3Fix};
use crate::solver::RigidBody;
/// Directional wind + turbulent gust volume.
#[derive(Debug, Clone, Copy)]
pub struct WindZone {
/// Geometry of the wind pocket.
pub shape: ZoneShape,
/// Air density (kg/m³). Sea-level ISA = 1.225.
pub air_density_kg_m3: Fix128,
/// Nominal wind direction (unit vector). The builder normalises
/// on construction; the field can be non-unit if the caller wants
/// to bake wind magnitude in.
pub direction: Vec3Fix,
/// Base wind speed (m/s) applied along `direction`.
pub base_speed_m_s: Fix128,
/// Turbulence amplitude scaled by `sin(2π · gust_frequency · t)`
/// and added to the base wind vector.
pub turbulence_amplitude: Fix128,
/// Gust temporal frequency in Hz.
pub gust_frequency_hz: Fix128,
/// Drag coefficient `C_d`. Cloth panels are often ~1.2; unit
/// spheres are 0.47.
pub drag_coefficient: Fix128,
/// Reference cross-sectional area `A` (m²). Callers who want
/// particle-sized cloth can pass the mesh triangle area; for
/// bodies the projected area of the enclosing sphere is a
/// reasonable proxy.
pub reference_area_m2: Fix128,
}
impl WindZone {
/// Preset for a light outdoor breeze (3 m/s base + 0.5 m/s gust,
/// C_d = 1.2, A = 0.5 m²).
#[must_use]
pub fn light_breeze(shape: ZoneShape) -> Self {
Self {
shape,
air_density_kg_m3: Fix128::from_ratio(1225, 1000),
direction: Vec3Fix::new(Fix128::ONE, Fix128::ZERO, Fix128::ZERO),
base_speed_m_s: Fix128::from_int(3),
turbulence_amplitude: Fix128::from_ratio(1, 2),
gust_frequency_hz: Fix128::from_ratio(1, 2),
drag_coefficient: Fix128::from_ratio(12, 10),
reference_area_m2: Fix128::from_ratio(1, 2),
}
}
/// Preset for a storm-force wind (20 m/s base + 5 m/s gust).
#[must_use]
pub fn storm(shape: ZoneShape) -> Self {
Self {
shape,
air_density_kg_m3: Fix128::from_ratio(1225, 1000),
direction: Vec3Fix::new(Fix128::ONE, Fix128::ZERO, Fix128::ZERO),
base_speed_m_s: Fix128::from_int(20),
turbulence_amplitude: Fix128::from_int(5),
gust_frequency_hz: Fix128::from_ratio(3, 2),
drag_coefficient: Fix128::from_ratio(12, 10),
reference_area_m2: Fix128::from_ratio(1, 2),
}
}
/// Instantaneous wind velocity vector at simulation time `t_s`.
///
/// The base direction is scaled by `base_speed_m_s`, then the
// LIMITATION(COV-ENV-032): the turbulence gust `turbulence · sin(2π · f · t)` is added along the same direction
/// turbulence gust `turbulence · sin(2π · f · t)` is added along
/// the same direction. Callers who need cross-wind gusts can
/// override this via [`WindZone::instantaneous_wind_vector`] after
/// construction.
#[must_use]
pub fn instantaneous_wind_vector(&self, t_s: Fix128) -> Vec3Fix {
let base = self.direction * self.base_speed_m_s;
// sin(2π · f · t). We can call the crate's fixed-point sine
// because it is CORDIC-based and cheap.
let two_pi = Fix128::PI * Fix128::from_int(2);
let phase = two_pi * self.gust_frequency_hz * t_s;
let gust_scalar = self.turbulence_amplitude * phase.sin();
base + self.direction * gust_scalar
}
/// Aerodynamic force on `body` at simulation time `t_s`. Returns
/// zero if the body centre is outside the zone.
#[must_use]
pub fn force_on(&self, body: &RigidBody, t_s: Fix128) -> Vec3Fix {
if !self.shape.contains(body.position) {
return Vec3Fix::ZERO;
}
let wind = self.instantaneous_wind_vector(t_s);
self.aerodynamic_force(wind, body.velocity)
}
/// Aerodynamic force on a cloth / hair particle at simulation time
/// `t_s`. `position` is checked against the zone; `velocity` is
/// the particle's current velocity.
#[must_use]
pub fn force_on_particle(&self, position: Vec3Fix, velocity: Vec3Fix, t_s: Fix128) -> Vec3Fix {
if !self.shape.contains(position) {
return Vec3Fix::ZERO;
}
let wind = self.instantaneous_wind_vector(t_s);
self.aerodynamic_force(wind, velocity)
}
fn aerodynamic_force(&self, wind_velocity: Vec3Fix, body_velocity: Vec3Fix) -> Vec3Fix {
let relative = wind_velocity - body_velocity;
let half = Fix128::from_ratio(1, 2);
let coeff = half * self.air_density_kg_m3 * self.drag_coefficient * self.reference_area_m2;
if coeff.is_zero() {
return Vec3Fix::ZERO;
}
// `relative.length_squared()` (|v_rel|^2) overflows Fix128 once
// |v_rel| exceeds ~3.04e9, well before the coefficient-weighted
// magnitude — the quantity that actually needs to stay
// representable — does (e.g. |v_rel| = 4e9 gives a finite,
// representable 6.0e18 N once scaled by `coeff`, but the bare
// |v_rel|^2 = 1.6e19 does not fit). Scale the velocity by
// `sqrt(coeff)` *before* squaring instead of squaring first and
// scaling down afterward: `(k * relative).length_squared() == coeff
// * |v_rel|^2` exactly (k = sqrt(coeff)), without ever materializing
// the bare |v_rel|^2.
let k = coeff.sqrt();
let scaled = relative * k;
let scaled_speed_sq = scaled.length_squared();
if scaled_speed_sq.is_zero() {
return Vec3Fix::ZERO;
}
let scaled_speed = scaled_speed_sq.sqrt();
// magnitude / speed == coeff * speed == k * scaled_speed (both
// `k` and `scaled_speed` carry one factor of the coefficient /
// velocity scaling respectively, so their product recovers
// coeff * |v_rel| without ever computing |v_rel| or |v_rel|^2 bare).
// Direction of the aerodynamic force is aligned with the relative
// velocity.
relative * (k * scaled_speed)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::buoyancy_zone::ZoneShape;
use crate::math::QuatFix;
use crate::solver::{BodyType, RigidBody};
fn wide_aabb() -> ZoneShape {
ZoneShape::Aabb {
min: Vec3Fix::new(
Fix128::from_int(-10),
Fix128::from_int(-10),
Fix128::from_int(-10),
),
max: Vec3Fix::new(
Fix128::from_int(10),
Fix128::from_int(10),
Fix128::from_int(10),
),
}
}
fn resting_body_at(x: i64, y: i64, z: i64) -> RigidBody {
RigidBody {
position: Vec3Fix::new(
Fix128::from_int(x),
Fix128::from_int(y),
Fix128::from_int(z),
),
velocity: Vec3Fix::ZERO,
inv_mass: Fix128::ONE,
inv_inertia: Vec3Fix::ZERO,
prev_position: Vec3Fix::ZERO,
rotation: QuatFix::IDENTITY,
angular_velocity: Vec3Fix::ZERO,
prev_rotation: QuatFix::IDENTITY,
restitution: Fix128::ZERO,
friction: Fix128::ZERO,
gravity_scale: Fix128::ONE,
linear_damping: Fix128::ONE,
angular_damping: Fix128::ONE,
is_sensor: false,
body_type: BodyType::Dynamic,
kinematic_target: None,
}
}
#[test]
fn light_breeze_preset_configured() {
let zone = WindZone::light_breeze(wide_aabb());
assert_eq!(zone.base_speed_m_s, Fix128::from_int(3));
assert!(zone.direction.x > Fix128::ZERO);
}
#[test]
fn storm_preset_stronger_than_breeze() {
let breeze = WindZone::light_breeze(wide_aabb());
let storm = WindZone::storm(wide_aabb());
assert!(storm.base_speed_m_s > breeze.base_speed_m_s);
assert!(storm.turbulence_amplitude > breeze.turbulence_amplitude);
}
#[test]
fn wind_vector_at_zero_time_matches_base() {
let zone = WindZone::light_breeze(wide_aabb());
let v = zone.instantaneous_wind_vector(Fix128::ZERO);
// sin(0) is close to 0 under CORDIC, so only base term remains
// to within one ULP of the CORDIC accuracy.
let expected = zone.direction * zone.base_speed_m_s;
let dx = v.x - expected.x;
let dy = v.y - expected.y;
let dz = v.z - expected.z;
let tolerance = Fix128::from_ratio(1, 100);
assert!(dx.abs() < tolerance);
assert!(dy.abs() < tolerance);
assert!(dz.abs() < tolerance);
}
#[test]
fn force_zero_outside_zone() {
let zone = WindZone::light_breeze(wide_aabb());
let body = resting_body_at(100, 0, 0); // outside AABB.
let f = zone.force_on(&body, Fix128::ZERO);
assert_eq!(f, Vec3Fix::ZERO);
}
#[test]
fn force_direction_aligned_with_wind() {
let zone = WindZone::light_breeze(wide_aabb());
let body = resting_body_at(0, 0, 0);
let f = zone.force_on(&body, Fix128::ZERO);
// Wind is +x, body is resting, so force must be +x.
assert!(f.x > Fix128::ZERO);
assert_eq!(f.y, Fix128::ZERO);
assert_eq!(f.z, Fix128::ZERO);
}
#[test]
fn force_reverses_when_body_moves_faster_than_wind() {
let zone = WindZone::light_breeze(wide_aabb());
let mut body = resting_body_at(0, 0, 0);
body.velocity = Vec3Fix::new(Fix128::from_int(10), Fix128::ZERO, Fix128::ZERO);
let f = zone.force_on(&body, Fix128::ZERO);
// Body outruns the wind → drag pulls back (−x).
assert!(f.x < Fix128::ZERO);
}
#[test]
fn particle_force_matches_body_force_for_same_state() {
let zone = WindZone::light_breeze(wide_aabb());
let body = resting_body_at(0, 0, 0);
let f_body = zone.force_on(&body, Fix128::ZERO);
let f_particle = zone.force_on_particle(body.position, body.velocity, Fix128::ZERO);
assert_eq!(f_body, f_particle);
}
#[test]
fn zero_force_when_wind_matches_body_velocity() {
let zone = WindZone::light_breeze(wide_aabb());
let mut body = resting_body_at(0, 0, 0);
body.velocity = zone.direction * zone.base_speed_m_s;
let f = zone.force_on(&body, Fix128::ZERO);
assert_eq!(f, Vec3Fix::ZERO);
}
}