Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_PowerLineJointEntity.c
Go to the documentation of this file.
1 [EntityEditorProps(category: "GameScripted/Spectating", description: "Spectator camera, controlled by arrows and numpad keys", color: "0 0 255 255")]
2 class SCR_PowerLineJointEntityClass: GenericEntityClass
3 {
4 };
5 
6 //------------------------------------------------------------------------------------------------
8 {
9  [Attribute("1.0")]
10  protected float m_fSagInMeters;
11 
12  [Attribute("0.1")]
13  protected float m_AngleRad;
14 
15  [Attribute("0.5")]
16  protected float m_MovementSpeed;
17 
18  #ifdef WORKBENCH
19  protected static const int SEGMENTS = 64;
20  protected const int POINTS = SEGMENTS + 1;
21 
22  protected vector m_DebugLine[POINTS];
23  protected vector m_DebugLinePivots[POINTS];
24  protected vector m_vDirectionLevelNorm;
25  protected vector m_vConnectedJointPos;
26  protected ref array<vector> m_Curve = {};
27  protected ref array<vector> m_CurvePivots = {};
28  protected ref array<ref Shape> m_aShapes = {};
29 
30  private float m_fTimeAccu;
31 
32 
33  //------------------------------------------------------------------------------------------------
34  bool GetJoint()
35  {
36  WorldEditorAPI api = _WB_GetEditorAPI();
37  IEntitySource entSrc = api.EntityToSource(this);
38  int childCount = entSrc.GetNumChildren();
39 
40  if (childCount != 0)
41  {
42  for (int i = 0; i < childCount; i++)
43  {
44  IEntitySource childSrc = entSrc.GetChild(i);
45  IEntity child = api.SourceToEntity(childSrc);
46  m_vConnectedJointPos = child.GetOrigin();
47  //Print(m_vConnectedJointPos);
48  }
49  return true;
50  }
51  else
52  {
53  IEntitySource parent = entSrc.GetParent();
54  SCR_PowerLineJointEntity parentEnt = SCR_PowerLineJointEntity.Cast(api.SourceToEntity(parent));
55  //parentEnt.OnChildMoved();
56  return false;
57  }
58  }
59 
60  //------------------------------------------------------------------------------------------------
61  void OnChildMoved()
62  {
63  //DrawDebug();
64  }
65 
66  //------------------------------------------------------------------------------------------------
67  void GenerateCurve()
68  {
69  m_Curve.Clear();
70  m_CurvePivots.Clear();
71  if(m_fSagInMeters <= 0)
72  m_fSagInMeters = 0.0001;
73  vector posThis = GetOrigin();
74  //m_vConnectedJointPos = posThis + "50 10 0";//remove
75 
76  vector jointPos1, jointPos2;
77  if(posThis[1] <= m_vConnectedJointPos[1])
78  {
79  jointPos1 = posThis;
80  jointPos2 = m_vConnectedJointPos;
81  }
82  else
83  {
84  jointPos1 = m_vConnectedJointPos;
85  jointPos2 = posThis;
86  }
87 
88  m_vDirectionLevelNorm = jointPos2 - jointPos1;
89  m_vDirectionLevelNorm[1] = 0;
90  m_vDirectionLevelNorm.Normalize();
91 
92  float heightDifferenceM = jointPos2[1] - jointPos1[1];
93 
94  vector jointPos1Adj = jointPos1;//this pos adjusted to height of the connected joint
95  jointPos1Adj[1] = jointPos2[1];
96  float distanceLevel = vector.Distance(jointPos1Adj, jointPos2);
97 
98  float s1 = m_fSagInMeters / distanceLevel;//step 1
99 
100  float c = (0.5 * 0.5) / s1;//step 2
101 
102  float s2 = (heightDifferenceM / distanceLevel) + s1;//step 3
103 
104  float x2 = Math.Sqrt(s2 * c);// step 4
105 
106  float xc = 0.5 + x2; //step 5
107 
108  float x1f = (0.5 / xc) * -1;//step 6
109  float x2f = x2 / xc;//step 6
110 
111 
112  float segmentSize = distanceLevel / SEGMENTS;
113 
114  int segment = 0;
115  array<float> curveYValues = {};
116 
117  float step = 1 / SEGMENTS;
118  float value = 0;
119 
120  for(int i = 0; i < POINTS;i++)
121  {
122  //Print(i);
123  float x = Math.Lerp(x1f, x2f, value) * xc;
124  value += step;
125  //Print(value);
126  //Print(x);
127  float y = (x*x) / c;
128  //Print(y);
129  curveYValues.Insert(y);
130  }
131 
132 
133  for(int i = 0; i < POINTS; i++)
134  {
135  float curveY = curveYValues.Get(i);
136  float y = curveY * distanceLevel;
137  y -= m_fSagInMeters;
138 
139 
140  vector pos = m_vDirectionLevelNorm * (segmentSize * i);
141  pos[1] = pos[1] + y;
142  pos = pos + jointPos1;
143  m_Curve.Insert(pos);
144  //Print(pos);
145  }
146  }
147 
148  //------------------------------------------------------------------------------------------------
149  void GetPivots()
150  {
151  vector firstPoint = m_Curve[0];
152  vector lastPoint = m_Curve[SEGMENTS];
153 
154  for(int i = 0; i <= POINTS; i++)
155  {
156  vector pivot = vector.Lerp(firstPoint,lastPoint,(float)i/SEGMENTS);
157  //Print((float)i/POINTS);
158  m_CurvePivots.Insert(pivot);
159  //Print(pos);
160  }
161  }
162 
163  //------------------------------------------------------------------------------------------------
164  void DrawDebugShape()
165  {
166  for(int i = 0; i < POINTS; i++)
167  {
168  m_DebugLine[i] = m_Curve[i];
169  //Print(m_DebugLine[i]);
170  }
171  //m_aShapes.Insert(Shape.CreateLines(ARGB(255, 255, 255, 0), 0, m_DebugLine, POINTS));
172  Shape.CreateLines(ARGB(255, 5, 5, 5), ShapeFlags.ONCE, m_DebugLine, POINTS);
173  //m_aShapes.Insert(Shape.CreateSphere(ARGB(255, 255, 0, 0), ShapeFlags.NOZBUFFER, m_vConnectedJointPos, 0.1));
174  }
175 
176  //------------------------------------------------------------------------------------------------
177  void DrawDebugShapePivots()
178  {
179  for(int i = 0; i < POINTS; i++)
180  {
181  m_DebugLinePivots[i] = m_CurvePivots[i];
182  }
183  m_aShapes.Insert(Shape.CreateLines(ARGB(123, 0, 0, 0), 0, m_DebugLinePivots, POINTS));
184  //m_aShapes.Insert(Shape.CreateSphere(ARGB(255, 255, 0, 0), ShapeFlags.NOZBUFFER, m_vConnectedJointPos, 0.1));
185  }
186 
187  //------------------------------------------------------------------------------------------------
188  void ModifyShape(float timeSlice)
189  {
190  float speed = m_MovementSpeed;
191  m_fTimeAccu += speed * timeSlice;
192  //Print(m_fTimeAccu);
193  float val1 = Math.Sin(m_fTimeAccu) * 0.5 + 0.5;
194  float val2 = Math.Lerp(-m_AngleRad, m_AngleRad, val1);
195 
196  float sin = Math.Sin(val2);
197  float cos = Math.Cos(val2);
198 
199  //rotation matrix start
200  vector rotMat[4];
201  rotMat[0][0] = cos;
202  rotMat[0][1] = sin;
203  rotMat[0][2] = 0;
204 
205  rotMat[1][0] = -sin;
206  rotMat[1][1] = cos;
207  rotMat[1][2] = 0;
208 
209  rotMat[2][0] = 0;
210  rotMat[2][1] = 0;
211  rotMat[2][2] = 1;
212 
213  rotMat[3][0] = 0;
214  rotMat[3][1] = 0;
215  rotMat[3][2] = 0;
216  //rotation matrix end
217 
218  vector rightDir = m_vDirectionLevelNorm * "0 1 0";
219 
220  vector pointMat[4];
221  pointMat[0] = rightDir.Normalized();
222  pointMat[1] = "0 -1 0";
223  pointMat[2] = m_vDirectionLevelNorm;
224  //Print(pointMat);
225  vector resultMat[4];
226  Math3D.MatrixMultiply4(pointMat, rotMat, resultMat);
227  //Print(resultMat);
228  for(int i = 0; i < POINTS; i++)
229  {
230  float length = vector.Distance(m_Curve[i], m_CurvePivots[i]);
231  vector v = resultMat[1] * length + m_CurvePivots[i];
232  //Print(v);
233  m_Curve[i] = v;
234  }
235 
236 
237  }
238 
239  //------------------------------------------------------------------------------------------------
240  void DrawDebug(float timeSlice)
241  {
242  if(m_vConnectedJointPos != "0 0 0")
243  {
244  ModifyShape(timeSlice);
245  DrawDebugShape();
246  }
247 
248  }
249 
250  //------------------------------------------------------------------------------------------------
251 
252  override bool _WB_OnKeyChanged(BaseContainer src, string key, BaseContainerList ownerContainers, IEntity parent)
253  {
254  if (key == "m_fSagInMeters")
255  {
256  src.Get(key, m_fSagInMeters);
257  }
258 
259  if (key == "m_AngleRad")
260  {
261  src.Get(key, m_AngleRad);
262  }
263 
264  if (key == "m_MovementSpeed")
265  {
266  src.Get(key, m_MovementSpeed);
267  }
268 
269  if( GetJoint() )
270  {
271  if(m_vConnectedJointPos != "0 0 0")
272  {
273  m_aShapes.Clear();
274  GenerateCurve();
275  GetPivots();
276  //DrawDebugShapePivots();
277  }
278  }
279  m_MovementSpeed = Math.RandomFloat(m_MovementSpeed * 0.9, m_MovementSpeed * 1.1);
280  m_fTimeAccu = Math.RandomFloat(0, 100);
281  return true;
282  }
283 
284  //------------------------------------------------------------------------------------------------
285  override void _WB_AfterWorldUpdate(float timeSlice)
286  {
287  if(m_vConnectedJointPos == "0 0 0")
288  {
289  if( GetJoint() )
290  {
291  if(m_vConnectedJointPos != "0 0 0")
292  {
293  m_aShapes.Clear();
294  GenerateCurve();
295  GetPivots();
296  //DrawDebugShapePivots();
297  }
298  }
299  }
300 
301  DrawDebug(timeSlice);
302  }
303 
304 
305 
306  //------------------------------------------------------------------------------------------------
307  void SCR_PowerLineJointEntity(IEntitySource src, IEntity parent)
308  {
309  m_fTimeAccu = Math.RandomFloat(0, 100);
310  m_MovementSpeed = Math.RandomFloat(m_MovementSpeed * 0.9, m_MovementSpeed * 1.1);
311  //Print(m_AngleRad);
312  }
313 
314  //------------------------------------------------------------------------------------------------
316  {
317  m_aShapes.Clear();
318  }
319  #endif
320 };
SCR_PowerLineJointEntityClass
Definition: SCR_PowerLineJointEntity.c:2
EntityEditorProps
enum EQueryType EntityEditorProps(category:"GameScripted/Sound", description:"THIS IS THE SCRIPT DESCRIPTION.", color:"0 0 255 255")
Definition: SCR_AmbientSoundsComponent.c:12
DrawDebugShape
protected void DrawDebugShape(bool draw)
Definition: SCR_ScenarioFrameworkLayerBase.c:829
SCR_PowerLineJointEntity
Definition: SCR_PowerLineJointEntity.c:7
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
GetOrigin
vector GetOrigin()
Definition: SCR_AIUtilityComponent.c:279
Attribute
typedef Attribute
Post-process effect of scripted camera.
category
params category
Definition: SCR_VehicleDamageManagerComponent.c:180