Class: MSPhysics::CommonContext

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

Overview

CommonContext contains methods that both BodyContext and ControllerContext objects have in common.

Since:

  • 1.0.0

Direct Known Subclasses

BodyContext, ControllerContext

Variables collapse

Instances collapse

Time collapse

User Input collapse

Functions collapse

Instance Method Summary collapse

Methods inherited from Entity

#inspect, #to_s

Constructor Details

#initializeCommonContext

Returns a new instance of CommonContext

Since:

  • 1.0.0

Instance Method Details

#accumulator(rate, delay = 0.0) ⇒ Fixnum

Increment the accumulator by one at a specific rate and offset.

Examples:

Using in controller

accumulator(0.25, 2)

Parameters:

  • rate (Numeric)

    Accumulate every [rate] seconds.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Fixnum)

    The accumulator value

Since:

  • 1.0.0

#delete_global_var(name) ⇒ Boolean

Remove global variable from hash.

Parameters:

  • name (String, Symbol)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0

#delete_var(name) ⇒ Boolean

Remove variable from hash.

Parameters:

  • name (String, Symbol)

Returns:

  • (Boolean)

    success

Since:

  • 1.0.0

#frameFixnum

Get simulation frame.

Examples:

Using in controller

(frame > 100) ? 1 : 0

Returns:

  • (Fixnum)

Since:

  • 1.0.0

#get_global_var(name) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.

Get global variable value.

Parameters:

  • name (String, Symbol)

Returns:

  • (Object)

    Variable value or nil if variable with the specified name doesn't exist.

Since:

  • 1.0.0

#get_set_global_var(name, value) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.

Get original global variable value and assign a new value.

Parameters:

  • name (String, Symbol)
  • value (Object)

Returns:

  • (Object)

    Original value or nil if originally the variable didn't exist.

Since:

  • 1.0.0

#get_set_var(name, value) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.

Get original variable value and assign a new value.

Parameters:

  • name (String, Symbol)
  • value (Object)

Returns:

  • (Object)

    Original value or nil if originally the variable didn't exist.

Since:

  • 1.0.0

#get_var(name) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.

Get variable value.

Examples:

Joint controller based on a script

# The following is pasted to one of the script tabs
onStart {
  set_var('angle', 0.0) # This variable is accessible across all script and controller contexts.
  @step = 0.2 # This variable is accessible within this script context only.
}
onTick {
  cur_angle = get_var('angle') # Local variable accessible within a block.
  # Control angle with keys g and r, that is within the boundaries -30 and 12 degrees.
  if key('r') == 1 && cur_angle < 12.0
    cur_angle += @step
  elsif key('g') == 1 && cur_angle > -30.0
    cur_angle -= @step
  end
  # Clamp the angle value to ensure its within range
  cur_angle = AMS.clamp(cur_angle, -30.0, 12.0)
  # Assign new value to the 'angle' variable
  set_var('angle', cur_angle)
}

# The following is pasted to one or more of the joint controllers
get_var('angle')

Parameters:

  • name (String, Symbol)

Returns:

  • (Object)

    Variable value or nil if variable with the specified name doesn't exist.

Since:

  • 1.0.0

#joybutton(button) ⇒ Fixnum

Note:

Button name parameter is not case sensitive.

Get joy-button value.

Examples:

onUpdate {
  value = joybutton(:lt)
  simulation.display_note value.to_s
}

Parameters:

  • button (String, Symbol)

    Button name. Valid names are:

    • A : The A button.

    • B : The B button.

    • X : The X button.

    • Y : The Y button.

    • LB : The left shoulder button, above the trigger.

    • RB : The right shoulder button, above the trigger.

    • LT : The left trigger.

    • RT : The right trigger.

    • back : The back button.

    • start : The start button.

    • leftb : The left thumbstick button.

    • rightb : The right thumbstick button.

Returns:

  • (Fixnum)

    1 if the button is down; 0 if the button is up.

Since:

  • 1.0.0

#joypadFixnum

Get joy-pad value.

  • 0 if hat is centered

  • 1 if hat is up

  • 2 if hat is right

  • 4 if hat is down

  • 8 if hat is left

  • 12 if hat is left-down

  • 9 if hat is left-up

  • 6 if hat is right-down

  • 3 if hat is right-up

