Class: MSPhysics::Body

Inherits:
Entity
  • Object
show all
Defined in:
RubyExtension/MSPhysics/body.rb

Overview

The Body class represents a physics body in simulation. Every body in simulation is designed to have its own Body object.

Since:

  • 1.0.0

Force Control Functions collapse

Contact Related Functions collapse

Joint Associated Functions collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#inspect, #to_s

Constructor Details

#initialize(world, entity, shape_id, offset_tra, type_id) ⇒ Body #initialize(body, transformation, reapply_forces, type_id) ⇒ Body

Returns a new instance of Body

Overloads:

  • #initialize(world, entity, shape_id, offset_tra, type_id) ⇒ Body
    Note:

    The specified entity must have all axis perpendicular to each other; otherwise, it is considered invalid. The entity will also be considered invalid if its collision shape turns out flat (not considering “null” or “static_mesh” collision shapes).

    Create a new body.

    Parameters:

    • world (World)
    • entity (Sketchup::Group, Sketchup::ComponentInstance)
    • shape_id (Fixnum)

      Shape ID. See SHAPE_NAMES

    • offset_tra (Geom::Transformation, nil)

      A local transform to apply to the collision.

    • type_id (Fixnum)

      Body type: 0 -> dynamic; 1 -> kinematic.

    Raises:

    • (TypeError)

      if the specified world is invalid.

    • (TypeError)

      if the specified entity is invalid.

    • (TypeError)

      if the specified collision shape is invalid.

  • #initialize(body, transformation, reapply_forces, type_id) ⇒ Body

    Create a clone of an existing body.

    Parameters:

    • body (Body)

      A body Object.

    • transformation (Geom::Transformation, Array<Numeric>, nil)

      New transformation matrix or nil to create new body at the current location.

    • reapply_forces (Boolean)

      Whether to reapply force and torque.

    • type_id (Fixnum)

      Body type: 0 -> dynamic; 1 -> kinematic.

    Raises:

    • (TypeError)

      if the specified body is invalid.

    • (TypeError)

      if the specified transformation matrix is not acceptable.

Since:

  • 1.0.0


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'RubyExtension/MSPhysics/body.rb', line 150

def initialize(*args)
  if args.size == 5
    MSPhysics::World.validate(args[0])
    MSPhysics::Collision.validate_entity(args[1])
    @group = args[1]
    collision = MSPhysics::Collision.create(args[0], @group, args[2], args[3])
    @address = MSPhysics::Newton::Body.create(args[0].address, collision, @group.transformation, args[4], args[0].default_material_id, @group)
    if args[2] == 0
      bb = AMS::Group.get_bounding_box_from_faces(@group, true, nil, &MSPhysics::Collision::ENTITY_VALIDATION_PROC)
      scale = AMS::Geometry.get_matrix_scale(@group.transformation)
      c = bb.center
      c.x *= scale.x
      c.y *= scale.y
      c.z *= scale.z
      MSPhysics::Newton::Body.set_centre_of_mass(@address, c)
      if bb.width.to_f < MSPhysics::EPSILON || bb.height.to_f < MSPhysics::EPSILON || bb.depth.to_f < MSPhysics::EPSILON
        MSPhysics::Newton::Body.set_volume(@address, 1.0)
      else
        v = bb.width * bb.height * bb.depth * 0.0254**3
        MSPhysics::Newton::Body.set_volume(@address, v)
      end
    end
    MSPhysics::Newton::Collision.destroy(collision)
  elsif args.size == 4
    # Create a clone of an existing body.
    Body.validate(args[0])
    orig_group = args[0].group
    unless orig_group.valid?
      raise(TypeError, 'The specified body references a deleted group/component. Copying bodies with invalid linked entities is not acceptable!', caller)
    end
    definition = AMS::Group.get_definition(orig_group)
    @group = Sketchup.active_model.entities.add_instance(definition, orig_group.transformation)
    @group.name = orig_group.name
    @group.layer = orig_group.layer
    @group.material = orig_group.material
    @address = MSPhysics::Newton::Body.copy(args[0].address, args[1], args[2], args[3], @group)
    @group.transformation = MSPhysics::Newton::Body.get_matrix(@address)
    # Preserve source definition
    if MSPhysics::Replay.record_enabled? && @group.is_a?(Sketchup::Group)
      MSPhysics::Replay.preset_definition(@group, definition)
    end
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 4..5 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_user_data(@address, self)
  @context = MSPhysics::BodyContext.new(self)
  @look_at_joint = nil
  @attached_bodies = {}
end

Class Method Details

.all_bodiesArray<Body>

Note:

Bodies that do not have a MSPhysics::Body instance are not included in the array.

Get all bodies.

Returns:

Since:

  • 1.0.0


119
120
121
# File 'RubyExtension/MSPhysics/body.rb', line 119

def all_bodies
  MSPhysics::Newton.get_all_bodies() { |ptr, data| data.is_a?(MSPhysics::Body) ? data : nil }
end

.bodies_aabb_overlap?(body1, body2) ⇒ Boolean

Determine if the bounding boxes of two bodies overlap.

Parameters:

Returns:

  • (Boolean)

Since:

  • 1.0.0


71
72
73
74
# File 'RubyExtension/MSPhysics/body.rb', line 71

def bodies_aabb_overlap?(body1, body2)
  validate2(body1, body2)
  MSPhysics::Newton::Bodies.aabb_overlap?(body1.address, body2.address)
end

.bodies_collidable?(body1, body2) ⇒ Boolean

Determine if two bodies can collide with each other.

Parameters:

Returns:

  • (Boolean)

Since:

  • 1.0.0


80
81
82
83
# File 'RubyExtension/MSPhysics/body.rb', line 80

def bodies_collidable?(body1, body2)
  validate2(body1, body2)
  MSPhysics::Newton::Bodies.collidable?(body1.address, body2.address)
end

.bodies_touching?(body1, body2) ⇒ Boolean

Determine if two bodies are in contact.

Parameters:

Returns:

  • (Boolean)

Since:

  • 1.0.0


89
90
91
92
# File 'RubyExtension/MSPhysics/body.rb', line 89

def bodies_touching?(body1, body2)
  validate2(body1, body2)
  MSPhysics::Newton::Bodies.touching?(body1.address, body2.address)
end

.body_by_address(address) ⇒ Body?

Get body by body address.

Parameters:

  • address (Fixnum)

Returns:

Since:

  • 1.0.0


62
63
64
65
# File 'RubyExtension/MSPhysics/body.rb', line 62

def body_by_address(address)
  data = MSPhysics::Newton::Body.get_user_data(address.to_i)
  data.is_a?(MSPhysics::Body) ? data : nil
end

.closest_points(body1, body2) ⇒ Array<Geom::Point3d>?

Note:

This works with convex and compound bodies only. Nil will be returned if one the passed bodies have a static mesh or a null collision.

Get closest collision points between two bodies.

Parameters:

Returns:

  • (Array<Geom::Point3d>, nil)

    [contact_pt1, contact_pt2]

Since:

  • 1.0.0


101
102
103
104
# File 'RubyExtension/MSPhysics/body.rb', line 101

def closest_points(body1, body2)
  validate2(body1, body2)
  MSPhysics::Newton::Bodies.get_closest_points(body1.address, body2.address)
end

.force_between_bodies(body1, body2) ⇒ Geom::Vector3d

Get contact force between two bodies.

Parameters:

Returns:

  • (Geom::Vector3d)

    force in newtons (kg*m/s/s).

Since:

  • 1.0.0


110
111
112
113
# File 'RubyExtension/MSPhysics/body.rb', line 110

def force_between_bodies(body1, body2)
  validate2(body1, body2)
  MSPhysics::Newton::Bodies.get_force_in_between(body1.address, body2.address)
end

