keplerian_sim/body.rs
1use crate::Orbit;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// A struct representing a celestial body.
7#[derive(Clone, Debug, PartialEq)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub struct Body {
10 /// The name of the celestial body.
11 pub name: String,
12
13 /// The mass of the celestial body, in kilograms.
14 pub mass: f64,
15
16 /// The radius of the celestial body, in meters.
17 pub radius: f64,
18
19 /// The orbit of the celestial body, if it is orbiting one.
20 pub orbit: Option<Orbit>,
21}
22
23impl Body {
24 /// Creates a new `Body` instance.
25 ///
26 /// # Arguments
27 ///
28 /// * `name` - The name of the celestial body.
29 /// * `mass` - The mass of the celestial body, in kilograms.
30 /// * `radius` - The radius of the celestial body, in meters.
31 /// * `orbit` - An optional orbit for the celestial body.
32 ///
33 /// # Returns
34 ///
35 /// A new `Body` instance.
36 pub fn new(name: String, mass: f64, radius: f64, orbit: Option<Orbit>) -> Self {
37 Self {
38 name,
39 mass,
40 radius,
41 orbit,
42 }
43 }
44
45 /// Releases the body from its orbit.
46 pub fn release_from_orbit(&mut self) {
47 self.orbit = None;
48 }
49}
50
51impl Default for Body {
52 /// Creates a default `Body` instance.
53 ///
54 /// Currently, this function returns the Earth.
55 /// However, do not rely on this behavior, as it may change in the future.
56 fn default() -> Self {
57 Self {
58 name: "Earth".to_string(),
59 mass: 5.972e24,
60 radius: 6.371e6,
61 orbit: None,
62 }
63 }
64}