PolyPhysicsScreenEntity.cpp 7.3 KB

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