Class: Jolt::BodyCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jolt/body_collection.rb

Constant Summary collapse

MOTION_TYPES =
{static: 0, kinematic: 1, dynamic: 2}.freeze
INVALID_BODY_ID =
0xffff_ffff

Instance Method Summary collapse

Constructor Details

#initialize(system) ⇒ BodyCollection

Returns a new instance of BodyCollection.



10
11
12
# File 'lib/jolt/body_collection.rb', line 10

def initialize(system)
  @system = system
end

Instance Method Details

#[](id) ⇒ Object



54
55
56
# File 'lib/jolt/body_collection.rb', line 54

def [](id)
  @system.__body(id)
end

#create(shape:, position: [0, 0, 0], rotation: [0, 0, 0, 1], motion: :dynamic, layer: nil, activate: nil, friction: 0.2, restitution: 0.0, linear_damping: 0.05, angular_damping: 0.05, gravity_factor: 1.0, mass: nil, ccd: false, sensor: false, user_data: nil) ⇒ Object



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
# File 'lib/jolt/body_collection.rb', line 14

def create(shape:, position: [0, 0, 0], rotation: [0, 0, 0, 1],
           motion: :dynamic, layer: nil, activate: nil, friction: 0.2,
           restitution: 0.0, linear_damping: 0.05, angular_damping: 0.05,
           gravity_factor: 1.0, mass: nil, ccd: false, sensor: false,
           user_data: nil)
  @system.__check_alive!
  validate_shape!(shape)
  motion_type = MOTION_TYPES.fetch(motion.respond_to?(:to_sym) ? motion.to_sym : nil)
  if !motion_type.zero? && shape.must_be_static?
    raise InvalidArgumentError, "#{shape.kind} shapes can only be used by static bodies"
  end
  layer ||= motion_type.zero? ? :non_moving : :moving
  layer_id = @system.layers.object_layer_id(layer)
  settings = create_settings(shape, position, rotation, motion_type, layer_id)
  configure_settings(
    settings,
    friction:, restitution:, linear_damping:, angular_damping:,
    gravity_factor:, mass:, ccd:, sensor:, motion_type:
  )
  id = Native.JPH_BodyInterface_CreateAndAddBody(
    @system.__body_interface, settings, activation(activate, motion_type)
  )
  raise InitializationError, "failed to create body" if id == INVALID_BODY_ID

  Body.new(@system, id).tap do |body|
    @system.__register_body(body, shape)
    body.user_data = user_data unless user_data.nil?
  end
rescue KeyError
  raise InvalidArgumentError, "motion must be one of: #{MOTION_TYPES.keys.join(", ")}"
ensure
  Native.JPH_BodyCreationSettings_Destroy(settings) if settings && !settings.null?
end

#each(&block) ⇒ Object



48
49
50
51
52
# File 'lib/jolt/body_collection.rb', line 48

def each(&block)
  return enum_for(:each) unless block

  @system.__bodies_snapshot.each(&block)
end

#sizeObject Also known as: length



58
59
60
# File 'lib/jolt/body_collection.rb', line 58

def size
  @system.__bodies_snapshot.size
end