Class: Jolt::FixedStepper

Inherits:
Object
  • Object
show all
Defined in:
lib/jolt/fixed_stepper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hz: 60, max_substeps: 5) ⇒ FixedStepper

Returns a new instance of FixedStepper.



7
8
9
10
11
12
13
14
15
16
# File 'lib/jolt/fixed_stepper.rb', line 7

def initialize(hz: 60, max_substeps: 5)
  frequency = Conversions.positive_float(hz, "hz")
  unless max_substeps.is_a?(Integer) && max_substeps.positive?
    raise InvalidArgumentError, "max_substeps must be a positive integer"
  end

  @step = 1.0 / frequency
  @max_substeps = max_substeps
  @accumulator = 0.0
end

Instance Attribute Details

#max_substepsObject (readonly)

Returns the value of attribute max_substeps.



5
6
7
# File 'lib/jolt/fixed_stepper.rb', line 5

def max_substeps
  @max_substeps
end

#stepObject (readonly)

Returns the value of attribute step.



5
6
7
# File 'lib/jolt/fixed_stepper.rb', line 5

def step
  @step
end

Instance Method Details

#advance(delta_time) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jolt/fixed_stepper.rb', line 18

def advance(delta_time)
  raise ArgumentError, "a block is required" unless block_given?

  @accumulator += Conversions.non_negative_float(delta_time, "delta_time")
  substeps = 0
  while @accumulator >= @step && substeps < @max_substeps
    yield @step
    @accumulator -= @step
    substeps += 1
  end
  @accumulator %= @step if @accumulator >= @step
  @accumulator / @step
end

#resetObject



32
33
34
35
# File 'lib/jolt/fixed_stepper.rb', line 32

def reset
  @accumulator = 0.0
  self
end