Welt2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. if( info.circular && info.hasSize && info.size.x && info.size.y )
  107. {
  108. while( position.x > info.size.x )
  109. position.x -= (float)info.size.x;
  110. while( position.x < 0 )
  111. position.x += (float)info.size.x;
  112. while( position.y > info.size.y )
  113. position.y -= (float)info.size.y;
  114. while( position.y < 0 )
  115. position.y += (float)info.size.y;
  116. }
  117. return rSpeed != 0 || speed != Vertex( 0, 0 );
  118. }
  119. bool Object2D::istPunktInnen( Vertex p ) const
  120. {
  121. return 0;
  122. }
  123. bool Object2D::istLinieInnen( Vertex a, Vertex b ) const
  124. {
  125. return 0;
  126. }
  127. bool Object2D::istModelInnen( const Object2D *zObj, Vertex *sp, bool end ) const
  128. {
  129. return 0;
  130. }
  131. Mat3< float > Object2D::getObjectMatrix() const
  132. {
  133. return Mat3<float>::translation( position ) * Mat3<float>::rotation( rotation ) * Mat3<float>::scaling( size );
  134. }
  135. Mat3< float > Object2D::getInverseObjectMatrix() const
  136. {
  137. return Mat3<float>::scaling( 1 / size ) * Mat3<float>::rotation( -rotation ) * Mat3<float>::translation( -position );
  138. }
  139. Vertex Object2D::getObjectPos( Vertex worldPos ) const
  140. {
  141. return ( worldPos - position ).rotation( -rotation ) * ( 1 / size );
  142. }
  143. Vertex Object2D::getObjectDir( Vertex worldDir ) const
  144. {
  145. return Vertex( worldDir.x, worldDir.y ).rotation( -rotation ) * ( 1 / size );
  146. }
  147. Vertex Object2D::getWorldPos( Vertex objectPos ) const
  148. {
  149. return ( Vertex( objectPos ) * size ).rotation( rotation ) + position;
  150. }
  151. Vertex Object2D::getWorldDir( Vertex objectDir ) const
  152. {
  153. return ( Vertex( objectDir ) * size ).rotation( rotation );
  154. }
  155. Vertex Object2D::getSpeed() const
  156. {
  157. return speed;
  158. }
  159. Vertex Object2D::getPosition() const
  160. {
  161. return position;
  162. }
  163. float Object2D::getDrehungSpeed() const
  164. {
  165. return rSpeed;
  166. }
  167. float Object2D::getDrehung() const
  168. {
  169. return rotation;
  170. }
  171. float Object2D::getSize() const
  172. {
  173. return size;
  174. }
  175. bool Object2D::calcHitPoint( Vertex pos, Vertex dir, Vertex &hitpoint ) const
  176. {
  177. return 0;
  178. }
  179. float Object2D::getLuftWiederstand() const
  180. {
  181. return 0;
  182. }
  183. float Object2D::getMasse() const
  184. {
  185. return 0;
  186. }
  187. Object2D *Object2D::getThis()
  188. {
  189. ref++;
  190. return this;
  191. }
  192. Object2D *Object2D::release()
  193. {
  194. if( !--ref )
  195. delete this;
  196. return 0;
  197. }
  198. Welt2D::Welt2D()
  199. {
  200. objects = new RCArray< Object2D >();
  201. memset( &info, 0, sizeof( WeltInfo ) );
  202. ref = 1;
  203. }
  204. Welt2D::~Welt2D()
  205. {
  206. objects->release();
  207. }
  208. void Welt2D::setAirResistance( float resistance )
  209. {
  210. info.airResistance = resistance;
  211. }
  212. void Welt2D::setSize( int width, int height )
  213. {
  214. info.size.x = width;
  215. info.size.y = height;
  216. }
  217. void Welt2D::setSize( bool hasSize )
  218. {
  219. info.hasSize = hasSize;
  220. }
  221. void Welt2D::setCircular( bool circular )
  222. {
  223. info.circular = circular;
  224. }
  225. void Welt2D::addObject( Object2D *obj )
  226. {
  227. objects->add( obj );
  228. }
  229. void Welt2D::removeObject( Object2D *obj )
  230. {
  231. int anz = objects->getEintragAnzahl();
  232. for( int i = 0; i < anz; i++ )
  233. {
  234. if( objects->get( i ) == obj )
  235. {
  236. objects->remove( i );
  237. i--;
  238. }
  239. }
  240. }
  241. void Welt2D::removeAll()
  242. {
  243. objects->leeren();
  244. }
  245. void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
  246. {
  247. maxRad = maxRad * maxRad;
  248. for( auto obj = objects->getArray(); obj.set; obj++ )
  249. {
  250. if( info.circular && info.hasSize && info.size.x && info.size.y )
  251. {
  252. Vertex offsets[] = {
  253. Vertex( 0, 0 ),
  254. Vertex( (float)info.size.x, 0 ),
  255. Vertex( 0, (float)info.size.y ),
  256. Vertex( (float)info.size.x, (float)info.size.y ),
  257. Vertex( (float)-info.size.x, 0 ),
  258. Vertex( 0, (float)-info.size.y ),
  259. Vertex( (float)-info.size.x, (float)-info.size.y ),
  260. Vertex( (float)-info.size.x, (float)info.size.y ),
  261. Vertex( (float)info.size.x, (float)-info.size.y ) };
  262. Vertex offset;
  263. float minDist = INFINITY;
  264. for( Vertex p : offsets )
  265. {
  266. float dist = ( obj.var->getPosition() - (worldPos - p) ).getLengthSq();
  267. if( dist < minDist )
  268. {
  269. minDist = dist;
  270. offset = p;
  271. }
  272. }
  273. if( ( obj.var->getPosition() - (worldPos - offset) ).getLengthSq() < maxRad )
  274. obj.var->explosion( worldPos - offset, intensity );
  275. }
  276. else if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
  277. obj.var->explosion( worldPos, intensity );
  278. }
  279. }
  280. void Welt2D::impuls( Vertex worldPos, Vertex worldDir )
  281. {
  282. Vertex hitPoint;
  283. float dist = INFINITY;
  284. Object2D *o = 0;
  285. for( auto obj = objects->getArray(); obj.set; obj++ )
  286. {
  287. if( obj.var->calcHitPoint( worldPos, worldDir, hitPoint ) )
  288. {
  289. if( ( hitPoint - worldPos ).getLengthSq() < dist )
  290. {
  291. dist = ( hitPoint - worldPos ).getLengthSq();
  292. o = obj.var;
  293. }
  294. }
  295. }
  296. if( o )
  297. o->impuls( worldPos, worldDir );
  298. }
  299. bool Welt2D::tick( double zeit )
  300. {
  301. bool ret = 0;
  302. for( auto obj = objects->getArray(); obj.set; obj++ )
  303. {
  304. if( obj.next )
  305. {
  306. for( auto obj2 = *obj.next; obj2.set; obj2++ )
  307. obj.var->handleCollision( obj2 );
  308. }
  309. ret |= obj.var->tick( info, zeit );
  310. }
  311. return ret;
  312. }
  313. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj, int xOffset, int yOffset )
  314. {
  315. for( auto obj = objects->getArray(); obj.set; obj++ )
  316. {
  317. Rect2< float > bnd = obj.var->getBoundingBox();
  318. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  319. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  320. Mat3< float > km = kamMat * Mat3<float>::translation( Vertex( (float)xOffset, (float)yOffset ) );
  321. bnd.bottomRight = km * bnd.bottomRight;
  322. bnd.topLeft = km * bnd.topLeft;
  323. topRight = km * topRight;
  324. bottomLeft = km * bottomLeft;
  325. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  326. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  327. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  328. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  329. ( topRight.x >= 0 && topRight.x < size.x ) ||
  330. ( topRight.y >= 0 && topRight.y < size.y ) ||
  331. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  332. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  333. obj.var->render( km, zRObj );
  334. }
  335. }
  336. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
  337. {
  338. if( !info.hasSize || !info.circular )
  339. {
  340. for( auto obj = objects->getArray(); obj.set; obj++ )
  341. {
  342. Rect2< float > bnd = obj.var->getBoundingBox();
  343. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  344. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  345. bnd.bottomRight = kamMat * bnd.bottomRight;
  346. bnd.topLeft = kamMat * bnd.topLeft;
  347. topRight = kamMat * topRight;
  348. bottomLeft = kamMat * bottomLeft;
  349. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  350. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  351. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  352. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  353. ( topRight.x >= 0 && topRight.x < size.x ) ||
  354. ( topRight.y >= 0 && topRight.y < size.y ) ||
  355. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  356. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  357. obj.var->render( kamMat, zRObj );
  358. }
  359. }
  360. else if( info.circular )
  361. {
  362. render( kamMat, size, zRObj, 0, 0 );
  363. render( kamMat, size, zRObj, info.size.x, 0 );
  364. render( kamMat, size, zRObj, 0, info.size.y );
  365. render( kamMat, size, zRObj, info.size.x, info.size.y );
  366. render( kamMat, size, zRObj, -info.size.x, 0 );
  367. render( kamMat, size, zRObj, 0, -info.size.y );
  368. render( kamMat, size, zRObj, -info.size.x, -info.size.y );
  369. render( kamMat, size, zRObj, -info.size.x, +info.size.y );
  370. render( kamMat, size, zRObj, +info.size.x, -info.size.y );
  371. }
  372. }
  373. const WeltInfo &Welt2D::getWorldInfo() const
  374. {
  375. return info;
  376. }
  377. Welt2D *Welt2D::getThis()
  378. {
  379. ref++;
  380. return this;
  381. }
  382. Welt2D *Welt2D::release()
  383. {
  384. if( !--ref )
  385. delete this;
  386. return 0;
  387. }