Module: Box2D::ShapeDefinition

Defined in:
lib/box2d/shape_definition.rb

Class Method Summary collapse

Class Method Details

.apply_filter(filter, options) ⇒ Object



31
32
33
34
35
# File 'lib/box2d/shape_definition.rb', line 31

def apply_filter(filter, options)
  filter[:categoryBits] = Integer(options.fetch(:category, filter[:categoryBits]))
  filter[:maskBits] = Integer(options.fetch(:mask, filter[:maskBits]))
  filter[:groupIndex] = Integer(options.fetch(:group, filter[:groupIndex]))
end

.build(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/box2d/shape_definition.rb', line 7

def build(options)
  definition = Native.b2DefaultShapeDef
  material = definition[:material]
  material[:friction] = ValueConversion.non_negative_float(options.fetch(:friction, 0.6), label: "friction")
  material[:restitution] = coefficient(options.fetch(:restitution, 0.0), "restitution")
  material[:rollingResistance] = coefficient(options.fetch(:rolling_resistance, 0.0), "rolling_resistance")
  material[:tangentSpeed] = ValueConversion.finite_float(options.fetch(:tangent_speed, 0.0), label: "tangent_speed")
  material[:userMaterialId] = Integer(options.fetch(:material, 0))
  definition[:density] = ValueConversion.non_negative_float(options.fetch(:density, 1.0), label: "density")
  definition[:isSensor] = !!options.fetch(:sensor, false)
  definition[:enableSensorEvents] = !!options.fetch(:sensor_events, true)
  definition[:enableContactEvents] = !!options.fetch(:contact_events, true)
  definition[:enableHitEvents] = !!options.fetch(:hit_events, true)
  apply_filter(definition[:filter], options.fetch(:filter, {}))
  definition
end

.coefficient(value, label) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
# File 'lib/box2d/shape_definition.rb', line 37

def coefficient(value, label)
  number = ValueConversion.non_negative_float(value, label:)
  return number if number <= 1.0

  raise ArgumentError, "#{label} must be between 0 and 1"
end

.query_filter(options) ⇒ Object



24
25
26
27
28
29
# File 'lib/box2d/shape_definition.rb', line 24

def query_filter(options)
  filter = Native.b2DefaultQueryFilter
  filter[:categoryBits] = Integer(options.fetch(:category, filter[:categoryBits]))
  filter[:maskBits] = Integer(options.fetch(:mask, filter[:maskBits]))
  filter
end