From 4feb3a8fb81fa648c2c5f7a72029ff5b54cf0dcb Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Mon, 30 Sep 2024 12:05:43 -0400 Subject: learning rust project start --- projects/learning-rust/1.html | 58 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'projects/learning-rust/1.html') diff --git a/projects/learning-rust/1.html b/projects/learning-rust/1.html index 840a968..be6bfa7 100644 --- a/projects/learning-rust/1.html +++ b/projects/learning-rust/1.html @@ -1,7 +1,7 @@ - 1: Solar Systems + 1: Orbitals @@ -9,22 +9,33 @@
-

Learning Rust : Solar Systems

+

Learning Rust : Orbitals

How do I store non-owning references to collection members?
< + Home + >

I want to make a data-driven viewer for the solar system, as part of a game inspired by Aurora 4x but Rust is an entirely different beast from C.

+

So let's start with the types i'll use:

+

+pub const GRAVITATIONAL_CONSTANT : f64 = 6.67430e-11;
+
+pub type Second = u64;
+pub type Meter = f64;
+pub type Rad = f64;
+pub type Kilogram = f64;
+            

I have prior experience writing systems like this, so I figured it would be as easy as:


 pub struct Orbital {
     //Some optional non-owning reference to the parent orbital
-    mass : f64,
-    semi_major_axis : f64,
-    eccentricity : f64,
-    inclination : f64,
-    long_asc_node : f64,
-    argument_periapsis : f64,
-    mean_anomaly_epoch : f64
+    mass : Kilogram,
+    semi_major_axis : Rad,
+    eccentricity : Rad,
+    inclination : Rad,
+    long_asc_node : Rad,
+    argument_periapsis : Rad,
+    mean_anomaly_epoch : Rad
 }
             

The question then is: How do we store the reference to the parent orbital?

@@ -36,33 +47,38 @@ pub struct Orbital {

But not every part of the update function needs the parent orbital's position, so I can split the update into two parts:

1: Update the orbital elements relative to the parent object

2: Use the parent orbital's position to transform the relative coordinates into absolute coordinates

+

All the tick method needs is an immutable parent object to calculate the standard gravitational parameter of the one-body system being emulated.

So here's what I came up with.


 pub struct Orbital {
-    parent_index : usize,
-    //Temporary state variables for later update.
-    rel_pos : (f64, f64, f64)
+    origin : Option<usize>,
+    //Euler angles for a given time (T) relative to origin
+    rel_pos : Option<(Second, Meter, Rad, Rad, Rad)>
+    //Cartesian coordinates for a given time (T) relative to (0, 0, 0)
+    abs_pos : Option<(Second, Meter, Meter, Meter)>
     //Orbital parameters
-    mass : f64,
-    semi_major_axis : f64,
-    eccentricity : f64,
-    inclination : f64,
-    long_asc_node : f64,
-    argument_periapsis : f64,
-    mean_anomaly_epoch : f64
+    mass : Kilogram,
+    semi_major_axis : Meter,
+    eccentricity : Rad,
+    inclination : Rad,
+    long_asc_node : Rad,
+    argument_periapsis : Rad,
+    mean_anomaly_epoch : Rad
 }
 //...
 impl Orbital {
 //...
-    pub fn update_rel(&mut self) -> Orbital {
+    //Update relative 
+    pub fn tick(&mut self, &system: System, t: Second) {
         //...
     }
     
-    pub fn update(&self, &system : System) -> (f64, f64, f64) {
+    pub fn pos(&self, &system : System, t: Second) -> (Meter, Meter, Meter) {
         //...
     }
 }
         
+

Now comes the daunting task of storing our orbitals in a usable manner. Ideally, I want to iterate through the list of orbitals in a way that also lets me access other members of the orbital list from the tick and pos functions. It's a lot harder than it should be.

-- cgit v1.2.1