Module: Box2D::WorldQueries

Included in:
World
Defined in:
lib/box2d/world_queries.rb

Instance Method Summary collapse

Instance Method Details

#overlap_aabb(lower, upper, filter: {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/box2d/world_queries.rb', line 23

def overlap_aabb(lower, upper, filter: {}, &block)
  return enum_for(__method__, lower, upper, filter:) unless block

  ensure_access!
  bounds = Native::AABB.new
  bounds[:lowerBound] = ValueConversion.native_vec2(lower, label: "lower")
  bounds[:upperBound] = ValueConversion.native_vec2(upper, label: "upper")
  if bounds[:lowerBound][:x] > bounds[:upperBound][:x] || bounds[:lowerBound][:y] > bounds[:upperBound][:y]
    raise ArgumentError, "lower AABB bound must not exceed upper bound"
  end

  perform_overlap(block) do |callback|
    Native.b2World_OverlapAABB(@id, bounds, ShapeDefinition.query_filter(filter), callback, nil)
  end
end

#overlap_capsule(point1, point2, radius:, filter: {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/box2d/world_queries.rb', line 52

def overlap_capsule(point1, point2, radius:, filter: {}, &block)
  return enum_for(__method__, point1, point2, radius:, filter:) unless block

  ensure_access!
  points, count = native_query_points([point1, point2], minimum: 2, maximum: 2)
  proxy = Native.b2MakeProxy(
    points,
    count,
    ValueConversion.positive_float(radius, label: "radius")
  )
  overlap_proxy(proxy, filter, block)
end

#overlap_circle(center, radius:, filter: {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/box2d/world_queries.rb', line 39

def overlap_circle(center, radius:, filter: {}, &block)
  return enum_for(__method__, center, radius:, filter:) unless block

  ensure_access!
  point = ValueConversion.native_vec2(center, label: "center")
  proxy = Native.b2MakeProxy(
    point.pointer,
    1,
    ValueConversion.positive_float(radius, label: "radius")
  )
  overlap_proxy(proxy, filter, block)
end

#overlap_polygon(points, position: [0, 0], angle: 0, radius: 0.0, filter: {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/box2d/world_queries.rb', line 65

def overlap_polygon(points, position: [0, 0], angle: 0, radius: 0.0, filter: {}, &block)
  return enum_for(__method__, points, position:, angle:, radius:, filter:) unless block

  ensure_access!
  point_buffer, count = native_query_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

  proxy = Native.b2MakeOffsetProxy(
    hull.pointer + Native::Hull.offset_of(:points),
    hull[:count],
    ValueConversion.non_negative_float(radius, label: "radius"),
    ValueConversion.native_vec2(position, label: "position"),
    ValueConversion.native_rot(angle)
  )
  overlap_proxy(proxy, filter, block)
end

#raycast(from, to, filter: {}) ⇒ Object



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

def raycast(from, to, filter: {})
  ensure_access!
  origin = ValueConversion.native_vec2(from, label: "from")
  destination = ValueConversion.native_vec2(to, label: "to")
  translation = Native::Vec2.new
  translation[:x] = destination[:x] - origin[:x]
  translation[:y] = destination[:y] - origin[:y]
  result = Native.b2World_CastRayClosest(@id, origin, translation, ShapeDefinition.query_filter(filter))
  return unless result[:hit]

  Hit.new(
    shape: shape_for_id(result[:shapeId]),
    point: ValueConversion.vec2(result[:point]),
    normal: ValueConversion.vec2(result[:normal]),
    fraction: result[:fraction]
  )
end