I have achieved a throwable cube

This commit is contained in:
Jay Christy
2023-10-19 16:27:22 -04:00
parent c82b10f53a
commit 43215f6221
2 changed files with 215 additions and 67 deletions

View File

@@ -1,4 +1,18 @@
use bevy::{prelude::{Commands, ResMut, Assets, Mesh, StandardMaterial, PbrBundle, shape, Color, Transform, SpatialBundle, Camera3dBundle, Vec3, PointLight, PointLightBundle}, utils::default};
use bevy::{
prelude::{
shape, Assets, Camera3dBundle, Color, Commands, Mesh, PbrBundle, PointLight,
PointLightBundle, ResMut, SpatialBundle, StandardMaterial, Transform, Vec3,
},
transform::TransformBundle,
utils::default,
};
use bevy_openxr::xr_input::interactions::{Touched, XRInteractable, XRInteractableState};
use bevy_rapier3d::{
prelude::{Collider, RigidBody},
render::ColliderDebugColor,
};
use crate::Grabbable;
/// set up a simple 3D scene
pub fn setup_scene(
@@ -6,19 +20,37 @@ pub fn setup_scene(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
/*
* Ground
*/
let ground_size = 2.5;
let ground_height = 0.1;
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
transform: Transform::from_xyz(0.0, -ground_height, 0.0),
..default()
},
Collider::cuboid(ground_size, ground_height, ground_size),
));
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
RigidBody::Dynamic,
Collider::cuboid(0.05, 0.05, 0.05),
ColliderDebugColor(Color::hsl(220.0, 1.0, 0.3)),
XRInteractable,
XRInteractableState::default(),
Grabbable,
Touched(false),
));
// light
commands.spawn(PointLightBundle {
@@ -42,4 +74,45 @@ pub fn setup_scene(
),
..default()
},));
}
}
pub fn setup_physics(mut commands: Commands) {
/*
* Create the cubes
*/
let num = 8;
let rad = 1.0;
let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let centerz = shift * (num / 2) as f32;
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
let mut color = 0;
let colors = [
Color::hsl(220.0, 1.0, 0.3),
Color::hsl(180.0, 1.0, 0.3),
Color::hsl(260.0, 1.0, 0.7),
];
for j in 0usize..20 {
for i in 0..num {
for k in 0usize..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 3.0;
let z = k as f32 * shift - centerz + offset;
color += 1;
commands.spawn((
TransformBundle::from(Transform::from_xyz(x, y, z)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
ColliderDebugColor(colors[color % 3]),
));
}
}
offset -= 0.05 * rad * (num as f32 - 1.0);
}
}