Examples:

onUpdate {
  value = joypad()
  simulation.display_note value.to_s
}

Returns:

  • (Fixnum)

    Returns one of the following values:

Since:

  • 1.0.0

#joystick(axis) ⇒ Numeric

Note:

Axis name parameter is not case sensitive.

Get joystick value.

Examples:

onUpdate {
  value = joystick(:leftx)
  simulation.display_note value.to_s
}

Parameters:

  • axis (String, Symbol)

    Axis name. Valid names are:

    • 'leftx' : X-XAXIS position on left thumbstick.

    • 'lefty' : Y-XAXIS position on left thumbstick.

    • 'leftz' : Left trigger position.

    • 'rightx' : X-XAXIS position on right thumbstick.

    • 'righty' : Y-XAXIS position on right thumbstick.

    • 'rightz' : Right trigger position.

Returns:

  • (Numeric)

    Stick position on the axis, a ranging value from -1.0 to 1.0.

Since:

  • 1.0.0

#key(vk) ⇒ Fixnum

Note:

The vk parameter is not case sensitive.

Get state of a keyboard key.

Examples:

Using in controller

key('space') * 10

Parameters:

  • vk (String, Symbol, Fixnum)

    Virtual key code or name.

Returns:

  • (Fixnum)

    1 if down, 0 if up.

See Also:

Since:

  • 1.0.0

#key_slider(name, key1, key2, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric

Create a new range slider or get slider value if slider with the specified name already exists.

Examples:

Using in controller

key_slider('Lift', 'up', 'down', 0, -4, 26, 5)

Parameters:

  • name (String)

    Slider name.

  • key1 (String)

    The positive directing key.

  • key2 (String)

    The negative directing key.

  • default_value (Numeric) (defaults to: 0.0)

    Starting value.

  • min (Numeric) (defaults to: 0.0)

    Minimum value.

  • max (Numeric) (defaults to: 1.0)

    Maximum value.

  • step (Numeric) (defaults to: 1.0)

    Accumulation step in units per second.

Returns:

  • (Numeric)

    Slider value.

See Also:

Since:

  • 1.0.0

#leftxNumeric

Output from keys D and A or X-axis position on the left joystick.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#leftyNumeric

Output from keys W and S or Y-axis position on the left joystick.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#leftzNumeric

Output from keys E and Q or position of the joy controller's left trigger.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#numxFixnum

Output from keys NUMPAD6 and NUMPAD4 or centered-X-axis position on the joy-pad.

Returns:

  • (Fixnum)

    -1, 0, or 1.

Since:

  • 1.0.0

#numyFixnum

Output from keys NUMPAD8 and NUMPAD5 or centered-Y-axis position on the joy-pad.

Returns:

  • (Fixnum)

    -1, 0, or 1.

Since:

  • 1.0.0

#oscillator(frequency, delay = 0.0) ⇒ Numeric

Calculate the value of a sine curve at a particular world time.

Examples:

Using in controller

oscillator(0.8) * 4

Parameters:

  • frequency (Numeric)

    Number of times to oscillate per second.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#oscillator2(frequency, delay = 0.0) ⇒ Numeric

Compute the value of a shifted sine curve at a particular world time.

Parameters:

  • frequency (Numeric)

    Number of times to oscillate per second.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Numeric)

    A value ranging from 0.0 to 1.0.

Since:

  • 1.0.0

#oscillator2_slope(frequency, delay = 0.0) ⇒ Numeric

Calculate the slope of a shifted sine curve at a particular world time.

Parameters:

  • frequency (Numeric)

    Number of times to oscillate per second.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Numeric)

    A value ranging from -πf to πf, where f is the frequency.

Since:

  • 1.0.0

#oscillator_slope(frequency, delay = 0.0) ⇒ Numeric

Calculate the slope of a sine curve at a particular world time.

Parameters:

  • frequency (Numeric)

    Number of times to oscillate per second.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Numeric)

    A value ranging from -2πf to 2πf, where f is the frequency.

Since:

  • 1.0.0

#repeater(rate, hold, delay = 0.0) ⇒ Fixnum

Compute a repeater value based on rate, hold, and delay.

Examples:

