fOdeSimple.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. unit fOdeSimple;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. Vcl.Graphics,
  7. Vcl.Controls,
  8. Vcl.Forms,
  9. Vcl.Dialogs,
  10. Vcl.StdCtrls,
  11. Vcl.ComCtrls,
  12. Vcl.ExtCtrls,
  13. GLS.Scene,
  14. GLS.Objects,
  15. GLS.GeomObjects,
  16. GLS.Cadencer,
  17. GLS.SceneViewer,
  18. GLS.ShadowPlane,
  19. GLS.Graph, GLS.VectorTypes,
  20. GLS.VectorGeometry,
  21. GLS.Coordinates,
  22. GLS.BaseClasses,
  23. Physics.ODEManager;
  24. type
  25. TFormOdeSimple = class(TForm)
  26. GLScene1: TGLScene;
  27. GLSceneViewer1: TGLSceneViewer;
  28. GLCadencer1: TGLCadencer;
  29. GLCamera1: TGLCamera;
  30. GLDummyCube1: TGLDummyCube;
  31. GLLightSource1: TGLLightSource;
  32. ODEObjects: TGLDummyCube;
  33. Panel1: TPanel;
  34. GLODEManager1: TGLODEManager;
  35. Spawn: TButton;
  36. cbObjects: TComboBox;
  37. Label1: TLabel;
  38. GLRenderPoint1: TGLRenderPoint;
  39. GLHeightField1: TGLHeightField;
  40. chbElements: TCheckBox;
  41. chbContacts: TCheckBox;
  42. TrackBar1: TTrackBar;
  43. Label2: TLabel;
  44. GLPlane1: TGLPlane;
  45. cbSurface: TComboBox;
  46. Label3: TLabel;
  47. procedure GLCadencer1Progress(Sender: TObject; const deltaTime,
  48. newTime: Double);
  49. procedure GLSceneViewer1MouseDown(Sender: TObject;
  50. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  51. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  52. X, Y: Integer);
  53. procedure SpawnClick(Sender: TObject);
  54. procedure GLHeightField1GetHeight(const x, y: Single; var z: Single;
  55. var Color: TVector4f; var TexPoint: TTexPoint);
  56. procedure TrackBar1Change(Sender: TObject);
  57. procedure cbSurfaceChange(Sender: TObject);
  58. procedure chbElementsClick(Sender: TObject);
  59. procedure chbContactsClick(Sender: TObject);
  60. private
  61. public
  62. mx, my: Integer;
  63. procedure DoSphere;
  64. procedure DoBox;
  65. procedure DoCapsule;
  66. procedure DoCylinder;
  67. // CONE IS CURRENTLY UNSUPPOETED FOR ODE 0.9
  68. // procedure DoCone;
  69. end;
  70. var
  71. FormOdeSimple: TFormOdeSimple;
  72. implementation
  73. {$R *.dfm}
  74. procedure TFormOdeSimple.GLCadencer1Progress(Sender: TObject; const deltaTime,
  75. newTime: Double);
  76. begin
  77. GLODEManager1.Step(deltaTime);
  78. end;
  79. procedure TFormOdeSimple.GLSceneViewer1MouseDown(Sender: TObject;
  80. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  81. begin
  82. mx := X;
  83. my := Y;
  84. end;
  85. procedure TFormOdeSimple.GLSceneViewer1MouseMove(Sender: TObject;
  86. Shift: TShiftState; X, Y: Integer);
  87. begin
  88. if ssLeft in Shift then
  89. GLCamera1.MoveAroundTarget(my - Y, mx - X);
  90. mx := X;
  91. my := Y;
  92. end;
  93. procedure TFormOdeSimple.SpawnClick(Sender: TObject);
  94. begin
  95. case cbObjects.ItemIndex of
  96. 0: DoSphere;
  97. 1: DoBox;
  98. 2: DoCapsule;
  99. 3: DoCylinder;
  100. // 4 : DoCone; // CONE IS CURRENTLY UNSUPPOETED FOR ODE 0.9
  101. end;
  102. end;
  103. procedure TFormOdeSimple.DoSphere;
  104. var
  105. sphere: TGLSphere;
  106. dyn: TGLODEDynamic;
  107. begin
  108. sphere := TGLSphere(ODEObjects.AddNewChild(TGLSphere));
  109. sphere.Position.SetPoint(5 * random - 2.5, 2, 5 * random - 2.5);
  110. sphere.Radius := 0.3 * (random + 1);
  111. sphere.Material.FrontProperties.Diffuse.RandomColor;
  112. dyn := TGLODEDynamic.Create(sphere.Behaviours);
  113. // ELEMENTS MUST BE ADDED BEFORE SETTING MANAGER
  114. with TGLODEElementSphere(dyn.AddNewElement(TGLODEElementSphere)) do
  115. Radius := sphere.Radius;
  116. dyn.Manager := GLODEManager1;
  117. end;
  118. procedure TFormOdeSimple.DoBox;
  119. var
  120. cube: TGLCube;
  121. dyn: TGLODEDynamic;
  122. begin
  123. cube := TGLCube(ODEObjects.AddNewChild(TGLCube));
  124. cube.Position.SetPoint(5 * random - 2.5, 2, 5 * random - 2.5);
  125. cube.CubeWidth := 0.5 * (random + 1);
  126. cube.CubeHeight := 0.5 * (random + 1);
  127. cube.CubeDepth := 0.5 * (random + 1);
  128. cube.Material.FrontProperties.Ambient.RandomColor;
  129. dyn := TGLODEDynamic.Create(cube.Behaviours);
  130. // ELEMENTS MUST BE ADDED BEFORE SETTING MANAGER
  131. with TGLODEElementBox(dyn.AddNewElement(TGLODEElementBox)) do
  132. begin
  133. BoxWidth := cube.CubeWidth;
  134. BoxHeight := cube.CubeHeight;
  135. BoxDepth := cube.CubeDepth;
  136. end;
  137. dyn.Manager := GLODEManager1;
  138. end;
  139. procedure TFormOdeSimple.DoCapsule;
  140. var
  141. capsule: TGLCylinder;
  142. dyn: TGLODEDynamic;
  143. begin
  144. capsule := TGLCylinder(ODEObjects.AddNewChild(TGLCylinder));
  145. capsule.Material.FrontProperties.Diffuse.RandomColor;
  146. capsule.Material.FrontProperties.Diffuse.RandomColor;
  147. with capsule do
  148. begin
  149. Position.SetPoint(5 * random - 2.5, 2, 5 * random - 2.5);
  150. BottomRadius := 0.25 * (random + 1);
  151. TopRadius := BottomRadius;
  152. Height := random + 1;
  153. Parts := [cySides];
  154. with TGLSphere(AddNewChild(TGLSphere)) do
  155. begin
  156. Position.Y := 0.5 * Height;
  157. Radius := BottomRadius;
  158. Bottom := 0;
  159. end;
  160. with TGLSphere(AddNewChild(TGLSphere)) do
  161. begin
  162. Position.Y := -0.5 * Height;
  163. Radius := BottomRadius;
  164. Top := 0;
  165. end;
  166. end;
  167. dyn := TGLODEDynamic.Create(capsule.Behaviours);
  168. // ELEMENTS MUST BE ADDED BEFORE SETTING MANAGER
  169. with TGLODEElementCapsule(dyn.AddNewElement(TGLODEElementCapsule)) do
  170. begin
  171. Radius := capsule.BottomRadius;
  172. Length := capsule.Height;
  173. Direction.SetVector(0, 1, 0);
  174. Up.SetVector(0, 0, 1);
  175. end;
  176. dyn.Manager := GLODEManager1;
  177. end;
  178. procedure TFormOdeSimple.DoCylinder;
  179. var
  180. cylinder: TGLCylinder;
  181. dyn: TGLODEDynamic;
  182. begin
  183. cylinder := TGLCylinder(ODEObjects.AddNewChild(TGLCylinder));
  184. with cylinder do
  185. begin
  186. Position.SetPoint(5 * random - 2.5, 2, 5 * random - 2.5);
  187. BottomRadius := 0.25 * (random + 1);
  188. TopRadius := BottomRadius;
  189. Height := random + 1;
  190. end;
  191. dyn := TGLODEDynamic.Create(cylinder.Behaviours);
  192. // ELEMENTS MUST BE ADDED BEFORE SETTING MANAGER
  193. with TGLODEElementCylinder(dyn.AddNewElement(TGLODEElementCylinder)) do
  194. begin
  195. Radius := cylinder.BottomRadius;
  196. Length := cylinder.Height;
  197. end;
  198. dyn.Manager := GLODEManager1;
  199. end;
  200. // CONE IS CURRENTLY UNSUPPOETED FOR ODE 0.9
  201. {
  202. procedure TForm1.DoCone;
  203. var
  204. cone : TGLCone;
  205. dyn : TGLODEDynamic;
  206. begin
  207. cone:=TGLCone(ODEObjects.AddNewChild(TGLCone));
  208. with cone do begin
  209. Position.SetPoint(5*random-2.5,2,5*random-2.5);
  210. BottomRadius:=0.25*(Random+1);
  211. Height:=random+1;
  212. end;
  213. dyn:=TGLODEDynamic.Create(cone.Behaviours);
  214. dyn.Manager:=GLODEManager1;
  215. with TODEElementCone(dyn.AddNewElement(TODEElementCone)) do begin
  216. Radius:=cone.BottomRadius;
  217. Length:=cone.Height;
  218. Direction.SetVector(0,1,0);
  219. Up.SetVector(0,0,1);
  220. Position.SetPoint(0,-cone.Height/2,0);
  221. end;
  222. end;
  223. }
  224. procedure TFormOdeSimple.GLHeightField1GetHeight(const x, y: Single; var z: Single;
  225. var Color: TVector4f; var TexPoint: TTexPoint);
  226. begin
  227. z := 0.5 * cos(x) * sin(y);
  228. end;
  229. procedure TFormOdeSimple.chbContactsClick(Sender: TObject);
  230. begin
  231. GLODEManager1.Visible := chbContacts.Checked;
  232. end;
  233. procedure TFormOdeSimple.chbElementsClick(Sender: TObject);
  234. begin
  235. TGLODEHeightField(GLHeightField1.Behaviours[0]).RenderContacts := chbElements.Checked;
  236. end;
  237. procedure TFormOdeSimple.TrackBar1Change(Sender: TObject);
  238. begin
  239. with TGLODEHeightField(GLHeightField1.Behaviours[0]) do
  240. ContactResolution := 0.25 + (10 - TrackBar1.Position) / 20;
  241. end;
  242. procedure TFormOdeSimple.cbSurfaceChange(Sender: TObject);
  243. begin
  244. if cbSurface.ItemIndex = 0 then
  245. begin
  246. GLPlane1.Visible := True;
  247. chbElements.Enabled := False;
  248. GetODEStatic(GLPlane1).Manager := GLODEManager1;
  249. end
  250. else
  251. begin
  252. GLPlane1.Visible := False;
  253. GetODEStatic(GLPlane1).Manager := nil;
  254. end;
  255. if cbSurface.ItemIndex = 1 then
  256. begin
  257. GLHeightField1.Visible := True;
  258. chbContacts.Enabled := True;
  259. GetODEHeightField(GLHeightField1).Manager := GLODEManager1;
  260. end
  261. else
  262. begin
  263. GLHeightField1.Visible := False;
  264. chbContacts.Enabled := False;
  265. GetODEHeightField(GLHeightField1).Manager := nil;
  266. end;
  267. end;
  268. end.