PolyPhysicsScreenEntity.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #define PI 3.14159265
  20. #include "PolyPhysicsScreenEntity.h"
  21. #include "PolyLogger.h"
  22. #include "PolyMesh.h"
  23. #include "PolyPolygon.h"
  24. #include "PolyScreenEntity.h"
  25. #include "PolyScreenMesh.h"
  26. using namespace Polycode;
  27. PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, Number worldScale, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
  28. screenEntity = entity;
  29. Vector3 entityScale = entity->getCompoundScale();
  30. this->worldScale = worldScale;
  31. collisionOnly = false;
  32. // Create body definition---------------------------------------
  33. b2BodyDef bodyDef;
  34. bodyDef.position.Set(screenEntity->getPosition2D().x/worldScale, screenEntity->getPosition2D().y/worldScale);
  35. bodyDef.angle = screenEntity->getRotation()*(PI/180.0f);
  36. bodyDef.bullet = isSensor;
  37. bodyDef.fixedRotation = fixedRotation;
  38. if(isStatic)
  39. bodyDef.type = b2_staticBody;
  40. else
  41. bodyDef.type = b2_dynamicBody;
  42. // Create the body
  43. body = world->CreateBody(&bodyDef);
  44. // Create fixture definition---------------------------------------------
  45. b2FixtureDef fDef;
  46. fDef.friction = friction;
  47. fDef.restitution = restitution;
  48. fDef.density = density;
  49. fDef.isSensor = isSensor;
  50. fDef.filter.groupIndex = groupIndex;
  51. // Create Shape definition (Circle/Rectangle/Polygon)---------------------------
  52. switch(entType) {
  53. case ENTITY_CIRCLE: {
  54. b2CircleShape Shape;
  55. // Set fixture shape to shape definition
  56. fDef.shape = &Shape;
  57. // Create the shape
  58. Shape.m_radius = screenEntity->getWidth()/(worldScale*2.0f);
  59. // Create the fixture
  60. fixture = body->CreateFixture(&fDef);
  61. break;
  62. }
  63. case ENTITY_RECT: {
  64. b2PolygonShape Shape;
  65. // Set fixture shape to shape definition
  66. fDef.shape = &Shape;
  67. // Create the shape
  68. Shape.SetAsBox(screenEntity->getWidth()/(worldScale*2.0f) * entityScale.x, screenEntity->getHeight()/(worldScale*2.0f) * entityScale.y);
  69. // Create the fixture
  70. fixture = body->CreateFixture(&fDef);
  71. break;
  72. }
  73. case ENTITY_MESH: {
  74. b2PolygonShape Shape;
  75. // Set fixture shape to shape definition
  76. fDef.shape = &Shape;
  77. // Get the screenmesh of the entity
  78. ScreenMesh* screenMesh = dynamic_cast<ScreenMesh*>(entity);
  79. if(screenMesh) {
  80. for(short i=0, polycount=screenMesh->getMesh()->getPolygonCount(); i < polycount; i++) {
  81. // Get the next polygon
  82. Polygon* poly = screenMesh->getMesh()->getPolygon(i);
  83. // Get the vertex count of current polygon
  84. unsigned short vertexcount = poly->getVertexCount();
  85. if (vertexcount >= 3 && vertexcount <= 8) {
  86. // Create new vertices array based on vertexcount
  87. b2Vec2* vertices = new b2Vec2[vertexcount];
  88. // and copy from the screenmesh
  89. for(short index=0; index < vertexcount; index++) {
  90. vertices[index].x = poly->getVertex(index)->x/worldScale;
  91. vertices[index].y = poly->getVertex(index)->y/worldScale;
  92. }
  93. // Create the shape
  94. Shape.Set(vertices, vertexcount);
  95. // Create the fixture
  96. fixture = body->CreateFixture(&fDef);
  97. delete []vertices;
  98. }
  99. else { Logger::log("Between 3 and 8 vertices allowed per polygon\n"); }
  100. }
  101. }
  102. else { Logger::log("Tried to make a mesh collision object from a non-mesh\n"); }
  103. }
  104. }
  105. }
  106. void PhysicsScreenEntity::applyTorque(Number torque) {
  107. body->ApplyTorque(torque);
  108. }
  109. void PhysicsScreenEntity::applyForce(Vector2 force){
  110. body->SetAwake(true);
  111. body->ApplyForce(b2Vec2(force.x,force.y), b2Vec2(body->GetPosition().x,body->GetPosition().y));
  112. }
  113. ScreenEntity *PhysicsScreenEntity::getScreenEntity() {
  114. return screenEntity;
  115. }
  116. void PhysicsScreenEntity::setVelocity(Number fx, Number fy) {
  117. body->SetAwake(true);
  118. b2Vec2 f = body->GetLinearVelocity();
  119. if(fx != 0)
  120. f.x = fx;
  121. if(fy != 0)
  122. f.y = fy;
  123. body->SetLinearVelocity(f);
  124. }
  125. void PhysicsScreenEntity::setVelocityX( Number fx) {
  126. body->SetAwake(true);
  127. b2Vec2 f = body->GetLinearVelocity();
  128. f.x = fx;
  129. body->SetLinearVelocity(f);
  130. }
  131. void PhysicsScreenEntity::setVelocityY(Number fy) {
  132. body->SetAwake(true);
  133. b2Vec2 f = body->GetLinearVelocity();
  134. f.y = fy;
  135. body->SetLinearVelocity(f);
  136. }
  137. void PhysicsScreenEntity::applyImpulse(Number fx, Number fy) {
  138. body->SetAwake(true);
  139. b2Vec2 f = b2Vec2(fx,fy);
  140. b2Vec2 p = body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
  141. body->ApplyLinearImpulse(f, p);
  142. }
  143. void PhysicsScreenEntity::setTransform(Vector2 pos, Number angle) {
  144. body->SetTransform(b2Vec2(pos.x/worldScale, pos.y/worldScale), angle*(PI/180.0f));
  145. }
  146. void PhysicsScreenEntity::Update() {
  147. if(collisionOnly) {
  148. Matrix4 matrix = screenEntity->getConcatenatedMatrix();
  149. b2Vec2 newPos;
  150. Number newRotation;
  151. Vector3 pos = matrix.getPosition();
  152. newPos.x = pos.x/worldScale;
  153. newPos.y = pos.y/worldScale;
  154. Number rx,ry,rz;
  155. matrix.getEulerAngles(&rx, &ry, &rz);
  156. newRotation = rz;
  157. body->SetAwake(true);
  158. body->SetTransform(newPos, newRotation * TORADIANS);
  159. } else {
  160. b2Vec2 position = body->GetPosition();
  161. Number angle = body->GetAngle();
  162. screenEntity->setRotation(angle*(180.0f/PI));
  163. screenEntity->setPosition(position.x*worldScale, position.y*worldScale);
  164. screenEntity->rebuildTransformMatrix();
  165. }
  166. }
  167. //==============================================
  168. //Add on functions to access the fixture library
  169. //==============================================
  170. // Gets the last fixture selected
  171. b2Fixture* PhysicsScreenEntity::getFixture() { return fixture; }
  172. // Returns specific fixture based on index starting from 0
  173. b2Fixture* PhysicsScreenEntity::getFixture(unsigned short index) {
  174. if(fixture) {
  175. short i = 0;
  176. for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) {
  177. if (i = index) {
  178. fixture = f;
  179. return fixture;
  180. }
  181. else {i++;}
  182. }
  183. Logger::log("That fixture index does not exist\n");
  184. return fixture = NULL;
  185. }
  186. Logger::log("Fixturelist is for mesh only\n");
  187. return fixture = NULL;
  188. }
  189. // I believe that at runtime you are not supposed to edit Shapes; However you still can
  190. // by getting a fixture(above) and then adding "->GetShape()" on the end to get the fixtures shape
  191. // Slight change to the destructor
  192. PhysicsScreenEntity::~PhysicsScreenEntity()
  193. {
  194. if(body)
  195. body->GetWorld()->DestroyBody(body); // DestroyBody deletes fixtures and shapes automaticaly according to box2d documentation
  196. }