forked from avianphysics/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminism_2d.rs
More file actions
246 lines (220 loc) · 6.96 KB
/
determinism_2d.rs
File metadata and controls
246 lines (220 loc) · 6.96 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//! An example providing a visual demonstration of the 2D cross-platform determinism test
//! in `src/tests/determinism_2d`.
//!
//! This scene is designed to produce a chaotic result engaging:
//!
//! - the contact solver
//! - speculative collision
//! - joints and joint limits
//!
//! Once the simulation has run for a while, a transform hash is computed.
//! The determinism test compares this to the expected value for every PR on multiple platforms using GitHub Actions.
//! Every time simulation behavior changes, the expected hash must be updated.
//!
//! This test is based on the `FallingHinges` test in the Box2D physics engine:
//! <https://github.com/erincatto/box2d/blob/90c2781f64775085035655661d5fe6542bf0fbd5/samples/sample_determinism.cpp>
use avian2d::{
math::{AdjustPrecision, PI, Scalar, Vector},
prelude::*,
};
use bevy::{
camera::ScalingMode, color::palettes::tailwind::CYAN_400,
input::common_conditions::input_just_pressed, prelude::*,
};
use bytemuck::{Pod, Zeroable};
// How many steps to record the hash for.
const STEP_COUNT: usize = 500;
const ROWS: u32 = 30;
const COLUMNS: u32 = 4;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
PhysicsPlugins::default().with_length_unit(0.5),
PhysicsDebugPlugin,
))
.init_resource::<Step>()
.add_systems(Startup, (setup_scene, setup_ui))
.add_systems(FixedUpdate, update_hash)
.add_systems(
PreUpdate,
// Reset the scene when the R key is pressed.
(clear_scene, setup_scene)
.chain()
.run_if(input_just_pressed(KeyCode::KeyR)),
)
.run();
}
#[derive(Resource, Default, Deref, DerefMut)]
struct Step(usize);
fn setup_scene(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn((
Camera2d,
Projection::from(OrthographicProjection {
scaling_mode: ScalingMode::FixedHorizontal {
viewport_width: 40.0,
},
..OrthographicProjection::default_2d()
}),
Transform::from_xyz(0.0, 7.5, 0.0),
));
let ground_shape = Rectangle::new(40.0, 2.0);
commands.spawn((
Name::new("Ground"),
RigidBody::Static,
Collider::from(ground_shape),
Mesh2d(meshes.add(ground_shape)),
MeshMaterial2d(materials.add(Color::WHITE)),
Transform::from_xyz(0.0, -1.0, 0.0),
));
let half_size = 0.25;
let square_shape = Rectangle::new(2.0 * half_size, 2.0 * half_size);
let square_collider = Collider::from(square_shape);
let square_mesh = meshes.add(square_shape);
let square_material = materials.add(Color::from(CYAN_400));
let offset = 0.4 * half_size;
let delta_x = 10.0 * half_size;
let x_root = -0.5 * delta_x * (COLUMNS as f32 - 1.0);
for col in 0..COLUMNS {
let x = x_root + col as f32 * delta_x;
let mut prev_entity = None;
for row in 0..ROWS {
let entity = commands
.spawn((
Name::new("Square ({col}, {row})"),
RigidBody::Dynamic,
square_collider.clone(),
Mesh2d(square_mesh.clone()),
MeshMaterial2d(square_material.clone()),
Transform::from_xyz(
x + offset * row as f32,
half_size + 2.0 * half_size * row as f32,
0.0,
)
.with_rotation(Quat::from_rotation_z(0.1 * row as f32 - 1.0)),
))
.id();
if row & 1 == 0 {
prev_entity = Some(entity);
} else {
commands.spawn((
Name::new(format!(
"Revolute Joint ({}, {})",
prev_entity.unwrap(),
entity
)),
RevoluteJoint::new(prev_entity.unwrap(), entity)
.with_angle_limits(-0.1 * PI, 0.2 * PI)
.with_point_compliance(0.0001)
.with_local_anchor1(Vec2::splat(half_size).adjust_precision())
.with_local_anchor2(Vec2::new(offset, -half_size).adjust_precision()),
JointCollisionDisabled,
));
prev_entity = None;
}
}
}
}
#[derive(Component)]
struct StepText;
#[derive(Component)]
struct HashText;
fn setup_ui(mut commands: Commands) {
let font = TextFont {
font_size: 20.0,
..default()
};
commands
.spawn((
Text::new("Step: "),
font.clone(),
Node {
position_type: PositionType::Absolute,
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
))
.with_child((TextSpan::new("0"), font.clone(), StepText));
commands
.spawn((
Text::new("Hash: "),
font.clone(),
Node {
position_type: PositionType::Absolute,
top: Val::Px(30.0),
left: Val::Px(5.0),
..default()
},
))
.with_child((TextSpan::default(), font.clone(), HashText));
commands.spawn((
Text::new("Press R to reset scene"),
font.clone(),
Node {
position_type: PositionType::Absolute,
top: Val::Px(5.0),
right: Val::Px(5.0),
..default()
},
));
}
fn clear_scene(
mut commands: Commands,
query: Query<
Entity,
Or<(
With<RigidBody>,
With<Collider>,
With<RevoluteJoint>,
With<Camera>,
)>,
>,
mut step: ResMut<Step>,
) {
step.0 = 0;
for entity in &query {
commands.entity(entity).despawn();
}
}
#[derive(Pod, Zeroable, Clone, Copy)]
#[repr(C)]
struct Isometry {
translation: Vector,
rotation: Scalar,
}
fn update_hash(
transforms: Query<(&Position, &Rotation), With<RigidBody>>,
mut step_text: Single<&mut TextSpan, With<StepText>>,
mut hash_text: Single<&mut TextSpan, (With<HashText>, Without<StepText>)>,
mut step: ResMut<Step>,
) {
step_text.0 = step.to_string();
step.0 += 1;
if step.0 > STEP_COUNT {
return;
}
let mut hash = 5381;
for (position, rotation) in &transforms {
let isometry = Isometry {
translation: position.0,
rotation: rotation.as_radians(),
};
hash = djb2_hash(hash, bytemuck::bytes_of(&isometry));
}
if step.0 == STEP_COUNT {
hash_text.0 = format!("0x{:x} (step {})", hash, step.0);
} else {
hash_text.0 = format!("0x{:x}", hash);
}
}
fn djb2_hash(mut hash: u32, data: &[u8]) -> u32 {
for &byte in data {
hash = (hash << 5).wrapping_add(hash + byte as u32);
}
hash
}