.validate(body, world = nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Verify that body is valid.

Parameters:

  • body (Body)
  • world (World, nil) (defaults to: nil)

    A world the body ought to belong to or nil.

Raises:

  • (TypeError)

    if body is invalid or destroyed.

Since:

  • 1.0.0


15
16
17
18
19
20
21
22
23
24
25
26
# File 'RubyExtension/MSPhysics/body.rb', line 15

def validate(body, world = nil)
  AMS.validate_type(body, MSPhysics::Body)
  unless body.valid?
    raise(TypeError, "Body #{body} is invalid/destroyed!", caller)
  end
  if world != nil
    AMS.validate_type(world, MSPhysics::World)
    if body.world.address != world.address
      raise(TypeError, "Body #{body} belongs to a different world!", caller)
    end
  end
end

.validate2(body1, body2, world = nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Verify that two bodies are valid and unique.

Parameters:

  • body1 (Body)
  • body2 (Body)
  • world (World, nil) (defaults to: nil)

    A world the body ought to belong to or nil.

Raises:

  • (TypeError)

    if at least one body is invalid or destroyed.

  • (TypeError)

    if both bodies link to the same address.

Since:

  • 1.0.0


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'RubyExtension/MSPhysics/body.rb', line 36

def validate2(body1, body2, world = nil)
  AMS.validate_type(body1, MSPhysics::Body)
  AMS.validate_type(body2, MSPhysics::Body)
  unless body1.valid?
    raise(TypeError, "Body1 #{body1} is invalid/destroyed!", caller)
  end
  unless body2.valid?
    raise(TypeError, "Body1 #{body1} is invalid/destroyed!", caller)
  end
  if body1.address == body2.address
    raise(TypeError, "Body1 #{body1} and body2 #{body2} link to the same address. Expected two unique bodies!", caller)
  end
  if world != nil
    AMS.validate_type(world, MSPhysics::World)
    if body1.world.address != world.address
      raise(TypeError, "Body1 #{body1} belongs to a different world!", caller)
    end
    if body2.world.address != world.address
      raise(TypeError, "Body2 #{body2} belongs to a different world!", caller)
    end
  end
end

Instance Method Details

#aabbGeom::BoundingBox

Get world axes aligned bounding box (AABB) of the body.

Returns:

  • (Geom::BoundingBox)

Since:

  • 1.0.0


833
834
835
836
837
# File 'RubyExtension/MSPhysics/body.rb', line 833

def aabb
  bb = Geom::BoundingBox.new
  bb.add MSPhysics::Newton::Body.get_aabb(@address)
  bb
end

#actual_matrix_scaleGeom::Vector3d

Get scale of the body matrix that is a product of group scale and collision scale.

Returns:

  • (Geom::Vector3d)

    A vector representing the local X-axis, Y-axis, and Z-axis scale factors of the actual body matrix.

Since:

  • 1.0.0


1285
1286
1287
# File 'RubyExtension/MSPhysics/body.rb', line 1285

def actual_matrix_scale
  MSPhysics::Newton::Body.get_actual_matrix_scale(@address)
end

#add_force(force) ⇒ Boolean #add_force(fx, fy, fz) ⇒ Boolean

Note:

Unlike the #set_force, this function doesn't overwrite original force, but rather adds force to the force accumulator.

Apply force on the body in Newtons (kg * m/s/s).

Overloads:

  • #add_force(force) ⇒ Boolean

    Parameters:

    • force (Geom::Vector3d, Array<Numeric>)
  • #add_force(fx, fy, fz) ⇒ Boolean

    Parameters:

    • fx (Numeric)
    • fy (Numeric)
    • fz (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


949
950
951
952
953
954
955
956
957
958
# File 'RubyExtension/MSPhysics/body.rb', line 949

def add_force(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.add_force(@address, data)
end

#add_impulse(center, delta_vel, timestep) ⇒ Boolean

Add an impulse to a specific point on the body.

Parameters:

  • center (Geom::Point3d, Array<Numeric>)

    The center of an impulse in global space.

  • delta_vel (Geom::Vector3d, Array<Numeric>)

    The desired change in velocity. The magnitude of velocity is assumed in meters per second (m/s).

  • timestep (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


925
926
927
# File 'RubyExtension/MSPhysics/body.rb', line 925

def add_impulse(center, delta_vel, timestep)
  MSPhysics::Newton::Body.add_impulse(@address, center, delta_vel, timestep)
end

#add_point_force(point, force) ⇒ Boolean

Add force to a specific point on the body.

Parameters:

  • point (Geom::Point3d, Array<Numeric>)

    Point on the body in global space.

  • force (Geom::Vector3d, Array<Numeric>)

    The magnitude of force is assumed in newtons (kg*m/s/s).

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


913
914
915
# File 'RubyExtension/MSPhysics/body.rb', line 913

def add_point_force(point, force)
  MSPhysics::Newton::Body.add_point_force(@address, point, force)
end

#add_torque(torque) ⇒ Boolean #add_torque(tx, ty, tz) ⇒ Boolean

Note:

Unlike the #set_torque, this function doesn't overwrite original torque, but rather adds torque to the torque accumulator.

Apply torque on the body in Newton-meters (kg * m/s/s * m).

Overloads:

  • #add_torque(torque) ⇒ Boolean

    Parameters:

    • torque (Geom::Vector3d, Array<Numeric>)
  • #add_torque(tx, ty, tz) ⇒ Boolean

    Parameters:

    • tx (Numeric)
    • ty (Numeric)
    • tz (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'RubyExtension/MSPhysics/body.rb', line 999

def add_torque(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.add_torque(@address, data)
end

#addressFixnum

Get pointer to the body.

Returns:

  • (Fixnum)

Since:

  • 1.0.0


208
209
210
# File 'RubyExtension/MSPhysics/body.rb', line 208

def address
  @address
end

#apply_aerodynamics(drag, wind) ⇒ Boolean

Note:

WIP

Apply fluid resistance to the body. The resistance force and torque is based upon the body's linear and angular velocity, orientation of its collision faces, and the drag coefficient.

Examples:

onUpdate {
  drag = 0.25 # drag coefficient of air
  wind = Geom::Vector3d.new(5,0,0) # 5 m/s along the X-axis
  this.apply_aerodynamics(drag, wind)
}

Parameters:

  • drag (Numeric)

    Drag coefficient.

  • wind (Geom::Vector3d)

    Velocity of the wind in meters per second.

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


1179
1180
1181
# File 'RubyExtension/MSPhysics/body.rb', line 1179

def apply_aerodynamics(drag, wind)
  MSPhysics::Newton::Body.apply_aerodynamics(@address, drag, wind)
end

#apply_buoyancy(plane_origin, plane_normal, density, linear_viscosity, angular_viscosity, linear_current, angular_current, timestep) ⇒ Boolean

Apply buoyancy to the body.

Examples:

onUpdate {
  plane_origin = Geom::Point3d.new(0,0,100)
  plane_normal = Z_AXIS
  density = 997.04 # density of water
  linear_viscosity = 0.01
  angular_viscosity = 0.01
  linear_current = Geom::Vector3d.new(5,0,0) # 5 m/s along the X-axis
  angular_current = Geom::Vector3d.new(0,0,0)
  timestep = simulation.update_timestep
  this.apply_buoyancy(plane_origin, plane_normal, density, linear_viscosity, angular_viscosity, linear_current, angular_current, timestep)
}

Parameters:

  • plane_origin (Geom::Point3d, Array<Numeric>)

    Plane origin.

  • plane_normal (Geom::Vector3d, Array<Numeric>)

    Plane normal.

  • density (Numeric)

    Fluid density in kilograms per cubic meter (kg/m^3).

  • linear_viscosity (Numeric)

    Linear viscosity, a value between 0.0 and 1.0.

  • angular_viscosity (Numeric)

    Angular viscosity, a value between 0.0 and 1.0.

  • linear_current (Geom::Vector3d, Array<Numeric>)

    Velocity of the fluid global space.

  • angular_current (Geom::Vector3d, Array<Numeric>)

    Omega of the fluid global space.

  • timestep (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


1162
1163
1164
# File 'RubyExtension/MSPhysics/body.rb', line 1162

def apply_buoyancy(plane_origin, plane_normal, density, linear_viscosity, angular_viscosity, linear_current, angular_current, timestep)
  MSPhysics::Newton::Body.apply_buoyancy(@address, plane_origin, plane_normal, density, linear_viscosity, angular_viscosity, linear_current, angular_current, timestep)
end

#apply_pick_and_drag(pick_pt, dest_pt, stiffness, damp, timestep) ⇒ Boolean

Apply pick and drag to the body.

Parameters:

  • pick_pt (Geom::Point3d, Array<Numeric>)

    Pick point, usually on the surface of the body, in global space.

  • dest_pt (Geom::Point3d, Array<Numeric>)

    Destination point in global space.

  • stiffness (Numeric)

    Stiffness, a value b/w 0.0 and 1.0.

  • damp (Numeric)

    Damper, a value b/w 0.0 and 1.0.

  • timestep (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


1131
1132
1133
# File 'RubyExtension/MSPhysics/body.rb', line 1131

def apply_pick_and_drag(pick_pt, dest_pt, stiffness, damp, timestep)
  MSPhysics::Newton::Body.apply_pick_and_drag(@address, pick_pt, dest_pt, stiffness, damp, timestep)
end

#attach(body) ⇒ MSPhysics::Fixed

Attach a particular body to this body with a Fixed joint.

Parameters:

Returns:

See Also:

Since:

  • 1.0.0


1333
1334
1335
1336
1337
1338
1339
1340
1341
# File 'RubyExtension/MSPhysics/body.rb', line 1333

def attach(body)
  Body.validate2(self, body, self.world)
  joint = @attached_bodies[body]
  return joint if joint && joint.valid?
  joint = MSPhysics::Fixed.new(self.world, self, body.get_position(1))
  joint.connect(body)
  @attached_bodies[body] = joint
  joint
end

#attached?(body) ⇒ Boolean

Determine whether a particular body is attached to this body.

Parameters:

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0


1384
1385
1386
1387
# File 'RubyExtension/MSPhysics/body.rb', line 1384

def attached?(body)
  joint = @attached_bodies[body]
  (joint && joint.valid?) ? true : false
end

#auto_sleep_enabled=(state) ⇒ Object

Note:

Keeping auto sleep on is a huge performance plus for simulation. Auto sleep enabled is the default state for a created body; however, for player control, AI control or some other special circumstance, the application may want to control the activation/deactivation of the body.

Set auto sleep state of the body. Auto sleep enables body to automatically go to sleep mode when at rest or become active when activated.

Parameters:

  • state (Boolean)

    true to set body auto-sleep on, or false to set body auto-sleep off.

Since:

  • 1.0.0


625
626
627
# File 'RubyExtension/MSPhysics/body.rb', line 625

def auto_sleep_enabled=(state)
  MSPhysics::Newton::Body.set_auto_sleep_state(@address, state)
end

#auto_sleep_enabled?Boolean

Get the auto-sleep state of the body.

Returns:

  • (Boolean)

    true if body auto-sleep is on, false if body auto-sleep is off.

Since:

  • 1.0.0


613
614
615
# File 'RubyExtension/MSPhysics/body.rb', line 613

def auto_sleep_enabled?
  MSPhysics::Newton::Body.get_auto_sleep_state(@address)
end

#clear_non_collidable_bodiesFixnum

Remove all bodies from the non-collidable list; the bodies that were set non-collidable by the #set_non_collidable_with function.

Returns:

  • (Fixnum)

    The number of bodies unmarked.

Since:

  • 1.0.0


659
660
661
# File 'RubyExtension/MSPhysics/body.rb', line 659

def clear_non_collidable_bodies
  MSPhysics::Newton::Body.clear_non_collidable_bodies(@address)
end

#collidable=(state) ⇒ Object

Set body collidable.

Parameters:

  • state (Boolean)

    true to set body collidable, false to set body non-collidable.

Since:

  • 1.0.0


578
579
580
# File 'RubyExtension/MSPhysics/body.rb', line 578

def collidable=(state)
  MSPhysics::Newton::Body.set_collidable(@address, state)
end

#collidable?Boolean

Determine whether body is collidable.

Returns:

  • (Boolean)

Since:

  • 1.0.0


571
572
573
# File 'RubyExtension/MSPhysics/body.rb', line 571

def collidable?
  MSPhysics::Newton::Body.is_collidable?(@address)
end

#collision_addressFixnum

Get pointer to the collision associated with the body.

Returns:

  • (Fixnum)

Since:

  • 1.0.0


214
215
216
# File 'RubyExtension/MSPhysics/body.rb', line 214

def collision_address
  MSPhysics::Newton::Body.get_collision(@address)
end

#collision_facesArray<Array<Geom::Point3d>>

Get collision faces of the body.

Returns:

  • (Array<Array<Geom::Point3d>>)

    An array of faces. Each face represents an array of points. Points are coordinated in global space.

Since:

  • 1.0.0


1097
1098
1099
# File 'RubyExtension/MSPhysics/body.rb', line 1097

def collision_faces
  MSPhysics::Newton::Body.get_collision_faces(@address)
end

#collision_faces2Array<Array<(Array<Geom::Point3d>, Geom::Point3d, Geom::Vector3d, Numeric)>>

Get collision faces of the body.

Returns:

  • (Array<Array<(Array<Geom::Point3d>, Geom::Point3d, Geom::Vector3d, Numeric)>>)

    An array of face data. Each face data contains four elements. The first element contains the array of face vertices (sorted counterclockwise) in global space. The second element represents face centroid in global space. The third element represents face normal in global space. And the last element represents face area in inches squared.

Since:

  • 1.0.0


1108
1109
1110
# File 'RubyExtension/MSPhysics/body.rb', line 1108

def collision_faces2
  MSPhysics::Newton::Body.get_collision_faces2(@address)
end

#collision_faces3Array<Array<(Geom::Point3d, Geom::Vector3d, Numeric)>>

Get collision faces of the body.

Returns:

  • (Array<Array<(Geom::Point3d, Geom::Vector3d, Numeric)>>)

    An array of face data. Each face data contains three elements. The first element represents face centroid in global space. The second element represents face normal in global space. And the last element represents face area in inches squared.

Since:

  • 1.0.0


1118
1119
1120
# File 'RubyExtension/MSPhysics/body.rb', line 1118

def collision_faces3
  MSPhysics::Newton::Body.get_collision_faces3(@address)
end

#connected_bodiesArray<Body>

Get all bodies connected to this body through joints.

Returns:

Since:

  • 1.0.0


1235
1236
1237
1238
1239
# File 'RubyExtension/MSPhysics/body.rb', line 1235

def connected_bodies
  MSPhysics::Newton::Body.get_connected_bodies(@address) { |ptr, data|
    data.is_a?(MSPhysics::Body) ? data : nil
  }
end

#connected_jointsArray<Joint, DoubleJoint>

Get joints whose child bodies associate to this body.

Returns:

  • (Array<Joint, DoubleJoint>)

Since:

  • 1.0.0


1227
1228
1229
1230
1231
# File 'RubyExtension/MSPhysics/body.rb', line 1227

def connected_joints
  MSPhysics::Newton::Body.get_connected_joints(@address) { |ptr, data|
    data.is_a?(MSPhysics::Joint) || data.is_a?(MSPhysics::DoubleJoint) ? data : nil
  }
end

#contact_points(inc_non_collidable) ⇒ Array<Geom::Point3d>

Get all contact points on the body.

Parameters:

  • inc_non_collidable (Boolean)

    Whether to included contacts from non-collidable bodies.

Returns:

  • (Array<Geom::Point3d>)

Since:

  • 1.0.0


1088
1089
1090
# File 'RubyExtension/MSPhysics/body.rb', line 1088

def contact_points(inc_non_collidable)
  MSPhysics::Newton::Body.get_contact_points(@address, inc_non_collidable)
end

#contacts(inc_non_collidable) ⇒ Array<Contact>

Get all contacts on the body.

Parameters:

  • inc_non_collidable (Boolean)

    Whether to include contacts from non-collidable bodies.

Returns:

Since:

  • 1.0.0


1061
1062
1063
1064
1065
# File 'RubyExtension/MSPhysics/body.rb', line 1061

def contacts(inc_non_collidable)
  MSPhysics::Newton::Body.get_contacts(@address, inc_non_collidable) { |ptr, data, point, normal, force, speed|
    data.is_a?(MSPhysics::Body) ? MSPhysics::Contact.new(data, point, normal, force, speed) : nil
  }
end

#contained_jointsArray<Joint, DoubleJoint>

Get joints whose parent bodies associate to this body.

Returns:

  • (Array<Joint, DoubleJoint>)

Since:

  • 1.0.0


1219
1220
1221
1222
1223
# File 'RubyExtension/MSPhysics/body.rb', line 1219

def contained_joints
  MSPhysics::Newton::Body.get_contained_joints(@address) { |ptr, data|
    data.is_a?(MSPhysics::Joint) || data.is_a?(MSPhysics::DoubleJoint) ? data : nil
  }
end

#contextBodyContext

Get the associated context.

Returns:

Since:

  • 1.0.0


239
240
241
# File 'RubyExtension/MSPhysics/body.rb', line 239

def context
  @context
end

#continuous_collision_check_enabled=(state) ⇒ Object

Note:

Continuous collision check is known to affect performance. Be cautions when using it. When performing box stacks it's better to reduce simulation update step, to 1/256 for instance, rather than enabling continuous collision check as smaller update step will keep simulation running smoothly while avoiding penetration at the same time.

Enable/disable continuous collision check for this body. Continuous collision check prevents this body from passing through other bodies at high speeds.

Parameters:

  • state (Boolean)

    Pass true to enable continuous collision check; false to disable continuous collision check.

Since:

  • 1.0.0


271
272
273
# File 'RubyExtension/MSPhysics/body.rb', line 271

def continuous_collision_check_enabled=(state)
  MSPhysics::Newton::Body.set_continuous_collision_state(@address, state)
end

#continuous_collision_check_enabled?Boolean

Determine whether continuous collision check is enabled for this body. Continuous collision check prevents this body from passing through other bodies at high speeds.

Returns:

  • (Boolean)

    Returns true if continuous collision check is on; false if continuous collision check is off.

Since:

  • 1.0.0


257
258
259
# File 'RubyExtension/MSPhysics/body.rb', line 257

def continuous_collision_check_enabled?
  MSPhysics::Newton::Body.get_continuous_collision_state(@address)
end

#copy(reapply_forces, type) ⇒ Body #copy(transformation, reapply_forces, type) ⇒ Body

Create a copy of the body.

Overloads:

  • #copy(reapply_forces, type) ⇒ Body

    Parameters:

    • reapply_forces (Boolean)

      Whether to reapply force and torque.

    • type (Fixnum)

      Type: 0 -> dynamic; 1 -> kinematic.

  • #copy(transformation, reapply_forces, type) ⇒ Body

    Parameters:

    • transformation (Geom::Transformation, Array<Numeric>)

      New transformation matrix.

    • reapply_forces (Boolean)

      Whether to reapply force and torque.

    • type (Fixnum)

      Type: 0 -> dynamic; 1 -> kinematic.

    Raises:

    • (TypeError)

      if the specified transformation matrix is not uniform.

Returns:

  • (Body)

    A new body object.

Since:

  • 1.0.0


1195
1196
1197
1198
1199
1200
1201
1202
1203
# File 'RubyExtension/MSPhysics/body.rb', line 1195

def copy(*args)
  if args.size == 2
    Body.new(self, nil, args[0], args[1])
  elsif args.size == 3
    Body.new(self, args[0], args[1], args[2])
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 2..3 arguments but got #{args.size}.", caller)
  end
end

#default_collision_scaleGeom::Vector3d

Note:

Does not include group scale.

Get default scale of the body collision.

Returns:

  • (Geom::Vector3d)

    A vector representing the local, default X-axis, Y-axis, and Z-axis scale factors of the collision.

Since:

  • 1.0.0


1277
1278
1279
# File 'RubyExtension/MSPhysics/body.rb', line 1277

def default_collision_scale
  MSPhysics::Newton::Body.get_default_collision_scale(@address)
end

#densityNumeric

Get body density in kilograms per cubic meter (kg / m^3).

Returns:

  • (Numeric)

Since:

  • 1.0.0


523
524
525
# File 'RubyExtension/MSPhysics/body.rb', line 523

def density
  MSPhysics::Newton::Body.get_density(@address)
end

#density=(value) ⇒ Object

Note:

Density and mass are correlated. If you change density the mass will automatically be recalculated.

Set body density in kilograms per cubic meter (kg / m^3).

Parameters:

  • value (Numeric)

Since:

  • 1.0.0


531
532
533
# File 'RubyExtension/MSPhysics/body.rb', line 531

def density=(value)
  MSPhysics::Newton::Body.set_density(@address, value)
end

#destroy(erase_entity = false) ⇒ nil

Destroy the body.

Parameters:

  • erase_entity (Boolean) (defaults to: false)

    Whether to erase the group/component associated with the body.

Returns:

  • (nil)

Since:

  • 1.0.0


247
248
249
250
# File 'RubyExtension/MSPhysics/body.rb', line 247

def destroy(erase_entity = false)
  @group.erase! if @group.valid? && erase_entity
  MSPhysics::Newton::Body.destroy(@address)
end

#detach(body) ⇒ Boolean

Detach a particular attached body from this body.

Parameters:

Returns:

  • (Boolean)

    success

See Also:

Since:

  • 1.0.0


1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
# File 'RubyExtension/MSPhysics/body.rb', line 1349

def detach(body)
  Body.validate2(self, body, self.world)
  joint = @attached_bodies[body]
  if joint && joint.valid?
    joint.destroy
    @attached_bodies.delete(body)
    true
  else
    false
  end
end

#detach_allFixnum

Detach all attached bodies from this body.

Returns:

  • (Fixnum)

    Number of bodies detached.

See Also:

Since:

  • 1.0.0


1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
# File 'RubyExtension/MSPhysics/body.rb', line 1366

def detach_all
  count = 0
  @attached_bodies.each { |body, joint|
    if joint.valid?
      joint.destroy
      count += 1
    end
  }
  @attached_bodies.clear
  count
end

#elasticityNumeric

Get body coefficient of restitution - bounciness - rebound ratio.

Returns:

  • (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


665
666
667
# File 'RubyExtension/MSPhysics/body.rb', line 665

def elasticity
  MSPhysics::Newton::Body.get_elasticity(@address)
end

#elasticity=(coefficient) ⇒ Object

Set body coefficient of restitution - bounciness - rebound ratio.

Examples:

A basketball has a rebound ratio of 0.83. This means the new

height of a basketball is 83% of original height within each bounce.

Parameters:

  • coefficient (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


673
674
675
# File 'RubyExtension/MSPhysics/body.rb', line 673

def elasticity=(coefficient)
  MSPhysics::Newton::Body.set_elasticity(@address, coefficient)
end

#friction_enabled=(state) ⇒ Object

Set friction state of the body.

Parameters:

  • state (Boolean)

    true to enable body friction, false to disable body friction.

Since:

  • 1.0.0


723
724
725
# File 'RubyExtension/MSPhysics/body.rb', line 723

def friction_enabled=(state)
  MSPhysics::Newton::Body.set_friction_state(@address, state)
end

#friction_enabled?Boolean

Get friction state of the body.

Returns:

  • (Boolean)

    true if body friction is enabled, false if body friction is disabled.

Since:

  • 1.0.0


716
717
718
# File 'RubyExtension/MSPhysics/body.rb', line 716

def friction_enabled?
  MSPhysics::Newton::Body.get_friction_state(@address)
end

#frozen=(state) ⇒ Object

Set body collidable.

Parameters:

  • state (Boolean)

    true to freeze the body, false unfreeze the body.

Since:

  • 1.0.0


591
592
593
# File 'RubyExtension/MSPhysics/body.rb', line 591

def frozen=(state)
  MSPhysics::Newton::Body.set_frozen(@address, state)
end

#frozen?Boolean

Determine whether body is frozen.

Returns:

  • (Boolean)

Since:

  • 1.0.0


584
585
586
# File 'RubyExtension/MSPhysics/body.rb', line 584

def frozen?
  MSPhysics::Newton::Body.is_frozen?(@address)
end

#get_angular_dampingGeom::Vetor3d

Get the viscous damping coefficient applied to the omega of the body.

Returns:

  • (Geom::Vetor3d)

    A vector representing the local damping coefficients of the axes, each between 0.0 and 1.0.

Since:

  • 1.0.0


872
873
874
# File 'RubyExtension/MSPhysics/body.rb', line 872

def get_angular_damping
  MSPhysics::Newton::Body.get_angular_damping(@address)
end

#get_centre_of_massGeom::Point3d

Get centre of mass of the body in local coordinates.

Examples:

Getting centre of mass in global space.

centre = body.get_cetre_of_mass.transform( body.get_matrix )

Returns:

  • (Geom::Point3d)

See Also:

Since:

  • 1.0.0


457
458
459
# File 'RubyExtension/MSPhysics/body.rb', line 457

def get_centre_of_mass
  MSPhysics::Newton::Body.get_centre_of_mass(@address)
end

#get_collision_scaleGeom::Vector3d

Note:

Does not include group scale.

Get body collision scale.

Returns:

  • (Geom::Vector3d)

    A vector representing the local X-axis, Y-axis, and Z-axis scale factors of the collision.

Since:

  • 1.0.0


1245
1246
1247
# File 'RubyExtension/MSPhysics/body.rb', line 1245

def get_collision_scale
  MSPhysics::Newton::Body.get_collision_scale(@address)
end

#get_euler_anglesGeom::Vector3d

Get body orientation in form of the three Euler angles.

Returns:

  • (Geom::Vector3d)

    An vector of three Euler angles expressed in radians: (roll, yaw, pitch).

Since:

  • 1.0.0


350
351
352
# File 'RubyExtension/MSPhysics/body.rb', line 350

def get_euler_angles
  MSPhysics::Newton::Body.get_euler_angles(@address)
end

#get_forceGeom::Vector3d

Note:

This does not include contact and joint reaction forces.

Get the net force, in Newtons, applied on the body after the last world update.

Returns:

  • (Geom::Vector3d)

Since:

  • 1.0.0


935
936
937
# File 'RubyExtension/MSPhysics/body.rb', line 935

def get_force
  MSPhysics::Newton::Body.get_force(@address)
end

#get_linear_dampingGeom::Vetor3d

Get the viscous damping coefficient applied to the velocity of the body.

Returns:

  • (Geom::Vetor3d)

    A vector representing the local damping coefficients of the axes, each between 0.0 and 1.0.

Since:

  • 1.0.0


842
843
844
# File 'RubyExtension/MSPhysics/body.rb', line 842

def get_linear_damping
  MSPhysics::Newton::Body.get_linear_damping(@address)
end

#get_matrixGeom::Transformation

Get body transformation matrix.

Returns:

  • (Geom::Transformation)

Since:

  • 1.0.0


283
284
285
# File 'RubyExtension/MSPhysics/body.rb', line 283

def get_matrix
  MSPhysics::Newton::Body.get_matrix(@address)
end

#get_non_collidable_bodiesArray<Body>

Get all bodies that are non-collidable with this body; the bodies that were set non-collidable by the #set_non_collidable_with function.

Returns:

  • (Array<Body>)

    An array of non-collidable bodies.

Since:

  • 1.0.0


650
651
652
653
654
# File 'RubyExtension/MSPhysics/body.rb', line 650

def get_non_collidable_bodies
  MSPhysics::Newton::Body.get_non_collidable_bodies(@address) { |ptr, data|
    data.is_a?(MSPhysics::Body) ? data : nil
  }
end

#get_omegaGeom::Vector3d

Get global angular velocity of the body.

Examples:

Each value of the omega vector represents angular velocity in radians
per second along X-axis, Y-axis, or Z-axis in global space. For example,
if omega of a body is (0,0,PI), it means that the body rotates along
Z-axis in global space at an angular velocity of 360 degrees per second.

Returns:

  • (Geom::Vector3d)

    The magnitude of the omega vector is represented in radians per second.

Since:

  • 1.0.0


428
429
430
# File 'RubyExtension/MSPhysics/body.rb', line 428

def get_omega
  MSPhysics::Newton::Body.get_omega(@address)
end

#get_position(mode = 0) ⇒ Geom::Point3d

Get body position.

Parameters:

  • mode (Fixnum) (defaults to: 0)
    • 0 - get body's origin in global space.

    • 1 - get body's centre of mass in global space.

Returns:

  • (Geom::Point3d)

Since:

  • 1.0.0


302
303
304
# File 'RubyExtension/MSPhysics/body.rb', line 302

def get_position(mode = 0)
  MSPhysics::Newton::Body.get_position(@address, mode.to_i)
end

#get_torqueGeom::Vector3d

Note:

This does not include contact and joint reaction torques.

Get the net torque, in Newton-meters, applied on the body after the last world update.

Returns:

  • (Geom::Vector3d)

Since:

  • 1.0.0


985
986
987
# File 'RubyExtension/MSPhysics/body.rb', line 985

def get_torque
  MSPhysics::Newton::Body.get_torque(@address)
end

#get_velocityGeom::Vector3d

Get global linear velocity of the body.

Returns:

  • (Geom::Vector3d)

    The magnitude of the velocity vector is represented in meters per second.

Since:

  • 1.0.0


378
379
380
# File 'RubyExtension/MSPhysics/body.rb', line 378

def get_velocity
  MSPhysics::Newton::Body.get_velocity(@address)
end

#gravity_enabled=(state) ⇒ Object

Enable/disable gravitational force on this body.

Parameters:

  • state (Boolean)

Since:

  • 1.0.0


1207
1208
1209
# File 'RubyExtension/MSPhysics/body.rb', line 1207

def gravity_enabled=(state)
  MSPhysics::Newton::Body.enable_gravity(@address, state)
end

#gravity_enabled?Boolean

Determine if gravitational force is enabled on this body.

Returns:

  • (Boolean)

Since:

  • 1.0.0


1213
1214
1215
# File 'RubyExtension/MSPhysics/body.rb', line 1213

def gravity_enabled?
  MSPhysics::Newton::Body.is_gravity_enabled?(@address)
end

#groupSketchup::Group, Sketchup::ComponentInstance

Get the group/component associated with the body.

Returns:

  • (Sketchup::Group, Sketchup::ComponentInstance)

Since:

  • 1.0.0


220
221
222
# File 'RubyExtension/MSPhysics/body.rb', line 220

def group
  @group
end

#inertiaGeom::Vector3d

Note:

Inertia is the rotational equivalent of mass. It may be used for damping angular velocity through applying dissipative torque.

Note:

If the body is static or has a zero mass, the magnitude of the return inertia will be zero.

Get body local inertia.

Examples:

Applying angular damping

onUpdate {
  # Damping ratio, a value between 0.00 - 1.00
  ratio = 0.1
  # Current angular velocity
  omega = this.get_omega
  # Get the rotational equivalent of mass
  inertia = this.inertia
  # Dissipative torque
  torque = Geom::Vector3d.new(
    -omega.x * inertia.x * ratio,
    -omega.y * inertia.y * ratio,
    -omega.z * inertia.z * ratio)
  # Apply torque
  this.add_torque(torque)
}

Returns:

  • (Geom::Vector3d)

Since:

  • 1.0.0


503
504
505
# File 'RubyExtension/MSPhysics/body.rb', line 503

def inertia
  MSPhysics::Newton::Body.get_inertia(@address)
end

#integrate_velocity(timestep) ⇒ nil

Note:

For this function to have an effect, the body must be kinematic and have a non-zero mass (non-static).

Integrate linear and angular velocity of the body.

Examples:

# Assuming this body is kinematic
onStart {
  @velocity = Geom::Vector3d.new(2,0,0)
  this.set_velocity(2,0,0)
}
onPreUpdate {
  this.integrate_velocity(simulation.update_timestep)
}

Parameters:

  • timestep (Numeric)

    Timestep in seconds.

Returns:

  • (nil)

Since:

  • 1.0.0


416
417
418
# File 'RubyExtension/MSPhysics/body.rb', line 416

def integrate_velocity(timestep)
  MSPhysics::Newton::Body.integrate_velocity(@address, timestep)
end

#kinetic_frictionNumeric

Get kinetic friction coefficient of the body.

Returns:

  • (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


703
704
705
# File 'RubyExtension/MSPhysics/body.rb', line 703

def kinetic_friction
  MSPhysics::Newton::Body.get_kinetic_friction(@address)
end

#kinetic_friction=(coefficient) ⇒ Object

Set kinetic friction coefficient of the body.

Parameters:

  • coefficient (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


709
710
711
# File 'RubyExtension/MSPhysics/body.rb', line 709

def kinetic_friction=(coefficient)
  MSPhysics::Newton::Body.set_kinetic_friction(@address, coefficient)
end

#look_at(pin_dir, accel = 40, damp = 10, strength = 0.9) ⇒ MSPhysics::UpVector?

Make the body's Z-axis to look in a particular direction.

Examples:

onTick {
  if (key(' ') == 1)
    dir = this.group.transformation.origin.vector_to(ORIGIN)
    this.look_at(dir, 40, 10, 0.9)
  else
    this.look_at(nil)
  end
}

Parameters:

  • pin_dir (Geom::Vector3d, nil)

    Direction in global space. Pass nil to disable the look at constraint.

  • accel (Numeric) (defaults to: 40)

    Rotational oscillation stiffness.

  • damp (Numeric) (defaults to: 10)

    Rotational oscillation damping coefficient.

  • strength (Numeric) (defaults to: 0.9)

    Rotational oscillation strength.

Returns:

Since:

  • 1.0.0


1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'RubyExtension/MSPhysics/body.rb', line 1307

def look_at(pin_dir, accel = 40, damp = 10, strength = 0.9)
  if pin_dir.nil?
    if @look_at_joint && @look_at_joint.valid?
      @look_at_joint.destroy
      @look_at_joint = nil
    end
    return
  end
  pin_dir = Geom::Vector3d.new(pin_dir) unless pin_dir.is_a?(Geom::Vector3d)
  if @look_at_joint.nil? || !@look_at_joint.valid?
    @look_at_joint = MSPhysics::UpVector.new(self.world, nil, Geom::Transformation.new(ORIGIN, @group.transformation.zaxis))
    @look_at_joint.connect(self)
  end
  @look_at_joint.set_pin_dir(pin_dir.transform(@look_at_joint.get_pin_matrix.inverse))
  @look_at_joint.accel = accel
  @look_at_joint.damp = damp
  @look_at_joint.strength = strength
  return @look_at_joint
end

#magnet_forceNumeric

Note:

This option has an effect if and only if magnet_mode is set to 1.

Get the maximum magnet force in Newton to be applied on the surrounding magnetic bodies.

Returns:

  • (Numeric)

See Also:

Since:

  • 1.0.0


766
767
768
# File 'RubyExtension/MSPhysics/body.rb', line 766

def magnet_force
  MSPhysics::Newton::Body.get_magnet_force(@address)
end

#magnet_force=(force) ⇒ Object

Note:

This option has an effect if and only if magnet_mode is set to 1.

Set the maximum magnet force in Newton to be applied on the surrounding magnetic bodies.

Parameters:

  • force (Numeric)

See Also:

Since:

  • 1.0.0


775
776
777
# File 'RubyExtension/MSPhysics/body.rb', line 775

def magnet_force=(force)
  MSPhysics::Newton::Body.set_magnet_force(@address, force)
end

#magnet_modeFixnum

Get the mode for controlling the way this magnet should work.

  • 1; to have the magnet force be calculated with the following equation: actual_magnet_force = f * (d - r)^2 / r^2; where f is the maximum magnet force (in Newtons), d is the distance between this magnet and a magnetic body (in meters), and r is the maximum magnet range (in meters). In this mode, the magnet_force and magnet_range methods are used for controlling the magnet.

  • 2; to have magnet force be computed with a slightly different equation: actual_magnet_force = s / d^2; where s is the magnet strength and d is the distance between this magnet and a magnetic body (in meters). In this mode, the magnet_strength method must be used for controlling the magnet.

Returns:

  • (Fixnum)

    Returns one of the following values:

Since:

  • 1.0.0


740
741
742
# File 'RubyExtension/MSPhysics/body.rb', line 740

def magnet_mode
  MSPhysics::Newton::Body.get_magnet_mode(@address)
end

#magnet_mode=(mode) ⇒ Object

Set the mode for controlling the way this magnet should work.

  • Pass 1 to have the magnet force be calculated with the following equation: actual_magnet_force = f * (d - r)^2 / r^2; where f is the maximum magnet force (in Newtons), d is the distance between this magnet and a magnetic body (in meters), and r is the maximum magnet range (in meters). In this mode, the magnet_force and magnet_range methods are used for controlling the magnet.

  • Pass 2 to have magnet force be computed with a slightly different equation: actual_magnet_force = s / d^2; where s is the magnet strength and d is the distance between this magnet and a magnetic body (in meters). In this mode, the magnet_strength method must be used for controlling the magnet.

Parameters:

  • mode (Fixnum)

Since:

  • 1.0.0


757
758
759
# File 'RubyExtension/MSPhysics/body.rb', line 757

def magnet_mode=(mode)
  MSPhysics::Newton::Body.set_magnet_mode(@address, mode)
end

#magnet_rangeNumeric

Note:

This option has an effect if and only if magnet_mode is set to 1.

Get the maximum magnet range in meters. Magnet force is distributed along the magnet range. Magnetic bodies outside the magnet range are not affected.

Returns:

  • (Numeric)

See Also:

Since:

  • 1.0.0


785
786
787
# File 'RubyExtension/MSPhysics/body.rb', line 785

def magnet_range
  MSPhysics::Newton::Body.get_magnet_range(@address)
end

#magnet_range=(range) ⇒ Object

Note:

This option has an effect if and only if magnet_mode is set to 1.

Set the maximum magnet range in meters. Magnet force is distributed along the magnet range. Magnetic bodies outside the magnet range are not affected.

Parameters:

  • range (Numeric)

See Also:

Since:

  • 1.0.0


795
796
797
# File 'RubyExtension/MSPhysics/body.rb', line 795

def magnet_range=(range)
  MSPhysics::Newton::Body.set_magnet_range(@address, range)
end

#magnet_strengthNumeric

Note:

This option has an effect if and only if magnet_mode is set to 2.

Get the magnet force magnitude to be applied on the surrounding magnetic bodies.

Returns:

  • (Numeric)

See Also:

Since:

  • 1.0.0


804
805
806
# File 'RubyExtension/MSPhysics/body.rb', line 804

def magnet_strength
  MSPhysics::Newton::Body.get_magnet_strength(@address)
end

#magnet_strength=(magnitude) ⇒ Object

Note:

This option has an effect if and only if magnet_mode is set to 2.

Set the magnet force magnitude to be applied on the surrounding magnetic bodies.

Parameters:

  • magnitude (Numeric)

See Also:

Since:

  • 1.0.0


813
814
815
# File 'RubyExtension/MSPhysics/body.rb', line 813

def magnet_strength=(magnitude)
  MSPhysics::Newton::Body.set_magnet_strength(@address, magnitude)
end

#magnetic=(state) ⇒ Object

Set body magnetic. Magnetic bodies will be affected by other bodies with magnetism.

Parameters:

  • state (Boolean)

    true to set body magnetic, false to set body non-magnetic.

Since:

  • 1.0.0


827
828
829
# File 'RubyExtension/MSPhysics/body.rb', line 827

def magnetic=(state)
  MSPhysics::Newton::Body.set_magnetic(@address, state)
end

#magnetic?Boolean

Determine whether body is magnetic.

Returns:

  • (Boolean)

Since:

  • 1.0.0


819
820
821
# File 'RubyExtension/MSPhysics/body.rb', line 819

def magnetic?
  MSPhysics::Newton::Body.is_magnetic?(@address)
end

#massNumeric

Get body mass in kilograms (kg).

Returns:

  • (Numeric)

Since:

  • 1.0.0


509
510
511
# File 'RubyExtension/MSPhysics/body.rb', line 509

def mass
  MSPhysics::Newton::Body.get_mass(@address)
end

#mass=(value) ⇒ Object

Note:

Mass and density are correlated. If you change mass the density will automatically be recalculated.

Set body mass in kilograms (kg).

Parameters:

  • value (Numeric)

Since:

  • 1.0.0


517
518
519
# File 'RubyExtension/MSPhysics/body.rb', line 517

def mass=(value)
  MSPhysics::Newton::Body.set_mass(@address, value)
end

#net_contact_forceGeom::Vector3d

Get total force generated from contacts on the body.

Returns:

  • (Geom::Vector3d)

    Magnitude of the net force is retrieved in newtons (kg*m/s/s).

Since:

  • 1.0.0


1053
1054
1055
# File 'RubyExtension/MSPhysics/body.rb', line 1053

def net_contact_force
  MSPhysics::Newton::Body.get_net_contact_force(@address)
end

#net_joint_forceGeom::Vector3d

Get total linear tension, in Newtons, applied by contained and connected joints.

Returns:

  • (Geom::Vector3d)

Since:

  • 1.0.3


1038
1039
1040
# File 'RubyExtension/MSPhysics/body.rb', line 1038

def net_joint_force
  MSPhysics::Newton::Body.get_net_joint_tension1(@address)
end

#net_joint_torqueGeom::Vector3d

Get total angular tension, in Newton-meters, applied by contained and connected joints.

Returns:

  • (Geom::Vector3d)

Since:

  • 1.0.3


1046
1047
1048
# File 'RubyExtension/MSPhysics/body.rb', line 1046

def net_joint_torque
  MSPhysics::Newton::Body.get_net_joint_tension2(@address)
end

#non_collidable_with?(body) ⇒ Boolean

Determine whether this body is non-collidable with a particular body.

Parameters:

  • body (Body)

    The body to test.

Returns:

  • (Boolean)

    true if this body is non-collidable with the given body, false if this body is collidable with the given body.

Since:

  • 1.0.0


633
634
635
# File 'RubyExtension/MSPhysics/body.rb', line 633

def non_collidable_with?(body)
  MSPhysics::Newton::Body.is_non_collidable_with?(@address, body.address)
end

#normal_matrixGeom::transformation

Get body matrix with no scale factors.

Returns:

  • (Geom::transformation)

Since:

  • 1.0.0


277
278
279
# File 'RubyExtension/MSPhysics/body.rb', line 277

def normal_matrix
  MSPhysics::Newton::Body.get_normal_matrix(@address)
end

#point_velocity(point) ⇒ Geom::Vector3d

Get velocity at a specific point on the body.

Parameters:

  • point (Geom::Point3d, Array<Numeric>)

    A point in global space.

Returns:

  • (Geom::Vector3d)

    Velocity at the given point. The magnitude of velocity is retrieved in meters per second (m/s).

Since:

  • 1.0.0


903
904
905
# File 'RubyExtension/MSPhysics/body.rb', line 903

def point_velocity(point)
  MSPhysics::Newton::Body.get_point_velocity(@address, point)
end

#reset_mass_properties(density) ⇒ Boolean

Reset/recalculate body volume and mass.

Parameters:

  • density (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


552
553
554
# File 'RubyExtension/MSPhysics/body.rb', line 552

def reset_mass_properties(density)
  MSPhysics::Newton::Body.reset_mass_properties(@address, density)
end

#rotationArray<Numeric>

Get body orientation in form of the unit quaternion.

Returns:

  • (Array<Numeric>)

    An array of four numeric values: [q0, q1, q2, q3] - [x,y,z,w].

Since:

  • 1.0.0


343
344
345
# File 'RubyExtension/MSPhysics/body.rb', line 343

def rotation
  MSPhysics::Newton::Body.get_rotation(@address)
end

#set_angular_damping(damp) ⇒ nil #set_angular_damping(dx, dy, dz) ⇒ nil

Set the viscous damping coefficient applied to the omega of the body.

Overloads:

  • #set_angular_damping(damp) ⇒ nil

    Parameters:

    • damp (Geom::Vector3d, Array<Numeric>)

      Each value of the damp vector is assumed as an angular coefficient, a value b/w 0.0 and 1.0.

  • #set_angular_damping(dx, dy, dz) ⇒ nil

    Parameters:

    • dx (Numeric)

      Local X-axis damping coefficient, a value b/w 0.0 and 1.0.

    • dy (Numeric)

      Local Y-axis damping coefficient, a value b/w 0.0 and 1.0.

    • dz (Numeric)

      Local Z-axis damping coefficient, a value b/w 0.0 and 1.0.

Returns:

  • (nil)

Since:

  • 1.0.0


888
889
890
891
892
893
894
895
896
897
# File 'RubyExtension/MSPhysics/body.rb', line 888

def set_angular_damping(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_angular_damping(@address, data)
end

#set_centre_of_mass(centre) ⇒ nil #set_velocity(px, py, pz) ⇒ nil

Set centre of mass of the body in local coordinates.

Overloads:

  • #set_centre_of_mass(centre) ⇒ nil

    Parameters:

    • centre (Geom::Point3d, Array<Numeric>)

      A point is assumed in body coordinates.

  • #set_velocity(px, py, pz) ⇒ nil

    Parameters:

    • px (Numeric)

      X position in local space.

    • py (Numeric)

      Y position in local space.

    • pz (Numeric)

      Z position in local space.

Returns:

  • (nil)

Since:

  • 1.0.0


470
471
472
473
474
475
476
477
478
479
# File 'RubyExtension/MSPhysics/body.rb', line 470

def set_centre_of_mass(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_centre_of_mass(@address, data)
end

#set_collision_scale(scale) ⇒ nil #set_collision_scale(sx, sy, sz) ⇒ nil

Note:

Does not include group scale.

Set body collision scale.

Overloads:

  • #set_collision_scale(scale) ⇒ nil

    Parameters:

    • scale (Geom::Vector3d, Array<Numeric>)

      A vector representing the local X-axis, Y-axis, and Z-axis scale factors of the body collision.

  • #set_collision_scale(sx, sy, sz) ⇒ nil

    Parameters:

    • sx (Numeric)

      Scale along the local X-axis of the body, a value between 0.01 and 100.

    • sy (Numeric)

      Scale along the local Y-axis of the body, a value between 0.01 and 100.

    • sz (Numeric)

      Scale along the local Z-axis of the body, a value between 0.01 and 100.

Returns:

  • (nil)

Since:

  • 1.0.0


1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'RubyExtension/MSPhysics/body.rb', line 1262

def set_collision_scale(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_collision_scale(@address, data)
end

#set_euler_angles(angles) ⇒ nil #set_euler_angles(roll, yaw, pitch) ⇒ nil

Note:

The angles are assumed in radians.

Set body orientation via the three Euler angles.

Overloads:

  • #set_euler_angles(angles) ⇒ nil

    Parameters:

    • angles (Geom::Vector3d, Array<Numeric>)

      A vector representing the roll, yaw, and pitch Euler angles in radians.

  • #set_euler_angles(roll, yaw, pitch) ⇒ nil

    Parameters:

    • roll (Numeric)
    • yaw (Numeric)
    • pitch (Numeric)

Returns:

  • (nil)

Since:

  • 1.0.0


364
365
366
367
368
369
370
371
372
373
# File 'RubyExtension/MSPhysics/body.rb', line 364

def set_euler_angles(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_euler_angles(@address, data)
end

#set_force(force) ⇒ Boolean #set_force(fx, fy, fz) ⇒ Boolean

Note:

Unlike the #add_force, this function overwrites original force, thus discarding the previously applied force.

Apply force on the body in Newton (kg * m/s/s).

Overloads:

  • #set_force(force) ⇒ Boolean

    Parameters:

    • force (Geom::Vector3d, Array<Numeric>)
  • #set_force(fx, fy, fz) ⇒ Boolean

    Parameters:

    • fx (Numeric)
    • fy (Numeric)
    • fz (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


970
971
972
973
974
975
976
977
978
979
# File 'RubyExtension/MSPhysics/body.rb', line 970

def set_force(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_force(@address, data)
end

#set_linear_damping(damp) ⇒ Object #set_linear_damping(dx, dy, dz) ⇒ Object

Set the viscous damping coefficient applied to the velocity of the body.

Overloads:

  • #set_linear_damping(damp) ⇒ Object

    Parameters:

    • damp (Geom::Vector3d, Array<Numeric>)

      Each value of the damp vector is assumed as a local damping coefficient, a value b/w 0.0 and 1.0.

  • #set_linear_damping(dx, dy, dz) ⇒ Object

    Parameters:

    • dx (Numeric)

      Local X-axis damping coefficient, a value b/w 0.0 and 1.0.

    • dy (Numeric)

      Local Y-axis damping coefficient, a value b/w 0.0 and 1.0.

    • dz (Numeric)

      Local Z-axis damping coefficient, a value b/w 0.0 and 1.0.

Since:

  • 1.0.0


858
859
860
861
862
863
864
865
866
867
# File 'RubyExtension/MSPhysics/body.rb', line 858

def set_linear_damping(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_linear_damping(@address, data)
end

#set_matrix(matrix) ⇒ nil

Set body transformation matrix.

Parameters:

  • matrix (Geom::Transformation, Array<Numeric>)

Returns:

  • (nil)

Raises:

  • (TypeError)

    if some or all matrix axis are not perpendicular to each other.

  • (TypeError)

    if some or all matrix axis have a scale of zero.

Since:

  • 1.0.0


293
294
295
# File 'RubyExtension/MSPhysics/body.rb', line 293

def set_matrix(matrix)
  MSPhysics::Newton::Body.set_matrix(@address, matrix)
end

#set_non_collidable_with(body, state) ⇒ nil

Set this body non-collidable with a particular body.

Parameters:

  • body (Body)
  • state (Boolean)

    true to set this body non-collidable with another, false to set this body collidable with another.

Returns:

  • (nil)

Since:

  • 1.0.0


642
643
644
645
# File 'RubyExtension/MSPhysics/body.rb', line 642

def set_non_collidable_with(body, state)
  Body.validate2(self, body, self.world)
  MSPhysics::Newton::Body.set_non_collidable_with(@address, body.address, state)
end

#set_omega(omega) ⇒ nil #set_omega(vx, vy, vz) ⇒ nil

Set global angular velocity of the body.

Overloads:

  • #set_omega(omega) ⇒ nil

    Parameters:

    • omega (Geom::Vector3d, Array<Numeric>)

      The magnitude of the omega vector is assumed in radians per second.

  • #set_omega(vx, vy, vz) ⇒ nil

    Parameters:

    • vx (Numeric)

      Omega in radians per second along X-axis.

    • vy (Numeric)

      Omega in radians per second along Y-axis.

    • vz (Numeric)

      Omega in radians per second along Z-axis.

Returns:

  • (nil)

Since:

  • 1.0.0


441
442
443
444
445
446
447
448
449
450
# File 'RubyExtension/MSPhysics/body.rb', line 441

def set_omega(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_omega(@address, data)
end

#set_position(position, mode = 0) ⇒ nil #set_position(px, py, pz, mode = 0) ⇒ nil

Set body position.

Overloads:

  • #set_position(position, mode = 0) ⇒ nil

    Parameters:

    • position (Geom::Point3d, Array<Numeric>)

      A point in global space.

    • mode (Fixnum) (defaults to: 0)
      • 0 - reposition body origin to a desired location in global space.

      • 1 - reposition body centre of mass to a desired location in global space.

  • #set_position(px, py, pz, mode = 0) ⇒ nil

    Parameters:

    • px (Numeric)

      X position in global space.

    • py (Numeric)

      Y position in global space.

    • pz (Numeric)

      Z position in global space.

    • mode (Fixnum) (defaults to: 0)
      • 0 - reposition body origin to a desired location in global space.

      • 1 - reposition body centre of mass to a desired location in global space.

Returns:

  • (nil)

Since:

  • 1.0.0


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'RubyExtension/MSPhysics/body.rb', line 322

def set_position(*args)
  mode = 0
  if args.size == 4
    point = [args[0], args[1], args[2]]
    mode = args[3]
  elsif args.size == 3
    point = [args[0], args[1], args[2]]
  elsif args.size == 2
    point = args[0]
    mode = args[1]
  elsif args.size == 1
    point = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1..4 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_position(@address, point, mode)
end

#set_torque(torque) ⇒ Boolean #set_torque(tx, ty, tz) ⇒ Boolean

Note:

Unlike the #add_torque, this function overwrites original torque, thus discarding the previously applied torque.

Apply torque on the body in Newton-meters (kg * m/s/s * m).

Overloads:

  • #set_torque(torque) ⇒ Boolean

    Parameters:

    • torque (Geom::Vector3d, Array<Numeric>)
  • #set_torque(tx, ty, tz) ⇒ Boolean

    Parameters:

    • tx (Numeric)
    • ty (Numeric)
    • tz (Numeric)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0


1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# File 'RubyExtension/MSPhysics/body.rb', line 1020

def set_torque(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_torque(@address, data)
end

#set_velocity(velocity) ⇒ nil #set_velocity(vx, vy, vz) ⇒ nil

Set global linear velocity of the body.

Overloads:

  • #set_velocity(velocity) ⇒ nil

    Parameters:

    • velocity (Geom::Vector3d, Array<Numeric>)

      The magnitude of the velocity vector is assumed in meters per second.

  • #set_velocity(vx, vy, vz) ⇒ nil

    Parameters:

    • vx (Numeric)

      Velocity in meters per second along X-axis.

    • vy (Numeric)

      Velocity in meters per second along Y-axis.

    • vz (Numeric)

      Velocity in meters per second along Z-axis.

Returns:

  • (nil)

Since:

  • 1.0.0


391
392
393
394
395
396
397
398
399
400
# File 'RubyExtension/MSPhysics/body.rb', line 391

def set_velocity(*args)
  if args.size == 3
    data = [args[0], args[1], args[2]]
  elsif args.size == 1
    data = args[0]
  else
    raise(ArgumentError, "Wrong number of arguments! Expected 1 or 3 arguments but got #{args.size}.", caller)
  end
  MSPhysics::Newton::Body.set_velocity(@address, data)
end

#sleeping=(state) ⇒ Object

Note:

This function can only set body active, the sleeping is controlled by equilibrium.

Set body sleeping.

Parameters:

  • state (Boolean)

    true to set body sleeping, false to set body active.

Since:

  • 1.0.0


606
607
608
# File 'RubyExtension/MSPhysics/body.rb', line 606

def sleeping=(state)
  MSPhysics::Newton::Body.set_sleeping(@address, state)
end

#sleeping?Boolean

Determine whether body is sleeping. Sleeping bodies are bodies at rest.

Returns:

  • (Boolean)

Since:

  • 1.0.0


597
598
599
# File 'RubyExtension/MSPhysics/body.rb', line 597

def sleeping?
  MSPhysics::Newton::Body.is_sleeping?(@address)
end

#softnessNumeric

Get contact softness coefficient of the body.

Returns:

  • (Numeric)

    A value between 0.01 and 1.00.

Since:

  • 1.0.0


679
680
681
# File 'RubyExtension/MSPhysics/body.rb', line 679

def softness
  MSPhysics::Newton::Body.get_softness(@address)
end

#softness=(coefficient) ⇒ Object

Set contact softness coefficient of the body.

Parameters:

  • coefficient (Numeric)

    A value between 0.01 and 1.00.

Since:

  • 1.0.0


685
686
687
# File 'RubyExtension/MSPhysics/body.rb', line 685

def softness=(coefficient)
  MSPhysics::Newton::Body.set_softness(@address, coefficient)
end

#static=(state) ⇒ Object

Set body static.

Parameters:

  • state (Boolean)

    true to set body static, false to set body dynamic.

Since:

  • 1.0.0


565
566
567
# File 'RubyExtension/MSPhysics/body.rb', line 565

def static=(state)
  MSPhysics::Newton::Body.set_static(@address, state)
end

#static?Boolean

Determine whether body is static.

Returns:

  • (Boolean)

Since:

  • 1.0.0


558
559
560
# File 'RubyExtension/MSPhysics/body.rb', line 558

def static?
  MSPhysics::Newton::Body.is_static?(@address)
end

#static_frictionNumeric

Get static friction coefficient of the body.

Returns:

  • (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


691
692
693
# File 'RubyExtension/MSPhysics/body.rb', line 691

def static_friction
  MSPhysics::Newton::Body.get_static_friction(@address)
end

#static_friction=(coefficient) ⇒ Object

Set static friction coefficient of the body.

Parameters:

  • coefficient (Numeric)

    A value between 0.01 and 2.00.

Since:

  • 1.0.0


697
698
699
# File 'RubyExtension/MSPhysics/body.rb', line 697

def static_friction=(coefficient)
  MSPhysics::Newton::Body.set_static_friction(@address, coefficient)
end

#touching_bodies(inc_non_collidable) ⇒ Array<Body>

Get all bodies that are in contact with this body.

Parameters:

  • inc_non_collidable (Boolean)

    Whether to include contacts from non-collidable bodies.

Returns:

Since:

  • 1.0.0


1071
1072
1073
1074
1075
# File 'RubyExtension/MSPhysics/body.rb', line 1071

def touching_bodies(inc_non_collidable)
  MSPhysics::Newton::Body.get_touching_bodies(@address, inc_non_collidable) { |ptr, data|
    data.is_a?(MSPhysics::Body) ? data : nil
  }
end

#touching_with?(body) ⇒ Boolean

Determine if this body is in contact with another body.

Parameters:

  • body (Body)

    A body to test.

Returns:

  • (Boolean)

Since:

  • 1.0.0


1080
1081
1082
# File 'RubyExtension/MSPhysics/body.rb', line 1080

def touching_with?(body)
  MSPhysics::Newton::Bodies.touching?(@address, body.address)
end

#typeFixnum

Get body type.

Returns:

  • (Fixnum)

    Type: 0 -> dynamic; 1 -> kinematic

Since:

  • 1.0.0


226
227
228
# File 'RubyExtension/MSPhysics/body.rb', line 226

def type
  MSPhysics::Newton::Body.get_type(@address)
end

#valid?Boolean

Determine whether this body is valid - not destroyed.

Returns:

  • (Boolean)

Since:

  • 1.0.0


202
203
204
# File 'RubyExtension/MSPhysics/body.rb', line 202

def valid?
  MSPhysics::Newton::Body.is_valid?(@address)
end

#volumeNumeric

Get body volume in cubic meters (m^3).

Returns:

  • (Numeric)

Since:

  • 1.0.0


537
538
539
# File 'RubyExtension/MSPhysics/body.rb', line 537

def volume
  MSPhysics::Newton::Body.get_volume(@address)
end

#volume=(value) ⇒ Object

Note:

Volume and mass are correlated. If you change volume the mass will automatically be recalculated.

Set body volume in cubic meters (m^3).

Parameters:

  • value (Numeric)

Since:

  • 1.0.0


545
546
547
# File 'RubyExtension/MSPhysics/body.rb', line 545

def volume=(value)
  MSPhysics::Newton::Body.set_volume(@address, value)
end

#worldWorld

Get world in which the body was created.

Returns:

Since:

  • 1.0.0


232
233
234
235
# File 'RubyExtension/MSPhysics/body.rb', line 232

def world
  world_address = MSPhysics::Newton::Body.get_world(@address)
  MSPhysics::Newton::World.get_user_data(world_address)
end