forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prismatic_joint_3d.rs
74 lines (67 loc) · 2.16 KB
/
prismatic_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
68
69
70
71
72
73
74
use bevy::prelude::*;
use bevy_xpbd_3d::{math::*, prelude::*};
use examples_common_3d::XpbdExamplePlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, XpbdExamplePlugin))
.insert_resource(ClearColor(Color::rgb(0.05, 0.05, 0.1)))
.insert_resource(Msaa::Sample4)
.insert_resource(SubstepCount(50))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
let cube_mesh = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_material = materials.add(Color::rgb(0.8, 0.7, 0.6).into());
// Kinematic rotating "anchor" object
let anchor = commands
.spawn((
PbrBundle {
mesh: cube_mesh.clone(),
material: cube_material.clone(),
..default()
},
RigidBody::Kinematic,
AngularVelocity(Vector::Z * 1.5),
))
.id();
// Dynamic object rotating around anchor
let object = commands
.spawn((
PbrBundle {
mesh: cube_mesh,
material: cube_material,
transform: Transform::from_xyz(1.5, 0.0, 0.0),
..default()
},
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::cuboid(1.0, 1.0, 1.0), 1.0),
))
.id();
// Connect anchor and dynamic object
commands.spawn(
PrismaticJoint::new(anchor, object)
.with_local_anchor_1(Vector::X)
.with_free_axis(Vector::X)
.with_limits(0.5, 2.0),
);
// Directional light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 20_000.0,
shadows_enabled: true,
..default()
},
transform: Transform::default().looking_at(Vec3::new(-1.0, -2.5, -1.5), Vec3::Y),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::Z * 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}