fNewtonMousePick.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. unit fNewtonMousePick;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Variants,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. GLS.Scene,
  12. GLS.VectorTypes,
  13. GLS.Objects,
  14. Physics.NGDManager,
  15. GLS.Coordinates,
  16. GLS.Cadencer,
  17. GLS.SceneViewer,
  18. GLS.BaseClasses,
  19. GLS.VectorGeometry;
  20. type
  21. TFormNewtonMousePick = class(TForm)
  22. GLScene1: TGLScene;
  23. GLSceneViewer1: TGLSceneViewer;
  24. GLCadencer1: TGLCadencer;
  25. GLCamera1: TGLCamera;
  26. GLLightSource1: TGLLightSource;
  27. Floor: TGLCube;
  28. GLCube1: TGLCube;
  29. GLNGDManager1: TGLNGDManager;
  30. GLSphere1: TGLSphere;
  31. GLCube2: TGLCube;
  32. GLCube3: TGLCube;
  33. GLCube4: TGLCube;
  34. GLLines1: TGLLines;
  35. procedure GLCadencer1Progress(Sender: TObject;
  36. const deltaTime, newTime: Double);
  37. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  38. Shift: TShiftState; X, Y: Integer);
  39. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  40. X, Y: Integer);
  41. procedure GLSceneViewer1MouseUp(Sender: TObject; Button: TMouseButton;
  42. Shift: TShiftState; X, Y: Integer);
  43. procedure FormCreate(Sender: TObject);
  44. private
  45. point3d, FPaneNormal: TGLVector;
  46. public
  47. pickjoint: TGLNGDJoint;
  48. end;
  49. var
  50. FormNewtonMousePick: TFormNewtonMousePick;
  51. implementation
  52. {$R *.dfm}
  53. procedure TFormNewtonMousePick.FormCreate(Sender: TObject);
  54. begin
  55. pickjoint := TGLNGDJoint(GLNGDManager1.NewtonJoint.Items[0]);
  56. end;
  57. procedure TFormNewtonMousePick.GLCadencer1Progress(Sender: TObject;
  58. const deltaTime, newTime: Double);
  59. begin
  60. GLNGDManager1.Step(deltaTime);
  61. end;
  62. procedure TFormNewtonMousePick.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  63. Shift: TShiftState; X, Y: Integer);
  64. var
  65. PickedSceneObject: TGLBaseSceneObject;
  66. begin
  67. if Button = TMouseButton(mbLeft) then
  68. begin
  69. PickedSceneObject := GLSceneViewer1.Buffer.GetPickedObject(X, Y);
  70. if Assigned(PickedSceneObject) and Assigned
  71. (GetNGDDynamic(PickedSceneObject)) then
  72. pickjoint.ParentObject := PickedSceneObject
  73. else
  74. exit;
  75. point3d := VectorMake(GLSceneViewer1.Buffer.PixelRayToWorld(X, Y));
  76. // Attach the body
  77. pickjoint.KinematicControllerPick(point3d, paAttach);
  78. if Assigned(GLSceneViewer1.Camera.TargetObject) then
  79. FPaneNormal := GLSceneViewer1.Camera.AbsoluteVectorToTarget
  80. else
  81. FPaneNormal := GLSceneViewer1.Camera.AbsoluteDirection;
  82. end;
  83. end;
  84. procedure TFormNewtonMousePick.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  85. X, Y: Integer);
  86. var
  87. point2d, GotoPoint3d: TGLVector;
  88. begin
  89. if ssLeft in Shift then
  90. begin
  91. // Get the screenPoint with opengl correction [Height - Y] for the next function
  92. point2d := VectorMake(X, GLSceneViewer1.Height - Y, 0, 0);
  93. // Get the intersect point between the plane [parallel to camera] and mouse position
  94. if GLSceneViewer1.Buffer.ScreenVectorIntersectWithPlane(point2d, point3d,
  95. FPaneNormal, GotoPoint3d) then
  96. // Move the body to the new position
  97. pickjoint.KinematicControllerPick(GotoPoint3d, paMove);
  98. end
  99. else
  100. pickjoint.KinematicControllerPick(GotoPoint3d, paDetach);
  101. end;
  102. procedure TFormNewtonMousePick.GLSceneViewer1MouseUp(Sender: TObject; Button: TMouseButton;
  103. Shift: TShiftState; X, Y: Integer);
  104. begin
  105. // Detach the body
  106. if Button = TMouseButton(mbLeft) then
  107. pickjoint.KinematicControllerPick(NullHmgVector, paDetach);
  108. end;
  109. end.