Class: MSPhysics::World

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

Overview

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#inspect, #to_s

Constructor Details

#initializeWorld

class << self

Since:

  • 1.0.0



37
38
39
40
# File 'RubyExtension/MSPhysics/world.rb', line 37

def initialize
  @address = MSPhysics::Newton::World.create()
  MSPhysics::Newton::World.set_user_data(@address, self)
end

Class Method Details

.all_worldsArray<World>

Note:

Worlds that do not have a MSPhysics::World instance are not included in the array.

Get all worlds.

Returns:

Since:

  • 1.0.0



31
32
33
# File 'RubyExtension/MSPhysics/world.rb', line 31

def all_worlds
  MSPhysics::Newton.get_all_worlds() { |ptr, data| data.is_a?(MSPhysics::World) ? data : nil }
end

.validate(world) ⇒ 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 world is valid.

Parameters:

Raises:

  • (TypeError)

    if the world is invalid or destroyed.

Since:

  • 1.0.0



12
13
14
15
16
17
# File 'RubyExtension/MSPhysics/world.rb', line 12

def validate(world)
  AMS.validate_type(world, MSPhysics::World)
  unless world.valid?
    raise(TypeError, "World #{world} is invalid/destroyed!", caller)
  end
end

.world_by_address(address) ⇒ World?

Get world by world address.

Parameters:

  • address (Integer)

Returns:

  • (World, nil)

    A World object if successful.

Since:

  • 1.0.0



22
23
24
25
# File 'RubyExtension/MSPhysics/world.rb', line 22

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

Instance Method Details

#aabbGeom::BoundingBox?

Get world axes aligned bounding box, a bounding box in which all the bodies are included.

Returns:

  • (Geom::BoundingBox, nil)

    A bounding box object, containing the minimum and maximum points of the world bounding box. Nil is returned if the world has no bodies.

Since:

  • 1.0.0



280
281
282
283
284
285
286
# File 'RubyExtension/MSPhysics/world.rb', line 280

def aabb
  mm = MSPhysics::Newton::World.get_aabb(@address)
  return unless mm
  bb = Geom::BoundingBox.new
  bb.add(mm)
  bb
end

#add_explosion(center_point, blast_radius, blast_force) ⇒ Boolean

Add an explosion impulse at particular point in the world.

Examples:

# Adding an impulsive explosion
onStart {
  center_point = this.get_position(1)
  blast_radius = 100
  blast_force = 1000 / simulation.update_timestep
  world.add_explosion(center_point, blast_radius, blast_force)
}

Parameters:

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

    A point of impulse.

  • blast_radius (Numeric)

    A blast radius in meters. Objects beyond the blast radius are not affected.

  • blast_force (Numeric)

    Maximum blast force in Newtons. The force is distributed linearly along the blast radius.

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0



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

def add_explosion(center_point, blast_radius, blast_force)
  MSPhysics::Newton::World.add_explosion(@address, center_point, blast_radius.to_f, blast_force.to_f)
end

#addressInteger

Get pointer to the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



50
51
52
# File 'RubyExtension/MSPhysics/world.rb', line 50

def address
  @address
end

#bodiesArray<Body>

Note:

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

Get all bodies in the world.

Returns:

Since:

  • 1.0.0



103
104
105
# File 'RubyExtension/MSPhysics/world.rb', line 103

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

#bodies_in_aabb(min, max) ⇒ Array<Body>

Get all bodies in a particular bounds.

Parameters:

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

    Minimum point in the bounding box.

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

    Maximum point in the bounding box.

Returns:

Since:

  • 1.0.0



127
128
129
# File 'RubyExtension/MSPhysics/world.rb', line 127

def bodies_in_aabb(min, max)
  MSPhysics::Newton::World.get_bodies_in_aabb(@address, min, max) { |ptr, data| data.is_a?(MSPhysics::Body) ? data : nil }
end

#body_countInteger

Get the number of bodies in the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



133
134
135
# File 'RubyExtension/MSPhysics/world.rb', line 133

def body_count
  MSPhysics::Newton::World.get_body_count(@address)
end

#constraint_countInteger

Get the number of constraints in the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



139
140
141
# File 'RubyExtension/MSPhysics/world.rb', line 139

def constraint_count
  MSPhysics::Newton::World.get_constraint_count(@address)
end

#contact_merge_toleranceNumeric

Get world contact merge tolerance.

Returns:

  • (Numeric)

Since:

  • 1.0.0



303
304
305
# File 'RubyExtension/MSPhysics/world.rb', line 303

def contact_merge_tolerance
  MSPhysics::Newton::World.get_contact_merge_tolerance(@address)
end

#contact_merge_tolerance=(tolerance) ⇒ Object

Set world contact merge tolerance.

Parameters:

  • tolerance (Numeric)

    Default value is 0.001. Minimum value is 0.001.

Since:

  • 1.0.0



309
310
311
# File 'RubyExtension/MSPhysics/world.rb', line 309

def contact_merge_tolerance=(tolerance)
  MSPhysics::Newton::World.set_contact_merge_tolerance(@address, tolerance)
