misc/fixedstepper
고정 걸음.
- 원본 misc/fixedstepper.js 를 옮긴 것이다. 프레임 간격이 들쭉날쭉해도 규칙(물리, 판의 흐름)은 늘 같은 걸음(step_seconds)으로 돌린다.
- advance(time_delta, step_function) 가 받은 시간을 쌓아 두고 걸음만큼 찰 때마다 step_function(step_seconds) 을 부른다. 돌려주는 값은 남은 몫(0 ~ 1)이다. 앞 걸음과 지금 걸음 사이를 이 몫만큼 섞어 그리면 걸음이 화면 주사율과 어긋나 떨리지 않는다.
- 한 프레임에 maximum_steps_per_frame 걸음까지만 돈다. 그만큼 돌고도 남았으면 남은 시간을 버린다. (느린 기기에서 멈칫한 뒤 밀린 것을 한꺼번에 몰아 돌다 더 느려지지 않게)
- 원본과 다른 점: 원본의 걸음 함수는 false 를 돌려줄 때만 멈추고 아무것도 돌려주지 않아도 된다. 러스트 판은 늘 bool 을 돌려준다. (true 면 이어 가고, false 면 이번 프레임의 걸음을 거기서 멈추고 남은 시간은 쌓아 둔다)
Fixed stepper.
- A port of the original misc/fixedstepper.js. Even when frame intervals are uneven, rules (physics, the board's flow) always run in the same step (step_seconds).
- advance(time_delta, step_function) accumulates the given time and calls step_function(step_seconds) each time a full step is filled. The return value is the remaining fraction (0 ~ 1). Blending between the previous and current step by this fraction when drawing keeps the steps from jittering against the screen refresh rate.
- Runs at most maximum_steps_per_frame steps per frame. If time remains after that many, the remaining time is discarded. (so a slow device that stalls does not get even slower by running all the backlog at once)
- Difference from the original: the original step function stops only when it returns false and may return nothing. The Rust version always returns bool. (true continues, false stops this frame's steps there and keeps the remaining time accumulated)
FixedStepperstruct traits Clone, Debug, Default
고정 걸음.
- 매 프레임 advance 에 프레임 시간을 넘기면 쌓인 시간만큼 고정 걸음을 돌리고 섞을 몫을 돌려준다.
Fixed stepper.
- Passing the frame time to advance every frame runs fixed steps for the accumulated time and returns the fraction to blend.
use vanilla::FixedStepper;
let mut fixed_stepper = FixedStepper::new(0.125, 3);
let mut simulated_seconds = 0.0;
let alpha = fixed_stepper.advance(0.3125, |step_seconds| {
simulated_seconds += step_seconds;
true
});
assert_eq!(fixed_stepper.get_last_step_count(), 2);
assert_eq!(simulated_seconds, 0.25);
assert!((alpha - 0.5).abs() < 0.000001);생성. (한 걸음의 시간 초, 한 프레임에 도는 걸음의 상한. 시간이 잘못된 값이면 1 / 60 초, 상한이 0 이면 1 걸음)
Constructor. (time of one step in seconds, upper limit of steps run in one frame. 1 / 60 seconds if the time is invalid, 1 step if the limit is 0)
- step_secondsf64
- 한 걸음의 시간. (초) 0 이하이거나 유한하지 않으면 1 / 60 초.Time of one step. (seconds) 1 / 60 seconds if 0 or less or not finite.
- maximum_steps_per_frameu32
- 한 프레임에 도는 걸음의 상한. 0 이면 1 로 올린다.Upper limit of steps per frame. Raised to 1 if 0.
반환ReturnsFixedStepper 쌓인 시간이 0 인 새 고정 걸음.A new fixed stepper with 0 accumulated time.
짝Counterpart: vanilla.js FixedStepper.constructor
한 걸음의 시간 설정. (초, 0 보다 크고 유한해야 한다. 아니면 그대로 둔다)
Sets the time of one step. (seconds, must be greater than 0 and finite. Otherwise unchanged)
- step_secondsf64
- 새 걸음 시간. (초)New step time. (seconds)
짝Counterpart: vanilla.js FixedStepper.setStepSeconds
한 걸음의 시간 반환. (초)
Returns the duration of one step. (Seconds)
반환Returnsf64 지금 걸음 시간. (초)Current step time. (seconds)
짝Counterpart: vanilla.js FixedStepper.getStepSeconds
한 프레임에 도는 걸음의 상한 설정. (1 이상)
Sets the upper limit of steps per frame. (at least 1)
- maximum_steps_per_frameu32
- 새 상한. (0 이면 1 로 올린다)New limit. (raised to 1 if 0)
짝Counterpart: vanilla.js FixedStepper.setMaximumStepsPerFrame
한 프레임에 도는 걸음의 상한 반환.
Returns the maximum steps run per frame.
반환Returnsu32 지금 상한. (1 이상)Current limit. (at least 1)
짝Counterpart: vanilla.js FixedStepper.getMaximumStepsPerFrame
pub fn advance(&mut self, time_delta: f64, mut step_function: impl FnMut(f64) -> bool) -> f64#나아가기. (받은 시간만큼 걸음을 돌리고 남은 몫(0 ~ 1)을 돌려준다. 음수나 유한하지 않은 시간은 0 으로 친다)
Advances. (runs steps for the given time and returns the remaining fraction (0 ~ 1). Negative or non-finite time counts as 0)
- time_deltaf64
- 이번 프레임 시간. (초)This frame's time. (seconds)
- step_functionimpl FnMut(f64) -> bool
- 걸음마다 부를 함수. 걸음 시간(초)을 받고, true 면 이어 가고 false 면 이번 프레임의 걸음을 거기서 멈춘다. (멈춘 걸음은 세지 않고 그 시간은 쌓아 둔다)Function to call for each step. Receives the step time (seconds), true continues and false stops this frame's steps there. (the stopped step is not counted and its time stays accumulated)
반환Returnsf64 남은 몫. (쌓인 시간 / 걸음 시간, 0 ~ 1) 앞 걸음과 지금 걸음을 이 몫만큼 섞어 그린다.Remaining fraction. (accumulated time / step time, 0 ~ 1) Draw by blending the previous and current step by this fraction.
짝Counterpart: vanilla.js FixedStepper.advance
use vanilla::FixedStepper;
let mut fixed_stepper = FixedStepper::new(0.125, 3);
let alpha = fixed_stepper.advance(1.0, |_| true);
assert_eq!(fixed_stepper.get_last_step_count(), 3);
assert_eq!(alpha, 0.0);
let alpha = fixed_stepper.advance(0.25, |_| false);
assert_eq!(fixed_stepper.get_last_step_count(), 0);
assert_eq!(alpha, 1.0);남은 몫. (쌓인 시간 / 한 걸음, 0 ~ 1)
Remaining fraction. (Accumulated time / one step, 0 ~ 1)
반환Returnsf64 쌓인 시간을 걸음 시간으로 나눠 0 ~ 1 로 자른 값.Accumulated time divided by the step time, clamped to 0 ~ 1.
짝Counterpart: vanilla.js FixedStepper.getAlpha
지난 advance 에서 돈 걸음 수.
Number of steps run in the last advance.
반환Returnsu32 지난 advance 에서 끝까지 돈 걸음 수. (false 로 멈춘 걸음은 빼고, reset 뒤에는 0)Number of steps fully run in the last advance. (excluding steps stopped with false, 0 after reset)
짝Counterpart: vanilla.js FixedStepper.getLastStepCount
쌓인 시간 비우기. (판을 새로 시작하거나 붙잡았다 풀 때)
Clears the accumulated time. (When starting a new round or releasing a hold)
짝Counterpart: vanilla.js FixedStepper.reset