PolyCollisionSceneEntity.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "PolyCollisionSceneEntity.h"
  20. #include "PolyLogger.h"
  21. #include "PolyMesh.h"
  22. #include "PolyEntity.h"
  23. #include "PolySceneMesh.h"
  24. #include "btBulletCollisionCommon.h"
  25. using namespace Polycode;
  26. CollisionEntity::CollisionEntity(Entity *entity, int type, bool compoundChildren) {
  27. this->entity = entity;
  28. shape = NULL;
  29. this->type = type;
  30. enabled = true;
  31. lastPosition = entity->getPosition();
  32. btMatrix3x3 basisA;
  33. basisA.setIdentity();
  34. collisionObject = new btCollisionObject();
  35. collisionObject->getWorldTransform().setBasis(basisA);
  36. if(compoundChildren) {
  37. btCompoundShape* compoundShape = new btCompoundShape();
  38. for(int i=0; i < entity->getNumChildren(); i++) {
  39. Entity *child = (Entity*)entity->getChildAtIndex(i);
  40. btCollisionShape *childShape = createCollisionShape(child, child->collisionShapeType);
  41. btTransform transform;
  42. child->rebuildTransformMatrix();
  43. btScalar mat[16];
  44. Matrix4 ent_mat = child->getTransformMatrix();
  45. for(int i=0; i < 16; i++) {
  46. mat[i] = ent_mat.ml[i];
  47. }
  48. transform.setFromOpenGLMatrix(mat);
  49. compoundShape->addChildShape(transform, childShape);
  50. }
  51. shape = compoundShape;
  52. } else {
  53. shape = createCollisionShape(entity, type);
  54. }
  55. if(shape) {
  56. collisionObject->setCollisionShape(shape);
  57. }
  58. collisionObject->setUserPointer((void*)this);
  59. // if(type == SHAPE_MESH) {
  60. // concaveShape = dynamic_cast<btConcaveShape*>(shape);
  61. // } else {
  62. convexShape = dynamic_cast<btConvexShape*>(shape);
  63. // }
  64. }
  65. btCollisionShape *CollisionEntity::createCollisionShape(Entity *entity, int type) {
  66. btCollisionShape *collisionShape = NULL;
  67. Vector3 scale = entity->getCompoundScale();
  68. Vector3 bBox = entity->getLocalBoundingBox() * scale;
  69. Number largestSize = bBox.x;
  70. if(bBox.y > largestSize) {
  71. largestSize = bBox.y;
  72. }
  73. if(bBox.z > largestSize) {
  74. largestSize = bBox.z;
  75. }
  76. switch(type) {
  77. case SHAPE_CAPSULE:
  78. case CHARACTER_CONTROLLER:
  79. collisionShape = new btCapsuleShape(bBox.x/2.0f, bBox.y/2.0f);
  80. break;
  81. case SHAPE_CONE: {
  82. Number largest = bBox.x;
  83. if(bBox.z > largest) {
  84. largest = bBox.z;
  85. }
  86. collisionShape = new btConeShape(largest/2.0f, bBox.y);
  87. }
  88. break;
  89. case SHAPE_CYLINDER:
  90. {
  91. collisionShape = new btCylinderShape(btVector3(bBox.x/2.0, bBox.y/2.0f,bBox.z/2.0));
  92. }
  93. break;
  94. case SHAPE_PLANE:
  95. collisionShape = new btBoxShape(btVector3(bBox.x/2.0f, 0.05,bBox.z/2.0f));
  96. break;
  97. case SHAPE_BOX:
  98. collisionShape = new btBoxShape(btVector3(bBox.x/2.0f, bBox.y/2.0f,bBox.z/2.0f));
  99. break;
  100. case SHAPE_SPHERE:
  101. collisionShape = new btSphereShape(largestSize/2.0f);
  102. break;
  103. case SHAPE_MESH:
  104. {
  105. SceneMesh* sceneMesh = dynamic_cast<SceneMesh*>(entity);
  106. if(sceneMesh != NULL) {
  107. btConvexHullShape *hullShape = new btConvexHullShape();
  108. Mesh *mesh = sceneMesh->getMesh();
  109. for(int i=0; i < mesh->vertexPositionArray.data.size()-2; i += 3) {
  110. hullShape->addPoint(btVector3((btScalar)mesh->vertexPositionArray.data[i], (btScalar)mesh->vertexPositionArray.data[i+1],mesh->vertexPositionArray.data[i+2]));
  111. }
  112. collisionShape = hullShape;
  113. } else {
  114. Logger::log("Tried to make a mesh collision object from a non-mesh\n");
  115. collisionShape = new btBoxShape(btVector3(bBox.x/2.0f, bBox.y/2.0f,bBox.z/2.0f));
  116. }
  117. }
  118. break;
  119. }
  120. //collisionShape->setLocalScaling(btVector3(scale.x, scale.y, scale.z));
  121. return collisionShape;
  122. }
  123. void CollisionEntity::Update() {
  124. entity->rebuildTransformMatrix();
  125. btScalar mat[16];
  126. Matrix4 ent_mat = entity->getConcatenatedMatrix();
  127. for(int i=0; i < 16; i++) {
  128. mat[i] = ent_mat.ml[i];
  129. }
  130. collisionObject->getWorldTransform().setFromOpenGLMatrix(mat);
  131. }
  132. Entity *CollisionEntity::getEntity() {
  133. return entity;
  134. }
  135. CollisionEntity::~CollisionEntity() {
  136. delete shape;
  137. delete collisionObject;
  138. }