PolyPhysicsScreenEntity.cpp 9.1 KB

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