clean star pos

This commit is contained in:
WanderingPenwing 2024-10-05 12:55:18 +02:00
parent 0f5f353fd4
commit c0725dcceb

View file

@ -6,6 +6,7 @@ use bevy::math::*;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::f64::consts::PI;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct StarData { struct StarData {
@ -49,16 +50,32 @@ fn setup(
let stars = get_stars().unwrap(); let stars = get_stars().unwrap();
let star_size = 0.02;
let sky_radius = 2.0;
for star in stars { for star in stars {
info!("{:?}", star); info!("{:?}", star);
let star_pos = star_position(star) * sky_radius;
info!("{:?}", star_pos);
// commands.spawn((
// PbrBundle {//Plane3d::default().mesh().size(1., 1.)
// mesh: meshes.add(Cuboid::new(star_size, star_size, star_size)),
// material: materials.add(Color::srgb(1.0, 1.0, 1.0)),
// transform: Transform::from_xyz(star_pos.x, star_pos.y, star_pos.z),
// ..default()
// },
// Star,
// ));
} }
let star_size = 0.02;
commands.spawn(( commands.spawn((
PbrBundle {//Plane3d::default().mesh().size(1., 1.) PbrBundle {//Plane3d::default().mesh().size(1., 1.)
mesh: meshes.add(Cuboid::new(star_size, star_size, star_size)), mesh: meshes.add(Cuboid::new(star_size, star_size, star_size)),
material: materials.add(Color::srgb(1.0, 1.0, 1.0)), material: materials.add(Color::srgb(1.0, 0.0, 0.0)),
transform: Transform::from_xyz(1.0, 0.0, 0.0), transform: Transform::from_xyz(1.0, 0.0, 0.0),
..default() ..default()
}, },
@ -90,23 +107,45 @@ fn get_stars() -> std::io::Result<Vec<StarData>> {
Ok(stars) Ok(stars)
} }
// fn star_position_to_spherical(ra_hours: f32, dec_deg: f32, dec_min: f32, dec_sec: f32) -> Vec3 { fn star_position(star_data: StarData) -> Vec3 {
// // Convert declination to decimal degrees // Convert declination to decimal degrees
// let dec_decimal = declination_to_decimal(dec_deg, dec_min, dec_sec); let text_ra = star_data.ra;
// let text_dec = star_data.dec;
// // Convert Right Ascension from hours to degrees
// let ra_degrees = right_ascension_to_degrees(ra_hours); let ra_seconds: f64 = 3600.0 * text_ra[0..2].parse::<f64>().unwrap()
// + 60.0 * text_ra[4..6].parse::<f64>().unwrap()
// // Convert to spherical coordinates + text_ra[8..12].parse::<f64>().unwrap();
// let theta = ra_degrees.to_radians(); // RA as theta (azimuthal angle)
// let phi = (90.0 - dec_decimal).to_radians(); // Declination to phi (polar angle) info!(text_dec);
// // Parse Dec
// // Assuming a unit sphere, the radius (r) is 1. Calculate Cartesian coordinates. let formated_dec = text_dec
// let x = phi.sin() * theta.cos(); .replace("°", " ")
// let y = phi.sin() * theta.sin(); .replace("", " ")
// let z = phi.cos(); .replace("", " ");
// let dec_parts: Vec<&str> = formated_dec.split_whitespace().collect();
// Vec3::new(x, y, z)
// } let dec_deg: f64 = dec_parts[0].parse::<f64>().unwrap()
+ dec_parts[1].parse::<f64>().unwrap() / 60.0
+ dec_parts[2].parse::<f64>().unwrap() / 3600.0;
let dec_sign : f64 = if text_dec.starts_with('-') {
-1.0
} else {
1.0
};
return celestial_to_cartesian(ra_seconds/3600.0, dec_sign * dec_deg)
}
fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 {
let y_rot = 2.0 * PI * rah / 24.0;
let x_rot = 2.0 * PI * ded / 360.0;
let x : f32 = (y_rot.sin() * x_rot.cos()) as f32;
let y : f32 = x_rot.sin() as f32;
let z : f32 = (y_rot.cos() * x_rot.cos()) as f32;
Vec3::new(x, y, z)
}