PolyPhysicsSceneEntity.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "PolyPhysicsSceneEntity.h"
  20. using namespace Polycode;
  21. PhysicsVehicle::PhysicsVehicle(SceneEntity *entity, Number mass, Number friction,btDefaultVehicleRaycaster *rayCaster): PhysicsSceneEntity(entity, PhysicsSceneEntity::SHAPE_BOX, mass, friction, 1) {
  22. }
  23. void PhysicsVehicle::addWheel(SceneEntity *entity, Vector3 connection, Vector3 direction, Vector3 axle, Number suspentionRestLength, Number wheelRadius, bool isFrontWheel,Number suspensionStiffness, Number suspensionDamping, Number suspensionCompression, Number wheelFriction, Number rollInfluence) {
  24. vehicle->addWheel(btVector3(connection.x, connection.y, connection.z),
  25. btVector3(direction.x, direction.y, direction.z),
  26. btVector3(axle.x, axle.y, axle.z),
  27. suspentionRestLength,
  28. wheelRadius,
  29. tuning,
  30. isFrontWheel);
  31. PhysicsVehicleWheelInfo wheel_info;
  32. wheel_info.wheelEntity = entity;
  33. wheel_info.wheelIndex = wheels.size();
  34. btWheelInfo& wheel = vehicle->getWheelInfo(wheel_info.wheelIndex);
  35. wheel.m_suspensionStiffness = suspensionStiffness;
  36. wheel.m_wheelsDampingRelaxation = suspensionDamping;
  37. wheel.m_wheelsDampingCompression = suspensionCompression;
  38. wheel.m_frictionSlip = wheelFriction;
  39. wheel.m_rollInfluence = rollInfluence;
  40. wheels.push_back(wheel_info);
  41. }
  42. void PhysicsVehicle::setBrake(Number value, unsigned int wheelIndex) {
  43. if ( wheelIndex < wheels.size()) {
  44. vehicle->setBrake(value, wheelIndex);
  45. }
  46. }
  47. void PhysicsVehicle::setSteeringValue(Number value, unsigned int wheelIndex) {
  48. if ( wheelIndex < wheels.size()) {
  49. vehicle->setSteeringValue(value, wheelIndex);
  50. }
  51. }
  52. void PhysicsVehicle::applyEngineForce(Number force, unsigned int wheelIndex) {
  53. if ( wheelIndex < wheels.size()) {
  54. vehicle->applyEngineForce(force, wheelIndex);
  55. }
  56. }
  57. void PhysicsVehicle::Update() {
  58. Matrix4 m;
  59. for(int i=0; i < wheels.size(); i++) {
  60. PhysicsVehicleWheelInfo wheel_info = wheels[i];
  61. vehicle->updateWheelTransform(i,true);
  62. btScalar mat[16];
  63. // vehicle->getWheelTransformWS(i).getOpenGLMatrix(mat);
  64. vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(mat);
  65. for(int j=0; j < 16; j++) {
  66. m.ml[j] = mat[j];
  67. }
  68. wheel_info.wheelEntity->setTransformByMatrixPure(m);
  69. }
  70. PhysicsSceneEntity::Update();
  71. }
  72. PhysicsVehicle::~PhysicsVehicle() {
  73. }
  74. PhysicsCharacter::PhysicsCharacter(SceneEntity *entity, Number mass, Number friction, Number stepSize) : PhysicsSceneEntity(entity, PhysicsSceneEntity::CHARACTER_CONTROLLER, mass, friction, 1) {
  75. ghostObject = new btPairCachingGhostObject();
  76. Vector3 pos = entity->getPosition();
  77. btTransform transform;
  78. transform.setIdentity();
  79. transform.setOrigin(btVector3(pos.x,pos.y,pos.z));
  80. Quaternion q = entity->getRotationQuat();
  81. transform.setRotation(btQuaternion(q.x,q.y,q.z,q.w));
  82. ghostObject->setWorldTransform(transform);
  83. ghostObject->setCollisionShape (shape);
  84. ghostObject->setFriction(friction);
  85. ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);
  86. character = new btKinematicCharacterController (ghostObject,convexShape,btScalar(stepSize));
  87. }
  88. void PhysicsCharacter::setWalkDirection(Vector3 direction) {
  89. character->setWalkDirection(btVector3(direction.x, direction.y, direction.z));
  90. }
  91. void PhysicsCharacter::jump() {
  92. character->jump();
  93. }
  94. void PhysicsCharacter::Update() {
  95. btVector3 pos = ghostObject->getWorldTransform().getOrigin();
  96. sceneEntity->setPosition(pos.x(), pos.y(), pos.z());
  97. // sceneEntity->rebuildTransformMatrix();
  98. sceneEntity->dirtyMatrix(true);
  99. }
  100. PhysicsCharacter::~PhysicsCharacter() {
  101. }
  102. PhysicsSceneEntity::PhysicsSceneEntity(SceneEntity *entity, int type, Number mass, Number friction, Number restitution) : CollisionSceneEntity(entity, false, type) {
  103. this->mass = mass;
  104. btVector3 localInertia(0,0,0);
  105. Vector3 pos = entity->getPosition();
  106. btTransform transform;
  107. transform.setIdentity();
  108. transform.setOrigin(btVector3(pos.x,pos.y,pos.z));
  109. Quaternion q = entity->getRotationQuat();
  110. transform.setRotation(btQuaternion(q.x,q.y,q.z,q.w));
  111. if(mass != 0.0f) {
  112. shape->calculateLocalInertia(mass,localInertia);
  113. }
  114. if(type == CHARACTER_CONTROLLER) {
  115. } else {
  116. btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
  117. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia);
  118. rigidBody = new btRigidBody(rbInfo);
  119. // rigidBody->setActivationState(ISLAND_SLEEPING);
  120. rigidBody->setFriction(friction);
  121. rigidBody->setRestitution(restitution);
  122. }
  123. }
  124. void PhysicsSceneEntity::setFriction(Number friction) {
  125. rigidBody->setFriction(friction);
  126. }
  127. void PhysicsSceneEntity::Update() {
  128. Matrix4 m;
  129. btScalar* mat = (btScalar*) malloc(sizeof(btScalar) * 16);
  130. rigidBody->getWorldTransform().getOpenGLMatrix(mat);
  131. for(int i=0; i < 16; i++) {
  132. m.ml[i] = mat[i];
  133. }
  134. free(mat);
  135. sceneEntity->setTransformByMatrixPure(m);
  136. }
  137. SceneEntity *PhysicsSceneEntity::getSceneEntity() {
  138. return sceneEntity;
  139. }
  140. PhysicsSceneEntity::~PhysicsSceneEntity() {
  141. }