forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_colliders.rs
69 lines (63 loc) · 2.29 KB
/
async_colliders.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! An example showcasing how to create colliders for meshes and scenes
//! using `AsyncCollider` and `AsyncSceneCollider` respectively.
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
use examples_common_3d::XpbdExamplePlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, XpbdExamplePlugin))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
assets: ResMut<AssetServer>,
) {
// Spawn ground and generate a collider for the mesh using AsyncCollider
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(8.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
},
AsyncCollider(ComputedCollider::TriMesh),
RigidBody::Static,
));
// Spawn Ferris the crab and generate colliders for the scene using AsyncSceneCollider
commands.spawn((
SceneBundle {
// The model was made by RayMarch, licenced under CC0-1.0, and can be found here:
// https://github.com/RayMarch/ferris3d
scene: assets.load("ferris.glb#Scene0"),
transform: Transform::from_xyz(0.0, 2.0, 0.0).with_scale(Vec3::splat(2.0)),
..default()
},
// Create colliders using convex decomposition.
// This takes longer than creating a trimesh or convex hull collider,
// but is more performant for collision detection.
AsyncSceneCollider::new(Some(ComputedCollider::ConvexDecomposition(
VHACDParameters::default(),
)))
// Make the arms heavier to make it easier to stand upright
.with_density_for_name("armL_mesh", 5.0)
.with_density_for_name("armR_mesh", 5.0),
RigidBody::Dynamic,
));
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 3000.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(2.0, 8.0, 2.0),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-5.0, 3.5, 5.5).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}