Module: Jolt::ShapeBuilders

Included in:
Shape
Defined in:
lib/jolt/shape_builders.rb

Instance Method Summary collapse

Instance Method Details

#compound(children) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jolt/shape_builders.rb', line 54

def compound(children)
  Jolt.init
  children = children.to_a
  raise InvalidArgumentError, "compound requires at least one child" if children.empty?

  settings = Native.JPH_StaticCompoundShapeSettings_Create
  raise ShapeError, "failed to create compound settings" if settings.null?

  children.each_with_index do |child, index|
    shape, position, rotation = child
    raise InvalidArgumentError, "child #{index} shape must be a Jolt::Shape" unless shape.is_a?(Shape)

    position = Conversions.native_vec3(position || [0, 0, 0], name: "child position")
    rotation = Conversions.native_quat(rotation || [0, 0, 0, 1], name: "child rotation")
    Native.JPH_CompoundShapeSettings_AddShape2(
      settings, position.pointer, rotation.pointer, shape.native_pointer, index
    )
  end
  build(Native.JPH_StaticCompoundShape_Create(settings), :compound)
ensure
  Native.JPH_ShapeSettings_Destroy(settings) if settings && !settings.null?
end

#convex_hull(points, max_convex_radius: Shape::DEFAULT_CONVEX_RADIUS) ⇒ Object



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

def convex_hull(points, max_convex_radius: Shape::DEFAULT_CONVEX_RADIUS)
  Jolt.init
  coordinates = packed_floats(points, 3, "points")
  raise InvalidArgumentError, "convex hull requires at least 4 points" if coordinates.length < 12

  point_memory = float_memory(coordinates)
  settings = Native.JPH_ConvexHullShapeSettings_Create(
    point_memory, coordinates.length / 3,
    Conversions.non_negative_float(max_convex_radius, "max_convex_radius")
  )
  create_from_settings(settings, :convex_hull, :JPH_ConvexHullShapeSettings_CreateShape)
end

#heightfield(samples:, size:, offset: [0, 0, 0], scale: [1, 1, 1]) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jolt/shape_builders.rb', line 35

def heightfield(samples:, size:, offset: [0, 0, 0], scale: [1, 1, 1])
  Jolt.init
  unless size.is_a?(Integer) && size >= 4
    raise InvalidArgumentError, "size must be an integer greater than or equal to 4"
  end
  values = packed_floats(samples, 1, "samples")
  unless values.length == size * size
    raise InvalidArgumentError, "samples must contain size * size values"
  end

  sample_memory = float_memory(values)
  offset = Conversions.native_vec3(offset, name: "offset")
  scale = Conversions.native_vec3(scale, name: "scale")
  settings = Native.JPH_HeightFieldShapeSettings_Create(
    sample_memory, offset.pointer, scale.pointer, size, nil
  )
  create_from_settings(settings, :heightfield, :JPH_HeightFieldShapeSettings_CreateShape)
end

#mesh(vertices:, indices:) ⇒ Object



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

def mesh(vertices:, indices:)
  Jolt.init
  coordinates = packed_floats(vertices, 3, "vertices")
  triangles = packed_indices(indices)
  vertex_count = coordinates.length / 3
  raise InvalidArgumentError, "mesh requires at least 3 vertices" if vertex_count < 3
  raise InvalidArgumentError, "mesh requires at least 1 triangle" if triangles.empty?
  raise InvalidArgumentError, "mesh index is out of bounds" if triangles.any? { |index| index >= vertex_count }

  vertex_memory = float_memory(coordinates)
  triangle_memory = indexed_triangle_memory(triangles)
  settings = Native.JPH_MeshShapeSettings_Create2(
    vertex_memory, vertex_count, triangle_memory, triangles.length / 3
  )
  create_from_settings(settings, :mesh, :JPH_MeshShapeSettings_CreateShape)
end

#offset(shape, position: [0, 0, 0], rotation: [0, 0, 0, 1]) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/jolt/shape_builders.rb', line 83

def offset(shape, position: [0, 0, 0], rotation: [0, 0, 0, 1])
  validate_shape(shape)
  position = Conversions.native_vec3(position, name: "position")
  rotation = Conversions.native_quat(rotation)
  pointer = Native.JPH_RotatedTranslatedShape_Create(
    position.pointer, rotation.pointer, shape.native_pointer
  )
  build(pointer, :offset)
end

#scaled(shape, scale) ⇒ Object



77
78
79
80
81
# File 'lib/jolt/shape_builders.rb', line 77

def scaled(shape, scale)
  validate_shape(shape)
  scale = Conversions.native_vec3(scale, name: "scale")
  build(Native.JPH_ScaledShape_Create(shape.native_pointer, scale.pointer), :scaled)
end