pub fn create_revolute_joint(
world: &mut World,
def: &RevoluteJointDef,
) -> JointIdExpand description
(b2CreateRevoluteJoint)
Examples found in repository?
examples/benchmark/human/mod.rs (line 116)
83pub(crate) fn attach_revolute_joint(
84 world: &mut World,
85 parent_body: BodyId,
86 child_body: BodyId,
87 pivot: Pos,
88 lower_angle: f32,
89 upper_angle: f32,
90 enable_limit: bool,
91 enable_motor: bool,
92 max_motor_torque: f32,
93 enable_spring: bool,
94 hertz: f32,
95 damping_ratio: f32,
96 draw_scale: f32,
97 frame_a_rotation: Option<Rot>,
98) -> JointId {
99 let mut joint_def = default_revolute_joint_def();
100 joint_def.base.body_id_a = parent_body;
101 joint_def.base.body_id_b = child_body;
102 joint_def.base.local_frame_a.p = body_get_local_point(world, parent_body, pivot);
103 if let Some(q) = frame_a_rotation {
104 joint_def.base.local_frame_a.q = q;
105 }
106 joint_def.base.local_frame_b.p = body_get_local_point(world, child_body, pivot);
107 joint_def.enable_limit = enable_limit;
108 joint_def.lower_angle = lower_angle;
109 joint_def.upper_angle = upper_angle;
110 joint_def.enable_motor = enable_motor;
111 joint_def.max_motor_torque = max_motor_torque;
112 joint_def.enable_spring = enable_spring;
113 joint_def.hertz = hertz;
114 joint_def.damping_ratio = damping_ratio;
115 joint_def.base.draw_scale = draw_scale;
116 create_revolute_joint(world, &joint_def)
117}More examples
examples/benchmark/scenes/pyramids.rs (line 65)
20pub fn create_joint_grid(world: &mut World) {
21 world_enable_sleeping(world, false);
22
23 let n: i32 = if BENCHMARK_DEBUG { 20 } else { 100 };
24
25 let mut bodies: Vec<BodyId> = Vec::with_capacity((n * n) as usize);
26
27 let mut shape_def = default_shape_def();
28 shape_def.density = 1.0;
29 shape_def.filter.category_bits = 2;
30 shape_def.filter.mask_bits = (!2u32) as u64;
31
32 let circle = Circle {
33 center: Vec2 { x: 0.0, y: 0.0 },
34 radius: 0.4,
35 };
36
37 let mut joint_def = default_revolute_joint_def();
38 joint_def.base.draw_scale = 0.4;
39
40 let mut body_def = default_body_def();
41
42 for k in 0..n {
43 for i in 0..n {
44 let fk = k as f32;
45 let fi = i as f32;
46
47 if k >= n / 2 - 3 && k <= n / 2 + 3 && i == 0 {
48 body_def.type_ = BodyType::Static;
49 } else {
50 body_def.type_ = BodyType::Dynamic;
51 }
52
53 body_def.position = to_pos(Vec2 { x: fk, y: -fi });
54
55 let body = create_body(world, &body_def);
56 create_circle_shape(world, body, &shape_def, &circle);
57
58 let index = bodies.len();
59
60 if i > 0 {
61 joint_def.base.body_id_a = bodies[index - 1];
62 joint_def.base.body_id_b = body;
63 joint_def.base.local_frame_a.p = Vec2 { x: 0.0, y: -0.5 };
64 joint_def.base.local_frame_b.p = Vec2 { x: 0.0, y: 0.5 };
65 create_revolute_joint(world, &joint_def);
66 }
67
68 if k > 0 {
69 joint_def.base.body_id_a = bodies[index - n as usize];
70 joint_def.base.body_id_b = body;
71 joint_def.base.local_frame_a.p = Vec2 { x: 0.5, y: 0.0 };
72 joint_def.base.local_frame_b.p = Vec2 { x: -0.5, y: 0.0 };
73 create_revolute_joint(world, &joint_def);
74 }
75
76 bodies.push(body);
77 }
78 }
79}examples/benchmark/scenes/machines.rs (line 97)
39pub fn create_spinner(world: &mut World) {
40 let ground_id;
41 {
42 let body_def = default_body_def();
43 ground_id = create_body(world, &body_def);
44
45 let q = make_rot(-2.0 * PI / SPINNER_POINT_COUNT as f32);
46 let mut p = Vec2 { x: 40.0, y: 0.0 };
47 let mut points: Vec<Vec2> = Vec::with_capacity(SPINNER_POINT_COUNT);
48 for _ in 0..SPINNER_POINT_COUNT {
49 points.push(Vec2 {
50 x: p.x,
51 y: p.y + 32.0,
52 });
53 p = rotate_vector(q, p);
54 }
55
56 // C: b2SurfaceMaterial material = { 0 }; material.friction = 0.1f;
57 let material = SurfaceMaterial {
58 friction: 0.1,
59 restitution: 0.0,
60 rolling_resistance: 0.0,
61 tangent_speed: 0.0,
62 user_material_id: 0,
63 custom_color: 0,
64 };
65
66 let mut chain_def = default_chain_def();
67 chain_def.points = points;
68 chain_def.is_loop = true;
69 chain_def.materials = vec![material];
70
71 create_chain(world, ground_id, &chain_def);
72 }
73
74 {
75 let mut body_def = default_body_def();
76 body_def.type_ = BodyType::Dynamic;
77 body_def.position = to_pos(Vec2 { x: 0.0, y: 12.0 });
78 body_def.enable_sleep = false;
79
80 let spinner_id = create_body(world, &body_def);
81
82 let box_shape = make_rounded_box(0.4, 20.0, 0.2);
83 let mut shape_def = default_shape_def();
84 shape_def.material.friction = 0.0;
85 create_polygon_shape(world, spinner_id, &shape_def, &box_shape);
86
87 let motor_speed = 5.0;
88 let max_motor_torque = f32::MAX;
89 let mut joint_def = default_revolute_joint_def();
90 joint_def.base.body_id_a = ground_id;
91 joint_def.base.body_id_b = spinner_id;
92 joint_def.base.local_frame_a.p = body_get_local_point(world, ground_id, body_def.position);
93 joint_def.enable_motor = true;
94 joint_def.motor_speed = motor_speed;
95 joint_def.max_motor_torque = max_motor_torque;
96
97 let joint_id = create_revolute_joint(world, &joint_def);
98 SPINNER_ID.with(|id| id.set(joint_id));
99 }
100
101 let capsule = Capsule {
102 center1: Vec2 { x: -0.25, y: 0.0 },
103 center2: Vec2 { x: 0.25, y: 0.0 },
104 radius: 0.25,
105 };
106 let circle = Circle {
107 center: Vec2 { x: 0.0, y: 0.0 },
108 radius: 0.35,
109 };
110 let square = make_square(0.35);
111
112 let mut body_def = default_body_def();
113 body_def.type_ = BodyType::Dynamic;
114 let mut shape_def = default_shape_def();
115 shape_def.material.friction = 0.1;
116 shape_def.material.restitution = 0.1;
117 shape_def.density = 0.25;
118
119 let body_count: i32 = if BENCHMARK_DEBUG { 499 } else { 2 * 3038 };
120
121 let mut x = -23.0;
122 let mut y = 2.0;
123 for i in 0..body_count {
124 body_def.position = to_pos(Vec2 { x, y });
125 let body_id = create_body(world, &body_def);
126
127 let remainder = i % 3;
128 if remainder == 0 {
129 create_capsule_shape(world, body_id, &shape_def, &capsule);
130 } else if remainder == 1 {
131 create_circle_shape(world, body_id, &shape_def, &circle);
132 } else if remainder == 2 {
133 create_polygon_shape(world, body_id, &shape_def, &square);
134 }
135
136 x += 0.5;
137
138 if x >= 23.0 {
139 x = -23.0;
140 y += 0.5;
141 }
142 }
143}
144
145// (StepSpinner)
146pub fn step_spinner(world: &mut World, _step_count: i32) -> f32 {
147 let joint_id = SPINNER_ID.with(|id| id.get());
148 revolute_joint_get_angle(world, joint_id)
149}
150
151// (CreateSmash)
152pub fn create_smash(world: &mut World) {
153 world_set_gravity(world, VEC2_ZERO);
154
155 {
156 let box_shape = make_box(4.0, 4.0);
157
158 let mut body_def = default_body_def();
159 body_def.type_ = BodyType::Dynamic;
160 body_def.position = to_pos(Vec2 { x: -20.0, y: 0.0 });
161 body_def.linear_velocity = Vec2 { x: 40.0, y: 0.0 };
162 let body_id = create_body(world, &body_def);
163
164 let mut shape_def = default_shape_def();
165 shape_def.density = 8.0;
166 create_polygon_shape(world, body_id, &shape_def, &box_shape);
167 }
168
169 let d = 0.4;
170 let box_shape = make_square(0.5 * d);
171
172 let mut body_def = default_body_def();
173 body_def.type_ = BodyType::Dynamic;
174 body_def.is_awake = false;
175
176 let shape_def = default_shape_def();
177
178 let columns: i32 = if BENCHMARK_DEBUG { 20 } else { 120 };
179 let rows: i32 = if BENCHMARK_DEBUG { 10 } else { 80 };
180
181 for i in 0..columns {
182 for j in 0..rows {
183 body_def.position = to_pos(Vec2 {
184 x: i as f32 * d + 30.0,
185 y: (j as f32 - rows as f32 / 2.0) * d,
186 });
187 let body_id = create_body(world, &body_def);
188 create_polygon_shape(world, body_id, &shape_def, &box_shape);
189 }
190 }
191}
192
193// (CreateTumbler)
194pub fn create_tumbler(world: &mut World) {
195 let ground_id;
196 {
197 let body_def = default_body_def();
198 ground_id = create_body(world, &body_def);
199 }
200
201 {
202 let mut body_def = default_body_def();
203 body_def.type_ = BodyType::Dynamic;
204 body_def.position = to_pos(Vec2 { x: 0.0, y: 10.0 });
205 let body_id = create_body(world, &body_def);
206
207 let mut shape_def = default_shape_def();
208 shape_def.density = 50.0;
209
210 let polygon = make_offset_box(0.5, 10.0, Vec2 { x: 10.0, y: 0.0 }, ROT_IDENTITY);
211 create_polygon_shape(world, body_id, &shape_def, &polygon);
212 let polygon = make_offset_box(0.5, 10.0, Vec2 { x: -10.0, y: 0.0 }, ROT_IDENTITY);
213 create_polygon_shape(world, body_id, &shape_def, &polygon);
214 let polygon = make_offset_box(10.0, 0.5, Vec2 { x: 0.0, y: 10.0 }, ROT_IDENTITY);
215 create_polygon_shape(world, body_id, &shape_def, &polygon);
216 let polygon = make_offset_box(10.0, 0.5, Vec2 { x: 0.0, y: -10.0 }, ROT_IDENTITY);
217 create_polygon_shape(world, body_id, &shape_def, &polygon);
218
219 let motor_speed = 25.0;
220
221 let mut joint_def = default_revolute_joint_def();
222 joint_def.base.body_id_a = ground_id;
223 joint_def.base.body_id_b = body_id;
224 joint_def.base.local_frame_a.p = Vec2 { x: 0.0, y: 10.0 };
225 joint_def.base.local_frame_b.p = Vec2 { x: 0.0, y: 0.0 };
226 joint_def.motor_speed = (PI / 180.0) * motor_speed;
227 joint_def.max_motor_torque = 1e8;
228 joint_def.enable_motor = true;
229
230 create_revolute_joint(world, &joint_def);
231 }
232
233 let grid_count: i32 = if BENCHMARK_DEBUG { 20 } else { 45 };
234
235 let polygon = make_box(0.125, 0.125);
236 let mut body_def = default_body_def();
237 body_def.type_ = BodyType::Dynamic;
238 let shape_def = default_shape_def();
239
240 let mut y = -0.2 * grid_count as f32 + 10.0;
241 for _ in 0..grid_count {
242 let mut x = -0.2 * grid_count as f32;
243
244 for _ in 0..grid_count {
245 body_def.position = to_pos(Vec2 { x, y });
246 let body_id = create_body(world, &body_def);
247
248 create_polygon_shape(world, body_id, &shape_def, &polygon);
249
250 x += 0.4;
251 }
252
253 y += 0.4;
254 }
255}