From 0c630d000e674812b4fee24213ad0c767afc2d94 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Fri, 31 May 2024 19:54:41 -0400 Subject: [PATCH] Adds AABB constructor stub for building with a mesh and center point --- crates/core/src/core.rs | 9 ++++-- crates/physics/src/components.rs | 8 +++++ crates/physics/src/lib.rs | 2 +- crates/physics/src/systems.rs | 55 ++++++++++++++++++++++---------- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/crates/core/src/core.rs b/crates/core/src/core.rs index 40ee0cf..f5a90be 100644 --- a/crates/core/src/core.rs +++ b/crates/core/src/core.rs @@ -1,14 +1,14 @@ use bevy::{pbr::wireframe::{Wireframe, WireframePlugin}, prelude::*}; -use physics::{PhysicsPlugin, components::Velocity}; +use physics::{components::{Velocity, AABB}, PhysicsPlugin}; pub struct CorePlugin; impl Plugin for CorePlugin { fn build(&self, app: &mut App) { - app.add_plugins((PhysicsPlugin {debug_draw_colliders: true})) + app.add_plugins(PhysicsPlugin {debug_draw_colliders: true}) .add_systems(Startup, setup); } } @@ -44,7 +44,10 @@ fn setup(mut commands: Commands, server: Res, mut ambient_light: Re scene: brick_handle, transform: Transform::from_xyz(0.0, 7.0, 0.0), .. default() - }, Wireframe)); + // TODO: need to figure out how to get the box dimensions from the loaded scene + }, + AABB::from_point_and_size(Vec3::new(0.0, 7.0, 0.0), Vec3::new(2.0, 2.0, 2.0)) + )); // BALL let ball_handle: Handle = server.load("ball.glb#Scene0"); diff --git a/crates/physics/src/components.rs b/crates/physics/src/components.rs index bdcc527..6b178df 100644 --- a/crates/physics/src/components.rs +++ b/crates/physics/src/components.rs @@ -43,6 +43,14 @@ impl AABB } } + pub fn from_point_and_mesh(center: Vec3, mesh: Handle) -> Self + { + // TODO: Build an AABB around the given mesh + // Find the smallest and largets vertices on each axis + // and use those to set the extents of the AABB + todo!() + } + // ACCESSORS pub fn get_center_point(&self) -> Vec3 { diff --git a/crates/physics/src/lib.rs b/crates/physics/src/lib.rs index 661f8fa..a5052ff 100644 --- a/crates/physics/src/lib.rs +++ b/crates/physics/src/lib.rs @@ -18,7 +18,7 @@ impl Plugin for PhysicsPlugin if self.debug_draw_colliders { - app.add_systems(Startup, systems::debug_draw_aabbs); + app.add_systems(Update, systems::debug_draw_aabbs); } } } diff --git a/crates/physics/src/systems.rs b/crates/physics/src/systems.rs index b034496..9150f2f 100644 --- a/crates/physics/src/systems.rs +++ b/crates/physics/src/systems.rs @@ -18,7 +18,7 @@ pub(crate) fn update_colliders() pub(crate) fn debug_draw_aabbs( mut commands: Commands, - aabbs: Query<&AABB>, + aabbs: Query<(Entity, &AABB), Without>, mut meshes: ResMut>, mut materials: ResMut> ){ @@ -26,26 +26,49 @@ pub(crate) fn debug_draw_aabbs( // TODO: Spawn one of these for each collider in the scene, // need to set the scale, rotation and translation to match the AABB + for (entity, _aabb) in aabbs.iter() + { + let cube_mesh_handle: Handle = meshes.add(Cuboid::default()); + commands.entity(entity).insert((PbrBundle + { + mesh: cube_mesh_handle, + material: materials.add(StandardMaterial + { + base_color: Color::Rgba { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.25 }, + alpha_mode: AlphaMode::Blend, + ..default() + }), + + transform: Transform::from_xyz(0.0, 7.25, -1.0) + //.with_rotation(Quat::from_rotation_y(45.0)) + .with_rotation(Quat::from_rotation_x(45.0)), + ..default() + }, + + Wireframe, + WireframeColor { color: Color::GREEN }, + )); + } + // TEST CUBE // Spawns a cube with a colored wireframe and partially transparent body // Should be able to update the material on the fly to change the colors // to red when the AABB is intersecting another AABB - let cube_mesh_handle: Handle = meshes.add(Cuboid::default()); - commands.spawn((PbrBundle { - mesh: cube_mesh_handle, - material: materials.add(StandardMaterial { - base_color: Color::Rgba { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.25 }, - alpha_mode: AlphaMode::Blend, - ..default() - }), - transform: Transform::from_xyz(0.0, 7.25, -1.0) - //.with_rotation(Quat::from_rotation_y(45.0)) - .with_rotation(Quat::from_rotation_x(45.0)), - ..default() - }, - Wireframe, WireframeColor { color: Color::GREEN }, - )); + // commands.spawn((PbrBundle { + // mesh: cube_mesh_handle, + // material: materials.add(StandardMaterial { + // base_color: Color::Rgba { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.25 }, + // alpha_mode: AlphaMode::Blend, + // ..default() + // }), + // transform: Transform::from_xyz(0.0, 7.25, -1.0) + // //.with_rotation(Quat::from_rotation_y(45.0)) + // .with_rotation(Quat::from_rotation_x(45.0)), + // ..default() + // }, + // Wireframe, WireframeColor { color: Color::GREEN }, + // )); } pub(crate) fn debug_update_colliders(colliders: Query<&ColliderDebug>)