Compare commits
8 Commits
0f81fcc905
...
e2171f695d
| Author | SHA256 | Date | |
|---|---|---|---|
| e2171f695d | |||
| b35458f2f5 | |||
| cd9f040258 | |||
| 70a155903e | |||
| ce3e08c1e7 | |||
| a339776e2f | |||
| 384ea0f7a1 | |||
| 4ebff662ac |
65
app/src/camera.rs
Normal file
65
app/src/camera.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use bevy::input::mouse::MouseMotion;
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub const CAMERA_SPEED: f32 = 20.0;
|
||||
pub const CAMERA_SENSITIVITY: f32 = 1.0;
|
||||
|
||||
pub struct CameraPlugin;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app
|
||||
.add_systems(Update, camera_movement_controller)
|
||||
.add_systems(Update, camera_rotation_controller);
|
||||
}
|
||||
}
|
||||
|
||||
fn camera_movement_controller(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut camera: Single<&mut Transform, With<Camera3d>>,
|
||||
) {
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
direction.z -= 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
direction.z += 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
direction.x -= 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
direction.x += 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::Space) {
|
||||
direction.y += 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ShiftLeft) {
|
||||
direction.y -= 1.0;
|
||||
}
|
||||
|
||||
let rotation = camera.rotation;
|
||||
camera.translation += rotation * direction * CAMERA_SPEED * time.delta_secs(); // or use time.delta_seconds()
|
||||
}
|
||||
|
||||
fn camera_rotation_controller(
|
||||
time: Res<Time>,
|
||||
mut mouse_motion: MessageReader<MouseMotion>,
|
||||
mut camera: Single<&mut Transform, With<Camera3d>>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
){
|
||||
for motion in mouse_motion.read() {
|
||||
let rotation: Vec3 = vec3(-motion.delta.y,-motion.delta.x,0.0) * time.delta_secs() * CAMERA_SENSITIVITY;
|
||||
let translation: Quat = Quat::from_scaled_axis(rotation);
|
||||
camera.rotation *= translation;
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyQ){
|
||||
camera.rotation *= Quat::from_scaled_axis(Vec3::Z * CAMERA_SENSITIVITY * time.delta_secs());
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyE){
|
||||
camera.rotation *= Quat::from_scaled_axis(Vec3::NEG_Z * CAMERA_SENSITIVITY * time.delta_secs());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,54 @@
|
||||
mod physics;
|
||||
mod camera;
|
||||
|
||||
use bevy::log;
|
||||
use bevy::prelude::*;
|
||||
use crate::camera::CameraPlugin;
|
||||
use crate::physics::{PhysicsPlugin, Velocity};
|
||||
use crate::physics::gravity::Gravity;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.insert_resource(Gravity(-0.981))
|
||||
.add_plugins(PhysicsPlugin)
|
||||
.add_plugins(CameraPlugin)
|
||||
.add_systems(Startup, setup)
|
||||
.run();
|
||||
}
|
||||
|
||||
/// set up a simple 3D scene
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
// circular base
|
||||
commands.spawn((
|
||||
Mesh3d(meshes.add(Circle::new(4.0))),
|
||||
MeshMaterial3d(materials.add(Color::WHITE)),
|
||||
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
|
||||
));
|
||||
// cube
|
||||
commands.spawn((
|
||||
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
|
||||
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
|
||||
Transform::from_xyz(0.0, 0.5, 0.0),
|
||||
Velocity::from_xyz(0.0, 2.0, 0.0),
|
||||
));
|
||||
// light
|
||||
commands.spawn((
|
||||
PointLight {
|
||||
shadows_enabled: true,
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(4.0, 8.0, 4.0),
|
||||
));
|
||||
// camera
|
||||
commands.spawn((
|
||||
Camera3d::default(),
|
||||
Transform::from_xyz(-2.5, 4.5, 9.0),
|
||||
));
|
||||
|
||||
log::info!("Setup finished");
|
||||
}
|
||||
|
||||
6
app/src/physics.rs
Normal file
6
app/src/physics.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod gravity;
|
||||
pub mod velocity;
|
||||
pub mod plugin;
|
||||
|
||||
pub use plugin::PhysicsPlugin;
|
||||
pub use velocity::Velocity;
|
||||
10
app/src/physics/gravity.rs
Normal file
10
app/src/physics/gravity.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use bevy::prelude::Resource;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct Gravity(pub f32);
|
||||
|
||||
impl Default for Gravity {
|
||||
fn default() -> Self {
|
||||
Gravity(-9.81) // default gravity
|
||||
}
|
||||
}
|
||||
27
app/src/physics/plugin.rs
Normal file
27
app/src/physics/plugin.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use bevy::app::{App, Plugin, Update};
|
||||
use bevy::prelude::{IntoScheduleConfigs, Query, Res, Time, Transform};
|
||||
use crate::physics::gravity::Gravity;
|
||||
use crate::physics::velocity::Velocity;
|
||||
|
||||
pub struct PhysicsPlugin;
|
||||
|
||||
impl Plugin for PhysicsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app
|
||||
.init_resource::<Gravity>()
|
||||
.add_systems(Update, apply_gravity)
|
||||
.add_systems(Update, velocity_timestep.after(apply_gravity));
|
||||
}
|
||||
}
|
||||
|
||||
fn velocity_timestep(time: Res<Time>, mut query: Query<(&Velocity, &mut Transform)>) {
|
||||
for (velocity, mut transform) in query.iter_mut() {
|
||||
transform.translation += velocity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_gravity(time: Res<Time>, gravity: Res<Gravity>, mut query: Query<&mut Velocity>) {
|
||||
for mut velocity in query.iter_mut() {
|
||||
velocity.0.y += gravity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
12
app/src/physics/velocity.rs
Normal file
12
app/src/physics/velocity.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use bevy::math::Vec3;
|
||||
use bevy::prelude::Component;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Velocity(pub Vec3);
|
||||
|
||||
impl Velocity {
|
||||
|
||||
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
|
||||
Velocity(Vec3::new(x, y, z))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user