GLS.Ragdoll.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.Ragdoll;
  5. (* Base abstract ragdoll class. Should be extended to use any physics system. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. GLS.Scene,
  10. GLS.PersistentClasses,
  11. GLS.VectorTypes,
  12. GLS.VectorGeometry,
  13. GLS.VectorFileObjects,
  14. GLS.VectorLists,
  15. GLS.Objects;
  16. type
  17. TGLRagdoll = class;
  18. TGLRagdolBone = class;
  19. TGLRagdolJoint = class
  20. end;
  21. TGLRagdolBoneList = class(TGLPersistentObjectList)
  22. private
  23. FRagdoll: TGLRagdoll;
  24. protected
  25. function GetRagdollBone(Index: Integer): TGLRagdolBone;
  26. public
  27. constructor Create(Ragdoll: TGLRagdoll); reintroduce;
  28. destructor Destroy; override;
  29. procedure WriteToFiler(writer: TGLVirtualWriter); override;
  30. procedure ReadFromFiler(reader: TGLVirtualReader); override;
  31. property Ragdoll: TGLRagdoll read FRagdoll;
  32. property Items[Index: Integer]: TGLRagdolBone read GetRagdollBone; default;
  33. end;
  34. TGLRagdolBone = class(TGLRagdolBoneList)
  35. private
  36. FOwner: TGLRagdolBoneList;
  37. FName: String;
  38. FBoneID: Integer; // Refering to TGLActor Bone
  39. FBoundMax: TAffineVector;
  40. FBoundMin: TAffineVector;
  41. FBoundBoneDelta: TAffineVector;
  42. // Stores the diference from the bone.GlobalMatrix to the center of the bone's bounding box
  43. FOrigin: TAffineVector;
  44. FSize: TAffineVector;
  45. FBoneMatrix: TGLMatrix;
  46. FJoint: TGLRagdolJoint;
  47. FOriginalMatrix: TGLMatrix;
  48. // Stores the Bone.GlobalMatrix before the ragdoll start
  49. FReferenceMatrix: TGLMatrix;
  50. // Stores the first bone matrix to be used as reference
  51. FAnchor: TAffineVector; // The position of the joint
  52. procedure CreateBoundingBox;
  53. procedure SetAnchor(const Anchor: TAffineVector);
  54. procedure AlignToSkeleton;
  55. procedure CreateBoundsChild;
  56. procedure StartChild;
  57. procedure AlignChild;
  58. procedure UpdateChild;
  59. procedure StopChild;
  60. protected
  61. function GetRagdollBone(Index: Integer): TGLRagdolBone;
  62. procedure Start; virtual; abstract;
  63. procedure Align; virtual; abstract;
  64. procedure Update; virtual; abstract;
  65. procedure Stop; virtual; abstract;
  66. public
  67. constructor CreateOwned(aOwner: TGLRagdolBoneList);
  68. constructor Create(Ragdoll: TGLRagdoll);
  69. destructor Destroy; override;
  70. procedure WriteToFiler(writer: TGLVirtualWriter); override;
  71. procedure ReadFromFiler(reader: TGLVirtualReader); override;
  72. property Owner: TGLRagdolBoneList read FOwner;
  73. property Name: String read FName write FName;
  74. property BoneID: Integer read FBoneID write FBoneID;
  75. property Origin: TAffineVector read FOrigin;
  76. property Size: TAffineVector read FSize;
  77. property BoneMatrix: TGLMatrix read FBoneMatrix;
  78. property ReferenceMatrix: TGLMatrix read FReferenceMatrix;
  79. property Anchor: TAffineVector read FAnchor;
  80. property Joint: TGLRagdolJoint read FJoint write FJoint;
  81. property Items[Index: Integer]: TGLRagdolBone read GetRagdollBone; default;
  82. end;
  83. TGLRagdoll = class(TGLPersistentObject)
  84. private
  85. FOwner: TGLBaseMesh;
  86. FRootBone: TGLRagdolBone;
  87. FEnabled: Boolean;
  88. FBuilt: Boolean;
  89. public
  90. constructor Create(aOwner: TGLBaseMesh); reintroduce;
  91. destructor Destroy; override;
  92. procedure WriteToFiler(writer: TGLVirtualWriter); override;
  93. procedure ReadFromFiler(reader: TGLVirtualReader); override;
  94. // Must be set before build the ragdoll
  95. procedure SetRootBone(RootBone: TGLRagdolBone);
  96. // Create the bounding box and setup the ragdoll do be started later
  97. procedure BuildRagdoll;
  98. procedure Start;
  99. procedure Update;
  100. procedure Stop;
  101. property Owner: TGLBaseMesh read FOwner;
  102. property RootBone: TGLRagdolBone read FRootBone;
  103. property Enabled: Boolean read FEnabled;
  104. end;
  105. // ------------------------------------------------------------------------
  106. implementation
  107. // ------------------------------------------------------------------------
  108. //--------------------------
  109. // TGLRagdolBoneList
  110. //--------------------------
  111. constructor TGLRagdolBoneList.Create(Ragdoll: TGLRagdoll);
  112. begin
  113. inherited Create;
  114. FRagdoll := Ragdoll;
  115. end;
  116. destructor TGLRagdolBoneList.Destroy;
  117. var
  118. i: Integer;
  119. begin
  120. for i := 0 to Count - 1 do
  121. Items[i].Destroy;
  122. inherited;
  123. end;
  124. function TGLRagdolBoneList.GetRagdollBone(Index: Integer): TGLRagdolBone;
  125. begin
  126. Result := TGLRagdolBone(List^[Index]);
  127. end;
  128. procedure TGLRagdolBoneList.ReadFromFiler(reader: TGLVirtualReader);
  129. begin
  130. inherited;
  131. // Not implemented
  132. end;
  133. procedure TGLRagdolBoneList.WriteToFiler(writer: TGLVirtualWriter);
  134. begin
  135. inherited;
  136. // Not implemented
  137. end;
  138. //----------------------------------
  139. // TGLRagdolBone
  140. //----------------------------------
  141. constructor TGLRagdolBone.Create(Ragdoll: TGLRagdoll);
  142. begin
  143. inherited Create(Ragdoll);
  144. end;
  145. procedure TGLRagdolBone.CreateBoundingBox;
  146. var
  147. bone: TGLSkeletonBone;
  148. i, j: Integer;
  149. BoneVertices: TGLAffineVectorList;
  150. BoneVertex, max, min: TAffineVector;
  151. invMat, mat: TGLMatrix;
  152. begin
  153. bone := Ragdoll.Owner.Skeleton.BoneByID(FBoneID);
  154. // Get all vertices weighted to this bone
  155. BoneVertices := TGLAffineVectorList.Create;
  156. for i := 0 to Ragdoll.Owner.MeshObjects.Count - 1 do
  157. with TGLSkeletonMeshObject(Ragdoll.Owner.MeshObjects[i]) do
  158. for j := 0 to Vertices.Count - 1 do
  159. if bone.BoneID = VerticesBonesWeights[j][0].BoneID then
  160. BoneVertices.FindOrAdd(Vertices[j]);
  161. invMat := bone.GlobalMatrix;
  162. InvertMatrix(invMat);
  163. // For each vertex, get the max and min XYZ (Bounding box)
  164. if BoneVertices.Count > 0 then
  165. begin
  166. BoneVertex := VectorTransform(BoneVertices[0], invMat);
  167. max := BoneVertex;
  168. min := BoneVertex;
  169. for i := 1 to BoneVertices.Count - 1 do
  170. begin
  171. BoneVertex := VectorTransform(BoneVertices[i], invMat);
  172. if (BoneVertex.X > max.X) then
  173. max.X := BoneVertex.X;
  174. if (BoneVertex.Y > max.Y) then
  175. max.Y := BoneVertex.Y;
  176. if (BoneVertex.Z > max.Z) then
  177. max.Z := BoneVertex.Z;
  178. if (BoneVertex.X < min.X) then
  179. min.X := BoneVertex.X;
  180. if (BoneVertex.Y < min.Y) then
  181. min.Y := BoneVertex.Y;
  182. if (BoneVertex.Z < min.Z) then
  183. min.Z := BoneVertex.Z;
  184. end;
  185. FBoundMax := max;
  186. FBoundMin := min;
  187. // Get the origin and subtract from the bone matrix
  188. FBoundBoneDelta := VectorScale(VectorAdd(FBoundMax, FBoundMin), 0.5);
  189. end
  190. else
  191. begin
  192. FBoundMax := NullVector;
  193. FBoundMin := NullVector;
  194. end;
  195. AlignToSkeleton;
  196. FReferenceMatrix := FBoneMatrix;
  197. mat := MatrixMultiply(bone.GlobalMatrix, FRagdoll.Owner.AbsoluteMatrix);
  198. // Set Joint position
  199. SetAnchor(AffineVectorMake(mat.V[3]));
  200. BoneVertices.Free; // NEW1
  201. end;
  202. constructor TGLRagdolBone.CreateOwned(aOwner: TGLRagdolBoneList);
  203. begin
  204. Create(aOwner.Ragdoll);
  205. FOwner := aOwner;
  206. aOwner.Add(Self);
  207. end;
  208. destructor TGLRagdolBone.Destroy;
  209. begin
  210. inherited;
  211. end;
  212. procedure TGLRagdolBone.AlignToSkeleton;
  213. var
  214. o: TAffineVector;
  215. bone: TGLSkeletonBone;
  216. mat, posMat: TGLMatrix;
  217. noBounds: Boolean;
  218. begin
  219. bone := Ragdoll.Owner.Skeleton.BoneByID(FBoneID);
  220. noBounds := VectorIsNull(FBoundMax) and VectorIsNull(FBoundMin);
  221. // Get the bone matrix relative to the Actor matrix
  222. mat := MatrixMultiply(bone.GlobalMatrix, FRagdoll.Owner.AbsoluteMatrix);
  223. // Set Rotation
  224. FBoneMatrix := mat;
  225. NormalizeMatrix(FBoneMatrix);
  226. if (noBounds) then
  227. begin
  228. FOrigin := AffineVectorMake(mat.V[3]);
  229. FSize := AffineVectorMake(0.1, 0.1, 0.1);
  230. end
  231. else
  232. begin
  233. // Set Origin
  234. posMat := mat;
  235. posMat.V[3] := NullHmgVector;
  236. o := VectorTransform(FBoundBoneDelta, posMat);
  237. FOrigin := VectorAdd(AffineVectorMake(mat.V[3]), o);
  238. // Set Size
  239. FSize := VectorScale(VectorSubtract(FBoundMax, FBoundMin), 0.9);
  240. FSize.X := FSize.X * VectorLength(mat.V[0]);
  241. FSize.Y := FSize.Y * VectorLength(mat.V[1]);
  242. FSize.Z := FSize.Z * VectorLength(mat.V[2]);
  243. end;
  244. // Put the origin in the BoneMatrix
  245. FBoneMatrix.V[3] := VectorMake(FOrigin, 1);
  246. end;
  247. function TGLRagdolBone.GetRagdollBone(Index: Integer): TGLRagdolBone;
  248. begin
  249. Result := TGLRagdolBone(List^[Index]);
  250. end;
  251. procedure TGLRagdolBone.ReadFromFiler(reader: TGLVirtualReader);
  252. begin
  253. inherited;
  254. end;
  255. procedure TGLRagdolBone.StartChild;
  256. var
  257. i: Integer;
  258. begin
  259. FOriginalMatrix := Ragdoll.Owner.Skeleton.BoneByID(FBoneID).GlobalMatrix;
  260. AlignToSkeleton;
  261. Start;
  262. for i := 0 to Count - 1 do
  263. Items[i].StartChild;
  264. end;
  265. procedure TGLRagdolBone.UpdateChild;
  266. var
  267. i: Integer;
  268. begin
  269. Update;
  270. for i := 0 to Count - 1 do
  271. Items[i].UpdateChild;
  272. end;
  273. procedure TGLRagdolBone.WriteToFiler(writer: TGLVirtualWriter);
  274. begin
  275. inherited;
  276. end;
  277. procedure TGLRagdolBone.StopChild;
  278. var
  279. i: Integer;
  280. begin
  281. Stop;
  282. Ragdoll.Owner.Skeleton.BoneByID(FBoneID).SetGlobalMatrix(FOriginalMatrix);
  283. for i := 0 to Count - 1 do
  284. Items[i].StopChild;
  285. end;
  286. procedure TGLRagdolBone.CreateBoundsChild;
  287. var
  288. i: Integer;
  289. begin
  290. CreateBoundingBox;
  291. for i := 0 to Count - 1 do
  292. Items[i].CreateBoundsChild;
  293. end;
  294. procedure TGLRagdolBone.SetAnchor(const Anchor: TAffineVector);
  295. begin
  296. FAnchor := Anchor;
  297. end;
  298. procedure TGLRagdolBone.AlignChild;
  299. var
  300. i: Integer;
  301. begin
  302. Align;
  303. Update;
  304. for i := 0 to Count - 1 do
  305. Items[i].AlignChild;
  306. end;
  307. { TGLRagdoll }
  308. constructor TGLRagdoll.Create(aOwner: TGLBaseMesh);
  309. begin
  310. FOwner := aOwner;
  311. FEnabled := False;
  312. FBuilt := False;
  313. end;
  314. destructor TGLRagdoll.Destroy;
  315. begin
  316. if FEnabled then
  317. Stop;
  318. inherited Destroy;
  319. end;
  320. procedure TGLRagdoll.ReadFromFiler(reader: TGLVirtualReader);
  321. begin
  322. inherited;
  323. end;
  324. procedure TGLRagdoll.SetRootBone(RootBone: TGLRagdolBone);
  325. begin
  326. FRootBone := RootBone;
  327. end;
  328. procedure TGLRagdoll.Start;
  329. begin
  330. Assert(FBuilt, 'First you need to build the ragdoll. BuildRagdoll;');
  331. if (FEnabled) then
  332. Exit;
  333. FEnabled := True;
  334. // First start the ragdoll in the reference position
  335. RootBone.StartChild;
  336. // Now align it to the animation
  337. RootBone.AlignChild;
  338. // Now it recalculate the vertices to use as reference
  339. FOwner.Skeleton.StartRagDoll;
  340. end;
  341. procedure TGLRagdoll.Update;
  342. begin
  343. if FEnabled then
  344. begin
  345. RootBone.UpdateChild;
  346. FOwner.Skeleton.MorphMesh(True);
  347. end;
  348. end;
  349. procedure TGLRagdoll.Stop;
  350. begin
  351. if not FEnabled then
  352. Exit;
  353. FEnabled := False;
  354. RootBone.StopChild;
  355. // Restore the old information
  356. FOwner.Skeleton.StopRagDoll;
  357. FOwner.Skeleton.MorphMesh(True);
  358. end;
  359. procedure TGLRagdoll.WriteToFiler(writer: TGLVirtualWriter);
  360. begin
  361. inherited;
  362. end;
  363. procedure TGLRagdoll.BuildRagdoll;
  364. begin
  365. Assert(RootBone <> nil,
  366. 'First you need to set the root bone. SetRootBone();');
  367. RootBone.CreateBoundsChild;
  368. FBuilt := True;
  369. end;
  370. end.