GLS.PhysForces.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //
  2. // The graphics engine GLScene https://github.com/glscene
  3. //
  4. unit GLS.PhysForces;
  5. interface
  6. uses
  7. System.Classes,
  8. Vcl.Dialogs,
  9. GLScene.VectorTypes,
  10. GLS.XCollection,
  11. GLS.Scene,
  12. GLScene.VectorGeometry,
  13. GLS.Behaviours,
  14. GLS.Coordinates,
  15. GLScene.Strings;
  16. type
  17. TGLForce = class;
  18. TGLForceType = (ftHookes, ftGravitation, ftCustom);
  19. TOnCustomForce = procedure() of object;
  20. TGLForce = class(TXCollectionItem)
  21. private
  22. fObject1: TGLBaseSceneObject;
  23. fObject2: TGLBaseSceneObject;
  24. fposition1: TGLCoordinates;
  25. fposition2: TGLCoordinates;
  26. object1Name: String;
  27. object2Name: String;
  28. // fOnCustomForce: TOnCustomForce;
  29. protected
  30. procedure Loaded; override;
  31. procedure SetName(const val: String); override;
  32. (* Returns the TGLBaseSceneObject on which the behaviour should be applied.
  33. Does NOT check for nil owners *)
  34. // function OwnerBaseSceneObject : TGLBaseSceneObject;
  35. public
  36. (* constructor Create(Collection: TCollection);override; *)
  37. // Override this function to write subclass data.
  38. procedure WriteToFiler(writer: TWriter); override;
  39. // Override this function to read subclass data.
  40. procedure ReadFromFiler(reader: TReader); override;
  41. constructor Create(aOwner: TXCollection); override;
  42. destructor Destroy; override;
  43. procedure Assign(Source: TPersistent); override;
  44. class function FriendlyName: String; override;
  45. class function FriendlyDescription: String; override;
  46. class function UniqueItem: Boolean; override;
  47. procedure SetObject1(const val: TGLBaseSceneObject);
  48. procedure SetObject2(const val: TGLBaseSceneObject);
  49. procedure SetPosition1(const val: TGLCoordinates);
  50. procedure SetPosition2(const val: TGLCoordinates);
  51. function CalculateForce(): TAffineVector; virtual;
  52. published
  53. property Object1: TGLBaseSceneObject read fObject1 write SetObject1;
  54. property Object2: TGLBaseSceneObject read fObject2 write SetObject2;
  55. property Position1: TGLCoordinates read fposition1 write SetPosition1;
  56. property Position2: TGLCoordinates read fposition2 write SetPosition2;
  57. // property OnCustomForce:TOnCustomForce read fOnCustomForce write fOnCustomForce;
  58. end;
  59. TGLHookesSpring = class(TGLForce)
  60. private
  61. fNaturalLength: Real;
  62. fElasticity: Real;
  63. fLength: Real;
  64. fExtension: Real;
  65. fDamping: TGLDamping;
  66. public
  67. procedure WriteToFiler(writer: TWriter); override;
  68. procedure ReadFromFiler(reader: TReader); override;
  69. constructor Create(aOwner: TXCollection); override;
  70. destructor Destroy; override;
  71. class function FriendlyName: String; override;
  72. class function FriendlyDescription: String; override;
  73. class function UniqueItem: Boolean; override;
  74. procedure SetDamping(const val: TGLDamping);
  75. function CalculateForce(): TAffineVector; override;
  76. published
  77. property NaturalLength: Real read fNaturalLength write fNaturalLength;
  78. property Elasticity: Real read fElasticity write fElasticity;
  79. property Damping: TGLDamping read fDamping write SetDamping;
  80. // property Name;
  81. end;
  82. TGLHookesString = class(TGLHookesSpring)
  83. protected
  84. // procedure WriteToFiler(writer : TWriter); override;
  85. // procedure ReadFromFiler(reader : TReader); override;
  86. public
  87. constructor Create(aOwner: TXCollection); override;
  88. destructor Destroy; override;
  89. class function FriendlyName: String; override;
  90. class function FriendlyDescription: String; override;
  91. class function UniqueItem: Boolean; override;
  92. function CalculateForce(): TAffineVector; override;
  93. end;
  94. implementation // -------------------------------------------------------------
  95. uses
  96. GLS.PhysInertias,
  97. GLS.PhysManager;
  98. constructor TGLForce.Create(aOwner: TXCollection);
  99. begin
  100. inherited; // Create(aOwner)
  101. fposition1 := TGLCoordinates.CreateInitialized(Self, NullHmgVector, csVector);
  102. fposition2 := TGLCoordinates.CreateInitialized(Self, NullHmgVector, csVector);
  103. // fObject1:=TGLBaseSceneObject.Create(Self);
  104. // fObject2:=TGLBaseSceneObject.Create(Self);
  105. end;
  106. destructor TGLForce.Destroy;
  107. begin
  108. fposition1.Free();
  109. fposition2.Free();
  110. // SetObject1(nil);
  111. // SetObject2(nil);
  112. // fObject1.Free();
  113. // fObject2.Free();
  114. inherited Destroy;
  115. end;
  116. procedure TGLForce.Assign(Source: TPersistent);
  117. begin
  118. // inherited Assign(Source);
  119. fposition1.Assign(TGLForce(Source).fposition1);
  120. fposition2.Assign(TGLForce(Source).fposition2);
  121. Object1 := TGLForce(Source).Object1;
  122. Object2 := TGLForce(Source).Object2;
  123. inherited Assign(Source);
  124. end;
  125. procedure TGLForce.SetObject1(const val: TGLBaseSceneObject);
  126. begin
  127. // if val.Behaviours.IndexOfClass(TGLBaseInertia) >=0 then
  128. fObject1 := val
  129. // else
  130. // messagedlg('Object1 does not have an inertia behaviour',mtWarning,[mbOk],0);
  131. end;
  132. procedure TGLForce.SetObject2(const val: TGLBaseSceneObject);
  133. begin
  134. // if val.Behaviours.IndexOfClass(TGLBaseInertia) >=0 then
  135. fObject2 := val
  136. // else
  137. // messagedlg('Object2 does not have an inertia behaviour',mtWarning,[mbOk],0);
  138. end;
  139. procedure TGLForce.SetPosition1(const val: TGLCoordinates);
  140. begin
  141. fposition1.Assign(val); // DB101
  142. end;
  143. procedure TGLForce.SetPosition2(const val: TGLCoordinates);
  144. begin
  145. fposition2.Assign(val);
  146. end;
  147. procedure TGLForce.Loaded;
  148. var
  149. PhysMan: TGLPhysManager;
  150. begin
  151. inherited Loaded;
  152. // not nice at all!!!
  153. // assumes owner is TGLForces belonging to TGLPhysicsManager
  154. PhysMan := TGLPhysManager(Self.Owner.Owner);
  155. if (object1Name <> '') then
  156. begin
  157. // PhysMan:=TGLPhysicsManager(Self.Owner.Owner);
  158. fObject1 := PhysMan.FindObjectByName(object1Name);
  159. // fObject1:=TGLBaseSceneObject(FindComponent(Object1Name));
  160. // Object1Name:='';
  161. end;
  162. if object2Name <> '' then
  163. begin
  164. fObject2 := PhysMan.FindObjectByName(object2Name);
  165. // Object2Name:='';
  166. end;
  167. end;
  168. class function TGLForce.FriendlyName: String;
  169. begin
  170. Result := 'Force';
  171. end;
  172. class function TGLForce.FriendlyDescription: String;
  173. begin
  174. Result := 'Physics Force';
  175. end;
  176. class function TGLForce.UniqueItem: Boolean;
  177. begin
  178. Result := false;
  179. end;
  180. procedure TGLForce.WriteToFiler(writer: TWriter);
  181. begin
  182. inherited WriteToFiler(writer);
  183. // messagedlg('Writing to filer'+GetNamePath,mtInformation,[mbOk],0);
  184. with writer do
  185. begin
  186. fposition1.WriteToFiler(writer);
  187. fposition2.WriteToFiler(writer);
  188. if Assigned(fObject1) then
  189. WriteString(fObject1.GetNamePath)
  190. else
  191. WriteString('');
  192. if Assigned(fObject2) then
  193. WriteString(fObject2.GetNamePath)
  194. else
  195. WriteString('');
  196. // WriteString(Object2Name);
  197. end;
  198. end;
  199. procedure TGLForce.ReadFromFiler(reader: TReader);
  200. begin
  201. // messagedlg('Reading from filer'+GetNamePath,mtInformation,[mbOk],0);
  202. inherited ReadFromFiler(reader);
  203. with reader do
  204. begin
  205. fposition1.ReadFromFiler(reader);
  206. fposition2.ReadFromFiler(reader);
  207. object1Name := ReadString;
  208. fObject1 := nil;
  209. object2Name := ReadString;
  210. fObject2 := nil;
  211. end;
  212. // Loaded;
  213. end;
  214. procedure TGLForce.SetName(const val: String);
  215. begin
  216. inherited SetName(val);
  217. // if Assigned(vGLBehaviourNameChangeEvent) then
  218. // vGLBehaviourNameChangeEvent(Self);
  219. end;
  220. function TGLForce.CalculateForce(): TAffineVector;
  221. begin
  222. //
  223. end;
  224. constructor TGLHookesSpring.Create(aOwner: TXCollection);
  225. begin
  226. inherited Create(aOwner);
  227. fNaturalLength := 1;
  228. fElasticity := 1;
  229. fDamping := TGLDamping.Create(Self);
  230. end;
  231. destructor TGLHookesSpring.Destroy;
  232. begin
  233. fDamping.Free;
  234. inherited Destroy;
  235. end;
  236. procedure TGLHookesSpring.WriteToFiler(writer: TWriter);
  237. begin
  238. inherited;
  239. with writer do
  240. begin
  241. WriteFloat(fNaturalLength); // :Real;
  242. WriteFloat(fElasticity); // :Real;
  243. WriteFloat(fLength); // :Real;
  244. WriteFloat(fExtension); // :Real;
  245. fDamping.WriteToFiler(writer);
  246. end;
  247. end;
  248. procedure TGLHookesSpring.ReadFromFiler(reader: TReader);
  249. begin
  250. inherited;
  251. with reader do
  252. begin
  253. fNaturalLength := ReadFloat(); // :Real;
  254. fElasticity := ReadFloat(); // :Real;
  255. fLength := ReadFloat(); // :Real;
  256. fExtension := ReadFloat(); // :Real;
  257. fDamping.ReadFromFiler(reader);
  258. end;
  259. end;
  260. procedure TGLHookesSpring.SetDamping(const val: TGLDamping);
  261. begin
  262. fDamping.Assign(val);
  263. end;
  264. function TGLHookesSpring.CalculateForce(): TAffineVector;
  265. var
  266. rvector, vvector: TAffineVector;
  267. Inertia1, Inertia2: TGLParticleInertia;
  268. begin
  269. if (fObject1 = nil) or (fObject2 = nil) then
  270. Exit;
  271. Inertia2 := TGLParticleInertia
  272. (Object2.Behaviours.GetByClass(TGLParticleInertia));
  273. Inertia1 := TGLParticleInertia
  274. (Object1.Behaviours.GetByClass(TGLParticleInertia));
  275. // rvector:=VectorSubtract({VectorAdd(Object2.Position.asAffineVector,}VectorTransform(Position2.AsAffineVector,Object2.Matrix{)}),
  276. // {VectorAdd(Object1.Position.asAffineVector,}VectorTransform(Position1.AsAffineVector,Object1.Matrix){)});
  277. rvector := VectorSubtract(Object2.LocalToAbsolute(Position2.AsAffineVector),
  278. Object1.LocalToAbsolute(Position1.AsAffineVector));
  279. (*
  280. rvector:=VectorSubtract(VectorAdd(Object2.Position.asAffineVector,VectorTransform(Position2.AsAffineVector,Object2.Matrix)),
  281. VectorAdd(Object1.Position.asAffineVector,VectorTransform(Position1.AsAffineVector,Object1.Matrix)));
  282. *)
  283. fLength := VectorLength(rvector);
  284. NormalizeVector(rvector);
  285. fExtension := fLength - fNaturalLength;
  286. // fDamping.Calculate();
  287. Result := VectorScale(rvector, fElasticity * fExtension / fNaturalLength);
  288. if Assigned(Inertia2) then
  289. Inertia2.ApplyForce(Position2.AsAffineVector, VectorNegate(Result));
  290. if Assigned(Inertia1) then
  291. Inertia1.ApplyForce(Position1.AsAffineVector, Result);
  292. // TGLInertia(Object1.Behaviours.GetByClass(TGLInertia)).ApplyForce(Position1.AsAffineVector,Result);
  293. end;
  294. class function TGLHookesSpring.FriendlyName: String;
  295. begin
  296. Result := 'Hookes Spring';
  297. end;
  298. class function TGLHookesSpring.FriendlyDescription: String;
  299. begin
  300. Result := 'A spring obeying Hookes Law';
  301. end;
  302. class function TGLHookesSpring.UniqueItem: Boolean;
  303. begin
  304. Result := false;
  305. end;
  306. constructor TGLHookesString.Create(aOwner: TXCollection);
  307. begin
  308. inherited Create(aOwner);
  309. end;
  310. destructor TGLHookesString.Destroy;
  311. begin
  312. inherited Destroy;
  313. end;
  314. class function TGLHookesString.FriendlyName: String;
  315. begin
  316. Result := 'Hookes String';
  317. end;
  318. class function TGLHookesString.FriendlyDescription: String;
  319. begin
  320. Result := 'A string (that can go slack) obeying Hookes Law';
  321. end;
  322. class function TGLHookesString.UniqueItem: Boolean;
  323. begin
  324. Result := false;
  325. end;
  326. function TGLHookesString.CalculateForce(): TAffineVector;
  327. var
  328. rvector: TAffineVector;
  329. Inertia1, Inertia2: TGLParticleInertia;
  330. begin
  331. if (Object1 = nil) or (Object2 = nil) then
  332. Exit;
  333. rvector := VectorSubtract(Object2.LocalToAbsolute(Position2.AsAffineVector),
  334. Object1.LocalToAbsolute(Position1.AsAffineVector));
  335. // VectorAdd(Object2.Position.asAffineVector,VectorTransform(Object2.Position2.AsAffineVector,Object2.Matrix)),
  336. // VectorAdd(Object1.Position.asAffineVector,VectorTransform(Position1.AsAffineVector,Object1.Matrix)));
  337. fLength := VectorLength(rvector);
  338. if (fLength < fNaturalLength) then
  339. Result := NullVector
  340. else
  341. begin
  342. NormalizeVector(rvector);
  343. fExtension := fLength - fNaturalLength;
  344. Result := VectorScale(rvector, fElasticity * fExtension / fNaturalLength);
  345. // TGLInertia(Object2.Behaviours.GetByClass(TGLInertia)).ApplyForce(Position2.AsAffineVector,VectorNegate(Result));
  346. // TGLInertia(Object1.Behaviours.GetByClass(TGLInertia)).ApplyForce(Position1.AsAffineVector,Result);
  347. Inertia2 := TGLParticleInertia
  348. (Object2.Behaviours.GetByClass(TGLParticleInertia));
  349. Inertia1 := TGLParticleInertia
  350. (Object1.Behaviours.GetByClass(TGLParticleInertia));
  351. if Assigned(Inertia2) then
  352. Inertia2.ApplyForce(Position2.AsAffineVector, VectorNegate(Result));
  353. if Assigned(Inertia1) then
  354. Inertia1.ApplyForce(Position1.AsAffineVector, Result);
  355. end;
  356. // Result:= inherited CalculateForce();
  357. // if (fLength < fNaturalLength) then Result:=NullVector;
  358. end;
  359. initialization // -------------------------------------------------------------
  360. RegisterXCollectionItemClass(TGLHookesSpring);
  361. RegisterXCollectionItemClass(TGLHookesString);
  362. end.