


HKD302 = {};

function HKD302.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Trailer, specializations);
end;

function HKD302:load(xmlFile)

	self.spring = {};		
	self.spring.node = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.HKD302.spring(0)#index"));
	local x,y,z = getRotation(self.spring.node);
	
	self.spring.part = {x,y,z};
	self.spring.part = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.HKD302.spring(0)#part"));
	x,y,z = getTranslation(self.spring.part);
	
	self.spring.fixPoint = {x,y,z};
	self.spring.fixPoint = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.HKD302.spring(0)#fixPoint"));
	
	local ax, ay, az = getWorldTranslation(self.spring.part);
	local bx, by, bz = getWorldTranslation(self.spring.fixPoint);		
	self.spring.distance = Utils.vector3Length(ax-bx, ay-by, az-bz);	

	self.spring.target = {x,y,z};
	self.spring.target = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.HKD302.spring(0)#target"));
	
end;	
	

function HKD302:delete()
end;

function HKD302:mouseEvent(posX, posY, isDown, isUp, button)
end;

function HKD302:keyEvent(unicode, sym, modifier, isDown)
end;

function HKD302:update(dt)
	--if self:getIsActive() then		
		if self.spring.target ~= nil then		
			local ax, ay, az = getWorldTranslation(self.spring.node);
			local bx, by, bz = getWorldTranslation(self.spring.target);
			local x, y, z = worldDirectionToLocal(getParent(self.spring.node), bx-ax, by-ay, bz-az);
			
			local upX, upY, upZ = 0,1,0;
			if math.abs(y) > 0.99*Utils.vector3Length(x, y, z) then
				-- direction and up is parallel
				upY = 0;
				if y > 0 then
					upZ = 1;
				else
					upZ = -1;
				end;
			end;
			
			setDirection(self.spring.node, x, y, z, upX, upY, upZ);
			local distance = Utils.vector3Length(ax-bx, ay-by, az-bz);
			setTranslation(self.spring.part, 0, 0, distance-self.spring.distance);		
		end;		
	--end;
end;

function HKD302:draw()
end;

function HKD302:onAttach()
	setJointRotationLimit(self.componentJoints[2].jointIndex, 0, true, Utils.degToRad(-65), Utils.degToRad(65));	
end;

function HKD302:onDetach()
	setJointRotationLimit(self.componentJoints[2].jointIndex, 0, true, Utils.degToRad(-8), Utils.degToRad(8));	
end;

function HKD302:onLeave()
end;

function HKD302:onDeactivate()
end;

function HKD302:onDeactivateSounds()
end;


