Vehicles/VehicleScripts/CarriageDamping.lua

The carriage damping script implements a damping simulation of the vehicle carriage(s) using the position of the ingame wheels.

  25  CarriageDamping                         = CarriageDamping or {};

CarriageDamping:load(dataTable)

Load the configuration data for the carriage damping mechanism.

  29  function CarriageDamping:load(dataTable)
  30      if dataTable.carriageDamping == nil then
  31          printf("Warning: Using vehicle script 'CarriageDamping', but the data table 'carriageDamping' is missing (got a %s value, table expected)", type(dataTable.carriageDamping));
  32          return;
  33      end;
  34  
  35      self.setCarriageRotation        = VehicleManager:newFunction("setCarriageRotation");
  36      self.getDampingDeltaHeightX     = CarriageDamping.getDampingDeltaHeightX;
  37      self.getDampingDeltaHeightZ     = CarriageDamping.getDampingDeltaHeightZ;
  38      self.getWheelsPositionDeltaY    = CarriageDamping.getWheelsPositionDeltaY;
  39  
  40      self.carriageDamping            = dataTable.carriageDamping;
  41      self.carriages                  = {};
  42  
  43      self.carriages                  = {};
  44  
  45      for k,v in pairs(self.carriageDamping) do
  46          if v.carriage == nil or v.carriage == "" then
  47              print("Carriage index is either nil or an empty string!");
  48              return;
  49          end;
  50          if v.carriageOrientationPoint == nil or v.carriageOrientationPoint == "" then
  51              print("Carriage orientation point index is either nil or an empty string!");
  52              return;
  53          end;
  54          if v.carriageWheels == nil or type(v.carriageWheels) ~= "table" then
  55              print("Carriage wheels is either nil or has the wrong type (".. type(v.carriageWheels).." table expected)!");
  56              return;
  57          end;
  58          if v.carriageWheels.leftWheels == nil or type(v.carriageWheels.leftWheels) ~= "table" then
  59              print("Carriage wheels 'leftWheels' is either nil or has the wrong type (".. type(v.carriageWheels.leftWheels)..", table expected)!");
  60              return;
  61          end;
  62          if v.carriageWheels.rightWheels == nil or type(v.carriageWheels.rightWheels) ~= "table" then
  63              print("Carriage wheels 'rightWheels' is either nil or has the wrong type (".. type(v.carriageWheels.rightWheels)..", table expected)!");
  64              return;
  65          end;
  66  
  67          local leftWheels            = {};
  68          for k1,v1 in pairs(v.carriageWheels.leftWheels) do
  69              leftWheels[k1]          = getChild(self.id, v1);
  70          end;
  71          
  72          local rightWheels           = {};
  73          for k1,v1 in pairs(v.carriageWheels.rightWheels) do
  74              rightWheels[k1]         = getChild(self.id, v1);
  75          end;
  76  
  77          local carriageTable         = {
  78              carriage                = getChild(self.id, v.carriage),
  79              orientationPoint        = getChild(self.id, v.carriageOrientationPoint),
  80              wheels                  = {
  81                  leftWheels          = leftWheels,
  82                  rightWheels         = rightWheels,
  83              },
  84              dampingFactorX          = v.dampingFactorX,
  85              dampingFactorZ          = v.dampingFactorZ,
  86              dampingIntensity        = v.dampingIntensity,
  87          };
  88  
  89          self.carriages[k]           = carriageTable;
  90      end;
  91  end;

CarriageDamping:setCarriageRotation(carriageId, deltaHeightX, deltaHeightZ, dampingFactorX, dampingFactorZ, dampingIntensity)

Sets the carriage rotation according to the given values.

  95  function CarriageDamping:setCarriageRotation(carriageId, deltaHeightX, deltaHeightZ, dampingFactorX, dampingFactorZ, dampingIntensity)
  96      local rotX          = deltaHeightX * dampingFactorX * dampingIntensity;
  97      local rotZ          = deltaHeightZ * dampingFactorZ * dampingIntensity;
  98      
  99      setRotation(carriageId, rotX, 0, rotZ);
 100  end;

