avian3d-sandbox/src/main.rs
Joe Ardent a65e60a316 use torque for lean, use Single and Populated instead of Query
systems won't run if no entities match a Single or Populated query, so no need to return a Result or set the error handler
2025-04-30 16:58:20 -07:00

98 lines
2.7 KiB
Rust

use avian3d::prelude::{
Collider, ColliderDensity, CollisionLayers, CollisionMargin, Friction, PhysicsGizmos, RigidBody,
};
use bevy::{
color::{palettes::css::SILVER, Alpha},
pbr::MeshMaterial3d,
prelude::{
children, default, App, AppGizmoBuilder, Assets, ButtonInput, Color, Commands,
DefaultPlugins, Entity, GizmoConfig, KeyCode, Mesh, Mesh3d, Meshable, Plane3d, PointLight,
Query, Res, ResMut, SpawnRelated, Srgba, StandardMaterial, Startup, Transform, Update,
Vec3, Visibility, Window,
},
};
mod bike;
mod camera;
mod input;
mod physics;
use bike::CyberBikePlugin;
use camera::CamPlug;
use input::CyberInputPlugin;
use physics::CyberPhysicsPlugin;
fn main() {
let mut app = App::new();
app.add_plugins((
DefaultPlugins,
CamPlug,
CyberBikePlugin,
CyberInputPlugin,
CyberPhysicsPlugin,
))
.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::new(2.0, 2.0, 4.0)),
..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>>,
) {
let spatial_bundle = (Transform::default(), Visibility::default());
commands.spawn((
spatial_bundle,
RigidBody::Static,
Collider::cuboid(500.0, 5., 500.0),
CollisionMargin(0.1),
ColliderDensity(1000.0),
CollisionLayers::ALL,
Friction::new(0.9),
children![(
Mesh3d(meshes.add(Plane3d::default().mesh().size(500.0, 500.0))),
MeshMaterial3d(materials.add(Color::from(SILVER).with_alpha(0.7))),
Transform::from_xyz(0.0, 2.5, 0.0),
Visibility::Visible,
)],
));
commands.spawn((
PointLight {
intensity: 8_000_000.0,
range: 100.,
shadows_enabled: true,
..default()
},
Transform::from_xyz(8.0, 16.0, 8.0),
));
}
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();
}
}
}