Class: MSPhysics::Gear

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

Overview

Since:

  • 1.0.0

Constant Summary collapse

DEFAULT_RATIO =

Since:

  • 1.0.0

1.0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#inspect, #to_s

Constructor Details

#initialize(world, joint1, joint2) ⇒ Gear

Returns a new instance of Gear.

Parameters:

Since:

  • 1.0.0



51
52
53
54
55
56
57
58
# File 'RubyExtension/MSPhysics/gear.rb', line 51

def initialize(world, joint1, joint2)
  MSPhysics::World.validate(world)
  MSPhysics::Joint.validate(joint1, world)
  MSPhysics::Joint.validate(joint2, world)
  @address = MSPhysics::Newton::Gear.create(world.address, joint1.address, joint2.address)
  MSPhysics::Newton::Gear.set_user_data(@address, self)
  MSPhysics::Newton::Gear.set_ratio(@address, DEFAULT_RATIO)
end

Class Method Details

.all_gearsArray<Gear>

Note:

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

Get all gears.

Returns:

Since:

  • 1.0.0



42
43
44
# File 'RubyExtension/MSPhysics/gear.rb', line 42

def all_gears
  MSPhysics::Newton.get_all_gears() { |ptr, data| data.is_a?(MSPhysics::Gear) ? data : nil }
end

.gear_by_address(address) ⇒ Gear?

Get gear by address.

Parameters:

  • address (Integer)

Returns:

  • (Gear, nil)

    A Gear object if successful.

Raises:

  • (TypeError)

    if the address is invalid.

Since:

  • 1.0.0



33
34
35
36
# File 'RubyExtension/MSPhysics/gear.rb', line 33

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

.validate(gear, 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 gear is valid.

Parameters:

  • gear (Gear)
  • world (World, nil) (defaults to: nil)

    A world the gear ought to belong to or nil.

Raises:

  • (TypeError)

    if gear is invalid or destroyed.

Since:

  • 1.0.0



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

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

Instance Method Details

#addressInteger

Get pointer the gear.

Returns:

  • (Integer)

Since:

  • 1.0.0



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

def address
  @address
end

#destroyvoid

This method returns an undefined value.

Destroy gear.

Since:

  • 1.0.0



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

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

#joint1MSPhysics::Joint

Get the first joint the gear is linked to.

Returns:

Since:

  • 1.0.0



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

def joint1
  joint1_address = MSPhysics::Newton::Gear.get_joint1(@address)
  MSPhysics::Newton::Joint.get_user_data(joint1_address)
end

#joint2MSPhysics::Joint

Get the second joint the gear is linked to.

Returns:

Since:

  • 1.0.0



88
89
90
91
# File 'RubyExtension/MSPhysics/gear.rb', line 88

def joint2
  joint2_address = MSPhysics::Newton::Gear.get_joint2(@address)
  MSPhysics::Newton::Joint.get_user_data(joint2_address)
end

#ratioNumeric

Get gear ratio.

Returns:

  • (Numeric)

Since:

  • 1.0.0



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

def ratio
  MSPhysics::Newton::Gear.get_ratio(@address)
end

#ratio=(value) ⇒ Object

Set gear ratio.

Parameters:

  • value (Numeric)

Since:

  • 1.0.0



107
108
109
# File 'RubyExtension/MSPhysics/gear.rb', line 107

def ratio=(value)
  MSPhysics::Newton::Gear.set_ratio(@address, value)
end

#valid?Boolean

Determine whether gear is valid.

Returns:

  • (Boolean)

Since:

  • 1.0.0



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

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

#worldMSPhysics::World

Get the world the gear is associated to.

Returns:

Since:

  • 1.0.0



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

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