I have achieved a throwable cube
This commit is contained in:
@@ -1,11 +1,25 @@
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
||||||
prelude::{info, App, Commands, SpatialBundle, Startup, PostUpdate, IntoSystemSetConfigs, IntoSystemConfigs, Transform, Color}, transform::{TransformSystem, TransformBundle},
|
log::info,
|
||||||
|
prelude::{
|
||||||
|
App, Commands, IntoSystemConfigs, IntoSystemSetConfigs, PostUpdate, Query, Res,
|
||||||
|
SpatialBundle, Startup, Update, With, Without, Component, EventReader, Transform, GlobalTransform,
|
||||||
|
},
|
||||||
|
transform::TransformSystem,
|
||||||
};
|
};
|
||||||
use bevy_openxr::{
|
use bevy_openxr::{
|
||||||
|
input::XrInput,
|
||||||
|
resources::{XrFrameState, XrInstance, XrSession},
|
||||||
xr_input::{
|
xr_input::{
|
||||||
debug_gizmos::OpenXrDebugRenderer,
|
debug_gizmos::OpenXrDebugRenderer,
|
||||||
|
interactions::{
|
||||||
|
interactions, update_interactable_states, XRDirectInteractor, XRInteractorState,
|
||||||
|
XRRayInteractor, draw_interaction_gizmos, draw_socket_gizmos, socket_interactions, InteractionEvent,
|
||||||
|
},
|
||||||
|
oculus_touch::OculusController,
|
||||||
|
prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig},
|
||||||
trackers::{OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker},
|
trackers::{OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker},
|
||||||
|
Hand,
|
||||||
},
|
},
|
||||||
DefaultXrPlugins,
|
DefaultXrPlugins,
|
||||||
};
|
};
|
||||||
@@ -34,8 +48,26 @@ fn main() {
|
|||||||
//lets setup the starting scene
|
//lets setup the starting scene
|
||||||
.add_systems(Startup, setup_scene)
|
.add_systems(Startup, setup_scene)
|
||||||
.add_systems(Startup, spawn_controllers_example) //you need to spawn controllers or it crashes TODO:: Fix this
|
.add_systems(Startup, spawn_controllers_example) //you need to spawn controllers or it crashes TODO:: Fix this
|
||||||
//spawn rapier test physics
|
//add locomotion
|
||||||
.add_systems(Startup, setup_physics)
|
.add_systems(Update, proto_locomotion)
|
||||||
|
.insert_resource(PrototypeLocomotionConfig::default())
|
||||||
|
//lets add the interaction systems
|
||||||
|
.add_event::<InteractionEvent>()
|
||||||
|
.add_systems(Update, prototype_interaction_input)
|
||||||
|
.add_systems(Update, interactions.before(update_interactable_states))
|
||||||
|
.add_systems(Update, update_interactable_states)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
socket_interactions.before(update_interactable_states),
|
||||||
|
)
|
||||||
|
//add the grabbable system
|
||||||
|
.add_systems(Update, update_grabbables.after(update_interactable_states))
|
||||||
|
//draw the interaction gizmos
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
draw_interaction_gizmos.after(update_interactable_states),
|
||||||
|
)
|
||||||
|
.add_systems(Update, draw_socket_gizmos.after(update_interactable_states))
|
||||||
;
|
;
|
||||||
|
|
||||||
//configure rapier sets
|
//configure rapier sets
|
||||||
@@ -75,9 +107,8 @@ fn spawn_controllers_example(mut commands: Commands) {
|
|||||||
OpenXRController,
|
OpenXRController,
|
||||||
OpenXRTracker,
|
OpenXRTracker,
|
||||||
SpatialBundle::default(),
|
SpatialBundle::default(),
|
||||||
// XRRayInteractor,
|
XRDirectInteractor,
|
||||||
// AimPose(Transform::default()),
|
XRInteractorState::default(),
|
||||||
// XRInteractorState::default(),
|
|
||||||
));
|
));
|
||||||
//right hand
|
//right hand
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -85,59 +116,103 @@ fn spawn_controllers_example(mut commands: Commands) {
|
|||||||
OpenXRController,
|
OpenXRController,
|
||||||
OpenXRTracker,
|
OpenXRTracker,
|
||||||
SpatialBundle::default(),
|
SpatialBundle::default(),
|
||||||
// XRDirectInteractor,
|
XRDirectInteractor,
|
||||||
// XRInteractorState::default(),
|
XRInteractorState::default(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup_physics(mut commands: Commands) {
|
//TODO: find a real place for this
|
||||||
/*
|
fn prototype_interaction_input(
|
||||||
* Ground
|
oculus_controller: Res<OculusController>,
|
||||||
*/
|
frame_state: Res<XrFrameState>,
|
||||||
let ground_size = 200.1;
|
xr_input: Res<XrInput>,
|
||||||
let ground_height = 0.1;
|
instance: Res<XrInstance>,
|
||||||
|
session: Res<XrSession>,
|
||||||
commands.spawn((
|
mut right_interactor_query: Query<
|
||||||
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
|
(&mut XRInteractorState),
|
||||||
Collider::cuboid(ground_size, ground_height, ground_size),
|
(
|
||||||
));
|
With<XRDirectInteractor>,
|
||||||
|
With<OpenXRRightController>,
|
||||||
/*
|
Without<OpenXRLeftController>,
|
||||||
* Create the cubes
|
),
|
||||||
*/
|
>,
|
||||||
let num = 8;
|
mut left_interactor_query: Query<
|
||||||
let rad = 1.0;
|
(&mut XRInteractorState),
|
||||||
|
(
|
||||||
let shift = rad * 2.0 + rad;
|
With<XRDirectInteractor>,
|
||||||
let centerx = shift * (num / 2) as f32;
|
With<OpenXRLeftController>,
|
||||||
let centery = shift / 2.0;
|
Without<OpenXRRightController>,
|
||||||
let centerz = shift * (num / 2) as f32;
|
),
|
||||||
|
>,
|
||||||
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
|
) {
|
||||||
let mut color = 0;
|
//lock frame
|
||||||
let colors = [
|
let frame_state = *frame_state.lock().unwrap();
|
||||||
Color::hsl(220.0, 1.0, 0.3),
|
//get controller
|
||||||
Color::hsl(180.0, 1.0, 0.3),
|
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
|
||||||
Color::hsl(260.0, 1.0, 0.7),
|
//get controller triggers
|
||||||
];
|
let left_trigger = controller.trigger(Hand::Left);
|
||||||
|
let right_trigger = controller.trigger(Hand::Right);
|
||||||
for j in 0usize..20 {
|
//get the interactors and do state stuff
|
||||||
for i in 0..num {
|
let mut left_state = left_interactor_query.single_mut();
|
||||||
for k in 0usize..num {
|
if left_trigger > 0.8 {
|
||||||
let x = i as f32 * shift - centerx + offset;
|
*left_state = XRInteractorState::Selecting;
|
||||||
let y = j as f32 * shift + centery + 3.0;
|
} else {
|
||||||
let z = k as f32 * shift - centerz + offset;
|
*left_state = XRInteractorState::Idle;
|
||||||
color += 1;
|
}
|
||||||
|
let mut right_state = right_interactor_query.single_mut();
|
||||||
commands.spawn((
|
if right_trigger > 0.8 {
|
||||||
TransformBundle::from(Transform::from_xyz(x, y, z)),
|
*right_state = XRInteractorState::Selecting;
|
||||||
RigidBody::Dynamic,
|
} else {
|
||||||
Collider::cuboid(rad, rad, rad),
|
*right_state = XRInteractorState::Idle;
|
||||||
ColliderDebugColor(colors[color % 3]),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
offset -= 0.05 * rad * (num as f32 - 1.0);
|
#[derive(Component)]
|
||||||
|
pub struct Grabbable;
|
||||||
|
|
||||||
|
pub fn update_grabbables(
|
||||||
|
mut events: EventReader<InteractionEvent>,
|
||||||
|
mut grabbable_query: Query<(&mut Transform, With<Grabbable>, Without<XRDirectInteractor>, Option<&mut RigidBody>)>,
|
||||||
|
interactor_query: Query<(&GlobalTransform, &XRInteractorState, Without<Grabbable>)>,
|
||||||
|
) {
|
||||||
|
//so basically the idea is to try all the events?
|
||||||
|
for event in events.read() {
|
||||||
|
// info!("some event");
|
||||||
|
match grabbable_query.get_mut(event.interactable) {
|
||||||
|
Ok(mut grabbable_transform) => {
|
||||||
|
// info!("we got a grabbable");
|
||||||
|
//now we need the location of our interactor
|
||||||
|
match interactor_query.get(event.interactor) {
|
||||||
|
Ok(interactor_transform) => {
|
||||||
|
match interactor_transform.1 {
|
||||||
|
XRInteractorState::Idle => {
|
||||||
|
match grabbable_transform.3 {
|
||||||
|
Some(mut thing) => {
|
||||||
|
*thing = RigidBody::Dynamic;
|
||||||
|
},
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
XRInteractorState::Selecting => {
|
||||||
|
// info!("its a direct interactor?");
|
||||||
|
match grabbable_transform.3 {
|
||||||
|
Some(mut thing) => {
|
||||||
|
*thing = RigidBody::KinematicPositionBased;
|
||||||
|
},
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
*grabbable_transform.0 = interactor_transform.0.compute_transform();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// info!("not a direct interactor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// info!("not a grabbable?")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
/// set up a simple 3D scene
|
||||||
pub fn setup_scene(
|
pub fn setup_scene(
|
||||||
@@ -6,19 +20,37 @@ pub fn setup_scene(
|
|||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
) {
|
) {
|
||||||
|
/*
|
||||||
|
* Ground
|
||||||
|
*/
|
||||||
|
let ground_size = 2.5;
|
||||||
|
let ground_height = 0.1;
|
||||||
// plane
|
// plane
|
||||||
commands.spawn(PbrBundle {
|
commands.spawn((
|
||||||
|
PbrBundle {
|
||||||
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
|
mesh: meshes.add(shape::Plane::from_size(5.0).into()),
|
||||||
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
||||||
|
transform: Transform::from_xyz(0.0, -ground_height, 0.0),
|
||||||
..default()
|
..default()
|
||||||
});
|
},
|
||||||
|
Collider::cuboid(ground_size, ground_height, ground_size),
|
||||||
|
));
|
||||||
// cube
|
// cube
|
||||||
commands.spawn(PbrBundle {
|
commands.spawn((
|
||||||
|
PbrBundle {
|
||||||
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
|
||||||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
||||||
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
||||||
..default()
|
..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
|
// light
|
||||||
commands.spawn(PointLightBundle {
|
commands.spawn(PointLightBundle {
|
||||||
@@ -43,3 +75,44 @@ pub fn setup_scene(
|
|||||||
..default()
|
..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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user