From c0725dcceb24137636d3422bba7067ef6181bb0a Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sat, 5 Oct 2024 12:55:18 +0200 Subject: [PATCH] clean star pos --- src/main.rs | 81 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 501443d..7fe1e71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use bevy::math::*; use std::fs::File; use std::io::Read; use serde::{Deserialize, Serialize}; +use std::f64::consts::PI; #[derive(Serialize, Deserialize, Debug)] struct StarData { @@ -49,16 +50,32 @@ fn setup( let stars = get_stars().unwrap(); + let star_size = 0.02; + let sky_radius = 2.0; + 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(( 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)), + material: materials.add(Color::srgb(1.0, 0.0, 0.0)), transform: Transform::from_xyz(1.0, 0.0, 0.0), ..default() }, @@ -90,23 +107,45 @@ fn get_stars() -> std::io::Result> { Ok(stars) } -// fn star_position_to_spherical(ra_hours: f32, dec_deg: f32, dec_min: f32, dec_sec: f32) -> Vec3 { -// // Convert declination to decimal degrees -// let dec_decimal = declination_to_decimal(dec_deg, dec_min, dec_sec); -// -// // Convert Right Ascension from hours to degrees -// let ra_degrees = right_ascension_to_degrees(ra_hours); -// -// // Convert to spherical coordinates -// let theta = ra_degrees.to_radians(); // RA as theta (azimuthal angle) -// let phi = (90.0 - dec_decimal).to_radians(); // Declination to phi (polar angle) -// -// // Assuming a unit sphere, the radius (r) is 1. Calculate Cartesian coordinates. -// let x = phi.sin() * theta.cos(); -// let y = phi.sin() * theta.sin(); -// let z = phi.cos(); -// -// Vec3::new(x, y, z) -// } +fn star_position(star_data: StarData) -> Vec3 { + // Convert declination to decimal degrees + let text_ra = star_data.ra; + let text_dec = star_data.dec; + + let ra_seconds: f64 = 3600.0 * text_ra[0..2].parse::().unwrap() + + 60.0 * text_ra[4..6].parse::().unwrap() + + text_ra[8..12].parse::().unwrap(); + + info!(text_dec); + // Parse Dec + let formated_dec = text_dec + .replace("°", " ") + .replace("′", " ") + .replace("″", " "); + let dec_parts: Vec<&str> = formated_dec.split_whitespace().collect(); + + let dec_deg: f64 = dec_parts[0].parse::().unwrap() + + dec_parts[1].parse::().unwrap() / 60.0 + + dec_parts[2].parse::().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) +}