fNewtonDensityC.cpp 3.9 KB

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