Skip to content

Commit 63b3b6f

Browse files
committed
Make gravitational constant configurable
1 parent 5fbd3e7 commit 63b3b6f

3 files changed

Lines changed: 46 additions & 20 deletions

File tree

crates/forces/src/core/gravity.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@ use super::newton_laws::{AppliedForce, Mass};
22
use bevy::prelude::*;
33

44
// Simulation constants
5-
pub const GRAVITATIONAL_CONSTANT: f32 = 0.1;
5+
pub const DEFAULT_GRAVITATIONAL_CONSTANT: f32 = 0.1;
66

77
/// Resource for gravity simulation parameters
88
#[derive(Resource, Clone, Debug)]
99
pub struct GravityParams {
1010
/// Softening parameter to prevent singularities
1111
pub softening: f32,
12+
/// Gravitational constant controlling attraction strength
13+
pub gravitational_constant: f32,
1214
}
1315

1416
impl Default for GravityParams {
1517
fn default() -> Self {
16-
Self { softening: 5.0 }
18+
Self {
19+
softening: 5.0,
20+
gravitational_constant: DEFAULT_GRAVITATIONAL_CONSTANT,
21+
}
22+
}
23+
}
24+
25+
impl GravityParams {
26+
pub fn with_softening(mut self, softening: f32) -> Self {
27+
self.softening = softening;
28+
self
29+
}
30+
31+
pub fn with_gravitational_constant(mut self, gravitational_constant: f32) -> Self {
32+
self.gravitational_constant = gravitational_constant;
33+
self
1734
}
1835
}
1936

@@ -268,6 +285,7 @@ pub fn calculate_gravitational_attraction(
268285
>,
269286
) {
270287
let softening_squared = gravity_params.softening * gravity_params.softening;
288+
let gravitational_constant = gravity_params.gravitational_constant;
271289

272290
let sources: Vec<(Entity, Vec3, f32)> = query
273291
.iter()
@@ -286,7 +304,7 @@ pub fn calculate_gravitational_attraction(
286304
let direction = source_pos - affected_pos;
287305
let distance_squared = direction.length_squared();
288306
let softened_distance_squared = distance_squared + softening_squared;
289-
let force_magnitude = GRAVITATIONAL_CONSTANT * source_mass * affected_mass.value
307+
let force_magnitude = gravitational_constant * source_mass * affected_mass.value
290308
/ softened_distance_squared;
291309
force.force += direction.normalize() * force_magnitude;
292310
}
@@ -315,6 +333,8 @@ pub fn calculate_barnes_hut_attraction(
315333
.collect();
316334

317335
let quadtree = spatial::Quadtree::from_bodies(&bodies);
336+
let softening = gravity_params.softening;
337+
let gravitational_constant = gravity_params.gravitational_constant;
318338

319339
affected_query
320340
.par_iter_mut()
@@ -329,7 +349,8 @@ pub fn calculate_barnes_hut_attraction(
329349
position,
330350
&quadtree.root,
331351
theta,
332-
gravity_params.softening,
352+
softening,
353+
gravitational_constant,
333354
);
334355

335356
force.force += force_vector;
@@ -341,15 +362,16 @@ pub fn calculate_barnes_hut_force(
341362
node: &spatial::QuadtreeNode,
342363
theta: f32,
343364
softening: f32,
365+
gravitational_constant: f32,
344366
) -> Vec3 {
345367
let softening_squared = softening * softening;
346368

347369
if node.is_far_enough(affected_position, theta) {
348370
let direction = node.mass_properties.center_of_mass - affected_position;
349371
let distance_squared = direction.length_squared();
350372
let softened_distance_squared = distance_squared + softening_squared;
351-
let force_magnitude =
352-
GRAVITATIONAL_CONSTANT * node.mass_properties.total_mass / softened_distance_squared;
373+
let force_magnitude = gravitational_constant * node.mass_properties.total_mass
374+
/ softened_distance_squared;
353375
return direction.normalize() * force_magnitude;
354376
}
355377

@@ -364,27 +386,31 @@ pub fn calculate_barnes_hut_force(
364386
continue;
365387
}
366388

367-
total_force += {
368-
let distance_squared = direction.length_squared();
369-
let softened_distance_squared = distance_squared + softening_squared;
370-
let force_magnitude = GRAVITATIONAL_CONSTANT * mass / softened_distance_squared;
371-
direction.normalize() * force_magnitude
372-
};
389+
let softened_distance_squared = distance_squared + softening_squared;
390+
let force_magnitude =
391+
gravitational_constant * mass / softened_distance_squared;
392+
total_force += direction.normalize() * force_magnitude;
373393
}
374394

375395
return total_force;
376396
}
377397

378398
let mut total_force = Vec3::ZERO;
379399
for child_node in node.children.iter().flatten() {
380-
total_force += calculate_barnes_hut_force(affected_position, child_node, theta, softening);
400+
total_force += calculate_barnes_hut_force(
401+
affected_position,
402+
child_node,
403+
theta,
404+
softening,
405+
gravitational_constant,
406+
);
381407
}
382408

383409
total_force
384410
}
385411

386412
pub fn calculate_orbital_velocity(central_mass: f32, orbit_radius: f32) -> f32 {
387-
(GRAVITATIONAL_CONSTANT * central_mass / orbit_radius).sqrt()
413+
(DEFAULT_GRAVITATIONAL_CONSTANT * central_mass / orbit_radius).sqrt()
388414
}
389415

390416
pub fn calculate_elliptical_orbit_velocity(
@@ -393,13 +419,13 @@ pub fn calculate_elliptical_orbit_velocity(
393419
eccentricity: f32,
394420
is_periapsis: bool,
395421
) -> f32 {
396-
let mu = GRAVITATIONAL_CONSTANT * central_mass;
422+
let mu = DEFAULT_GRAVITATIONAL_CONSTANT * central_mass;
397423
let semimajor_axis = distance / (1.0 - eccentricity * if is_periapsis { 1.0 } else { -1.0 });
398424
(mu * (2.0 / distance - 1.0 / semimajor_axis)).sqrt()
399425
}
400426

401427
pub fn calculate_escape_velocity(central_mass: f32, distance: f32) -> f32 {
402-
(2.0 * GRAVITATIONAL_CONSTANT * central_mass / distance).sqrt()
428+
(2.0 * DEFAULT_GRAVITATIONAL_CONSTANT * central_mass / distance).sqrt()
403429
}
404430

405431
#[derive(Default)]

crates/forces/src/core/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ pub mod newton_laws;
77
pub mod prelude {
88
// Re-export from gravity module
99
pub use crate::core::gravity::{
10-
GRAVITATIONAL_CONSTANT, GravityAffected, GravityParams, GravitySource, MassiveBody,
11-
UniformGravity, calculate_elliptical_orbit_velocity, calculate_escape_velocity,
12-
calculate_gravitational_attraction, calculate_orbital_velocity,
10+
DEFAULT_GRAVITATIONAL_CONSTANT, GravityAffected, GravityParams, GravitySource,
11+
MassiveBody, UniformGravity, calculate_elliptical_orbit_velocity,
12+
calculate_escape_velocity, calculate_gravitational_attraction, calculate_orbital_velocity,
1313
};
1414

1515
// Re-export from newton_laws module

examples/basic_forces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
..default()
1212
}))
1313
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.1)))
14-
.insert_resource(GravityParams { softening: 10.0 }) // Better softening value for stability
14+
.insert_resource(GravityParams::default().with_softening(10.0)) // Better softening value for stability
1515
.add_systems(Startup, setup)
1616
.add_systems(
1717
Update,

0 commit comments

Comments
 (0)