update to bevy 0.15-rc.3

This commit is contained in:
Joe Ardent 2024-11-27 11:14:24 -08:00
parent 5bb6d10558
commit c366dc3ec6
4 changed files with 2165 additions and 1199 deletions

3215
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
argh = "0.1.12"
bevy = "0.12.0"
bevy_spatial = "0.7.0"
rand = "0.8.5"
argh = "0.1"
bevy = "0.15.0-rc.3"
bevy_spatial = { git = "https://github.com/laundmo/bevy-spatial.git", rev = "9ac313ac6173b440f50954b696dc2063513b4f37" } #"0.10.0"
rand = "0.8"

View file

@ -8,7 +8,7 @@ use bevy_spatial::{kdtree::KDTree3, SpatialAccess};
pub type NNTree = KDTree3<Toid>;
// toid stuff
const SPEED: f32 = 0.2;
const SPEED: f32 = 10.0;
const SPEED_DIFF_RANGE: f32 = 0.1; // +/- 10%
const MAX_DELTA_V: f32 = std::f32::consts::PI * 2.0; // basically 360 degrees/sec
@ -59,23 +59,19 @@ pub fn turkey_time(
let z = r.gen_range(-10.0..=10.0);
let y = r.gen_range(0.1..=5.5);
let pos = Vec3::new(x, MIN_ALTITUDE + y, z);
let spatial_bundle = SpatialBundle {
transform: Transform::from_translation(pos),
..Default::default()
};
let xform = Transform::from_translation(pos);
//dbg!(xform);
let spatial_bundle = (xform, Visibility::Visible);
commands
.spawn(spatial_bundle)
.insert((Velocity(vel), Buddies::default(), Toid { speed, buddies }))
.with_children(|t| {
t.spawn(SceneBundle {
scene: scene.to_owned(),
..Default::default()
})
// .insert(Transform::default());
.insert(Transform::from_rotation(Quat::from_axis_angle(
Vec3::Y,
-std::f32::consts::FRAC_PI_2,
)));
t.spawn(SceneRoot(scene.to_owned()))
.insert(Transform::default());
// .insert(Transform::from_rotation(Quat::from_axis_angle(
// Vec3::Y,
// -std::f32::consts::FRAC_PI_2,
// )));
})
.id()
}
@ -86,7 +82,7 @@ pub fn update_vel(
positions: Res<Positions>,
index: Res<NNTree>,
) {
let dt = time.delta_seconds();
let dt = time.delta_secs();
let max_delta = MAX_DELTA_V * dt;
for (xform, mut vel, toid, buddies, entity) in toids.iter_mut() {
let speed = toid.speed;
@ -160,13 +156,11 @@ pub fn update_pos(
time: Res<Time>,
) {
let mut new_look = Vec3::ZERO;
let dt = time.delta_seconds();
let dt = time.delta_secs();
for (mut xform, vel, entity) in toids.iter_mut() {
xform.translation += **vel * dt;
let look_at = xform.translation + **vel;
let right = look_at.cross(Vec3::Y).normalize();
let up = right.cross(look_at).normalize();
xform.look_at(look_at, up);
let look_at = vel.normalize();
xform.look_to(look_at, Vec3::Y);
*positions.entry(entity).or_insert(Vec3::ZERO) = xform.translation;
new_look += xform.translation;
}
@ -205,40 +199,41 @@ pub fn update_buddies(
pub fn update_gizmos(toids: Query<&Transform, With<Toid>>, mut gizmos: Gizmos) {
for toid in toids.iter() {
let pos = toid.translation;
let up = toid.up();
let forward = toid.forward();
let right = toid.right();
gizmos.ray(pos, pos + forward, Color::RED);
gizmos.ray(pos, pos + up, Color::BLUE);
gizmos.ray(pos, pos + right, Color::GREEN);
//dbg!(toid);
let up = toid.up().as_vec3();
let forward = toid.forward().as_vec3();
let right = toid.right().as_vec3();
gizmos.ray(pos, pos + forward, Color::srgb_u8(255, 0, 0));
//gizmos.ray(pos, pos + up, Color::srgb_u8(0, 0, 255));
//gizmos.ray(pos, pos + right, Color::srgb_u8(0, 255, 0));
}
}
pub fn rotate_camera(
mut query: Query<&mut Transform, With<Camera>>,
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
lookat: Res<LookAt>,
) {
let mut xform = query.single_mut();
let forward = xform.forward() * 0.7;
let right = xform.right();
let right = xform.right().as_vec3();
if keys.pressed(KeyCode::Right) {
if keys.pressed(KeyCode::ArrowRight) {
xform.translation += right;
}
if keys.pressed(KeyCode::Left) {
if keys.pressed(KeyCode::ArrowLeft) {
xform.translation -= right;
}
if keys.pressed(KeyCode::Up) {
if keys.pressed(KeyCode::ArrowUp) {
if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) {
xform.translation += Vec3::Y;
} else {
xform.translation += forward;
}
}
if keys.pressed(KeyCode::Down) {
if keys.pressed(KeyCode::ArrowDown) {
if keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight) {
xform.translation -= Vec3::Y;
} else {
@ -256,7 +251,7 @@ pub fn rotate_camera(
pub fn unit_vec(r: &mut impl rand::Rng) -> Vec3 {
let mut x1: f32 = 0.0;
let mut x2: f32 = 0.0;
let mut ssum = std::f32::MAX;
let mut ssum = f32::MAX;
while ssum >= 1.0 {
x1 = r.gen_range(-1.0..=1.0);
x2 = r.gen_range(-1.0..=1.0);

View file

@ -2,6 +2,7 @@ use std::time::Duration;
use audubon::*;
use bevy::{
color::palettes::css::BLACK,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
@ -20,16 +21,16 @@ fn main() {
))
.insert_resource(config)
.insert_resource(Positions::default())
.insert_resource(ClearColor(Color::rgb(0.64, 0.745, 0.937))) // a nice light blue
.insert_resource(ClearColor(Color::srgb(0.64, 0.745, 0.937))) // a nice light blue
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0,
})
.insert_resource(LookAt::default())
.add_systems(Startup, setup)
.add_systems(Startup, (setup, setup_ambient_light))
//.add_systems(Update, update_gizmos)
.add_systems(Update, (update_pos, update_buddies, update_vel).chain())
.add_systems(Update, (rotate_camera, bevy::window::close_on_esc))
.add_systems(Update, update_gizmos)
.add_systems(Update, (rotate_camera, close_on_esc))
.run();
}
@ -41,38 +42,61 @@ fn setup(
models: Res<AssetServer>,
) {
let rand = &mut rand::thread_rng();
let cam = Camera3d::default();
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 5.0, 25.).looking_at(Vec3::new(0.0, 5.0, 0.0), Vec3::Y),
..default()
});
let camera = commands
.spawn((
Transform::from_xyz(0., 5.0, 25.).looking_at(Vec3::new(0.0, 5.0, 0.0), Vec3::Y),
cam,
Visibility::Hidden,
))
.id();
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(500.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands
.spawn(Mesh3d(
meshes.add(Mesh::from(Plane3d::default().mesh().size(500.0, 500.0))),
))
.insert(MeshMaterial3d(materials.add(Color::srgb(0.3, 1.0, 0.3))));
let toid_model = models.load("models/boid.glb#Scene0");
let toid_model = models.load("models/toid3.glb#Scene0");
for _ in 0..config.toids {
let _ = turkey_time(&mut commands, &toid_model, rand);
}
// instructions
commands.spawn(
TextBundle::from_section(
"Up and down for camera forward and back; hold shift to change height.\nLeft or right to move left or right.\nPress 'ESC' to quit.",
TextStyle {
font_size: 20.,
..default()
},
)
.with_style(Style {
commands.spawn((
Text::new(
"Up and down for camera forward and back; hold shift to change height.\nLeft or right to move left or right.\nPress 'ESC' to quit."), TextFont {
font_size: 20.0,
..Default::default()
},
Node {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
TargetCamera(camera),
TextColor::WHITE));
}
fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
fn setup_ambient_light(mut ambient_light: ResMut<AmbientLight>) {
ambient_light.brightness = 100.0;
}