Class: Box2D::Shape

Inherits:
Handle show all
Defined in:
lib/box2d/shape.rb

Constant Summary collapse

ID_CLASS =
Native::ShapeId
TYPES =
{
  Native::ShapeType::CIRCLE_SHAPE => :circle,
  Native::ShapeType::CAPSULE_SHAPE => :capsule,
  Native::ShapeType::SEGMENT_SHAPE => :segment,
  Native::ShapeType::POLYGON_SHAPE => :polygon,
  Native::ShapeType::CHAIN_SEGMENT_SHAPE => :chain_segment
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Handle

#id, #world

Instance Method Summary collapse

Methods inherited from Handle

#destroyed?, #eql?, #hash, #valid?

Constructor Details

#initialize(world, id, body: nil) ⇒ Shape

Returns a new instance of Shape.



16
17
18
19
# File 'lib/box2d/shape.rb', line 16

def initialize(world, id, body: nil)
  super(world, id)
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



14
15
16
# File 'lib/box2d/shape.rb', line 14

def body
  @body
end

Instance Method Details

#aabbObject



97
98
99
100
101
# File 'lib/box2d/shape.rb', line 97

def aabb
  ensure_valid!
  bounds = Native.b2Shape_GetAABB(@id)
  [ValueConversion.vec2(bounds[:lowerBound]), ValueConversion.vec2(bounds[:upperBound])]
end

#densityObject



40
41
42
43
# File 'lib/box2d/shape.rb', line 40

def density
  ensure_valid!
  Native.b2Shape_GetDensity(@id)
end

#density=(value) ⇒ Object



45
46
47
48
# File 'lib/box2d/shape.rb', line 45

def density=(value)
  ensure_valid!
  Native.b2Shape_SetDensity(@id, ValueConversion.non_negative_float(value, label: "density"), true)
end

#destroyObject



21
22
23
24
25
26
27
28
# File 'lib/box2d/shape.rb', line 21

def destroy
  ensure_valid!
  Native.b2DestroyShape(@id, true)
  invalidate!
  @world.unregister_shape(self)
  @body&.unregister_shape(self)
  true
end

#filterObject



70
71
72
73
74
75
76
77
78
# File 'lib/box2d/shape.rb', line 70

def filter
  ensure_valid!
  native_filter = Native.b2Shape_GetFilter(@id)
  {
    category: native_filter[:categoryBits],
    mask: native_filter[:maskBits],
    group: native_filter[:groupIndex]
  }
end

#filter=(options) ⇒ Object



80
81
82
83
84
85
# File 'lib/box2d/shape.rb', line 80

def filter=(options)
  ensure_valid!
  native_filter = Native.b2Shape_GetFilter(@id)
  ShapeDefinition.apply_filter(native_filter, options)
  Native.b2Shape_SetFilter(@id, native_filter)
end

#frictionObject



50
51
52
53
# File 'lib/box2d/shape.rb', line 50

def friction
  ensure_valid!
  Native.b2Shape_GetFriction(@id)
end

#friction=(value) ⇒ Object



55
56
57
58
# File 'lib/box2d/shape.rb', line 55

def friction=(value)
  ensure_valid!
  Native.b2Shape_SetFriction(@id, ValueConversion.non_negative_float(value, label: "friction"))
end

#restitutionObject



60
61
62
63
# File 'lib/box2d/shape.rb', line 60

def restitution
  ensure_valid!
  Native.b2Shape_GetRestitution(@id)
end

#restitution=(value) ⇒ Object



65
66
67
68
# File 'lib/box2d/shape.rb', line 65

def restitution=(value)
  ensure_valid!
  Native.b2Shape_SetRestitution(@id, ShapeDefinition.coefficient(value, "restitution"))
end

#sensor?Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/box2d/shape.rb', line 35

def sensor?
  ensure_valid!
  Native.b2Shape_IsSensor(@id)
end

#typeObject



30
31
32
33
# File 'lib/box2d/shape.rb', line 30

def type
  ensure_valid!
  TYPES.fetch(Native.b2Shape_GetType(@id))
end

#user_dataObject



87
88
89
90
# File 'lib/box2d/shape.rb', line 87

def user_data
  ensure_valid!
  @world.shape_user_data(self)
end

#user_data=(value) ⇒ Object



92
93
94
95
# File 'lib/box2d/shape.rb', line 92

def user_data=(value)
  ensure_valid!
  @world.set_shape_user_data(self, value)
end