GLS.PhysicsForces.pas 12 KB

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