Class: Box2D::Body

Inherits:
Handle show all
Includes:
BodyShapes
Defined in:
lib/box2d/body.rb

Constant Summary collapse

ID_CLASS =
Native::BodyId
TYPES =
{
  Native::BodyType::STATIC_BODY => :static,
  Native::BodyType::KINEMATIC_BODY => :kinematic,
  Native::BodyType::DYNAMIC_BODY => :dynamic
}.freeze
TYPE_VALUES =
TYPES.invert.freeze

Instance Attribute Summary collapse

Attributes inherited from Handle

#id, #world

Instance Method Summary collapse

Methods included from BodyShapes

#box, #capsule, #chain, #circle, #polygon, #segment

Methods inherited from Handle

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

Constructor Details

#initialize(world, id) ⇒ Body

Returns a new instance of Body.



17
18
19
20
21
22
# File 'lib/box2d/body.rb', line 17

def initialize(world, id)
  super
  @shapes = []
  @chains = []
  @joints = []
end

Instance Attribute Details

#chainsObject (readonly)

Returns the value of attribute chains.



15
16
17
# File 'lib/box2d/body.rb', line 15

def chains
  @chains
end

#jointsObject (readonly)

Returns the value of attribute joints.



15
16
17
# File 'lib/box2d/body.rb', line 15

def joints
  @joints
end

#shapesObject (readonly)

Returns the value of attribute shapes.



15
16
17
# File 'lib/box2d/body.rb', line 15

def shapes
  @shapes
end

Instance Method Details

#angleObject



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

def angle
  ensure_valid!
  rotation = Native.b2Body_GetRotation(@id)
  Math.atan2(rotation[:s], rotation[:c])
end

#angle=(value) ⇒ Object



61
62
63
64
# File 'lib/box2d/body.rb', line 61

def angle=(value)
  ensure_valid!
  Native.b2Body_SetTransform(@id, Native.b2Body_GetPosition(@id), ValueConversion.native_rot(value))
end

#angular_velocityObject



76
77
78
79
# File 'lib/box2d/body.rb', line 76

def angular_velocity
  ensure_valid!
  Native.b2Body_GetAngularVelocity(@id)
end

#angular_velocity=(value) ⇒ Object



81
82
83
84
# File 'lib/box2d/body.rb', line 81

def angular_velocity=(value)
  ensure_valid!
  Native.b2Body_SetAngularVelocity(@id, ValueConversion.finite_float(value, label: "angular_velocity"))
end

#apply_force(force, point: nil, wake: true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/box2d/body.rb', line 97

def apply_force(force, point: nil, wake: true)
  ensure_valid!
  native_force = ValueConversion.native_vec2(force, label: "force")
  if point
    Native.b2Body_ApplyForce(@id, native_force, ValueConversion.native_vec2(point, label: "point"), !!wake)
  else
    Native.b2Body_ApplyForceToCenter(@id, native_force, !!wake)
  end
  self
end

#apply_impulse(impulse, point: nil, wake: true) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/box2d/body.rb', line 86

def apply_impulse(impulse, point: nil, wake: true)
  ensure_valid!
  native_impulse = ValueConversion.native_vec2(impulse, label: "impulse")
  if point
    Native.b2Body_ApplyLinearImpulse(@id, native_impulse, ValueConversion.native_vec2(point, label: "point"), !!wake)
  else
    Native.b2Body_ApplyLinearImpulseToCenter(@id, native_impulse, !!wake)
  end
  self
end

#apply_torque(torque, wake: true) ⇒ Object



108
109
110
111
112
# File 'lib/box2d/body.rb', line 108

def apply_torque(torque, wake: true)
  ensure_valid!
  Native.b2Body_ApplyTorque(@id, ValueConversion.finite_float(torque, label: "torque"), !!wake)
  self
end

#awake=(value) ⇒ Object



124
125
126
127
# File 'lib/box2d/body.rb', line 124

def awake=(value)
  ensure_valid!
  Native.b2Body_SetAwake(@id, !!value)
end

#awake?Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/box2d/body.rb', line 119

def awake?
  ensure_valid!
  Native.b2Body_IsAwake(@id)
end

#bullet=(value) ⇒ Object



134
135
136
137
# File 'lib/box2d/body.rb', line 134

def bullet=(value)
  ensure_valid!
  Native.b2Body_SetBullet(@id, !!value)
end

#bullet?Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/box2d/body.rb', line 129

def bullet?
  ensure_valid!
  Native.b2Body_IsBullet(@id)
end

#destroyObject



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

def destroy
  ensure_valid!
  Native.b2DestroyBody(@id)
  @joints.dup.each(&:invalidate_from_body!)
  @shapes.each { |shape| shape.send(:invalidate!) }
  @chains.each { |chain| chain.send(:invalidate!) }
  invalidate!
  @world.unregister_body(self)
  true
end

#linear_velocityObject



66
67
68
69
# File 'lib/box2d/body.rb', line 66

def linear_velocity
  ensure_valid!
  ValueConversion.vec2(Native.b2Body_GetLinearVelocity(@id))
end

#linear_velocity=(value) ⇒ Object



71
72
73
74
# File 'lib/box2d/body.rb', line 71

def linear_velocity=(value)
  ensure_valid!
  Native.b2Body_SetLinearVelocity(@id, ValueConversion.native_vec2(value, label: "linear_velocity"))
end

#massObject



114
115
116
117
# File 'lib/box2d/body.rb', line 114

def mass
  ensure_valid!
  Native.b2Body_GetMass(@id)
end

#positionObject



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

def position
  ensure_valid!
  ValueConversion.vec2(Native.b2Body_GetPosition(@id))
end

#position=(value) ⇒ Object



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

def position=(value)
  ensure_valid!
  Native.b2Body_SetTransform(@id, ValueConversion.native_vec2(value, label: "position"), Native.b2Body_GetRotation(@id))
end

#register_joint(joint) ⇒ Object



157
158
159
160
# File 'lib/box2d/body.rb', line 157

def register_joint(joint)
  @joints << joint
  joint
end

#typeObject



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

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

#type=(value) ⇒ Object



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

def type=(value)
  ensure_valid!
  Native.b2Body_SetType(@id, TYPE_VALUES.fetch(value) { raise ArgumentError, "invalid body type: #{value.inspect}" })
end

#unregister_chain(chain) ⇒ Object



153
154
155
# File 'lib/box2d/body.rb', line 153

def unregister_chain(chain)
  @chains.delete(chain)
end

#unregister_joint(joint) ⇒ Object



162
163
164
# File 'lib/box2d/body.rb', line 162

def unregister_joint(joint)
  @joints.delete(joint)
end

#unregister_shape(shape) ⇒ Object



149
150
151
# File 'lib/box2d/body.rb', line 149

def unregister_shape(shape)
  @shapes.delete(shape)
end

#user_dataObject



139
140
141
142
# File 'lib/box2d/body.rb', line 139

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

#user_data=(value) ⇒ Object



144
145
146
147
# File 'lib/box2d/body.rb', line 144

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