local function P()local P={}for l,L in pairs(constraints:GetChildren())do if L:IsA("\067\121\108\105\110\100\114\105\099\097\108\067\111\110\115\116\114\097\105\110\116")then table.insert(P,L)end end return P end local function l(P)local l={}local L=PackagedVehicle:FindFirstChild("\084\114\097\105\108\101\114")local function y(L)local y="\083\116\114\117\116\083\112\114\105\110\103"local d="\083\116\114\117\116\083\112\114\105\110\103\070"local c="\084\111\114\115\105\111\110\066\097\114\083\112\114\105\110\103"for L,X in pairs(L)do if X:IsA("\083\112\114\105\110\103\067\111\110\115\116\114\097\105\110\116")then if P=="\083\116\114\117\116\070\114\111\110\116"then if string.find(X.Name,d)then table.insert(l,X)end elseif P=="\083\116\114\117\116\082\101\097\114"then if not string.find(X.Name,d)and string.find(X.Name,y)then table.insert(l,X)end elseif P=="\084\111\114\115\105\111\110\066\097\114"then if string.find(X.Name,c)then table.insert(l,X)end end end end end y(constraints:GetChildren())if L then y(L.Constraints:GetChildren())end return l end local function L(P)return P.Attachment1.WorldAxis:Dot(P.Attachment1.Parent.RotVelocity)end local function y(P,l,L)P.Stiffness=l P.Damping=L end local function d(P)for l,L in pairs(Motors)do L.MotorMaxTorque=P end end local function c(P,l,L)for y,d in pairs(Motors)do if VehicleParameters.MaxSpeed==0 then d.MotorMaxTorque=0 else local y=VehicleParameters.MaxSpeed if L<0 and l<0 then y=VehicleParameters.ReverseSpeed end local c=math.abs(Chassis.driverSeat.Velocity.Magnitude/y)d.MotorMaxTorque=math.exp((-3*c)*c)*P end end end local function X(P)for l,L in pairs(Motors)do L.MotorMaxAngularAcceleration=P end end
--
local function getVehicleMotors()
	local motors = {}
	for _, c in pairs(constraints:GetChildren()) do
		if c:IsA("CylindricalConstraint") then
			table.insert(motors, c)
        end
    end
	return motors
end

local function getSprings(springType)
	local springs = {}
	local trailer = PackagedVehicle:FindFirstChild("Trailer")
	
	local function search(children)
		local searchStrutSpring = "StrutSpring"
		local searchFrontSpring = "StrutSpringF"
		local searchTorsionSpring = "TorsionBarSpring"
		for _, c in pairs(children) do
			if c:IsA("SpringConstraint") then
				if springType == "StrutFront" then
					if string.find(c.Name, searchFrontSpring) then
						table.insert(springs, c)
					end
				elseif springType == "StrutRear" then
					if (not string.find(c.Name, searchFrontSpring)) and string.find(c.Name, searchStrutSpring) then
						table.insert(springs, c) -- we have option of Mid and Rear for these
					end
				elseif springType == "TorsionBar" then
					if string.find(c.Name, searchTorsionSpring) then
						table.insert(springs, c)
					end
				end
			end
		end
	end

	search(constraints:GetChildren())
	if trailer then
		search(trailer.Constraints:GetChildren())
	end
	
	return springs
end

local function getMotorVelocity(motor)
	return motor.Attachment1.WorldAxis:Dot( motor.Attachment1.Parent.RotVelocity )
end

local function adjustSpring( spring, stiffness, damping )
	spring.Stiffness = stiffness
	spring.Damping = damping
end

local function setMotorTorque(torque)
	for _, motor in pairs(Motors) do		
		motor.MotorMaxTorque = torque
	end
end

local function setMotorTorqueDamped(torque, velocityDirection, accelDirection)
	for _, motor in pairs(Motors) do
		if VehicleParameters.MaxSpeed == 0 then 
			motor.MotorMaxTorque = 0
		else
			local maxSpeed = VehicleParameters.MaxSpeed
			if accelDirection < 0 and velocityDirection < 0 then
				maxSpeed = VehicleParameters.ReverseSpeed
			end
			
			local r = math.abs(Chassis.driverSeat.Velocity.Magnitude / maxSpeed)
			motor.MotorMaxTorque = math.exp( -3 * r * r ) * torque
		end
	end
end

local function setMotorMaxAcceleration(acceleration)
	for _, motor in pairs(Motors) do
		motor.MotorMaxAngularAcceleration = acceleration
	end
end
