VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Spring"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'Spring Constant
Public k1 As Double
Public k2 As Double

'Original Length
Public oLength As Double

'Masses to be connected
Public fromMass As Mass
Public toMass As Mass

'Breaking Force
Public BreakF As Double

'Plastic State Change Force
Public StateF As Double

'Broken?
Public State As Integer

Sub AddMass(ByRef ms As Mass)
    'Connects a mass to the Spring
    Set toMass = ms
    oLength = GetLength
End Sub
Public Function Broken() As Boolean
    'Is the spring broken?
    Dim Ret As Boolean
    If State = 3 Then
        Ret = True
    Else
        Ret = False
    End If
    Broken = Ret
End Function

Public Function Dist(ByRef obj1 As Mass, ByRef obj2 As Mass) As Double
    'Compute the distance between two masses
    Dist = Sqr(((obj1.X - obj2.X) ^ 2) + ((obj1.Y - obj2.Y) ^ 2) + ((obj1.Z - obj2.Z) ^ 2))
End Function
Function GetDeltaX() As Double
    'X displacement vector
    GetDeltaX = toMass.X - fromMass.X
End Function
Function GetDeltaY() As Double
    'Y displacement vector
    GetDeltaY = toMass.Y - fromMass.Y
End Function
Function GetDeltaZ() As Double
    'Z displacement vector
    GetDeltaZ = toMass.Z - fromMass.Z
End Function

Function GetForce() As Double
    'Calculate the force
    Dim Ret As Double
    Ret = 0
    
    If State = 1 Then
        Ret = Abs(Dist(fromMass, toMass) - oLength) * k1
    End If
    If State = 2 Then
        Ret = Abs(Dist(fromMass, toMass) - oLength) * k2
    End If
    
    GetForce = Ret
End Function
Function GetLength() As Double
    GetLength = Dist(fromMass, toMass)
End Function
Private Sub Class_Initialize()
    State = 1
    k1 = 1
    k2 = 1
    oLength = 1
    StateF = 30
    BreakF = 50
End Sub


Public Sub Break()
    'Break the spring
    State = 3
End Sub

Public Sub Plastic()
    'Make the spring plastic
    State = 2
    
    'Lock the original length to the stretched possition
    oLength = GetLength()
End Sub