Using in controller

repeater(1, 0.4, 0)

Parameters:

  • rate (Numeric)

    Repeat every [rate] seconds.

  • hold (Numeric)

    The time, in seconds, to hold the repeater turned on whenever it is triggered.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

Returns:

  • (Fixnum)

    1 or 0

Since:

  • 1.0.0

#rightxNumeric

Output from LEFT and RIGHT arrow keys or X-axis position on the right joystick.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#rightyNumeric

Output from UP and DOWN arrow keys or Y-axis position on the right joystick.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#rightzNumeric

Output from keys PageUp and PageDown or position of the joy controller's right trigger.

Returns:

  • (Numeric)

    A value ranging from -1.0 to 1.0.

Since:

  • 1.0.0

#set_global_var(name, value) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist throughout the session of the SketchUp process. They are preserved after simulation ends.

Set global variable value.

Parameters:

  • name (String, Symbol)
  • value (Object)

Returns:

  • (Object)

    The newly assigned value.

Since:

  • 1.0.0

#set_var(name, value) ⇒ Object

Note:

These variables are accessible in all body/controller scopes and exist only throughout the session of simulation. They are disposed of after simulation ends.

Set variable value.

Examples:

Joint controller based on a script

# The following is pasted to one of the script tabs
onStart {
  set_var('angle', 0.0) # This variable is accessible across all script and controller contexts.
  @step = 0.2 # This variable is accessible within this script context only.
}
onTick {
  cur_angle = get_var('angle') # Local variable accessible within a block.
  # Control angle with keys g and r, that is within the boundaries -30 and 12 degrees.
  if key('r') == 1 && cur_angle < 12.0
    cur_angle += @step
  elsif key('g') == 1 && cur_angle > -30.0
    cur_angle -= @step
  end
  # Clamp the angle value to ensure its within range
  cur_angle = AMS.clamp(cur_angle, -30.0, 12.0)
  # Assign new value to the 'angle' variable
  set_var('angle', cur_angle)
}

# The following is pasted to one or more of the joint controllers
get_var('angle')

Parameters:

  • name (String, Symbol)
  • value (Object)

Returns:

  • (Object)

    The newly assigned value.

Since:

  • 1.0.0

#simulationSimulation

Get Simulation instance.

Returns:

Since:

  • 1.0.0

#singular_repeater(rate, delay = 0.0, id = nil) ⇒ Fixnum

Compute a repeater value based on rate and delay that repeats only one time whenever it is triggered.

Examples:

Output frame every 0.25 seconds.

onTick {
  if singular_repeater(0.25) == 1
    puts frame
  end
}

Parameters:

  • rate (Numeric)

    Repeat every [rate] seconds.

  • delay (Numeric) (defaults to: 0.0)

    The time to wait, in seconds, before starting.

  • id (Object) (defaults to: nil)

    Unique repeat identifier.

Returns:

  • (Fixnum)

    1 or 0

Since:

  • 1.0.0

#slider(name, default_value = 0.0, min = 0.0, max = 1.0, step = 1.0) ⇒ Numeric

Create a new range slider or get slider value if slider with the specified name already exists.

Examples:

Using in controller

slider('Rotate', 0, -10, 10, 0.01)

Parameters:

  • name (String)

    Slider name.

  • default_value (Numeric) (defaults to: 0.0)

    Starting value.

  • min (Numeric) (defaults to: 0.0)

    Minimum value.

  • max (Numeric) (defaults to: 1.0)

    Maximum value.

  • step (Numeric) (defaults to: 1.0)

    Snap step.

Returns:

  • (Numeric)

    Slider value.

Since:

  • 1.0.0

#toggle_key(vk) ⇒ Fixnum

Note:

The vk parameter is not case sensitive.

Get toggled state of a keyboard key.

Examples:

Using in controller

toggle_key('space') * 10

Parameters:

  • vk (String, Symbol, Fixnum)

    Virtual key code or name.

Returns:

  • (Fixnum)

    1 if toggled down, 0 if toggled up.

See Also:

Since:

  • 1.0.0

#worldWorld

Get simulation World instance.

Examples:

Using in controller

(world.time > 4) ? 1 : 0

Returns:

Since:

  • 1.0.0