PolyPhysicsScreenEntity.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * PolyPhysicsScreenEntity.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 5/8/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #define PI 3.14159265
  10. #include "PolyPhysicsScreenEntity.h"
  11. using namespace Polycode;
  12. PhysicsScreenEntity::PhysicsScreenEntity(ScreenEntity *entity, b2World *world, float worldScale, int entType, float friction, float density, float restitution, bool isSensor, bool fixedRotation) {
  13. this->worldScale = worldScale;
  14. screenEntity = entity;
  15. bodyDef = new b2BodyDef();
  16. bodyDef->position.Set(screenEntity->getPosition()->x/worldScale, screenEntity->getPosition()->y/worldScale);
  17. bodyDef->angle = screenEntity->getRotation()*(PI/180.0f);
  18. bodyDef->bullet = isSensor;
  19. bodyDef->fixedRotation = fixedRotation;
  20. if(entType != ENTITY_STATICRECT)
  21. bodyDef->type = b2_dynamicBody;
  22. body = world->CreateBody(bodyDef);
  23. b2FixtureDef fDef;
  24. fDef.friction = friction;
  25. fDef.restitution = restitution;
  26. fDef.density = density;
  27. fDef.isSensor = isSensor;
  28. switch(entType) {
  29. case ENTITY_STATICRECT:
  30. {
  31. b2PolygonShape b2shape;
  32. b2shape.SetAsBox(screenEntity->getWidth()/(worldScale*2.0f), screenEntity->getHeight()/(worldScale*2.0f));
  33. fDef.shape = &b2shape;
  34. }
  35. break;
  36. case ENTITY_RECT:
  37. {
  38. b2PolygonShape b2shape;
  39. b2shape.SetAsBox(screenEntity->getWidth()/(worldScale*2.0f), screenEntity->getHeight()/(worldScale*2.0f));
  40. fDef.shape = &b2shape;
  41. }
  42. break;
  43. case ENTITY_CIRCLE:
  44. {
  45. b2CircleShape b2shape;
  46. b2shape.m_radius = screenEntity->getWidth()/(worldScale*2.0f);
  47. fDef.shape = &b2shape;
  48. }
  49. break;
  50. }
  51. fixture = body->CreateFixture(&fDef);
  52. lastPosition.x = screenEntity->getPosition2D().x;
  53. lastPosition.y = screenEntity->getPosition2D().y;
  54. collisionOnly = false;
  55. }
  56. void PhysicsScreenEntity::applyTorque(float torque) {
  57. body->ApplyTorque(torque);
  58. }
  59. void PhysicsScreenEntity::applyForce(Vector2 force){
  60. body->SetAwake(true);
  61. body->ApplyForce(b2Vec2(force.x,force.y), b2Vec2(body->GetPosition().x,body->GetPosition().y));
  62. }
  63. ScreenEntity *PhysicsScreenEntity::getScreenEntity() {
  64. return screenEntity;
  65. }
  66. void PhysicsScreenEntity::setTransform(Vector2 pos, float angle) {
  67. body->SetTransform(b2Vec2(pos.x/worldScale, pos.y/worldScale), angle*(PI/180.0f));
  68. }
  69. void PhysicsScreenEntity::Update() {
  70. b2Vec2 position = body->GetPosition();
  71. float32 angle = body->GetAngle();
  72. // if(lastRotation != screenEntity->getRotation() || collisionOnly) {
  73. // body->SetTransform(position, screenEntity->getRotation()*(PI/180.0f));
  74. // } else {
  75. screenEntity->setRotation(angle*(180.0f/PI));
  76. // }
  77. /*
  78. if(lastPosition != screenEntity->getPosition2D() || collisionOnly) {
  79. b2Vec2 newPos;
  80. newPos.x = screenEntity->getPosition2D().x/worldScale;
  81. newPos.y = screenEntity->getPosition2D().y/worldScale;
  82. body->SetTransform(newPos, screenEntity->getRotation()*(PI/180.0f));
  83. position.x = screenEntity->getPosition2D().x/worldScale;
  84. position.y = screenEntity->getPosition2D().y/worldScale;
  85. } else {
  86. */
  87. screenEntity->setPosition(position.x*worldScale, position.y*worldScale);
  88. // }
  89. screenEntity->dirtyMatrix(true);
  90. screenEntity->rebuildTransformMatrix();
  91. lastPosition.x = position.x*worldScale;
  92. lastPosition.y = position.y*worldScale;
  93. lastRotation = angle * (180.0f/PI);
  94. }
  95. PhysicsScreenEntity::~PhysicsScreenEntity() {
  96. }