Class: Pongo::SpringConstraint
Instance Attribute Summary collapse
#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
#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
#collidable ⇒ Object
Returns the value of attribute collidable.
7
8
9
|
# File 'lib/pongo/spring_constraint.rb', line 7
def collidable
@collidable
end
|
#p1 ⇒ Object
Returns the value of attribute p1.
7
8
9
|
# File 'lib/pongo/spring_constraint.rb', line 7
def p1
@p1
end
|
#p2 ⇒ Object
Returns the value of attribute p2.
7
8
9
|
# File 'lib/pongo/spring_constraint.rb', line 7
def p2
@p2
end
|
#rest_length ⇒ Object
Returns the value of attribute rest_length.
7
8
9
|
# File 'lib/pongo/spring_constraint.rb', line 7
def rest_length
@rest_length
end
|
#scp ⇒ Object
Returns the value of attribute scp.
7
8
9
|
# File 'lib/pongo/spring_constraint.rb', line 7
def scp
@scp
end
|
Instance Method Details
#angle ⇒ Object
24
25
26
|
# File 'lib/pongo/spring_constraint.rb', line 24
def angle
radian * MathUtil::ONE_EIGHTY_OVER_PI
end
|
#center ⇒ Object
28
29
30
|
# File 'lib/pongo/spring_constraint.rb', line 28
def center
(@p1.curr + @p2.curr) / 2
end
|
#check_particles_location ⇒ Object
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
61
62
63
|
# File 'lib/pongo/spring_constraint.rb', line 61
def collidable?
@collidable
end
|
#curr_length ⇒ Object
42
43
44
|
# File 'lib/pongo/spring_constraint.rb', line 42
def curr_length
@p1.curr.distance(@p2.curr)
end
|
#delta ⇒ Object
118
119
120
|
# File 'lib/pongo/spring_constraint.rb', line 118
def delta
@p1.curr - @p2.curr
end
|
#fixed ⇒ Object
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_limit ⇒ Object
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
|
#init ⇒ Object
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_display ⇒ Object
114
115
116
|
# File 'lib/pongo/spring_constraint.rb', line 114
def init_display
end
|
#is_connected_to?(p) ⇒ Boolean
83
84
85
|
# File 'lib/pongo/spring_constraint.rb', line 83
def is_connected_to?(p)
[@p1, @p2].include? p
end
|
#radian ⇒ Object
19
20
21
22
|
# File 'lib/pongo/spring_constraint.rb', line 19
def radian
d = delta
Math.atan2(d.y, d.x)
end
|
#rect_height ⇒ Object
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_scale ⇒ Object
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
|
#resolve ⇒ Object
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
|