Skip to content

Commit

Permalink
Merge pull request #45 from thombruce/feat/ship-orbit
Browse files Browse the repository at this point in the history
add simple gravitational impulse attracting ship to orbitables
  • Loading branch information
thombruce authored Oct 11, 2023
2 parents 1f0bf48 + f28bb4c commit 11b50d5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Simple gravitational impulse attracting ship to orbitables (star and planets)
- Add and use KDNode instead of Orbitable for nearest neighbour mapping

### Changed
Expand Down
33 changes: 33 additions & 0 deletions src/player/dynamic_orbit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy_rapier2d::prelude::ExternalImpulse;
use bevy_spatial::{kdtree::KDTree2, SpatialAccess};

use crate::{core::resources::state::GameState, world::spatial::KDNode};

use super::ship::Ship;

pub struct DynamicOrbitPlugin;
impl Plugin for DynamicOrbitPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
dynamic_orbital_positioning_system.run_if(in_state(GameState::Active)),
);
}
}

pub fn dynamic_orbital_positioning_system(
tree: Res<KDTree2<KDNode>>,
mut query: Query<(&Transform, &mut ExternalImpulse), With<Ship>>,
) {
let (transform, mut impulse) = query.single_mut();

// From Nav
let player_translation = transform.translation.xy();

if let Some((pos, _entity)) = tree.nearest_neighbour(player_translation) {
if pos.distance(player_translation) < 1500. {
impulse.impulse += (pos - player_translation).normalize() * 2000.;
}
}
}
4 changes: 3 additions & 1 deletion src/player/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

pub mod dynamic_orbit;
pub mod ship;

use self::ship::ShipPlugin;
use self::{dynamic_orbit::DynamicOrbitPlugin, ship::ShipPlugin};

pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(1.0),
ShipPlugin,
DynamicOrbitPlugin,
));

app.add_systems(Startup, setup);
Expand Down
4 changes: 2 additions & 2 deletions src/player/ship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn setup(mut commands: Commands, sprites: Res<SpriteAssets>) {
SpriteBundle {
texture: sprites.player_ship.clone(),
transform: Transform {
translation: Vec3::new(0., 0., 100.0),
translation: Vec3::new(200., 0., 100.0),
scale: Vec3::splat(0.5),
..default()
},
Expand Down Expand Up @@ -129,5 +129,5 @@ pub fn ship_flight_system(
velocity.angvel += rotation_factor * ship.rotation / 60.0;
}

impulse.impulse = (transform.rotation * (Vec3::Y * thrust_factor * ship.thrust)).truncate();
impulse.impulse += (transform.rotation * (Vec3::Y * thrust_factor * ship.thrust)).truncate();
}
2 changes: 1 addition & 1 deletion src/world/astronomy/orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

use crate::core::resources::{game_time::GameTime, state::GameState};

const ORBITAL_PERIOD_SCALING_FACTOR: f32 = 1.0;
pub const ORBITAL_PERIOD_SCALING_FACTOR: f32 = 1.0;

#[derive(Component, Clone, Debug)]
pub struct Orbit {
Expand Down

0 comments on commit 11b50d5

Please sign in to comment.