fNewtonDensityC.cpp 3.9 KB

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