Unit1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.Objects"
  12. #pragma link "Physics.ODEManager"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.Utils"
  15. #pragma link "GLS.SimpleNavigation"
  16. #pragma link "GLS.SceneViewer"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. //---------------------------------------------------------------------------
  20. __fastcall TForm1::TForm1(TComponent* Owner)
  21. : TForm(Owner)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TForm1::FormCreate(TObject *Sender)
  26. {
  27. // Initialize default values from the one of DesignTime;
  28. TrackBarMotionSpeed->Position = RoundInt(GetOrCreateOdeStatic(ConveyorBelt1)->Surface->Motion1);
  29. Friction->Text = FloatToStr(GetOrCreateOdeStatic(ConveyorBelt1)->Surface->Mu);
  30. FDirX->Text = "0";
  31. FDirY->Text = "0";
  32. FDirZ->Text = "1";
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::FormShow(TObject *Sender)
  36. {
  37. GLCadencer1->Enabled = true;
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  41. const double newTime)
  42. {
  43. GLODEManager1->Step(deltaTime);
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TForm1::GLODEManager1Collision(TObject *Sender, TObject *Object1,
  47. TObject *Object2, TdContact &Contact, bool &HandleCollision)
  48. {
  49. if (Object2 = GetOrCreateOdeStatic(ConveyorBelt1))
  50. {
  51. Contact.fdir1[0] = FDirectionVector.X; // x
  52. Contact.fdir1[1] = FDirectionVector.Y; // y
  53. Contact.fdir1[2] = FDirectionVector.Z; // z
  54. Contact.fdir1[3] = FDirectionVector.W; // not used
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TForm1::TrackBarMotionSpeedChange(TObject *Sender)
  59. {
  60. TGLODEStatic *AODEStatic;
  61. AODEStatic = (TGLODEStatic*)(ConveyorBelt1->Behaviours->Items[0]);
  62. AODEStatic->Surface->Motion1 = TrackBarMotionSpeed->Position;
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::FrictionChange(TObject *Sender)
  66. {
  67. TGLODEStatic *AODEStatic;
  68. AODEStatic = (TGLODEStatic*)(ConveyorBelt1->Behaviours->Items[0]);
  69. AODEStatic->Surface->Mu =
  70. Gls::Utils::StrToFloatDef(Friction->Text, GetOrCreateOdeStatic(ConveyorBelt1)->Surface->Mu);
  71. FrictionFeedback->Caption = Format("µs = %.2f", ARRAYOFCONST((GetOrCreateOdeStatic(ConveyorBelt1)->Surface->Mu)));
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::AddODECubeClick(TObject *Sender)
  75. {
  76. TGLCube *ACube;
  77. TGLODEDynamic *AODEDynamic;
  78. TGLODEElementBox *AODEElementBox;
  79. // Create a new GLScene cube and add it to the current GLScene1
  80. ACube = (TGLCube *)(SpawnPoint->AddNewChild(__classid(TGLCube)));
  81. ACube->Parent = GLScene1->Objects;
  82. ACube->Position->Assign(SpawnPoint->Position);
  83. ACube->Material->FrontProperties->Diffuse->RandomColor();
  84. // Add ODE Dynamic behaviour on it
  85. AODEDynamic = new TGLODEDynamic(ACube->Behaviours);
  86. AODEDynamic->Manager = GLODEManager1;
  87. // Set µs value to 1 (default=1000), just uses the one from the conveyor
  88. AODEDynamic->Surface->Mu = 1;
  89. // Finally create physical data in this behaviour
  90. AODEElementBox = (TGLODEElementBox *) AODEDynamic->AddNewElement(__classid(TGLODEElementBox));
  91. if (AODEElementBox)
  92. {
  93. AODEElementBox->BoxWidth = ACube->CubeWidth;
  94. AODEElementBox->BoxHeight = ACube->CubeHeight;
  95. AODEElementBox->BoxDepth = ACube->CubeDepth;
  96. }
  97. // The new camera target is the last added cube
  98. GLCamera1->TargetObject = ACube;
  99. // The spawn position is increased
  100. SpawnPoint->Position->Y = SpawnPoint->Position->Y + 1;
  101. }
  102. //---------------------------------------------------------------------------