PolyPhysicsScreenEntity.cpp 9.9 KB

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