Added a gravity system.
This commit is contained in:
@@ -2,12 +2,13 @@ use bevy::color::palettes::basic::RED;
|
||||
use bevy::log;
|
||||
use bevy::prelude::*; // Core Bevy types
|
||||
|
||||
const GRAVITY: f32 = -20.0;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, velocity_timestep)
|
||||
.add_systems(Update, (apply_gravity, velocity_timestep).chain())
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -17,10 +18,17 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
|
||||
|
||||
let shape =meshes.add(Circle::new(50.0));
|
||||
|
||||
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(100.0, 0.0, 0.0), Velocity(vec3(0.0, 100.0, 0.0))));
|
||||
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(100.0, 0.0, 0.0), Velocity(vec3(
|
||||
0.0,
|
||||
100.0,
|
||||
0.0
|
||||
))));
|
||||
|
||||
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(0.0, 0.0, 0.0), Velocity(vec3(0.0, 100.0, 0.0))));
|
||||
}
|
||||
commands.spawn((Mesh2d(shape.clone()), MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(300.0, 0.0, 0.0), Velocity(vec3(
|
||||
0.0,
|
||||
50.0,
|
||||
0.0
|
||||
))));}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Velocity(Vec3);
|
||||
@@ -30,3 +38,9 @@ fn velocity_timestep(time: Res<Time>, mut query: Query<(&Velocity, &mut Transfor
|
||||
transform.translation += velocity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_gravity(time: Res<Time>, mut query: Query<&mut Velocity>) {
|
||||
for mut velocity in query.iter_mut() {
|
||||
velocity.0.y += GRAVITY * time.delta_secs();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user