-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
distance_joint_3d.rs
67 lines (61 loc) · 1.84 KB
/
distance_joint_3d.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
use avian3d::{math::*, prelude::*};
use bevy::prelude::*;
use examples_common_3d::ExampleCommonPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
ExampleCommonPlugin,
PhysicsPlugins::default(),
PhysicsDebugPlugin::default(),
))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_mesh = meshes.add(Cuboid::default());
let cube_material = materials.add(Color::srgb(0.8, 0.7, 0.6));
// Spawn a static cube and a dynamic cube that is connected to it by a distance joint.
let static_cube = commands
.spawn((
Mesh3d(cube_mesh.clone()),
MeshMaterial3d(cube_material.clone()),
RigidBody::Static,
Collider::cuboid(1., 1., 1.),
))
.id();
let dynamic_cube = commands
.spawn((
Mesh3d(cube_mesh),
MeshMaterial3d(cube_material),
Transform::from_xyz(-2.0, -0.5, 0.0),
RigidBody::Dynamic,
Collider::cuboid(1., 1., 1.),
))
.id();
// Add a distance joint to keep the cubes at a certain distance from each other.
commands.spawn(
DistanceJoint::new(static_cube, dynamic_cube)
.with_local_anchor_2(0.5 * Vector::ONE)
.with_rest_length(1.5)
.with_compliance(1.0 / 400.0),
);
// Light
commands.spawn((
PointLight {
intensity: 2_000_000.0,
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 0.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}