Added a gravity system.
This commit is contained in:
@@ -2,12 +2,13 @@ use bevy::color::palettes::basic::RED;
|
|||||||
use bevy::log;
|
use bevy::log;
|
||||||
use bevy::prelude::*; // Core Bevy types
|
use bevy::prelude::*; // Core Bevy types
|
||||||
|
|
||||||
|
const GRAVITY: f32 = -20.0;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
.add_systems(Update, velocity_timestep)
|
.add_systems(Update, (apply_gravity, velocity_timestep).chain())
|
||||||
.run();
|
.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));
|
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)]
|
#[derive(Component)]
|
||||||
struct Velocity(Vec3);
|
struct Velocity(Vec3);
|
||||||
@@ -29,4 +37,10 @@ fn velocity_timestep(time: Res<Time>, mut query: Query<(&Velocity, &mut Transfor
|
|||||||
for (velocity, mut transform) in query.iter_mut() {
|
for (velocity, mut transform) in query.iter_mut() {
|
||||||
transform.translation += velocity.0 * time.delta_secs();
|
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