PolyPhysicsScreenEntity.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, int entType, float friction, float density, float restitution, bool isSensor) {
  13. screenEntity = entity;
  14. bodyDef = new b2BodyDef();
  15. bodyDef->position.Set(screenEntity->getPosition()->x/10.0f, screenEntity->getPosition()->y/10.0f);
  16. bodyDef->angle = screenEntity->getRotation()*(PI/180.0f);
  17. body = world->CreateBody(bodyDef);
  18. bodyDef->isBullet = isSensor;
  19. switch(entType) {
  20. case ENTITY_STATICRECT:{
  21. b2PolygonDef *groundShapeDef = new b2PolygonDef();
  22. groundShapeDef->isSensor = isSensor;
  23. groundShapeDef->SetAsBox(screenEntity->getWidth()/20.0f, screenEntity->getHeight()/20.0f);
  24. shape = body->CreateShape(groundShapeDef);
  25. }
  26. break;
  27. case ENTITY_RECT: {
  28. b2PolygonDef *shapeDef;
  29. shapeDef = new b2PolygonDef();
  30. shapeDef->SetAsBox(screenEntity->getWidth()/20.0f, screenEntity->getHeight()/20.0f);
  31. shapeDef->density = density;
  32. shapeDef->friction = friction;
  33. shapeDef->restitution = restitution;
  34. shapeDef->isSensor = isSensor;
  35. shape = body->CreateShape(shapeDef);
  36. }
  37. break;
  38. case ENTITY_CIRCLE: {
  39. b2CircleDef *shapeDef = new b2CircleDef();
  40. shapeDef->radius = screenEntity->getWidth()/20.0f;
  41. shapeDef->density = density;
  42. // shapeDef->SetAsBox(screenEntity->getWidth()/20.0f, screenEntity->getHeight()/20.0f);
  43. shapeDef->friction = friction;
  44. shapeDef->restitution = restitution;
  45. shapeDef->isSensor = isSensor;
  46. shape = body->CreateShape(shapeDef);
  47. }
  48. break;
  49. }
  50. lastPosition.x = screenEntity->getPosition2D().x;
  51. lastPosition.y = screenEntity->getPosition2D().y;
  52. body->SetMassFromShapes();
  53. collisionOnly = false;
  54. }
  55. void PhysicsScreenEntity::applyTorque(float torque) {
  56. body->ApplyTorque(torque);
  57. }
  58. void PhysicsScreenEntity::applyForce(Vector2 force){
  59. body->WakeUp();
  60. body->ApplyForce(b2Vec2(force.x,force.y), b2Vec2(body->GetPosition().x,body->GetPosition().y));
  61. }
  62. ScreenEntity *PhysicsScreenEntity::getScreenEntity() {
  63. return screenEntity;
  64. }
  65. void PhysicsScreenEntity::Update() {
  66. b2Vec2 position = body->GetPosition();
  67. float32 angle = body->GetAngle();
  68. if(lastRotation != screenEntity->getRotation() || collisionOnly) {
  69. body->SetXForm(position, screenEntity->getRotation()*(PI/180.0f));
  70. } else {
  71. screenEntity->setRotation(angle*(180.0f/PI));
  72. }
  73. if(lastPosition != screenEntity->getPosition2D() || collisionOnly) {
  74. b2Vec2 newPos;
  75. newPos.x = screenEntity->getPosition2D().x/10.0f;
  76. newPos.y = screenEntity->getPosition2D().y/10.0f;
  77. body->SetXForm(newPos, screenEntity->getRotation()*(PI/180.0f));
  78. position.x = screenEntity->getPosition2D().x/10.0f;
  79. position.y = screenEntity->getPosition2D().y/10.0f;
  80. } else {
  81. screenEntity->setPosition(position.x*10.0f, position.y*10.0f);
  82. }
  83. lastPosition.x = position.x*10.0f;
  84. lastPosition.y = position.y*10.0f;
  85. lastRotation = angle * (180.0f/PI);
  86. }
  87. PhysicsScreenEntity::~PhysicsScreenEntity() {
  88. }