Class: BulldogPhysics::Spring

Inherits:
ForceGenerator show all
Defined in:
lib/RigidBodies/spring.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_connection_pt, other_body, other_connection_point, spring_constant, rest_length) ⇒ Spring

Returns a new instance of Spring.



11
12
13
# File 'lib/RigidBodies/spring.rb', line 11

def initialize(local_connection_pt, other_body, other_connection_point, spring_constant, rest_length)
	@conection_point, @other, @other_connection_point, @spring_constant, @rest_length = local_connection_pt, other_body, other_connection_point, spring_constant, rest_length
end

Instance Attribute Details

#connection_pointObject (readonly)

connection point in local coords



5
6
7
# File 'lib/RigidBodies/spring.rb', line 5

def connection_point
  @connection_point
end

#otherObject (readonly)

particle at the other end of the spring



7
8
9
# File 'lib/RigidBodies/spring.rb', line 7

def other
  @other
end

#other_connection_pointObject (readonly)

point of connection of the spring to the other object in that objects local coords



6
7
8
# File 'lib/RigidBodies/spring.rb', line 6

def other_connection_point
  @other_connection_point
end

#rest_lengthObject (readonly)

Returns the value of attribute rest_length.



9
10
11
# File 'lib/RigidBodies/spring.rb', line 9

def rest_length
  @rest_length
end

#spring_constantObject (readonly)

Returns the value of attribute spring_constant.



8
9
10
# File 'lib/RigidBodies/spring.rb', line 8

def spring_constant
  @spring_constant
end

Instance Method Details

#update_force(body, duration) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/RigidBodies/spring.rb', line 15

def update_force(body, duration)
	# calc ends in world space
	lws = body.get_point_in_world_space(@conection_point)
	ows = other.get_point_in_world_space(@other_connection_point)

	# calculate the vector of the string
	force = lws - ows

	# calculate magnitude of the force
	magnitude = force.magnitude
	magnitude = (magnitude - @rest_length).abs
	magnitude *= @spring_constant

	# calculate and apply the final force
	force.normalize
	force *= -magnitude
	body.add_force_at_point(force, lws)
end