2
0

PolyPhysicsSceneEntity.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "PolyQuaternion.h"
  22. #include "PolyCollisionSceneEntity.h"
  23. #include "btBulletCollisionCommon.h"
  24. #include "btBulletDynamicsCommon.h"
  25. #include <vector>
  26. class btKinematicCharacterController;
  27. class btPairCachingGhostObject;
  28. namespace Polycode {
  29. class Entity;
  30. /**
  31. * A wrapper around Entity that provides physics information.
  32. */
  33. class _PolyExport PhysicsEntity : public CollisionEntity {
  34. public:
  35. PhysicsEntity(Entity *entity, int type, Number mass, Number friction, Number restitution, bool compoundChildren = false);
  36. virtual ~PhysicsEntity();
  37. virtual void Update();
  38. /** @name Physics scene entity
  39. * Public methods
  40. */
  41. //@{
  42. void wakeUp();
  43. void setFriction(Number friction);
  44. int getType() { return type; }
  45. void setSpin(Vector3 spin);
  46. void setMass(Number mass);
  47. Vector3 getVelocity();
  48. Vector3 getSpin();
  49. void setRotation(Quaternion quat);
  50. void setVelocity(Vector3 velocity);
  51. void warpTo(Vector3 position, bool resetRotation);
  52. void applyImpulse(Vector3 direction, Vector3 point);
  53. //@}
  54. // ----------------------------------------------------------------------------------------------------------------
  55. /**
  56. * Box shape
  57. */
  58. static const int SHAPE_BOX = 0;
  59. /**
  60. * Terrain shape
  61. */
  62. static const int SHAPE_TERRAIN = 1;
  63. /**
  64. * Sphere shape
  65. */
  66. static const int SHAPE_SPHERE = 2;
  67. /**
  68. * Mesh shape
  69. */
  70. static const int SHAPE_MESH = 3;
  71. /**
  72. * Character controller shape
  73. */
  74. static const int CHARACTER_CONTROLLER = 4;
  75. /**
  76. * Capsule shape
  77. */
  78. static const int SHAPE_CAPSULE = 5;
  79. /**
  80. * Plane shape
  81. */
  82. static const int SHAPE_PLANE = 6;
  83. /**
  84. * Cone shape
  85. */
  86. static const int SHAPE_CONE = 7;
  87. /**
  88. * Cylinder shape
  89. */
  90. static const int SHAPE_CYLINDER = 8;
  91. btRigidBody* rigidBody;
  92. protected:
  93. Number mass;
  94. btDefaultMotionState* myMotionState;
  95. };
  96. typedef PhysicsEntity PhysicsSceneEntity;
  97. /**
  98. * A Physics character controller.
  99. */
  100. class _PolyExport PhysicsCharacter : public PhysicsEntity {
  101. public:
  102. PhysicsCharacter(Entity *entity, Number mass, Number friction, Number stepSize);
  103. virtual ~PhysicsCharacter();
  104. virtual void Update();
  105. /** @name Physics character
  106. * Public methods
  107. */
  108. //@{
  109. void setWalkDirection(Vector3 direction);
  110. void jump();
  111. void setVelocityForTime(const Vector3 &velocity, Number time);
  112. void warpCharacter(Vector3 position);
  113. void setJumpSpeed(Number jumpSpeed);
  114. void setFallSpeed(Number fallSpeed);
  115. void setMaxJumpHeight(Number maxJumpHeight);
  116. bool onGround();
  117. //@}
  118. // ----------------------------------------------------------------------------------------------------------------
  119. btKinematicCharacterController *character;
  120. btPairCachingGhostObject *ghostObject;
  121. protected:
  122. };
  123. /**
  124. * Physics vehicle wheel info.
  125. */
  126. class PhysicsVehicleWheelInfo {
  127. public:
  128. /**
  129. * Wheel index.
  130. */
  131. unsigned int wheelIndex;
  132. /**
  133. * Wheel scene entity.
  134. */
  135. Entity *wheelEntity;
  136. };
  137. /**
  138. * A physics vehicle controller.
  139. */
  140. class _PolyExport PhysicsVehicle : public PhysicsEntity {
  141. public:
  142. PhysicsVehicle(Entity *entity, Number mass, Number friction, btDefaultVehicleRaycaster *rayCaster);
  143. /** @name Physics vehicle
  144. * Public methods
  145. */
  146. //@{
  147. void addWheel(Entity *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);
  148. void applyEngineForce(Number force, unsigned int wheelIndex);
  149. void setSteeringValue(Number value, unsigned int wheelIndex);
  150. void setBrake(Number value, unsigned int wheelIndex);
  151. void warpVehicle(Vector3 position);
  152. //@}
  153. // ----------------------------------------------------------------------------------------------------------------
  154. void Update();
  155. virtual ~PhysicsVehicle();
  156. btRaycastVehicle::btVehicleTuning tuning;
  157. btDefaultVehicleRaycaster *rayCaster;
  158. btRaycastVehicle *vehicle;
  159. protected:
  160. std::vector<PhysicsVehicleWheelInfo> wheels;
  161. };
  162. }