Welt2D.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "Welt2D.h"
  2. #include "Bild.h"
  3. using namespace Framework;
  4. Object2D::Object2D()
  5. {
  6. rSpeed = 0;
  7. rotation = 0;
  8. size = 1;
  9. ref = 1;
  10. }
  11. Object2D::~Object2D()
  12. {}
  13. void Object2D::explosion( Vertex worldPos, float intensity )
  14. {
  15. intensity /= ( position - worldPos ).getLengthSq();
  16. speed += ( position - worldPos ) * intensity;
  17. }
  18. void Object2D::impuls( Vertex start, Vertex speed )
  19. {}
  20. void Object2D::setPosition( Vertex pos )
  21. {
  22. position = pos;
  23. }
  24. void Object2D::setPosition( float x, float y )
  25. {
  26. position = Vertex( x, y );
  27. }
  28. void Object2D::setDrehung( float drehung )
  29. {
  30. rotation = drehung;
  31. while( rotation > PI * 2 )
  32. rotation -= (float)PI * 2;
  33. while( rotation < 0 )
  34. rotation += (float)PI * 2;
  35. }
  36. void Object2D::addDrehung( float drehung )
  37. {
  38. rotation += drehung;
  39. while( rotation > PI * 2 )
  40. rotation -= (float)PI * 2;
  41. while( rotation < 0 )
  42. rotation += (float)PI * 2;
  43. }
  44. void Object2D::setSize( float size )
  45. {
  46. this->size = size;
  47. }
  48. void Object2D::addSize( float size )
  49. {
  50. this->size += size;
  51. }
  52. bool Object2D::tick( double zeit )
  53. {
  54. rotation += rSpeed * (float)zeit;
  55. position += speed * (float)zeit;
  56. return rSpeed != 0 || speed != Vertex( 0, 0 );
  57. }
  58. Mat3< float > Object2D::getObjectMatrix() const
  59. {
  60. return Mat3<float>::translation( position ) * Mat3<float>::rotation( rotation ) * Mat3<float>::scaling( size );
  61. }
  62. Vertex Object2D::getPosition() const
  63. {
  64. return position;
  65. }
  66. float Object2D::getDrehung() const
  67. {
  68. return rotation;
  69. }
  70. float Object2D::getSize() const
  71. {
  72. return size;
  73. }
  74. Object2D *Object2D::getThis()
  75. {
  76. ref++;
  77. return this;
  78. }
  79. Object2D *Object2D::release()
  80. {
  81. if( !--ref )
  82. delete this;
  83. return 0;
  84. }
  85. Welt2D::Welt2D()
  86. {
  87. objects = new RCArray< Object2D >();
  88. ref = 1;
  89. }
  90. Welt2D::~Welt2D()
  91. {
  92. objects->release();
  93. }
  94. void Welt2D::addObject( Object2D *obj )
  95. {
  96. objects->add( obj );
  97. }
  98. void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
  99. {
  100. maxRad = maxRad * maxRad;
  101. for( auto obj = objects->getArray(); obj.set; obj++ )
  102. {
  103. if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
  104. obj.var->explosion( worldPos, intensity );
  105. }
  106. }
  107. void impuls( Vertex start, Vertex speed )
  108. {}
  109. bool Welt2D::tick( double zeit )
  110. {
  111. bool ret = 0;
  112. for( auto obj = objects->getArray(); obj.set; obj++ )
  113. ret |= obj.var->tick( zeit );
  114. return ret;
  115. }
  116. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
  117. {
  118. for( auto obj = objects->getArray(); obj.set; obj++ )
  119. {
  120. Rect< float > bnd = obj.var->getBoundingBox();
  121. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  122. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  123. bnd.bottomRight = kamMat * bnd.bottomRight;
  124. bnd.topLeft = kamMat * bnd.topLeft;
  125. topRight = kamMat * topRight;
  126. bottomLeft = kamMat * bottomLeft;
  127. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  128. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  129. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  130. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  131. ( topRight.x >= 0 && topRight.x < size.x ) ||
  132. ( topRight.y >= 0 && topRight.y < size.y ) ||
  133. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  134. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  135. obj.var->render( kamMat, zRObj );
  136. }
  137. }
  138. Welt2D *Welt2D::getThis()
  139. {
  140. ref++;
  141. return this;
  142. }
  143. Welt2D *Welt2D::release()
  144. {
  145. if( !--ref )
  146. delete this;
  147. return 0;
  148. }