initial commit, spawns a body and mesh on a plane.
This commit is contained in:
commit
040bf10f41
5 changed files with 4878 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
4
.rustfmt.toml
Normal file
4
.rustfmt.toml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
wrap_comments = true
|
||||||
|
edition = "2021"
|
4627
Cargo.lock
generated
Normal file
4627
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "physics-test"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
avian3d = "0.1.1"
|
||||||
|
bevy = { version = "0.14.0", features = ["bevy_dev_tools"] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
no-mesh = []
|
235
src/main.rs
Normal file
235
src/main.rs
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
use avian3d::prelude::*;
|
||||||
|
use bevy::{self, color::palettes::css::SILVER, prelude::*};
|
||||||
|
use bike::CyberBikePlugin;
|
||||||
|
use camera::CamPlug;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut app = App::new();
|
||||||
|
app.add_plugins((
|
||||||
|
DefaultPlugins,
|
||||||
|
PhysicsPlugins::default(),
|
||||||
|
PhysicsDebugPlugin::default(),
|
||||||
|
CamPlug,
|
||||||
|
CyberBikePlugin,
|
||||||
|
))
|
||||||
|
.insert_gizmo_config(
|
||||||
|
PhysicsGizmos {
|
||||||
|
contact_point_color: Some(Srgba::GREEN.into()),
|
||||||
|
contact_normal_color: Some(Srgba::WHITE.into()),
|
||||||
|
joint_separation_color: Some(Srgba::RED.into()),
|
||||||
|
#[cfg(feature = "no-mesh")]
|
||||||
|
hide_meshes: true,
|
||||||
|
//axis_lengths: Some(Vec3::ZERO),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
GizmoConfig::default(),
|
||||||
|
)
|
||||||
|
.add_systems(Startup, ground_and_light)
|
||||||
|
.add_systems(Update, close_on_esc)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ground_and_light(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
RigidBody::Static,
|
||||||
|
Collider::cuboid(50.0, 0.5, 50.0),
|
||||||
|
PbrBundle {
|
||||||
|
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
|
||||||
|
material: materials.add(Color::from(SILVER)),
|
||||||
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
commands.spawn(PointLightBundle {
|
||||||
|
point_light: PointLight {
|
||||||
|
intensity: 8_000_000.0,
|
||||||
|
range: 100.,
|
||||||
|
shadows_enabled: true,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
transform: Transform::from_xyz(8.0, 16.0, 8.0),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod bike {
|
||||||
|
use std::f32::consts::FRAC_PI_2;
|
||||||
|
|
||||||
|
use avian3d::prelude::*;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct CyberBikeBody;
|
||||||
|
|
||||||
|
fn spawn_bike(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
let xform = Transform::from_xyz(0.0, 4.0, 0.0);
|
||||||
|
let body_collider =
|
||||||
|
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
|
||||||
|
|
||||||
|
let _bike = commands
|
||||||
|
.spawn(SpatialBundle::from_transform(xform))
|
||||||
|
.insert((
|
||||||
|
RigidBody::Dynamic,
|
||||||
|
body_collider,
|
||||||
|
SleepingDisabled,
|
||||||
|
CyberBikeBody,
|
||||||
|
//CatControllerState::default(),
|
||||||
|
ColliderDensity(0.06),
|
||||||
|
LinearDamping(0.1),
|
||||||
|
AngularDamping(2.0),
|
||||||
|
LinearVelocity::ZERO,
|
||||||
|
AngularVelocity::ZERO,
|
||||||
|
ExternalForce::ZERO.with_persistence(false),
|
||||||
|
ExternalTorque::ZERO.with_persistence(false),
|
||||||
|
))
|
||||||
|
.with_children(|builder| {
|
||||||
|
let color = Color::srgb(0.7, 0.05, 0.7);
|
||||||
|
let mut rotation = Transform::from_rotation(Quat::from_rotation_y(FRAC_PI_2));
|
||||||
|
rotation.rotate_x(FRAC_PI_2);
|
||||||
|
let pbr_bundle = PbrBundle {
|
||||||
|
mesh: meshes.add(Capsule3d::new(0.5, 1.45)),
|
||||||
|
transform: rotation,
|
||||||
|
material: materials.add(color),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
builder.spawn(pbr_bundle);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CyberBikePlugin;
|
||||||
|
|
||||||
|
impl Plugin for CyberBikePlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(Startup, spawn_bike);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod camera {
|
||||||
|
use bevy::{prelude::*, utils::HashSet};
|
||||||
|
|
||||||
|
use crate::bike::CyberBikeBody;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct CyberCameras;
|
||||||
|
|
||||||
|
#[derive(Debug, Resource)]
|
||||||
|
pub struct DebugCamOffset {
|
||||||
|
pub rot: f32,
|
||||||
|
pub dist: f32,
|
||||||
|
pub alt: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DebugCamOffset {
|
||||||
|
fn default() -> Self {
|
||||||
|
DebugCamOffset {
|
||||||
|
rot: 60.0,
|
||||||
|
dist: 10.0,
|
||||||
|
alt: 4.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_camera(mut commands: Commands) {
|
||||||
|
commands
|
||||||
|
.spawn(Camera3dBundle::default())
|
||||||
|
.insert(CyberCameras);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_camera_pos(
|
||||||
|
mut offset: ResMut<DebugCamOffset>,
|
||||||
|
mut keys: ResMut<ButtonInput<KeyCode>>,
|
||||||
|
) {
|
||||||
|
let keyset: HashSet<_> = keys.get_pressed().collect();
|
||||||
|
let shifted = keyset.contains(&KeyCode::ShiftLeft) || keyset.contains(&KeyCode::ShiftRight);
|
||||||
|
|
||||||
|
for key in keyset {
|
||||||
|
match key {
|
||||||
|
KeyCode::ArrowLeft => offset.rot -= 5.0,
|
||||||
|
KeyCode::ArrowRight => offset.rot += 5.0,
|
||||||
|
KeyCode::ArrowUp => {
|
||||||
|
if shifted {
|
||||||
|
bevy::log::info!("up, shifted");
|
||||||
|
offset.alt += 0.5;
|
||||||
|
} else {
|
||||||
|
bevy::log::info!("up");
|
||||||
|
offset.dist -= 0.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::ArrowDown => {
|
||||||
|
if shifted {
|
||||||
|
bevy::log::info!("down, shifted");
|
||||||
|
offset.alt -= 0.5;
|
||||||
|
} else {
|
||||||
|
bevy::log::info!("down");
|
||||||
|
offset.dist += 0.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let just_released: HashSet<_> = keys.get_just_released().cloned().collect();
|
||||||
|
if !just_released.is_empty() {
|
||||||
|
let released_shift = just_released.contains(&KeyCode::ShiftLeft)
|
||||||
|
|| just_released.contains(&KeyCode::ShiftRight);
|
||||||
|
keys.reset_all();
|
||||||
|
if !released_shift && shifted {
|
||||||
|
keys.press(KeyCode::ShiftLeft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn follow_bike(
|
||||||
|
mut camera: Query<&mut Transform, (With<CyberCameras>, Without<CyberBikeBody>)>,
|
||||||
|
bike: Query<&Transform, (With<CyberBikeBody>, Without<CyberCameras>)>,
|
||||||
|
offset: Res<DebugCamOffset>,
|
||||||
|
) {
|
||||||
|
let bike_xform = *bike.single();
|
||||||
|
let up = Vec3::Y;
|
||||||
|
|
||||||
|
let mut ncx = bike_xform;
|
||||||
|
ncx.rotate(Quat::from_axis_angle(up, offset.rot.to_radians()));
|
||||||
|
ncx.translation += ncx.forward() * offset.dist;
|
||||||
|
ncx.translation += ncx.up() * offset.alt;
|
||||||
|
ncx.look_at(bike_xform.translation, up);
|
||||||
|
|
||||||
|
let mut cam_xform = camera.single_mut();
|
||||||
|
*cam_xform = ncx;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CamPlug;
|
||||||
|
|
||||||
|
impl Plugin for CamPlug {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.insert_resource(DebugCamOffset::default())
|
||||||
|
.add_systems(Startup, spawn_camera)
|
||||||
|
.add_systems(Update, (follow_bike, update_camera_pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue