Class: Box2D::FixedStepper

Inherits:
Object
  • Object
show all
Defined in:
lib/box2d/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.

Raises:

  • (ArgumentError)


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

def initialize(hz: 60, max_substeps: 5)
  frequency = ValueConversion.positive_float(hz, label: "hz")
  @step = 1.0 / frequency
  @max_substeps = Integer(max_substeps)
  raise ArgumentError, "max_substeps must be greater than zero" unless @max_substeps.positive?

  @accumulator = 0.0
end

Instance Attribute Details

#max_substepsObject (readonly)

Returns the value of attribute max_substeps.



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

def max_substeps
  @max_substeps
end

#stepObject (readonly)

Returns the value of attribute step.



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

def step
  @step
end

Instance Method Details

#advance(delta_time) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/box2d/fixed_stepper.rb', line 16

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

  delta = ValueConversion.non_negative_float(delta_time, label: "delta_time")
  @accumulator = [@accumulator + delta, @step * @max_substeps].min

  steps = 0
  epsilon = @step * 1e-12
  while @accumulator + epsilon >= @step && steps < @max_substeps
    yield @step
    @accumulator -= @step
    @accumulator = 0.0 if @accumulator.abs < epsilon
    steps += 1
  end

  [@accumulator / @step, 1.0 - Float::EPSILON].min
end

#resetObject



34
35
36
37
# File 'lib/box2d/fixed_stepper.rb', line 34

def reset
  @accumulator = 0.0
  self
end