CPhysic.h
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:2k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #ifndef _CPHYSIC_H_
  2. #define _CPHYSIC_H_
  3. #include <Newton.h>
  4. #include "object_physics.h"
  5. class CPhysicObject;
  6. // callback for applying powers to body
  7. void ForceAndTorqueCallback(const NewtonBody* body);
  8. // callback for transforming matrix of graphic object
  9. void TransformCallback(const NewtonBody* body, const dFloat* matrix);
  10. // if object actives/deactives, this is called
  11. void AutoactiveCallback(const NewtonBody *b, unsigned state);
  12. int GCBegin(const NewtonMaterial* material, const NewtonBody* body0, const NewtonBody* body1);
  13. int GCProcess(const NewtonMaterial* material, const NewtonContact* contact);
  14. void GCEnd(const NewtonMaterial* material);
  15. // this is because normally unfreezing needs newton world pointer, and this does not
  16. void NewtonBodyUnfreeze(NewtonBody *b);
  17. void NewtonBodyDestroy(NewtonBody *b);
  18. class CPhysic
  19. {
  20. public:
  21. NewtonWorld *nWorld;
  22. float timeStep;
  23. float fSpeed;
  24. float fSlowSpeed;
  25. float fNormalSpeed;
  26. bool bSlowMode;
  27. CPhysic()
  28. {
  29. fSlowSpeed = 0.1f;
  30. fNormalSpeed = 1;
  31. fSpeed = fNormalSpeed;
  32. bSlowMode = false;
  33. }
  34. void SwitchSlowMode()
  35. {
  36. bSlowMode = !bSlowMode;
  37. SetSpeed();
  38. }
  39. void SetSpeed(float spd=-1)
  40. {
  41. if (spd == -1)
  42. {
  43. if (bSlowMode) fSpeed = fSlowSpeed;
  44. else fSpeed = fNormalSpeed;
  45. }
  46. else fSpeed = spd;
  47. }
  48. int GetActive() // LOOOOL, tato funkcia rulez =D
  49. {
  50. int active;
  51. AutoactiveCallback((NewtonBody*)&active, -1);
  52. return active;
  53. }
  54. void Init()
  55. {
  56. // create newton world
  57. nWorld = NewtonCreate(NULL, NULL);
  58. CVector mn(-1, -1, -1); CVector mx(1, 1, 1);
  59. mn *= 100;
  60. mx *= 100;
  61. NewtonSetWorldSize(nWorld, &mn[0], &mx[0]);
  62. int defaultID = NewtonMaterialGetDefaultGroupID(nWorld);
  63. NewtonMaterialSetCollisionCallback(nWorld, defaultID, defaultID, 0, GCBegin, GCProcess, GCEnd);
  64. }
  65. void AddObject(CPhysicObject *o);
  66. void Frame(float frameTime);
  67. };
  68. #endif