CarriageDamping:getDampingDeltaHeightX(leftWheelsYDelta, rightWheelsYDelta)

Returns the average position difference between the front and the back wheels.

 104  function CarriageDamping:getDampingDeltaHeightX(leftWheelsYDelta, rightWheelsYDelta)
 105      local leftWheelsLength              = #leftWheelsYDelta;
 106      local rightWheelsLength             = #rightWheelsYDelta;
 107  
 108      -- calculate the height difference on the X axis of the carriage
 109      local avgHeight1X                   = (leftWheelsYDelta[1] + rightWheelsYDelta[1]) / 2;
 110      local avgHeight2X                   = (leftWheelsYDelta[leftWheelsLength] + rightWheelsYDelta[rightWheelsLength]) / 2;
 111  
 112      local deltaHeightX                  = avgHeight1X - avgHeight2X;
 113  
 114      return deltaHeightX;
 115  end;

CarriageDamping:getDampingDeltaHeightZ(leftWheelsYDelta, rightWheelsYDelta)

Returns the average position difference between the left and the right wheels.

 119  function CarriageDamping:getDampingDeltaHeightZ(leftWheelsYDelta, rightWheelsYDelta)
 120      local leftWheelsLength              = #leftWheelsYDelta;
 121      local rightWheelsLength             = #rightWheelsYDelta;
 122  
 123      -- calculate the height difference on the Z axis of the carriage
 124      local leftWheelsDeltaYSum           = 0;
 125      for k,v in pairs(leftWheelsYDelta) do
 126          leftWheelsDeltaYSum             = leftWheelsDeltaYSum + v;
 127      end;
 128      local avgHeight1Z                   = leftWheelsDeltaYSum / leftWheelsLength;
 129      
 130      local rightWheelsDeltaYSum          = 0;
 131      for k,v in pairs(rightWheelsYDelta) do
 132          rightWheelsDeltaYSum            = rightWheelsDeltaYSum + v;
 133      end;
 134      local avgHeight2Z                   = rightWheelsDeltaYSum / rightWheelsLength;
 135  
 136      local deltaHeightZ                  = avgHeight1Z - avgHeight2Z;
 137  
 138      return deltaHeightZ;
 139  end;

CarriageDamping:getWheelsPositionDeltaY(orientationPointId, wheelId)

Returns the position difference of the wheel and the orientation point on the y axis.

 143  function CarriageDamping:getWheelsPositionDeltaY(orientationPointId, wheelId)
 144      local _a, orientationPosY, _b       = getWorldPosition(orientationPointId);
 145      local _c, wheelY, _d                = getWorldPosition(wheelId);
 146  
 147      return orientationPosY - wheelY
 148  end;

CarriageDamping:update(dt)

Simulates the damping process using the real time position of the vehicle wheels.

 152  function CarriageDamping:update(dt)
 153      for k,v in pairs(self.carriages) do 
 154          -- get orientation point position
 155          local leftWheelsYDelta      = {};
 156          for k1,v1 in pairs(v.wheels.leftWheels) do
 157              leftWheelsYDelta[k1]    = self:getWheelsPositionDeltaY(v.orientationPoint, v1);
 158          end;
 159  
 160          local rightWheelsYDelta     = {};
 161          for k1,v1 in pairs(v.wheels.rightWheels) do
 162              rightWheelsYDelta[k1]   = self:getWheelsPositionDeltaY(v.orientationPoint, v1);
 163          end;
 164  
 165          local dampingDeltaX         = self:getDampingDeltaHeightX(leftWheelsYDelta, rightWheelsYDelta);
 166          local dampingDeltaZ         = self:getDampingDeltaHeightZ(leftWheelsYDelta, rightWheelsYDelta);
 167  
 168          -- apply the deltas to the local position of the carriage 
 169          self:setCarriageRotation(v.carriage, dampingDeltaX, dampingDeltaZ, v.dampingFactorX, v.dampingFactorZ ,v.dampingIntensity);
 170      end;
 171  end;

All contents of this page may be used for modding use for Winter Resort Simulator - Season 2 only. Any use exceeding this regulation is not permitted.

Copyright (C) HR Innoways, 2021. All Rights Reserved.