Skip to content

Commit

Permalink
Merge pull request #48 from Jondolf/better-examples
Browse files Browse the repository at this point in the history
Improve examples
  • Loading branch information
Jondolf authored Jun 18, 2023
2 parents d58a58a + 5a8dd74 commit 0799738
Show file tree
Hide file tree
Showing 24 changed files with 640 additions and 1,369 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,19 @@ fn setup(

https://user-images.githubusercontent.com/57632562/230185604-b40441a2-48d8-4566-9b9e-be4825f4877e.mp4

To see more complete examples of the various features of Bevy XPBD, check out the 2D and 3D examples in [/crates/bevy_xpbd_2d/examples](/crates/bevy_xpbd_2d/examples) and [/crates/bevy_xpbd_3d/examples](/crates/bevy_xpbd_3d/examples) respectively.
## More examples

You can find lots of 2D and 3D examples in [/crates/bevy_xpbd_2d/examples](/crates/bevy_xpbd_2d/examples) and [/crates/bevy_xpbd_3d/examples](/crates/bevy_xpbd_3d/examples) respectively.

The examples support both `f32` and `f64` precisions, so the code contains some feature-dependent types like `Scalar` and `Vector`.
In actual usage these are not needed, so you can just use `f32` or `f64` types depending on the features you have chosen.

By default the examples use `f64`. To run the `f32` versions, you need to disable default features and manually choose the dimension
and precision:

```
cargo run --example cubes --no-default-features --features 3d,f32
```

## Current features

Expand Down
20 changes: 11 additions & 9 deletions crates/bevy_xpbd_2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[features]
default = [ "2d", "f32", "simd" ]
default = [ "2d", "f64", "simd" ]
2d = []
f32 = [ "dep:parry2d" ]
f64 = [ "dep:parry2d-f64" ]
Expand All @@ -24,8 +24,6 @@ bevy_prototype_debug_lines = { version = "0.10.1", optional = true }
parry2d = { version = "0.13.1", optional = true }
parry2d-f64 = { version = "0.13.1", optional = true }
nalgebra = { version = "0.32.2", features = [ "convert-glam023" ] }
console_error_panic_hook = "0.1.7"
web-sys = "0.3.59"
derive_more = "0.99"

[dev-dependencies]
Expand All @@ -36,21 +34,25 @@ insta = "1.0"
itertools = "0.10"

[[example]]
name = "chain2d"
required-features = ["2d", "f32"]
name = "chain_2d"
required-features = ["2d"]

[[example]]
name = "collision_layers"
required-features = ["2d"]

[[example]]
name = "fixed_joint_2d"
required-features = ["2d", "f32"]
required-features = ["2d"]

[[example]]
name = "move_marbles"
required-features = ["2d", "f32"]
required-features = ["2d"]

[[example]]
name = "prismatic_joint_2d"
required-features = ["2d", "f32"]
required-features = ["2d"]

[[example]]
name = "revolute_joint_2d"
required-features = ["2d", "f32"]
required-features = ["2d"]
147 changes: 0 additions & 147 deletions crates/bevy_xpbd_2d/examples/chain2d.rs

This file was deleted.

87 changes: 87 additions & 0 deletions crates/bevy_xpbd_2d/examples/chain_2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#![allow(clippy::unnecessary_cast)]

use bevy::{prelude::*, sprite::MaterialMesh2dBundle, window::PrimaryWindow};
use bevy_xpbd_2d::prelude::*;
use examples_common_2d::XpbdExamplePlugin;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(XpbdExamplePlugin)
.insert_resource(ClearColor(Color::rgb(0.05, 0.05, 0.1)))
.insert_resource(SubstepCount(50))
.insert_resource(Gravity(Vector::NEG_Y * 1000.0))
.add_startup_system(setup)
.add_system(follow_mouse)
.run();
}

#[derive(Component)]
struct FollowMouse;

fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn(Camera2dBundle::default());

let particle_count = 100;
let particle_radius = 1.2;
let particle_mesh = MaterialMesh2dBundle {
mesh: meshes
.add(shape::Circle::new(particle_radius as f32).into())
.into(),
material: materials.add(ColorMaterial::from(Color::rgb(0.2, 0.7, 0.9))),
..default()
};

// Spawn kinematic particle that can follow the mouse
let mut previous_particle = commands
.spawn((particle_mesh.clone(), RigidBody::Kinematic, FollowMouse))
.id();

// Spawn the rest of the particles, connecting each one to the previous one with joints
for i in 1..particle_count {
let current_particle = commands
.spawn((
particle_mesh.clone(),
RigidBody::Dynamic,
Position(i as Scalar * Vector::NEG_Y * (particle_radius * 2.0 + 1.0)),
MassPropertiesBundle::new_computed(&Collider::ball(particle_radius), 1.0),
))
.id();

commands.spawn(
RevoluteJoint::new(previous_particle, current_particle)
.with_local_anchor_2(Vector::Y * (particle_radius * 2.0 + 1.0))
.with_compliance(0.0000001),
);

previous_particle = current_particle;
}
}

fn follow_mouse(
buttons: Res<Input<MouseButton>>,
windows: Query<&Window, With<PrimaryWindow>>,
camera: Query<(&Camera, &GlobalTransform)>,
mut follower: Query<&mut Position, With<FollowMouse>>,
) {
if buttons.pressed(MouseButton::Left) {
let window = windows.single();
let (camera, camera_transform) = camera.single();
let mut follower_position = follower.single_mut();

// Set position of follower to cursor position in world coordinates
// https://bevy-cheatbook.github.io/cookbook/cursor2world.html
if let Some(pos) = window.cursor_position() {
let window_size = Vec2::new(window.width(), window.height());
let ndc = (pos / window_size) * 2.0 - Vec2::ONE;
let ndc_to_world =
camera_transform.compute_matrix() * camera.projection_matrix().inverse();
let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0));
follower_position.0 = world_pos.truncate().adjust_precision();
}
}
}
Loading

0 comments on commit 0799738

Please sign in to comment.