Skip to content

Commit

Permalink
Merge pull request #447 from extrawurst/examples-testbed3
Browse files Browse the repository at this point in the history
testbed3d: converted 3d examples to be usable in testbed
  • Loading branch information
sebcrozet authored Nov 16, 2023
2 parents 8a3c744 + 244e257 commit cec05c3
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 105 deletions.
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/boxes3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
Expand Down
36 changes: 22 additions & 14 deletions bevy_rapier3d/examples/despawn3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

#[derive(Component)]
pub struct Despawn;

#[derive(Resource, Default)]
pub struct DespawnResource {
pub entity: Option<Entity>,
timer: Timer,
}

fn main() {
Expand All @@ -24,28 +27,29 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands, mut res: ResMut<DespawnResource>) {
res.timer = Timer::from_seconds(5.0, TimerMode::Once);

commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..Default::default()
});
}

pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource>) {
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;

let ground_entity = commands
.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
))
.id();
despawn.entity = Some(ground_entity);
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height, ground_size),
Despawn,
));

/*
* Create the cubes
*/
Expand Down Expand Up @@ -86,12 +90,16 @@ pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource
}
}

pub fn despawn(mut commands: Commands, time: Res<Time>, mut despawn: ResMut<DespawnResource>) {
if time.elapsed_seconds() > 5.0 {
if let Some(entity) = despawn.entity {
pub fn despawn(
mut commands: Commands,
time: Res<Time>,
mut despawn: ResMut<DespawnResource>,
query: Query<Entity, With<Despawn>>,
) {
if despawn.timer.tick(time.delta()).finished() {
for entity in &query {
println!("Despawning ground entity");
commands.entity(entity).despawn();
despawn.entity = None;
}
}
}
4 changes: 2 additions & 2 deletions bevy_rapier3d/examples/events3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 25.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}

fn display_events(
pub fn display_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
) {
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/joints3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(15.0, 5.0, 42.0)
.looking_at(Vec3::new(13.0, 1.0, 1.0), Vec3::Y),
Expand Down
88 changes: 40 additions & 48 deletions bevy_rapier3d/examples/joints_despawn3.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

#[derive(Component)]
pub struct Despawn;

#[derive(Resource, Default)]
pub struct DespawnResource {
entities: Vec<Entity>,
timer: Timer,
}

fn main() {
Expand All @@ -24,20 +27,17 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands, mut res: ResMut<DespawnResource>) {
res.timer = Timer::from_seconds(5.0, TimerMode::Once);

commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(15.0, 5.0, 42.0)
.looking_at(Vec3::new(13.0, 1.0, 1.0), Vec3::Y),
..Default::default()
});
}

fn create_prismatic_joints(
commands: &mut Commands,
origin: Vect,
num: usize,
despawn: &mut DespawnResource,
) {
fn create_prismatic_joints(commands: &mut Commands, origin: Vect, num: usize) {
let rad = 0.4;
let shift = 1.0;

Expand All @@ -63,27 +63,22 @@ fn create_prismatic_joints(
.limits([-2.0, 2.0]);
let joint = ImpulseJoint::new(curr_parent, prism);

curr_parent = commands
.spawn((
TransformBundle::from(Transform::from_xyz(origin.x, origin.y, origin.z + dz)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
joint,
))
.id();
let mut entity = commands.spawn((
TransformBundle::from(Transform::from_xyz(origin.x, origin.y, origin.z + dz)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad, rad),
joint,
));

curr_parent = entity.id();

if i == 2 {
despawn.entities.push(curr_parent);
entity.insert(Despawn);
}
}
}

fn create_revolute_joints(
commands: &mut Commands,
origin: Vec3,
num: usize,
despawn: &mut DespawnResource,
) {
fn create_revolute_joints(commands: &mut Commands, origin: Vec3, num: usize) {
let rad = 0.4;
let shift = 2.0;

Expand Down Expand Up @@ -143,20 +138,14 @@ fn create_revolute_joints(
curr_parent = handles[3];

if i == 1 {
despawn.entities.push(handles[0]);
despawn.entities.push(handles[1]);
despawn.entities.push(handles[2]);
despawn.entities.push(handles[3]);
for e in handles {
commands.entity(e).insert(Despawn);
}
}
}
}

fn create_fixed_joints(
commands: &mut Commands,
origin: Vec3,
num: usize,
despawn: &mut ResMut<DespawnResource>,
) {
fn create_fixed_joints(commands: &mut Commands, origin: Vec3, num: usize) {
let rad = 0.4;
let shift = 1.0;

Expand Down Expand Up @@ -209,8 +198,7 @@ fn create_fixed_joints(
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
let entity = children.spawn(ImpulseJoint::new(parent_entity, joint)).id();
despawn.entities.push(entity);
children.spawn((ImpulseJoint::new(parent_entity, joint), Despawn));
});
}

Expand All @@ -219,7 +207,7 @@ fn create_fixed_joints(
}
}

fn create_ball_joints(commands: &mut Commands, num: usize, despawn: &mut DespawnResource) {
fn create_ball_joints(commands: &mut Commands, num: usize) {
let rad = 0.4;
let shift = 1.0;

Expand Down Expand Up @@ -252,9 +240,9 @@ fn create_ball_joints(commands: &mut Commands, num: usize, despawn: &mut Despawn
// NOTE: we want to attach multiple impulse joints to this entity, so
// we need to add the components to children of the entity. Otherwise
// the second joint component would just overwrite the first one.
let entity = children.spawn(ImpulseJoint::new(parent_entity, joint)).id();
let mut entity = children.spawn(ImpulseJoint::new(parent_entity, joint));
if i == 2 {
despawn.entities.push(entity);
entity.insert(Despawn);
}
});
}
Expand All @@ -277,19 +265,23 @@ fn create_ball_joints(commands: &mut Commands, num: usize, despawn: &mut Despawn
}
}

pub fn setup_physics(mut commands: Commands, mut despawn: ResMut<DespawnResource>) {
create_prismatic_joints(&mut commands, Vec3::new(20.0, 10.0, 0.0), 5, &mut despawn);
create_revolute_joints(&mut commands, Vec3::new(20.0, 0.0, 0.0), 3, &mut despawn);
create_fixed_joints(&mut commands, Vec3::new(0.0, 10.0, 0.0), 5, &mut despawn);
create_ball_joints(&mut commands, 15, &mut despawn);
pub fn setup_physics(mut commands: Commands) {
create_prismatic_joints(&mut commands, Vec3::new(20.0, 10.0, 0.0), 5);
create_revolute_joints(&mut commands, Vec3::new(20.0, 0.0, 0.0), 3);
create_fixed_joints(&mut commands, Vec3::new(0.0, 10.0, 0.0), 5);
create_ball_joints(&mut commands, 15);
}

pub fn despawn(mut commands: Commands, time: Res<Time>, mut despawn: ResMut<DespawnResource>) {
if time.elapsed_seconds() > 5.0 {
for entity in &despawn.entities {
pub fn despawn(
mut commands: Commands,
time: Res<Time>,
mut despawn: ResMut<DespawnResource>,
query: Query<Entity, With<Despawn>>,
) {
if despawn.timer.tick(time.delta()).finished() {
for entity in &query {
println!("Despawning joint entity");
commands.entity(*entity).despawn();
commands.entity(entity).despawn();
}
despawn.entities.clear();
}
}
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/locked_rotations3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(10.0, 3.0, 0.0)
.looking_at(Vec3::new(0.0, 3.0, 0.0), Vec3::Y),
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/multiple_colliders3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
Expand Down
4 changes: 2 additions & 2 deletions bevy_rapier3d/examples/ray_casting3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
.run();
}

fn setup_graphics(mut commands: Commands) {
pub fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-30.0, 30.0, 100.0)
.looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn setup_physics(mut commands: Commands) {
}
}

fn cast_ray(
pub fn cast_ray(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
rapier_context: Res<RapierContext>,
Expand Down
Loading

0 comments on commit cec05c3

Please sign in to comment.