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
/*
* // Copyright (c) 2021 Feng Yang
* //
* // I am making my contributions/submissions to this project solely in my
* // personal capacity and am not conveying any rights to any intellectual
* // property of any third parties.
*/
use std::sync::{RwLock, Arc};
use log::info;
use crate::animation::*;
use std::time::SystemTime;
pub struct PhysicsAnimationData {
_current_frame: Frame,
_is_using_fixed_sub_time_steps: bool,
_number_of_fixed_sub_time_steps: usize,
_current_time: f64,
}
impl PhysicsAnimationData {
pub fn new() -> PhysicsAnimationData {
return PhysicsAnimationData {
_current_frame: Frame::new(-1, 1.0 / 60.0),
_is_using_fixed_sub_time_steps: true,
_number_of_fixed_sub_time_steps: 1,
_current_time: 0.0,
};
}
}
///
/// # Abstract base class for physics-based animation.
///
/// This class represents physics-based animation by adding time-integration
/// specific functions to Animation class.
///
pub trait PhysicsAnimation: Animation {
///
/// ## Returns true if fixed sub-time stepping is used.
///
/// When performing a time-integration, it is often required to take
/// sub-time stepping for better results. The sub-stepping can be either
/// fixed rate or adaptive, and this function returns which feature is
/// currently selected.
///
/// \return True if using fixed sub time steps, false otherwise.
///
fn is_using_fixed_sub_time_steps(&self) -> bool {
return self.view()._is_using_fixed_sub_time_steps;
}
///
/// ## Sets true if fixed sub-time stepping is used.
///
/// When performing a time-integration, it is often required to take
/// sub-time stepping for better results. The sub-stepping can be either
/// fixed rate or adaptive, and this function sets which feature should be
/// selected.
///
/// - parameter: isUsing True to enable fixed sub-stepping.
///
fn set_is_using_fixed_sub_time_steps(&mut self, is_using: bool) {
self.view_mut()._is_using_fixed_sub_time_steps = is_using;
}
///
/// ## Returns the number of fixed sub-time steps.
///
/// When performing a time-integration, it is often required to take
/// sub-time stepping for better results. The sub-stepping can be either
/// fixed rate or adaptive, and this function returns the number of fixed
/// sub-steps.
///
/// \return The number of fixed sub-time steps.
///
fn number_of_fixed_sub_time_steps(&self) -> usize {
return self.view()._number_of_fixed_sub_time_steps;
}
///
/// ## Sets the number of fixed sub-time steps.
///
/// When performing a time-integration, it is often required to take
/// sub-time stepping for better results. The sub-stepping can be either
/// fixed rate or adaptive, and this function sets the number of fixed
/// sub-steps.
///
/// - parameter: numberOfSteps The number of fixed sub-time steps.
///
fn set_number_of_fixed_sub_time_steps(&mut self, number_of_steps: usize) {
self.view_mut()._number_of_fixed_sub_time_steps = number_of_steps;
}
/// Advances a single frame.
fn advance_single_frame(&mut self) {
let mut f = self.view_mut()._current_frame.clone();
f.advance_single();
self.update(&f);
}
///
/// ## Returns current frame.
///
fn current_frame(&self) -> Frame {
return self.view()._current_frame.clone();
}
///
/// ## Sets current frame cursor (but do not invoke update()).
///
fn set_current_frame(&mut self, frame: &Frame) {
self.view_mut()._current_frame = frame.clone();
}
///
/// ## Returns current time in seconds.
///
/// This function returns the current time which is calculated by adding
/// current frame + sub-time steps it passed.
///
fn current_time_in_seconds(&self) -> f64 {
return self.view()._current_time;
}
///
/// ## Called when a single time-step should be advanced.
///
/// When Animation::update function is called, this class will internally
/// subdivide a frame into sub-steps if needed. Each sub-step, or time-step,
/// is then taken to move forward in time. This function is called for each
/// time-step, and a subclass that inherits PhysicsAnimation class should
/// implement this function for its own physics model.
///
/// - parameter: timeIntervalInSeconds The time interval in seconds
///
fn on_advance_time_step(&mut self, time_interval_in_seconds: f64);
///
/// ## Returns the required number of sub-time steps for given time interval.
///
/// The required number of sub-time step can be different depending on the
/// physics model behind the implementation. Override this function to
/// implement own logic for model specific sub-time stepping for given
/// time interval.
///
/// - parameter: timeIntervalInSeconds The time interval in seconds.
///
/// \return The required number of sub-time steps.
///
fn number_of_sub_time_steps(&self, _: f64) -> usize {
// Returns number of fixed sub-time steps by default
return self.view()._number_of_fixed_sub_time_steps;
}
///
/// ## Called at frame 0 to initialize the physics state.
///
/// Inheriting classes can override this function to setup initial condition
/// for the simulation.
///
fn on_initialize(&mut self) {}
fn on_update(&mut self, frame: &Frame) {
if frame.index > self.view()._current_frame.index {
if self.view()._current_frame.index < 0 {
self.initialize();
}
let number_of_frames = frame.index - self.view()._current_frame.index;
for _ in 0..number_of_frames {
self.advance_time_step(frame.time_interval_in_seconds);
}
self.view_mut()._current_frame = frame.clone();
}
}
fn advance_time_step(&mut self, time_interval_in_seconds: f64) {
self.view_mut()._current_time = self.view()._current_frame.time_in_seconds();
if self.is_using_fixed_sub_time_steps() {
info!("Using fixed sub-timesteps: {}", self.view()._number_of_fixed_sub_time_steps);
// Perform fixed time-stepping
let actual_time_interval = time_interval_in_seconds / self.view()._number_of_fixed_sub_time_steps as f64;
for _ in 0..self.view()._number_of_fixed_sub_time_steps {
info!("Begin onAdvanceTimeStep: {} (1/{}) seconds", actual_time_interval, 1.0 / actual_time_interval);
let timer = SystemTime::now();
self.on_advance_time_step(actual_time_interval);
info!("End onAdvanceTimeStep (took {} seconds)", timer.elapsed().unwrap().as_secs_f64());
self.view_mut()._current_time += actual_time_interval;
}
} else {
info!("Using adaptive sub-timesteps");
// Perform adaptive time-stepping
let mut remaining_time = time_interval_in_seconds;
while remaining_time > f64::EPSILON {
let num_steps = self.number_of_sub_time_steps(remaining_time);
let actual_time_interval = remaining_time / num_steps as f64;
info!("Number of remaining sub-timesteps: {}", num_steps);
info!("Begin onAdvanceTimeStep: {} (1/{}) seconds", actual_time_interval, 1.0 / actual_time_interval);
let timer = SystemTime::now();
self.on_advance_time_step(actual_time_interval);
info!("End onAdvanceTimeStep (took {} seconds)", timer.elapsed().unwrap().as_secs_f64());
remaining_time -= actual_time_interval;
self.view_mut()._current_time += actual_time_interval;
}
}
}
fn initialize(&mut self) {
self.on_initialize();
}
fn view(&self) -> &PhysicsAnimationData;
fn view_mut(&mut self) -> &mut PhysicsAnimationData;
}
pub type PhysicsAnimationPtr = Arc<RwLock<dyn PhysicsAnimation>>;