summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-03-20 10:04:35 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-03-20 10:04:35 +0200
commitc76373fc35ce72a136bcb86437edf1d9c0ed3aa0 (patch)
tree4068fe7b78153c4205424c761534c2cce21dc9c3 /src
parentfea43de46ac411fff5ac30260bc46bb00d212ba0 (diff)
downloadmartian-rescue-rs-c76373fc35ce72a136bcb86437edf1d9c0ed3aa0.tar.gz
martian-rescue-rs-c76373fc35ce72a136bcb86437edf1d9c0ed3aa0.tar.bz2
martian-rescue-rs-c76373fc35ce72a136bcb86437edf1d9c0ed3aa0.zip
feat: init project with working physics sim
Diffstat (limited to 'src')
-rw-r--r--src/main.rs130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..12150ca 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,133 @@
+use avian2d::prelude::*;
+use bevy::{ecs::bundle, prelude::*, window::WindowResolution, winit::WinitSettings};
fn main() {
println!("Hello, world!");
+ App::new()
+ .add_plugins(DefaultPlugins)
+ .insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9)))
+ .insert_resource(WinitSettings::game())
+ .add_plugins(PhysicsPlugins::default())
+ .add_systems(Startup, setup_init)
+ .add_systems(Startup, setup_stellar_body)
+ .add_systems(Startup, setup_starting_planet)
+ .add_systems(Update, hello_system)
+ .add_systems(Update, update_gravitational_forces)
+ .run();
+}
+fn hello_system() {
+ println!("hello");
+}
+#[derive(Component)]
+struct Starship {
+ name: String,
+ mass_kg: f32,
+}
+#[derive(Component)]
+struct StellarBody {
+ name: String,
+ mass_kg: f32,
+}
+enum GameLevel {
+ Ground,
+ Space,
+ Nars,
+}
+#[derive(PartialEq, Eq)]
+enum PlanetType {
+ Starting,
+ Objective,
+}
+
+#[derive(Component)]
+struct Player {
+ display_name: String,
+ level: GameLevel,
+}
+#[derive(Component)]
+struct StartPlanet {
+ display_name: String,
+ planet_mass_kg: f32,
+ radius: f32,
+}
+#[derive(Component)]
+struct DestPlanet {
+ display_name: String,
+ planet_mass_kg: f32,
+ radius: f32,
+}
+fn setup_stellar_body(mut cmd: Commands) {
+ let objective_planet = DestPlanet {
+ display_name: String::from("Objective Planet"),
+ planet_mass_kg: 50.0,
+ radius: 5.0,
+ };
+ let r = (&objective_planet).radius;
+ let m = (&objective_planet).planet_mass_kg;
+ cmd.spawn((
+ objective_planet,
+ RigidBody::Dynamic,
+ Transform::from_xyz(250.0, 0.0, 0.0),
+ Sprite::from_color(Color::BLACK, Vec2 { x: 30., y: 30. }),
+ ExternalForce::new(Vec2 {x: 4000., y: -4000.}).with_persistence(false),
+ Collider::circle(30.),
+ Mass(m),
+ ));
+}
+fn calculate_force_vector(
+ mass_actor: f32,
+ mass_incident: f32,
+ pos_actor: Vec2,
+ pos_incident: Vec2,
+) -> Vec2 {
+ let GRAV_CONST = 2.0;
+ (GRAV_CONST * mass_actor * mass_incident / (pos_actor.distance_squared(pos_incident)))
+ * Vec2 {
+ x: pos_actor.x - pos_incident.x,
+ y: pos_actor.y - pos_incident.y,
+ }
+ /*Vec2 {
+ x: pos_actor.x - pos_incident.x,
+ y: pos_actor.y - pos_incident.y,
+ }.normalize() * 50000.0*/
+}
+fn update_gravitational_forces<'b>(
+ mut query_start_planet: Query<(Entity, &StartPlanet, &Transform)>,
+ mut query_dest_planet: Query<(Entity, &DestPlanet, &mut ExternalForce, &Transform)>,
+) {
+ let (es, ps, ts) = query_start_planet.single_mut();
+ let (ed, pd, mut exd, td) = query_dest_planet.single_mut();
+ let f = calculate_force_vector(
+ ps.planet_mass_kg,
+ pd.planet_mass_kg,
+ ts.translation.xy(),
+ td.translation.xy(),
+ );
+ println!("force vector: {}, transform_d: {}", f, td.translation);
+ exd.apply_force(f);
+}
+fn setup_starting_planet(mut cmd: Commands) {
+ let start_planet = StartPlanet {
+ display_name: String::from("Earth 2"),
+ planet_mass_kg: 5000.0,
+ radius: 10.0,
+ };
+ let collider_radius = (&start_planet).radius;
+ let planet_mass_kg = (&start_planet).planet_mass_kg;
+ cmd.spawn((
+ start_planet,
+ RigidBody::Static,
+ Transform::from_xyz(0.0, 0.0, 0.0),
+ Sprite::from_color(Color::WHITE, Vec2 { x: 5.0, y: 5.0 }),
+ Collider::circle(5.),
+ Mass(planet_mass_kg),
+ ));
+}
+type PlayerBundle = (Player, Sprite, Transform);
+fn setup_init(mut commands: Commands) {
+ let player = Player {
+ display_name: String::from("player"),
+ level: GameLevel::Ground,
+ };
+ commands.spawn(Camera2d);
+ println!("Setup initial things");
}