Welt2D.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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::setSpeed( Vertex speed )
  21. {
  22. this->speed = speed;
  23. }
  24. void Object2D::setSpeed( float x, float y )
  25. {
  26. speed.x = x, speed.y = y;
  27. }
  28. void Object2D::setPosition( Vertex pos )
  29. {
  30. position = pos;
  31. }
  32. void Object2D::setPosition( float x, float y )
  33. {
  34. position = Vertex( x, y );
  35. }
  36. void Object2D::setDrehungSpeed( float ds )
  37. {
  38. rSpeed = ds;
  39. }
  40. void Object2D::setDrehung( float drehung )
  41. {
  42. rotation = drehung;
  43. while( rotation > PI * 2 )
  44. rotation -= (float)PI * 2;
  45. while( rotation < 0 )
  46. rotation += (float)PI * 2;
  47. }
  48. void Object2D::addDrehung( float drehung )
  49. {
  50. rotation += drehung;
  51. while( rotation > PI * 2 )
  52. rotation -= (float)PI * 2;
  53. while( rotation < 0 )
  54. rotation += (float)PI * 2;
  55. }
  56. void Object2D::setSize( float size )
  57. {
  58. this->size = size;
  59. }
  60. void Object2D::addSize( float size )
  61. {
  62. this->size += size;
  63. }
  64. bool Object2D::handleCollision( Object2D *obj )
  65. {
  66. Vertex hp;
  67. if( istModelInnen( obj, &hp ) )
  68. {
  69. Vertex v1 = getSpeed() + getWorldDir( getObjectPos( hp ).rotation( rSpeed ) - getObjectPos( hp ) );
  70. Vertex v2 = obj->getSpeed() + getWorldDir( obj->getObjectPos( hp ).rotation( obj->getDrehungSpeed() ) - obj->getObjectPos( hp ) );
  71. if( ( position - obj->getPosition() ).getLengthSq() > ( position + v1 * 0.03f - obj->getPosition() - v2 * 0.03f ).getLengthSq() )
  72. {
  73. float m1 = getMasse();
  74. float m2 = obj->getMasse();
  75. if( m1 == 0 || m2 == 0 )
  76. return 0;
  77. rSpeed = 0;
  78. speed = Vertex( 0, 0 );
  79. obj->setDrehungSpeed( 0 );
  80. obj->setSpeed( 0, 0 );
  81. if( v2.x || v2.y )
  82. impuls( hp - v2, v2 * ( m2 / ( m1 + m2 ) ) );
  83. if( v1.x || v1.y )
  84. obj->impuls( hp - v1, v1 * ( m1 / ( m1 + m2 ) ) );
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. }
  90. bool Object2D::tick( const WeltInfo &info, double zeit )
  91. {
  92. rotation += rSpeed * (float)zeit;
  93. while( rotation > PI * 2 )
  94. rotation -= (float)PI * 2;
  95. while( rotation < 0 )
  96. rotation += (float)PI * 2;
  97. position += speed * (float)zeit;
  98. while( zeit > 1 )
  99. {
  100. rSpeed -= rSpeed - ( rSpeed / ( 1 + info.airResistance * getLuftWiederstand() ) );
  101. speed -= speed - ( speed / ( 1 + info.airResistance * getLuftWiederstand() ) );
  102. zeit -= 1;
  103. }
  104. rSpeed -= ( rSpeed - ( rSpeed / ( 1 + info.airResistance * getLuftWiederstand() ) ) ) * (float)zeit;
  105. speed -= ( speed - ( speed / ( 1 + info.airResistance * getLuftWiederstand() ) ) ) * (float)zeit;
  106. return rSpeed != 0 || speed != Vertex( 0, 0 );
  107. }
  108. bool Object2D::istPunktInnen( Vertex p ) const
  109. {
  110. return 0;
  111. }
  112. bool Object2D::istLinieInnen( Vertex a, Vertex b ) const
  113. {
  114. return 0;
  115. }
  116. bool Object2D::istModelInnen( const Object2D *zObj, Vertex *sp, bool end ) const
  117. {
  118. return 0;
  119. }
  120. Mat3< float > Object2D::getObjectMatrix() const
  121. {
  122. return Mat3<float>::translation( position ) * Mat3<float>::rotation( rotation ) * Mat3<float>::scaling( size );
  123. }
  124. Mat3< float > Object2D::getInverseObjectMatrix() const
  125. {
  126. return Mat3<float>::scaling( 1 / size ) * Mat3<float>::rotation( -rotation ) * Mat3<float>::translation( -position );
  127. }
  128. Vertex Object2D::getObjectPos( Vertex worldPos ) const
  129. {
  130. return ( worldPos - position ).rotation( -rotation ) * ( 1 / size );
  131. }
  132. Vertex Object2D::getObjectDir( Vertex worldDir ) const
  133. {
  134. return Vertex( worldDir.x, worldDir.y ).rotation( -rotation ) * ( 1 / size );
  135. }
  136. Vertex Object2D::getWorldPos( Vertex objectPos ) const
  137. {
  138. return ( Vertex( objectPos ) * size ).rotation( rotation ) + position;
  139. }
  140. Vertex Object2D::getWorldDir( Vertex objectDir ) const
  141. {
  142. return ( Vertex( objectDir ) * size ).rotation( rotation );
  143. }
  144. Vertex Object2D::getSpeed() const
  145. {
  146. return speed;
  147. }
  148. Vertex Object2D::getPosition() const
  149. {
  150. return position;
  151. }
  152. float Object2D::getDrehungSpeed() const
  153. {
  154. return rSpeed;
  155. }
  156. float Object2D::getDrehung() const
  157. {
  158. return rotation;
  159. }
  160. float Object2D::getSize() const
  161. {
  162. return size;
  163. }
  164. bool Object2D::calcHitPoint( Vertex pos, Vertex dir, Vertex &hitpoint ) const
  165. {
  166. return 0;
  167. }
  168. float Object2D::getLuftWiederstand() const
  169. {
  170. return 0;
  171. }
  172. float Object2D::getMasse() const
  173. {
  174. return 0;
  175. }
  176. Object2D *Object2D::getThis()
  177. {
  178. ref++;
  179. return this;
  180. }
  181. Object2D *Object2D::release()
  182. {
  183. if( !--ref )
  184. delete this;
  185. return 0;
  186. }
  187. Welt2D::Welt2D()
  188. {
  189. objects = new RCArray< Object2D >();
  190. memset( &info, 0, sizeof( WeltInfo ) );
  191. ref = 1;
  192. }
  193. Welt2D::~Welt2D()
  194. {
  195. objects->release();
  196. }
  197. void Welt2D::setAirResistance( float resistance )
  198. {
  199. info.airResistance = resistance;
  200. }
  201. void Welt2D::addObject( Object2D *obj )
  202. {
  203. objects->add( obj );
  204. }
  205. void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
  206. {
  207. maxRad = maxRad * maxRad;
  208. for( auto obj = objects->getArray(); obj.set; obj++ )
  209. {
  210. if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
  211. obj.var->explosion( worldPos, intensity );
  212. }
  213. }
  214. void Welt2D::impuls( Vertex worldPos, Vertex worldDir )
  215. {
  216. Vertex hitPoint;
  217. float dist = INFINITY;
  218. Object2D *o = 0;
  219. for( auto obj = objects->getArray(); obj.set; obj++ )
  220. {
  221. if( obj.var->calcHitPoint( worldPos, worldDir, hitPoint ) )
  222. {
  223. if( ( hitPoint - worldPos ).getLengthSq() < dist )
  224. {
  225. dist = ( hitPoint - worldPos ).getLengthSq();
  226. o = obj.var;
  227. }
  228. }
  229. }
  230. if( o )
  231. o->impuls( worldPos, worldDir );
  232. }
  233. bool Welt2D::tick( double zeit )
  234. {
  235. bool ret = 0;
  236. for( auto obj = objects->getArray(); obj.set; obj++ )
  237. {
  238. if( obj.next )
  239. {
  240. for( auto obj2 = *obj.next; obj2.set; obj2++ )
  241. obj.var->handleCollision( obj2 );
  242. }
  243. ret |= obj.var->tick( info, zeit );
  244. }
  245. return ret;
  246. }
  247. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
  248. {
  249. for( auto obj = objects->getArray(); obj.set; obj++ )
  250. {
  251. Rect2< float > bnd = obj.var->getBoundingBox();
  252. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  253. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  254. bnd.bottomRight = kamMat * bnd.bottomRight;
  255. bnd.topLeft = kamMat * bnd.topLeft;
  256. topRight = kamMat * topRight;
  257. bottomLeft = kamMat * bottomLeft;
  258. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  259. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  260. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  261. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  262. ( topRight.x >= 0 && topRight.x < size.x ) ||
  263. ( topRight.y >= 0 && topRight.y < size.y ) ||
  264. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  265. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  266. obj.var->render( kamMat, zRObj );
  267. }
  268. }
  269. Welt2D *Welt2D::getThis()
  270. {
  271. ref++;
  272. return this;
  273. }
  274. Welt2D *Welt2D::release()
  275. {
  276. if( !--ref )
  277. delete this;
  278. return 0;
  279. }