PolyPhysicsSceneEntity.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyCollisionSceneEntity.h"
  22. #include "btBulletCollisionCommon.h"
  23. #include "btBulletDynamicsCommon.h"
  24. #include <vector>
  25. class btKinematicCharacterController;
  26. class btPairCachingGhostObject;
  27. namespace Polycode {
  28. /**
  29. * A wrapper around SceneEntity that provides physics information.
  30. */
  31. class _PolyExport PhysicsSceneEntity : public CollisionSceneEntity {
  32. public:
  33. PhysicsSceneEntity(SceneEntity *entity, int type, Number mass, Number friction, Number restitution);
  34. virtual ~PhysicsSceneEntity();
  35. virtual void Update();
  36. /** @name Physics scene entity
  37. * Public methods
  38. */
  39. //@{
  40. SceneEntity *getSceneEntity();
  41. void setFriction(Number friction);
  42. int getType() { return type; }
  43. void setVelocity(Vector3 velocity);
  44. void warpTo(Vector3 position, bool resetRotation);
  45. //@}
  46. // ----------------------------------------------------------------------------------------------------------------
  47. /**
  48. * Box shape
  49. */
  50. static const int SHAPE_BOX = 0;
  51. /**
  52. * Terrain shape
  53. */
  54. static const int SHAPE_TERRAIN = 1;
  55. /**
  56. * Sphere shape
  57. */
  58. static const int SHAPE_SPHERE = 2;
  59. /**
  60. * Mesh shape
  61. */
  62. static const int SHAPE_MESH = 3;
  63. /**
  64. * Character controller shape
  65. */
  66. static const int CHARACTER_CONTROLLER = 4;
  67. /**
  68. * Capsule shape
  69. */
  70. static const int SHAPE_CAPSULE = 5;
  71. /**
  72. * Plane shape
  73. */
  74. static const int SHAPE_PLANE = 6;
  75. /**
  76. * Cone shape
  77. */
  78. static const int SHAPE_CONE = 7;
  79. /**
  80. * Cylinder shape
  81. */
  82. static const int SHAPE_CYLINDER = 8;
  83. bool enabled;
  84. btRigidBody* rigidBody;
  85. protected:
  86. Number mass;
  87. };
  88. /**
  89. * A Physics character controller.
  90. */
  91. class _PolyExport PhysicsCharacter : public PhysicsSceneEntity {
  92. public:
  93. PhysicsCharacter(SceneEntity *entity, Number mass, Number friction, Number stepSize);
  94. virtual ~PhysicsCharacter();
  95. virtual void Update();
  96. /** @name Physics character
  97. * Public methods
  98. */
  99. //@{
  100. void setWalkDirection(Vector3 direction);
  101. void jump();
  102. void warpCharacter(Vector3 position);
  103. void setJumpSpeed(Number jumpSpeed);
  104. void setFallSpeed(Number fallSpeed);
  105. void setMaxJumpHeight(Number maxJumpHeight);
  106. bool onGround();
  107. //@}
  108. // ----------------------------------------------------------------------------------------------------------------
  109. btKinematicCharacterController *character;
  110. btPairCachingGhostObject *ghostObject;
  111. protected:
  112. };
  113. /**
  114. * Physics vehicle wheel info.
  115. */
  116. class PhysicsVehicleWheelInfo {
  117. public:
  118. /**
  119. * Wheel index.
  120. */
  121. unsigned int wheelIndex;
  122. /**
  123. * Wheel scene entity.
  124. */
  125. SceneEntity *wheelEntity;
  126. };
  127. /**
  128. * A physics vehicle controller.
  129. */
  130. class _PolyExport PhysicsVehicle : public PhysicsSceneEntity {
  131. public:
  132. PhysicsVehicle(SceneEntity *entity, Number mass, Number friction, btDefaultVehicleRaycaster *rayCaster);
  133. /** @name Physics vehicle
  134. * Public methods
  135. */
  136. //@{
  137. void addWheel(SceneEntity *entity, Vector3 connection, Vector3 direction, Vector3 axle, Number suspentionRestLength, Number wheelRadius, bool isFrontWheel,Number suspensionStiffness = 20.0f, Number suspensionDamping = 1.0f, Number suspensionCompression = 4.0f, Number wheelFriction = 10000.0f, Number rollInfluence = 0.05f);
  138. void applyEngineForce(Number force, unsigned int wheelIndex);
  139. void setSteeringValue(Number value, unsigned int wheelIndex);
  140. void setBrake(Number value, unsigned int wheelIndex);
  141. void warpVehicle(Vector3 position);
  142. //@}
  143. // ----------------------------------------------------------------------------------------------------------------
  144. void Update();
  145. virtual ~PhysicsVehicle();
  146. btRaycastVehicle::btVehicleTuning tuning;
  147. btDefaultVehicleRaycaster *rayCaster;
  148. btRaycastVehicle *vehicle;
  149. protected:
  150. std::vector<PhysicsVehicleWheelInfo> wheels;
  151. };
  152. }