Class: Pongo::SpringConstraint

Inherits:
AbstractConstraint show all
Defined in:
lib/pongo/spring_constraint.rb

Direct Known Subclasses

AngularConstraint

Instance Attribute Summary collapse

Attributes inherited from AbstractConstraint

#stiffness

Attributes inherited from AbstractItem

#always_repaint, #display_object, #display_object_offset, #display_object_rotation, #fill_alpha, #fill_color, #line_alpha, #line_color, #line_thickness, #renderer, #solid, #user_data, #visible

Instance Method Summary collapse

Methods inherited from AbstractItem

#add_event_listener, #always_redraw!, #always_redraw=, #always_redraw?, #cleanup, #dispatch_event, #draw, #has_event_listener, #set_fill, #set_line, #set_style, #visible!, #visible?

Constructor Details

#initialize(p1, p2, options = {}) ⇒ SpringConstraint

Returns a new instance of SpringConstraint.



9
10
11
12
13
14
15
16
17
# File 'lib/pongo/spring_constraint.rb', line 9

def initialize(p1, p2, options={})
  options = {:stiffness => 0.5, :collidable => false, :rect_height => 1, :rect_scale => 1, :scale_to_length => false}.update(options)
  super(options[:stiffness])
  @p1 = p1
  @p2 = p2
  check_particles_location
  @rest_length = curr_length
  set_collidable(options[:collidable], options[:rect_height], options[:rect_scale], options[:scale_to_length])
end

Instance Attribute Details

#collidableObject

Returns the value of attribute collidable.



7
8
9
# File 'lib/pongo/spring_constraint.rb', line 7

def collidable
  @collidable
end

#p1Object

Returns the value of attribute p1.



7
8
9
# File 'lib/pongo/spring_constraint.rb', line 7

def p1
  @p1
end

#p2Object

Returns the value of attribute p2.



7
8
9
# File 'lib/pongo/spring_constraint.rb', line 7

def p2
  @p2
end

#rest_lengthObject

Returns the value of attribute rest_length.



7
8
9
# File 'lib/pongo/spring_constraint.rb', line 7

def rest_length
  @rest_length
end

#scpObject

Returns the value of attribute scp.



7
8
9
# File 'lib/pongo/spring_constraint.rb', line 7

def scp
  @scp
end

Instance Method Details

#angleObject



24
25
26
# File 'lib/pongo/spring_constraint.rb', line 24

def angle
  radian * MathUtil::ONE_EIGHTY_OVER_PI
end

#centerObject



28
29
30
# File 'lib/pongo/spring_constraint.rb', line 28

def center
  (@p1.curr + @p2.curr) / 2
end

#check_particles_locationObject



122
123
124
125
126
# File 'lib/pongo/spring_constraint.rb', line 122

def check_particles_location
  if @p1.curr.x == @p2.curr.x and @p1.curr.y == @p2.curr.y
    @p2.curr.x += 0.0001
  end
end

#collidable?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pongo/spring_constraint.rb', line 61

def collidable?
  @collidable
end

#curr_lengthObject



42
43
44
# File 'lib/pongo/spring_constraint.rb', line 42

def curr_length
  @p1.curr.distance(@p2.curr)
end

#deltaObject



118
119
120
# File 'lib/pongo/spring_constraint.rb', line 118

def delta
  @p1.curr - @p2.curr
end

#fixedObject Also known as: fixed?



87
88
89
# File 'lib/pongo/spring_constraint.rb', line 87

def fixed
  @p1.fixed? and @p2.fixed?
end

#fixed_end_limitObject



65
66
67
# File 'lib/pongo/spring_constraint.rb', line 65

def fixed_end_limit
  @scp.fixed_end_limit
end

#fixed_end_limit=(f) ⇒ Object



69
70
71
72
73
# File 'lib/pongo/spring_constraint.rb', line 69

def fixed_end_limit=(f)
  if @scp
    @scp.fixed_end_limit = f
  end
end

#initObject



92
93
94
95
96
97
98
99
100
# File 'lib/pongo/spring_constraint.rb', line 92

def init
  cleanup
  if collidable?
    @scp.init
  else
    init_display
  end
  draw
end

#init_displayObject



114
115
116
# File 'lib/pongo/spring_constraint.rb', line 114

def init_display
  #raise NotImplementedError
end

#is_connected_to?(p) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/pongo/spring_constraint.rb', line 83

def is_connected_to?(p)
  [@p1, @p2].include? p
end

#radianObject



19
20
21
22
# File 'lib/pongo/spring_constraint.rb', line 19

def radian
  d = delta
  Math.atan2(d.y, d.x)
end

#rect_heightObject



46
47
48
# File 'lib/pongo/spring_constraint.rb', line 46

def rect_height
  @scp.rect_height
end

#rect_height=(h) ⇒ Object



50
51
52
53
54
# File 'lib/pongo/spring_constraint.rb', line 50

def rect_height=(h)
  if @scp
    @scp.rect_height = h
  end
end

#rect_scaleObject



38
39
40
# File 'lib/pongo/spring_constraint.rb', line 38

def rect_scale
  @scp.rect_scale
end

#rect_scale=(s) ⇒ Object



32
33
34
35
36
# File 'lib/pongo/spring_constraint.rb', line 32

def rect_scale=(s)
  if @scp
    @scp.rect_scale = s
  end
end

#resolveObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pongo/spring_constraint.rb', line 102

def resolve
  return if fixed?

  delta_length = curr_length
  diff = (delta_length - rest_length) / 
    (delta_length * (@p1.inv_mass + @p2.inv_mass))
  dmds = delta * (diff * stiffness)

  @p1.curr.minus!(dmds * @p1.inv_mass)
  @p2.curr.plus!( dmds * @p2.inv_mass)
end

#set_collidable(b, rect_height, rect_scale, scale_to_length) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/pongo/spring_constraint.rb', line 75

def set_collidable(b, rect_height, rect_scale, scale_to_length)
  if @collidable = b
    @scp = SpringConstraintParticle.new(@p1, @p2, self, rect_height, rect_scale, scale_to_length)
  else
    @scp = nil
  end
end