fdPhysXScatter.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. unit fdPhysXScatter;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.Messages,
  6. System.SysUtils,
  7. System.Variants,
  8. System.Classes,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.ExtCtrls,
  14. Vcl.StdCtrls,
  15. Stage.VectorTypes,
  16. Stage.VectorGeometry,
  17. Stage.Keyboard,
  18. GLS.Scene,
  19. GLS.Objects,
  20. GLS.Coordinates,
  21. GLS.Cadencer,
  22. GLS.SceneViewer,
  23. GLS.Color,
  24. GLS.XCollection,
  25. GLS.BaseClasses,
  26. GLS.GeomObjects,
  27. GLS.Gui,
  28. GLS.Windows,
  29. PhysX.Import;
  30. type
  31. TFormPhysXScatter = class(TForm)
  32. GLScene: TGLScene;
  33. GLSV: TGLSceneViewer;
  34. Cadencer: TGLCadencer;
  35. Camera: TGLCamera;
  36. Center: TGLDummyCube;
  37. Light: TGLLightSource;
  38. Timer: TTimer;
  39. Edit1: TEdit;
  40. Plane: TGLPlane;
  41. Light1: TGLLightSource;
  42. procedure CadencerProgress(Sender: TObject;
  43. const deltaTime, newTime: Double);
  44. procedure FormCreate(Sender: TObject);
  45. procedure FormDestroy(Sender: TObject);
  46. procedure TimerTimer(Sender: TObject);
  47. procedure GLSVMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  48. private
  49. public
  50. mx, my: Integer;
  51. end;
  52. var
  53. FormPhysXScatter: TFormPhysXScatter;
  54. Plane: dword;
  55. boxes, capsules, spheres: array [0 .. 100] of dword;
  56. GLCube: array [0 .. 100] of TGLCube;
  57. GLCapsule: array [0 .. 100] of TGLCapsule;
  58. GLSphere: array [0 .. 100] of TGLSphere;
  59. type
  60. TVec = packed record
  61. X, Y, Z: single;
  62. end;
  63. type
  64. TQuat = packed record
  65. X, Y, Z, W: single;
  66. end;
  67. implementation //=============================================================
  68. {$R *.dfm}
  69. function ByteToStr(b: byte): string;
  70. const
  71. t: array [0 .. 9] of char = ('0', '1', '2', '3', '4', '5', '6', '7',
  72. '8', '9');
  73. var
  74. e: byte;
  75. d: byte;
  76. s: byte;
  77. begin
  78. s := b div 100;
  79. b := b mod 100;
  80. d := b div 10;
  81. e := b mod 10;
  82. if s > 0 then
  83. begin
  84. result := t[s] + t[d] + t[e];
  85. end
  86. else
  87. begin
  88. if d > 0 then
  89. result := t[d] + t[e]
  90. else
  91. result := t[e];
  92. end;
  93. end;
  94. procedure DemoInit;
  95. var
  96. i: Integer;
  97. dMaj, dMin, dBug: dword;
  98. begin
  99. SDK_Version(@dMaj, @dMin, @dBug);
  100. FormPhysXScatter.Caption := 'PhysX SDK version ' + ByteToStr(dMaj) + '.' +
  101. ByteToStr(dMin) + '.' + ByteToStr(dBug);
  102. InitNx;
  103. CreateGroundPlane(@Plane);
  104. for i := 0 to High(boxes) do
  105. begin
  106. CreateBox(@boxes[i], 0.5, 0.5, 0.5, 10);
  107. SetActorMass(boxes[i], 10);
  108. SetActorGlobalPosition(boxes[i], 0, i * 2, i * 0.5 - 20);
  109. end;
  110. for i := 0 to High(capsules) do
  111. begin
  112. CreateCylinder(@capsules[i], 0.5, 1, 10);
  113. SetActorMass(capsules[i], 10);
  114. SetActorGlobalPosition(capsules[i], 5, i * 2, i * 0.5 - 20);
  115. end;
  116. for i := 0 to High(spheres) do
  117. begin
  118. CreateSphere(@spheres[i], 0.5, 10);
  119. SetActorMass(spheres[i], 10);
  120. SetActorGlobalPosition(spheres[i], -5, i * 2, i * 0.5 - 20);
  121. end;
  122. end;
  123. procedure DrawBox(b: dword; i: Integer);
  124. var
  125. v: TVec;
  126. q: TQuat;
  127. q1: TQuaternion;
  128. begin
  129. GetActorGlobalPosition(b, @v.X, @v.Y, @v.Z);
  130. GetActorGlobalOrientation(b, @q.X, @q.Y, @q.Z, @q.W);
  131. MakeVector(q1.ImagPart, q.X, q.Y, q.Z);
  132. q1.RealPart := q.W;
  133. NormalizeQuaternion(q1);
  134. GLCube[i].Matrix^ := QuaternionToMatrix(q1);
  135. GLCube[i].Position.SetPoint(v.X, v.Y, v.Z);
  136. end;
  137. procedure DrawCapsule(b: dword; i: Integer);
  138. var
  139. v: TVec;
  140. q: TQuat;
  141. q1: TQuaternion;
  142. begin
  143. GetActorGlobalPosition(b, @v.X, @v.Y, @v.Z);
  144. GetActorGlobalOrientation(b, @q.X, @q.Y, @q.Z, @q.W);
  145. MakeVector(q1.ImagPart, q.X, q.Y, q.Z);
  146. q1.RealPart := q.W;
  147. NormalizeQuaternion(q1);
  148. GLCapsule[i].Matrix^ := QuaternionToMatrix(q1);
  149. GLCapsule[i].Pitch(90);
  150. GLCapsule[i].Position.SetPoint(v.X, v.Y, v.Z);
  151. end;
  152. procedure DrawSphere(b: dword; i: Integer);
  153. var
  154. v: TVec;
  155. q: TQuat;
  156. q1: TQuaternion;
  157. begin
  158. GetActorGlobalPosition(b, @v.X, @v.Y, @v.Z);
  159. GetActorGlobalOrientation(b, @q.X, @q.Y, @q.Z, @q.W);
  160. MakeVector(q1.ImagPart, q.X, q.Y, q.Z);
  161. q1.RealPart := q.W;
  162. NormalizeQuaternion(q1);
  163. GLSphere[i].Matrix^ := QuaternionToMatrix(q1);
  164. GLSphere[i].Position.SetPoint(v.X, v.Y, v.Z);
  165. end;
  166. procedure DemoFree;
  167. begin
  168. ReleaseNx;
  169. end;
  170. procedure TFormPhysXScatter.CadencerProgress(Sender: TObject;
  171. const deltaTime, newTime: Double);
  172. var
  173. i: Integer;
  174. begin
  175. SimulateNx(deltaTime);
  176. GetResultsNx;
  177. for i := 0 to 100 do
  178. begin
  179. DrawBox(boxes[i], i);
  180. DrawCapsule(capsules[i], i);
  181. DrawSphere(spheres[i], i);
  182. end;
  183. if IsKeyDown(' ') then
  184. begin
  185. for i := 0 to 100 do
  186. begin
  187. ActorAddForce(boxes[i], 0, 100, 0);
  188. ActorAddForce(capsules[i], 0, 100, 0);
  189. ActorAddForce(spheres[i], 0, 100, 0);
  190. end;
  191. end;
  192. end;
  193. procedure TFormPhysXScatter.FormCreate(Sender: TObject);
  194. var
  195. i: Integer;
  196. begin
  197. for i := 0 to High(GLCube) do
  198. begin
  199. GLCube[i] := TGLCube.CreateAsChild(GLScene.Objects);
  200. end;
  201. for i := 0 to High(GLCapsule) do
  202. begin
  203. GLCapsule[i] := TGLCapsule.CreateAsChild(GLScene.Objects);
  204. end;
  205. for i := 0 to High(GLSphere) do
  206. begin
  207. GLSphere[i] := TGLSphere.CreateAsChild(GLScene.Objects);
  208. end;
  209. DemoInit;
  210. end;
  211. procedure TFormPhysXScatter.FormDestroy(Sender: TObject);
  212. begin
  213. DemoFree;
  214. end;
  215. procedure TFormPhysXScatter.TimerTimer(Sender: TObject);
  216. begin
  217. Edit1.Text := Format('%.1f FPS', [GLSV.FramesPerSecond]);
  218. GLSV.ResetPerformanceMonitor;
  219. end;
  220. procedure TFormPhysXScatter.GLSVMouseMove(Sender: TObject; Shift: TShiftState;
  221. X, Y: Integer);
  222. begin
  223. if Shift <> [] then
  224. begin
  225. Camera.MoveAroundTarget(my - Y, mx - X);
  226. mx := X;
  227. my := Y;
  228. end;
  229. end;
  230. end.