Module: Box2D::BodyShapes

Included in:
Body
Defined in:
lib/box2d/body_shapes.rb

Instance Method Summary collapse

Instance Method Details

#box(half_width, half_height, center: [0, 0], angle: 0, **options) ⇒ Object



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

def box(half_width, half_height, center: [0, 0], angle: 0, **options)
  ensure_valid!
  width = ValueConversion.positive_float(half_width, label: "half_width")
  height = ValueConversion.positive_float(half_height, label: "half_height")
  polygon = Native.b2MakeOffsetBox(
    width,
    height,
    ValueConversion.native_vec2(center, label: "center"),
    ValueConversion.native_rot(angle)
  )
  create_shape(:b2CreatePolygonShape, polygon, options)
end

#capsule(point1, point2, radius:, **options) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/box2d/body_shapes.rb', line 26

def capsule(point1, point2, radius:, **options)
  ensure_valid!
  geometry = Native::Capsule.new
  geometry[:center1] = ValueConversion.native_vec2(point1, label: "point1")
  geometry[:center2] = ValueConversion.native_vec2(point2, label: "point2")
  geometry[:radius] = ValueConversion.positive_float(radius, label: "radius")
  create_shape(:b2CreateCapsuleShape, geometry, options)
end

#chain(points, loop: false, **options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/box2d/body_shapes.rb', line 57

def chain(points, loop: false, **options)
  ensure_valid!
  point_buffer, count = native_points(points, minimum: 4)
  if loop
    first = Native::Vec2.new(point_buffer)
    last = Native::Vec2.new(point_buffer + (count - 1) * Native::Vec2.size)
    validate_separation!(last, first, label: "loop endpoints")
  end
  definition = Native.b2DefaultChainDef
  definition[:points] = point_buffer
  definition[:count] = count
  definition[:isLoop] = !!loop
  definition[:enableSensorEvents] = !!options.fetch(:sensor_events, true)
  ShapeDefinition.apply_filter(definition[:filter], options.fetch(:filter, {}))

  material_buffer = FFI::MemoryPointer.new(Native::SurfaceMaterial)
  material = Native.b2DefaultSurfaceMaterial
  configure_material(material, options)
  material_buffer.put_bytes(0, material.pointer.read_bytes(Native::SurfaceMaterial.size))
  definition[:materials] = material_buffer
  definition[:materialCount] = 1

  native_id = Native.b2CreateChain(@id, definition.pointer)
  chain = Chain.new(@world, native_id, body: self)
  @chains << chain
  @world.register_chain(chain)
end

#circle(radius:, center: [0, 0], **options) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/box2d/body_shapes.rb', line 18

def circle(radius:, center: [0, 0], **options)
  ensure_valid!
  geometry = Native::Circle.new
  geometry[:center] = ValueConversion.native_vec2(center, label: "center")
  geometry[:radius] = ValueConversion.positive_float(radius, label: "radius")
  create_shape(:b2CreateCircleShape, geometry, options)
end

#polygon(points, radius: 0.0, **options) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/box2d/body_shapes.rb', line 35

def polygon(points, radius: 0.0, **options)
  ensure_valid!
  point_buffer, count = native_points(points, minimum: 3, maximum: 8)
  hull = Native.b2ComputeHull(point_buffer, count)
  raise ArgumentError, "points do not form a valid convex hull" if hull[:count] < 3

  polygon = Native.b2MakePolygon(
    hull.pointer,
    ValueConversion.non_negative_float(radius, label: "radius")
  )
  create_shape(:b2CreatePolygonShape, polygon, options)
end

#segment(point1, point2, **options) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/box2d/body_shapes.rb', line 48

def segment(point1, point2, **options)
  ensure_valid!
  geometry = Native::Segment.new
  geometry[:point1] = ValueConversion.native_vec2(point1, label: "point1")
  geometry[:point2] = ValueConversion.native_vec2(point2, label: "point2")
  validate_separation!(geometry[:point1], geometry[:point2], label: "segment endpoints")
  create_shape(:b2CreateSegmentShape, geometry, options)
end