-
Notifications
You must be signed in to change notification settings - Fork 41
/
physics.rs
335 lines (282 loc) · 10.2 KB
/
physics.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// This example does a physics demo, because physics is fun :)
use skulpin::skia_safe;
use skulpin::app::AppHandler;
use skulpin::app::AppUpdateArgs;
use skulpin::app::AppDrawArgs;
use skulpin::app::AppError;
use skulpin::app::AppBuilder;
use skulpin::app::VirtualKeyCode;
use skulpin::LogicalSize;
// Used for physics
type Vector2 = rapier2d::na::Vector2<f32>;
use rapier2d::dynamics::{
JointSet, RigidBodySet, IntegrationParameters, RigidBodyBuilder, RigidBodyHandle,
};
use rapier2d::geometry::{BroadPhase, NarrowPhase, ColliderSet, ColliderBuilder};
use rapier2d::pipeline::PhysicsPipeline;
// use ncollide2d::shape::{Cuboid, ShapeHandle, Ball};
// use nphysics2d::object::{
// ColliderDesc, RigidBodyDesc, DefaultBodySet, DefaultColliderSet, Ground, BodyPartHandle,
// DefaultBodyHandle,
// };
// use nphysics2d::force_generator::DefaultForceGeneratorSet;
// use nphysics2d::joint::DefaultJointConstraintSet;
// use nphysics2d::world::{DefaultMechanicalWorld, DefaultGeometricalWorld};
const GROUND_THICKNESS: f32 = 0.2;
const GROUND_HALF_EXTENTS_WIDTH: f32 = 3.0;
const BALL_RADIUS: f32 = 0.2;
const GRAVITY: f32 = -9.81;
const BALL_COUNT: usize = 5;
// Will contain all the physics simulation state
struct Physics {
// geometrical_world: DefaultGeometricalWorld<f32>,
// mechanical_world: DefaultMechanicalWorld<f32>,
//
// bodies: DefaultBodySet<f32>,
// colliders: DefaultColliderSet<f32>,
// joint_constraints: DefaultJointConstraintSet<f32>,
// force_generators: DefaultForceGeneratorSet<f32>,
//
circle_body_handles: Vec<RigidBodyHandle>,
physics_pipeline: PhysicsPipeline,
gravity: Vector2,
integration_parameters: IntegrationParameters,
broad_phase: BroadPhase,
narrow_phase: NarrowPhase,
rigid_body_set: RigidBodySet,
collider_set: ColliderSet,
joint_set: JointSet,
last_update: std::time::Instant,
accumulated_time: f64,
}
impl Physics {
fn new() -> Self {
//
// Basic physics system setup
//
let physics_pipeline = PhysicsPipeline::new();
let gravity = Vector2::new(0.0, GRAVITY);
let integration_parameters = IntegrationParameters::default();
let broad_phase = BroadPhase::new();
let narrow_phase = NarrowPhase::new();
let mut rigid_body_set = RigidBodySet::new();
let mut collider_set = ColliderSet::new();
let joint_set = JointSet::new();
//
// Create the "ground"
//
let ground_body = RigidBodyBuilder::new_static()
.translation(0.0, -GROUND_THICKNESS)
.build();
let ground_body_handle = rigid_body_set.insert(ground_body);
let ground_collider =
ColliderBuilder::cuboid(GROUND_HALF_EXTENTS_WIDTH, GROUND_THICKNESS).build();
collider_set.insert(ground_collider, ground_body_handle, &mut rigid_body_set);
//
// Create falling objects
//
let shift = (BALL_RADIUS + 0.01) * 2.0;
let centerx_base = shift * (BALL_COUNT as f32) / 2.0;
let centery = shift / 2.0;
let height = 3.0;
let mut circle_body_handles = vec![];
for i in 0usize..BALL_COUNT {
for j in 0usize..BALL_COUNT {
// Vary the x so the balls don't stack
let centerx = if j % 2 == 0 {
centerx_base + 0.1
} else {
centerx_base - 0.1
};
let x = i as f32 * shift - centerx;
let y = j as f32 * shift + centery + height;
let rigid_body = RigidBodyBuilder::new_dynamic().translation(x, y).build();
let rigid_body_handle = rigid_body_set.insert(rigid_body);
let ball_collider = ColliderBuilder::ball(BALL_RADIUS).density(1.0).build();
// Insert the collider to the body set.
collider_set.insert(ball_collider, rigid_body_handle, &mut rigid_body_set);
circle_body_handles.push(rigid_body_handle);
}
}
let last_update = std::time::Instant::now();
Physics {
physics_pipeline,
gravity,
integration_parameters,
broad_phase,
narrow_phase,
rigid_body_set,
collider_set,
joint_set,
circle_body_handles,
last_update,
accumulated_time: 0.0,
}
}
fn update(&mut self) {
let now = std::time::Instant::now();
let time_since_last_update = (now - self.last_update).as_secs_f64();
self.accumulated_time += time_since_last_update;
self.last_update = now;
const STEP_TIME: f64 = 1.0 / 60.0;
while self.accumulated_time > STEP_TIME {
self.accumulated_time -= STEP_TIME;
// Run the simulation.
self.physics_pipeline.step(
&self.gravity,
&self.integration_parameters,
&mut self.broad_phase,
&mut self.narrow_phase,
&mut self.rigid_body_set,
&mut self.collider_set,
&mut self.joint_set,
None,
None,
&(),
);
}
}
}
fn main() {
// Setup logging
env_logger::Builder::from_default_env()
.filter_level(log::LevelFilter::Debug)
.init();
let example_app = ExampleApp::new();
AppBuilder::new()
.inner_size(LogicalSize::new(900, 600))
.run(example_app);
}
struct ExampleApp {
last_fps_text_change: Option<std::time::Instant>,
fps_text: String,
physics: Physics,
circle_colors: Vec<skia_safe::Paint>,
}
impl ExampleApp {
pub fn new() -> Self {
fn create_circle_paint(color: skia_safe::Color4f) -> skia_safe::Paint {
let mut paint = skia_safe::Paint::new(color, None);
paint.set_anti_alias(true);
paint.set_style(skia_safe::paint::Style::Stroke);
paint.set_stroke_width(0.02);
paint
}
let circle_colors = vec![
create_circle_paint(skia_safe::Color4f::new(0.2, 1.0, 0.2, 1.0)),
create_circle_paint(skia_safe::Color4f::new(1.0, 1.0, 0.2, 1.0)),
create_circle_paint(skia_safe::Color4f::new(1.0, 0.2, 0.2, 1.0)),
create_circle_paint(skia_safe::Color4f::new(0.2, 0.2, 1.0, 1.0)),
];
ExampleApp {
last_fps_text_change: None,
fps_text: "".to_string(),
physics: Physics::new(),
circle_colors,
}
}
}
impl AppHandler for ExampleApp {
fn update(
&mut self,
update_args: AppUpdateArgs,
) {
let time_state = update_args.time_state;
let input_state = update_args.input_state;
let app_control = update_args.app_control;
let now = time_state.current_instant();
//
// Quit if user hits escape
//
if input_state.is_key_down(VirtualKeyCode::Escape) {
app_control.enqueue_terminate_process();
}
//
// Update FPS once a second
//
let update_text_string = match self.last_fps_text_change {
Some(last_update_instant) => (now - last_update_instant).as_secs_f32() >= 1.0,
None => true,
};
// Refresh FPS text
if update_text_string {
let fps = time_state.updates_per_second();
self.fps_text = format!("Fps: {:.1}", fps);
self.last_fps_text_change = Some(now);
}
// Update physics
self.physics.update();
}
fn draw(
&mut self,
draw_args: AppDrawArgs,
) {
let coordinate_system_helper = draw_args.coordinate_system_helper;
let canvas = draw_args.canvas;
let x_half_extents = GROUND_HALF_EXTENTS_WIDTH * 1.5;
let y_half_extents = x_half_extents
/ (coordinate_system_helper.surface_extents().width as f32
/ coordinate_system_helper.surface_extents().height as f32);
coordinate_system_helper
.use_visible_range(
canvas,
skia_safe::Rect {
left: -x_half_extents,
right: x_half_extents,
top: y_half_extents + 1.0,
bottom: -y_half_extents + 1.0,
},
skia_safe::matrix::ScaleToFit::Center,
)
.unwrap();
// Generally would want to clear data every time we draw
canvas.clear(skia_safe::Color::from_argb(255, 0, 0, 0));
// Make a color to draw with
let mut paint = skia_safe::Paint::new(skia_safe::Color4f::new(0.0, 1.0, 0.0, 1.0), None);
paint.set_anti_alias(true);
paint.set_style(skia_safe::paint::Style::Stroke);
paint.set_stroke_width(0.02);
canvas.draw_rect(
skia_safe::Rect {
left: -GROUND_HALF_EXTENTS_WIDTH,
top: 0.0,
right: GROUND_HALF_EXTENTS_WIDTH,
bottom: -GROUND_THICKNESS,
},
&paint,
);
for (i, circle_body) in self.physics.circle_body_handles.iter().enumerate() {
let position = self
.physics
.rigid_body_set
.get(*circle_body)
.unwrap()
.position()
.translation;
let paint = &self.circle_colors[i % self.circle_colors.len()];
canvas.draw_circle(
skia_safe::Point::new(position.x, position.y),
BALL_RADIUS,
paint,
);
}
coordinate_system_helper.use_logical_coordinates(canvas);
//
// Draw FPS text
//
let mut text_paint =
skia_safe::Paint::new(skia_safe::Color4f::new(1.0, 1.0, 0.0, 1.0), None);
text_paint.set_anti_alias(true);
text_paint.set_style(skia_safe::paint::Style::StrokeAndFill);
text_paint.set_stroke_width(1.0);
let mut font = skia_safe::Font::default();
font.set_size(20.0);
canvas.draw_str(self.fps_text.clone(), (50, 50), &font, &text_paint);
}
fn fatal_error(
&mut self,
error: &AppError,
) {
println!("{}", error);
}
}