Unit1.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "GLBaseClasses"
  9. #pragma link "GLCadencer"
  10. #pragma link "GLCoordinates"
  11. #pragma link "GLCrossPlatform"
  12. #pragma link "GLGeomObjects"
  13. #pragma link "GLHUDObjects"
  14. #pragma link "GLNGDManager"
  15. #pragma link "GLObjects"
  16. #pragma link "GLScene"
  17. #pragma link "GLSimpleNavigation"
  18. #pragma link "GLWin32Viewer"
  19. #pragma link "NGDImport"
  20. #pragma link "GLBitmapFont"
  21. #pragma resource "*.dfm"
  22. TForm1 *Form1;
  23. int __cdecl BuoyancyPlaneCallback(const int collisionID, void *context,
  24. const PNGDFloat globalSpaceMatrix, PNGDFloat globalSpacePlane)
  25. {
  26. Glvectorgeometry::TMatrix *BodyMatrix;
  27. Glvectorgeometry::TVector PlaneEquation;
  28. PVector pv;
  29. TForm1 *MyForm;
  30. // Get the matrix of the actual body
  31. BodyMatrix = (PMatrix) globalSpaceMatrix;
  32. MyForm = (TForm1 *) context;
  33. // this is the 4-value vector that represents the plane equation for
  34. // the buoyancy surface
  35. // This can be used to simulate boats and lighter than air vehicles etc..
  36. PlaneEquation = MyForm->GLPlane1->Direction->AsVector;
  37. // the distance along this normal, to the origin.
  38. PlaneEquation.W = MyForm->GLPlane1->Position->Y;
  39. globalSpacePlane = PlaneEquation.V;
  40. return 1;
  41. }
  42. //---------------------------------------------------------------------------
  43. __fastcall TForm1::TForm1(TComponent* Owner)
  44. : TForm(Owner)
  45. {
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TForm1::FormCreate(TObject *Sender)
  49. {
  50. // To use Buoyancy effect, set a custom forceAndTorqueEvent were you can call
  51. // NewtonBodyAddBuoyancyForce API function
  52. for (int i = 0; i < obj->Count - 1; i++)
  53. {
  54. GetNGDDynamic(obj->Children[i])->CustomForceAndTorqueEvent =
  55. NULL; //must be MyForceAndTorqueDensity();
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. void TForm1::Shoot(void)
  60. {
  61. TGLCube *Ball;
  62. TGLNGDDynamic *NGDDyn;
  63. Ball = (TGLCube *)(Mag->AddNewChild(__classid(TGLCube)));
  64. Ball->CubeWidth = 0.5;
  65. Ball->CubeHeight = 0.5;
  66. Ball->CubeDepth = 0.5;
  67. Ball->AbsolutePosition = GLCamera1->AbsolutePosition;
  68. NGDDyn = GetOrCreateNGDDynamic(Ball);
  69. NGDDyn->Manager = GLNGDManager1;
  70. NGDDyn->Density = 10;
  71. NGDDyn->UseGravity = false;
  72. NGDDyn->LinearDamping = 0;
  73. // Add impulse in the camera direction
  74. NGDDyn->AddImpulse(VectorScale(GLCamera1->AbsoluteVectorToTarget(), 100),
  75. Ball->AbsolutePosition);
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  79. const double newTime)
  80. {
  81. GLNGDManager1->Step(deltaTime);
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  85. TShiftState Shift, int X, int Y)
  86. {
  87. if (Button = TMouseButton(Glcrossplatform::mbMiddle))
  88. Shoot();
  89. }
  90. //---------------------------------------------------------------------------
  91. void TForm1::MyForceAndTorqueDensity(const PNewtonBody cbody,
  92. NGDFloat timestep, int threadIndex)
  93. {
  94. Glvectorgeometry::TVector worldGravity;
  95. TGLNGDDynamic *NGDDyn;
  96. float fluidDensity, fluidLinearViscosity, fluidAngularViscosity;
  97. worldGravity = GLNGDManager1->Gravity->AsVector;
  98. NGDDyn = (TGLNGDDynamic *)NewtonBodyGetUserData(cbody);
  99. // Add gravity to body: Weight= Mass*Gravity
  100. ScaleVector(worldGravity, NGDDyn->Mass);
  101. NewtonBodyAddForce(cbody, worldGravity.V);
  102. fluidDensity = SpinEdit1->Value;
  103. fluidLinearViscosity = SpinEdit2->Value / 10;
  104. fluidAngularViscosity = SpinEdit3->Value / 10;
  105. // We send Self as context for the callback
  106. NewtonBodyAddBuoyancyForce(cbody, fluidDensity / NGDDyn->Mass,
  107. fluidLinearViscosity, fluidAngularViscosity, worldGravity.V,
  108. BuoyancyPlaneCallback, Owner);
  109. }