Welt2D.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Mat3< float > Object2D::getInverseObjectMatrix() const
  63. {
  64. return Mat3<float>::scaling( 1 / size ) * Mat3<float>::rotation( -rotation ) * Mat3<float>::translation( -position );
  65. }
  66. Vertex Object2D::getObjectPos( Vertex worldPos ) const
  67. {
  68. return ( worldPos - position ).rotation( -rotation ) * ( 1 / size );
  69. }
  70. Vertex Object2D::getObjectDir( Vertex worldDir ) const
  71. {
  72. return Vertex( worldDir.x, worldDir.y ).rotation( -rotation ) * ( 1 / size );
  73. }
  74. Vertex Object2D::getPosition() const
  75. {
  76. return position;
  77. }
  78. float Object2D::getDrehung() const
  79. {
  80. return rotation;
  81. }
  82. float Object2D::getSize() const
  83. {
  84. return size;
  85. }
  86. bool Object2D::calcHitPoint( Vertex pos, Vertex dir, Vertex &hitpoint ) const
  87. {
  88. return 0;
  89. }
  90. Object2D *Object2D::getThis()
  91. {
  92. ref++;
  93. return this;
  94. }
  95. Object2D *Object2D::release()
  96. {
  97. if( !--ref )
  98. delete this;
  99. return 0;
  100. }
  101. Welt2D::Welt2D()
  102. {
  103. objects = new RCArray< Object2D >();
  104. ref = 1;
  105. }
  106. Welt2D::~Welt2D()
  107. {
  108. objects->release();
  109. }
  110. void Welt2D::addObject( Object2D *obj )
  111. {
  112. objects->add( obj );
  113. }
  114. void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
  115. {
  116. maxRad = maxRad * maxRad;
  117. for( auto obj = objects->getArray(); obj.set; obj++ )
  118. {
  119. if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
  120. obj.var->explosion( worldPos, intensity );
  121. }
  122. }
  123. void Welt2D::impuls( Vertex worldPos, Vertex worldDir )
  124. {
  125. Vertex hitPoint;
  126. float dist = INFINITY;
  127. Object2D *o = 0;
  128. for( auto obj = objects->getArray(); obj.set; obj++ )
  129. {
  130. if( obj.var->calcHitPoint( worldPos, worldDir, hitPoint ) )
  131. {
  132. if( ( hitPoint - worldPos ).getLengthSq() < dist )
  133. {
  134. dist = ( hitPoint - worldPos ).getLengthSq();
  135. o = obj.var;
  136. }
  137. }
  138. }
  139. if( o )
  140. o->impuls( worldPos, worldDir );
  141. }
  142. bool Welt2D::tick( double zeit )
  143. {
  144. bool ret = 0;
  145. for( auto obj = objects->getArray(); obj.set; obj++ )
  146. ret |= obj.var->tick( zeit );
  147. return ret;
  148. }
  149. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
  150. {
  151. for( auto obj = objects->getArray(); obj.set; obj++ )
  152. {
  153. Rect< float > bnd = obj.var->getBoundingBox();
  154. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  155. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  156. bnd.bottomRight = kamMat * bnd.bottomRight;
  157. bnd.topLeft = kamMat * bnd.topLeft;
  158. topRight = kamMat * topRight;
  159. bottomLeft = kamMat * bottomLeft;
  160. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  161. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  162. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  163. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  164. ( topRight.x >= 0 && topRight.x < size.x ) ||
  165. ( topRight.y >= 0 && topRight.y < size.y ) ||
  166. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  167. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  168. obj.var->render( kamMat, zRObj );
  169. }
  170. }
  171. Welt2D *Welt2D::getThis()
  172. {
  173. ref++;
  174. return this;
  175. }
  176. Welt2D *Welt2D::release()
  177. {
  178. if( !--ref )
  179. delete this;
  180. return 0;
  181. }