end

#continuous_convex_ray_cast(body, transformation, target, max_hits = 16) ⇒ Array<Hit>

Note:

If given body does not have a convex collision, the function will return an empty array.

Shoot a convex body from point1 to point2 and get all intersections.

Parameters:

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

    Body orientation and start position. Pass nil to ray cast from body’s current transformation.

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

    Ray destination point.

  • max_hits (Integer) (defaults to: 16)

    Maximum number of hits, a value b/w 1 and 256.

Returns:

Since:

  • 1.0.0



248
249
250
251
252
253
254
# File 'RubyExtension/MSPhysics/world.rb', line 248

def continuous_convex_ray_cast(body, transformation, target, max_hits = 16)
  MSPhysics::Body.validate(body, self)
  transformation = body.get_matrix unless transformation
  MSPhysics::Newton::World.continuous_convex_ray_cast(@address, body.collision_address, transformation, target, max_hits) { |ptr, data, point, vector, penetration|
    data.is_a?(MSPhysics::Body) ? Hit.new(data, point, vector) : nil
  }
end

#continuous_ray_cast(point1, point2) ⇒ Array<Hit>

Shoot a ray from point1 to point2 and get all intersections.

Parameters:

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

    Ray starting point.

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

    Ray destination point.

Returns:

Since:

  • 1.0.0



216
217
218
219
220
# File 'RubyExtension/MSPhysics/world.rb', line 216

def continuous_ray_cast(point1, point2)
  MSPhysics::Newton::World.continuous_ray_cast(@address, point1, point2) { |ptr, data, point, vector|
    data.is_a?(MSPhysics::Body) ? Hit.new(data, point, vector) : nil
  }
end

#convex_ray_cast(body, transformation, target) ⇒ Hit?

Note:

If given body does not have a convex collision, the function will return nil.

Shoot a convex body from point1 to point2 and get the closest intersection.

Parameters:

  • body (Body)

    A convex body.

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

    Body orientation and start position. Pass nil to ray cast from body’s current transformation.

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

    Ray destination point.

Returns:

  • (Hit, nil)

    A Hit object or nil if no intersections occurred.

Since:

  • 1.0.0



232
233
234
235
236
237
# File 'RubyExtension/MSPhysics/world.rb', line 232

def convex_ray_cast(body, transformation, target)
  MSPhysics::Body.validate(body, self)
  transformation = body.get_matrix unless transformation
  res = MSPhysics::Newton::World.convex_ray_cast(@address, body.collision_address, transformation, target)
  res && res[1].is_a?(MSPhysics::Body) ? Hit.new(res[1], res[2], res[3]) : nil
end

#cur_threads_countInteger

Get the number of threads currently used by the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



87
88
89
# File 'RubyExtension/MSPhysics/world.rb', line 87

def cur_threads_count
  MSPhysics::Newton::World.get_cur_threads_count(@address)
end

#default_material_idInteger

Get world default contact material id.

Returns:

  • (Integer)

Since:

  • 1.0.0



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

def default_material_id
  MSPhysics::Newton::World.get_default_material_id(@address)
end

#destroynil

Destroy the world.

Returns:

  • (nil)

Since:

  • 1.0.0



56
57
58
# File 'RubyExtension/MSPhysics/world.rb', line 56

def destroy
  MSPhysics::Newton::World.destroy(@address)
end

#destroy_all_bodiesInteger

Destroy all bodies in the world.

Returns:

  • (Integer)

    The number of bodies destroyed.

Since:

  • 1.0.0



145
146
147
# File 'RubyExtension/MSPhysics/world.rb', line 145

def destroy_all_bodies
  MSPhysics::Newton::World.destroy_all_bodies(@address)
end

#gearsArray<Gear>

Note:

Gears that do not have a Gear instance are not included in the array.

Get all gears in the world.

Returns:

Since:

  • 1.0.0



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

def gears
  MSPhysics::Newton::World.get_gears(@address) { |ptr, data| data.is_a?(MSPhysics::Gear) ? data : nil }
end

#get_gravityGeom::Vector3d

Get world gravity.

Returns:

  • (Geom::Vector3d)

    Gravitational acceleration vector. The magnitude of the vector is expressed in meters per second per second (m/s/s).

Since:

  • 1.0.0



152
153
154
# File 'RubyExtension/MSPhysics/world.rb', line 152

def get_gravity
  MSPhysics::Newton::World.get_gravity(@address)
end

#jointsArray<Joint>

Note:

Joints that do not have a Joint instance are not included in the array.

Get all joints in the world.

Returns:

Since:

  • 1.0.0



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

def joints
  MSPhysics::Newton::World.get_joints(@address) { |ptr, data| data.is_a?(MSPhysics::Joint) ? data : nil }
end

#material_thicknessNumeric

Get world material thickness in meters, an imaginary thickness between the collision geometry of two colliding bodies.

Returns:

  • (Numeric)

    A value between 0.0 and 1/32.0.

Since:

  • 1.0.0



192
193
194
# File 'RubyExtension/MSPhysics/world.rb', line 192

def material_thickness
  MSPhysics::Newton::World.get_material_thickness(@address)
end

#material_thickness=(thickness) ⇒ Object

Set world material thickness in meters, an imaginary thickness between the collision geometry of two colliding bodies.

Parameters:

  • thickness (Numeric)

    This value is clamped between 0.0 and 1/32.0.

Since:

  • 1.0.0



199
200
201
# File 'RubyExtension/MSPhysics/world.rb', line 199

def material_thickness=(thickness)
  MSPhysics::Newton::World.set_material_thickness(@address, thickness.to_f)
end

#max_possible_threads_countInteger

Get the maximum possible number of threads to be used by the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



68
69
70
# File 'RubyExtension/MSPhysics/world.rb', line 68

def max_possible_threads_count
  MSPhysics::Newton::World.get_max_possible_threads_count(@address)
end

#max_threads_countInteger

Get the desired maximum number of threads to be used by the world.

Returns:

  • (Integer)

Since:

  • 1.0.0



74
75
76
# File 'RubyExtension/MSPhysics/world.rb', line 74

def max_threads_count
  MSPhysics::Newton::World.get_max_threads_count(@address)
end

#max_threads_count=(count) ⇒ Object

Set the desired maximum number of threads to be used by the world.

Parameters:

  • count (Integer)

    This value is clamped between one and the maximum number of CPUs can be used in the system.

Since:

  • 1.0.0



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

def max_threads_count=(count)
  MSPhysics::Newton::World.set_max_threads_count(@address, count.to_i)
end

#ray_cast(point1, point2) ⇒ Hit?

Shoot a ray from point1 to point2 and get the closest intersection.

Parameters:

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

    Ray starting point.

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

    Ray destination point.

Returns:

  • (Hit, nil)

    A Hit object or nil if no intersections occurred.

Since:

  • 1.0.0



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

def ray_cast(point1, point2)
  res = MSPhysics::Newton::World.ray_cast(@address, point1, point2)
  res && res[1].is_a?(MSPhysics::Body) ? Hit.new(res[1], res[2], res[3]) : nil
end

#serialize_to_file(full_path) ⇒ nil

Serialize world into file.

Parameters:

  • full_path (String)

Returns:

  • (nil)

Since:

  • 1.0.0



297
298
299
# File 'RubyExtension/MSPhysics/world.rb', line 297

def serialize_to_file(full_path)
  MSPhysics::Newton::World.serialize_to_file(@address, full_path)
end

#set_gravity(acceleration) ⇒ nil #set_gravity(ax, ay, az) ⇒ nil

Set world gravity.

Overloads:

  • #set_gravity(acceleration) ⇒ nil

    Parameters:

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

      Gravitational acceleration vector. The magnitude of the vector is assumed in meters per second per second (m/s/s).

  • #set_gravity(ax, ay, az) ⇒ nil

    Parameters:

    • ax (Numeric)

      Acceleration along X-axis in m/s/s.

    • ay (Numeric)

      Acceleration along Y-axis in m/s/s.

    • az (Numeric)

      Acceleration along Z-axis in m/s/s.

Returns:

  • (nil)

Since:

  • 1.0.0



166
167
168
169
170
171
172
173
174
175
# File 'RubyExtension/MSPhysics/world.rb', line 166

def set_gravity(*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::World.set_gravity(@address, data)
end

#solver_modelInteger

Get world solver model.

Returns:

  • (Integer)

    Number of passes, a value between 1 and 256.

Since:

  • 1.0.0



179
180
181
# File 'RubyExtension/MSPhysics/world.rb', line 179

def solver_model
  MSPhysics::Newton::World.get_solver_model(@address)
end

#solver_model=(model) ⇒ Object

Set world solver model.

Parameters:

  • model (Integer)

    Number of passes, a value between 1 and 256.

Since:

  • 1.0.0



185
186
187
# File 'RubyExtension/MSPhysics/world.rb', line 185

def solver_model=(model)
  MSPhysics::Newton::World.set_solver_model(@address, model.to_i)
end

#timeNumeric

Get world elapsed time in seconds.

Returns:

  • (Numeric)

Since:

  • 1.0.0



290
291
292
# File 'RubyExtension/MSPhysics/world.rb', line 290

def time
  MSPhysics::Newton::World.get_time(@address)
end

#update(timestep) ⇒ Numeric

Note:

The smaller the time step the more accurate the simulation will be.

Update world by a time step in seconds.

Parameters:

  • timestep (Numeric)

    This value is clamped between 1/30.0 and 1/1200.0.

Returns:

  • (Numeric)

    The update time step.

Since:

  • 1.0.0



95
96
97
# File 'RubyExtension/MSPhysics/world.rb', line 95

def update(timestep)
  MSPhysics::Newton::World.update(@address, timestep)
end

#valid?Boolean

Determine if the world is valid (not destroyed).

Returns:

  • (Boolean)

Since:

  • 1.0.0



44
45
46
# File 'RubyExtension/MSPhysics/world.rb', line 44

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