Welt2D.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "Welt2D.h"
  2. #include "Bild.h"
  3. using namespace Framework;
  4. Object2D::Object2D()
  5. : ReferenceCounter()
  6. {
  7. rSpeed = 0;
  8. rotation = 0;
  9. size = 1;
  10. collision = 1;
  11. }
  12. Object2D::~Object2D()
  13. {}
  14. // Übergibt einen Void Funktionspointer auf eine Aktion die einmalig vom Hauptthread ausgeführt werden soll. (Passiert nach dem Tick)
  15. void Object2D::postAction( std::function< void() > action )
  16. {
  17. actions.push( action );
  18. }
  19. void Object2D::explosion( Vertex worldPos, float intensity )
  20. {
  21. intensity /= ( position - worldPos ).getLengthSq();
  22. speed += ( position - worldPos ) * intensity;
  23. }
  24. void Object2D::impuls( Vertex start, Vertex speed, float strength )
  25. {}
  26. void Object2D::setSpeed( Vertex speed )
  27. {
  28. this->speed = speed;
  29. }
  30. void Object2D::setSpeed( float x, float y )
  31. {
  32. speed.x = x, speed.y = y;
  33. }
  34. void Object2D::setPosition( Vertex pos )
  35. {
  36. position = pos;
  37. }
  38. void Object2D::setPosition( float x, float y )
  39. {
  40. position = Vertex( x, y );
  41. }
  42. void Object2D::setDrehungSpeed( float ds )
  43. {
  44. rSpeed = ds;
  45. }
  46. void Object2D::setDrehung( float drehung )
  47. {
  48. rotation = drehung;
  49. while( rotation > PI * 2 )
  50. rotation -= (float)PI * 2;
  51. while( rotation < 0 )
  52. rotation += (float)PI * 2;
  53. }
  54. void Object2D::addDrehung( float drehung )
  55. {
  56. rotation += drehung;
  57. while( rotation > PI * 2 )
  58. rotation -= (float)PI * 2;
  59. while( rotation < 0 )
  60. rotation += (float)PI * 2;
  61. }
  62. void Object2D::setSize( float size )
  63. {
  64. this->size = size;
  65. }
  66. void Object2D::addSize( float size )
  67. {
  68. this->size += size;
  69. }
  70. void Object2D::setCollision( bool handle )
  71. {
  72. collision = handle;
  73. }
  74. bool Object2D::handleCollision( Object2D *obj )
  75. {
  76. Vertex hp;
  77. if( istModelInnen( obj, &hp ) ) // hp wird auf den aufprallpunkt gesetzt
  78. {
  79. // Geschwindigkeit von diesem objekt mit rotation
  80. Vertex v1 = getSpeed() + getWorldDir( getObjectPos( hp ).rotation( rSpeed ) - getObjectPos( hp ) );
  81. // Geschwindigkeit des anderen Objektes mit rotation
  82. Vertex v2 = obj->getSpeed() + getWorldDir( obj->getObjectPos( hp ).rotation( obj->getDrehungSpeed() ) - obj->getObjectPos( hp ) );
  83. if( ( hp - obj->getPosition() ).getLengthSq() > ( hp + v1 * 0.03f - obj->getPosition() ).getLengthSq() ||
  84. ( hp - getPosition() ).getLengthSq() > ( hp + v2 * 0.03f - getPosition() ).getLengthSq() )
  85. { // nur wenn sie sich aufeinander zu bewegen
  86. float m1 = getMasse() * v1.getLength(); // fläche von Objekt 1
  87. float m2 = obj->getMasse() * v2.getLength(); // fläche von Objekt 2
  88. if( m1 == 0 || m2 == 0 )
  89. return 0; // falls ein objekt keine masse hat ignoriere die kollision
  90. float nm1 = m1 / ( m1 + m2 ); // koeffizient für objekt 2
  91. float nm2 = m2 / ( m1 + m2 ); // koeffizient für Objekt 1
  92. //rSpeed *= nm1; // Drehgeschwindigkeit anpassen (objekt 1)
  93. //speed *= nm1; // Bewegungsgeschwindigkeit anpassen (objekt 1)
  94. //obj->setDrehungSpeed( obj->getDrehungSpeed() * nm2 ); // Drehgeschwindigkeit anpassen (objekt 2)
  95. //obj->setSpeed( obj->getSpeed() * nm2 ); // Bewegungsgeschwindigkeit anpassen (objekt 2)
  96. float speedSumLength = getSpeed().getLength() + obj->getSpeed().getLength();
  97. rSpeed = 0;
  98. speed = Vertex();
  99. obj->setDrehungSpeed( 0 );
  100. obj->setSpeed( Vertex() );
  101. if( v2.x || v2.y )
  102. impuls( hp - v2, v2 );
  103. if( getSpeed().getLength() > 0 )
  104. setSpeed( getSpeed().normalize() * speedSumLength * nm2 );
  105. if( v1.x || v1.y )
  106. obj->impuls( hp - v1, v1 );
  107. if( obj->getSpeed().getLength() > 0 )
  108. obj->setSpeed( obj->getSpeed().normalize() * speedSumLength * nm1 );
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114. bool Object2D::tick( const WeltInfo &info, double zeit )
  115. {
  116. while( !actions.empty() )
  117. {
  118. actions.front()( );
  119. actions.pop();
  120. }
  121. rotation += rSpeed * (float)zeit;
  122. while( rotation > PI * 2 )
  123. rotation -= (float)PI * 2;
  124. while( rotation < 0 )
  125. rotation += (float)PI * 2;
  126. position += speed * (float)zeit;
  127. while( zeit > 1 )
  128. {
  129. rSpeed -= rSpeed - ( rSpeed / ( 1 + info.airResistance * getLuftWiederstand() ) );
  130. speed -= speed - ( speed / ( 1 + info.airResistance * getLuftWiederstand() ) );
  131. zeit -= 1;
  132. }
  133. rSpeed -= ( rSpeed - ( rSpeed / ( 1 + info.airResistance * getLuftWiederstand() ) ) ) * (float)zeit;
  134. speed -= ( speed - ( speed / ( 1 + info.airResistance * getLuftWiederstand() ) ) ) * (float)zeit;
  135. if( info.circular && info.hasSize && info.size.x && info.size.y )
  136. {
  137. while( position.x > (float)info.size.x )
  138. position.x -= (float)info.size.x;
  139. while( position.x < 0 )
  140. position.x += (float)info.size.x;
  141. while( position.y > ( float )info.size.y )
  142. position.y -= (float)info.size.y;
  143. while( position.y < 0 )
  144. position.y += (float)info.size.y;
  145. }
  146. return rSpeed != 0 || speed != Vertex( 0, 0 );
  147. }
  148. bool Object2D::istPunktInnen( Vertex p, bool ignoreTransparent ) const
  149. {
  150. return 0;
  151. }
  152. bool Object2D::istLinieInnen( Vertex a, Vertex b, bool ignoreTransparent ) const
  153. {
  154. return 0;
  155. }
  156. bool Object2D::istModelInnen( const Object2D *zObj, Vertex *sp, bool end, bool ignoreTransparent ) const
  157. {
  158. return 0;
  159. }
  160. Mat3< float > Object2D::getObjectMatrix() const
  161. {
  162. return Mat3<float>::translation( position ) * Mat3<float>::rotation( rotation ) * Mat3<float>::scaling( size );
  163. }
  164. Mat3< float > Object2D::getInverseObjectMatrix() const
  165. {
  166. return Mat3<float>::scaling( 1 / size ) * Mat3<float>::rotation( -rotation ) * Mat3<float>::translation( -position );
  167. }
  168. Vertex Object2D::getObjectPos( Vertex worldPos ) const
  169. {
  170. return ( worldPos - position ).rotation( -rotation ) * ( 1 / size );
  171. }
  172. Vertex Object2D::getObjectDir( Vertex worldDir ) const
  173. {
  174. return Vertex( worldDir.x, worldDir.y ).rotation( -rotation ) * ( 1 / size );
  175. }
  176. Vertex Object2D::getWorldPos( Vertex objectPos ) const
  177. {
  178. return ( Vertex( objectPos ) * size ).rotation( rotation ) + position;
  179. }
  180. Vertex Object2D::getWorldDir( Vertex objectDir ) const
  181. {
  182. return ( Vertex( objectDir ) * size ).rotation( rotation );
  183. }
  184. Vertex Object2D::getSpeed() const
  185. {
  186. return speed;
  187. }
  188. Vertex Object2D::getPosition() const
  189. {
  190. return position;
  191. }
  192. float Object2D::getDrehungSpeed() const
  193. {
  194. return rSpeed;
  195. }
  196. float Object2D::getDrehung() const
  197. {
  198. return rotation;
  199. }
  200. float Object2D::getSize() const
  201. {
  202. return size;
  203. }
  204. bool Object2D::calcHitPoint( Vertex pos, Vertex dir, Vertex &hitpoint ) const
  205. {
  206. return 0;
  207. }
  208. float Object2D::getLuftWiederstand() const
  209. {
  210. return 0;
  211. }
  212. float Object2D::getMasse() const
  213. {
  214. return 0;
  215. }
  216. bool Object2D::canCollide()
  217. {
  218. return collision;
  219. }
  220. Welt2D::Welt2D()
  221. : ReferenceCounter()
  222. {
  223. objects = new RCArray< Object2D >();
  224. memset( &info, 0, sizeof( WeltInfo ) );
  225. }
  226. Welt2D::~Welt2D()
  227. {
  228. objects->release();
  229. }
  230. void Welt2D::setAirResistance( float resistance )
  231. {
  232. info.airResistance = resistance;
  233. }
  234. void Welt2D::setSize( int width, int height )
  235. {
  236. info.size.x = width;
  237. info.size.y = height;
  238. }
  239. void Welt2D::setSize( bool hasSize )
  240. {
  241. info.hasSize = hasSize;
  242. }
  243. void Welt2D::setCircular( bool circular )
  244. {
  245. info.circular = circular;
  246. }
  247. Object2D *Welt2D::zObjectAt( int x, int y, bool ignoreTransparentFlag )
  248. {
  249. for( auto o = objects->getIterator(); o; o++ )
  250. {
  251. if( o->istPunktInnen( Punkt( x, y ), ignoreTransparentFlag ) )
  252. return o._;
  253. }
  254. return 0;
  255. }
  256. Object2D *Welt2D::getObjectAt( int x, int y, bool ignoreTransparentFlag )
  257. {
  258. Object2D *tmp = zObjectAt( x, y, ignoreTransparentFlag );
  259. return tmp ? dynamic_cast<Object2D *>( tmp->getThis() ) : 0;
  260. }
  261. void Welt2D::addObject( Object2D *obj )
  262. {
  263. objects->add( obj );
  264. }
  265. void Welt2D::removeObject( Object2D *zObj )
  266. {
  267. int anz = objects->getEintragAnzahl();
  268. for( int i = 0; i < anz; i++ )
  269. {
  270. if( objects->z( i ) == zObj )
  271. {
  272. objects->remove( i );
  273. i--;
  274. }
  275. }
  276. }
  277. void Welt2D::removeAll()
  278. {
  279. objects->leeren();
  280. }
  281. void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
  282. {
  283. maxRad = maxRad * maxRad;
  284. for( auto obj = objects->getIterator(); obj; obj++ )
  285. {
  286. if( info.circular && info.hasSize && info.size.x && info.size.y )
  287. {
  288. Vertex offsets[] = {
  289. Vertex( 0, 0 ),
  290. Vertex( (float)info.size.x, 0 ),
  291. Vertex( 0, (float)info.size.y ),
  292. Vertex( (float)info.size.x, (float)info.size.y ),
  293. Vertex( (float)-info.size.x, 0 ),
  294. Vertex( 0, (float)-info.size.y ),
  295. Vertex( (float)-info.size.x, (float)-info.size.y ),
  296. Vertex( (float)-info.size.x, (float)info.size.y ),
  297. Vertex( (float)info.size.x, (float)-info.size.y ) };
  298. Vertex offset;
  299. float minDist = INFINITY;
  300. for( Vertex p : offsets )
  301. {
  302. float dist = ( obj->getPosition() - ( worldPos - p ) ).getLengthSq();
  303. if( dist < minDist )
  304. {
  305. minDist = dist;
  306. offset = p;
  307. }
  308. }
  309. if( ( obj->getPosition() - ( worldPos - offset ) ).getLengthSq() < maxRad )
  310. obj->explosion( worldPos - offset, intensity );
  311. }
  312. else if( ( obj->getPosition() - worldPos ).getLengthSq() < maxRad )
  313. obj->explosion( worldPos, intensity );
  314. }
  315. }
  316. void Welt2D::impuls( Vertex worldPos, Vertex worldDir )
  317. {
  318. Vertex hitPoint;
  319. float dist = INFINITY;
  320. Object2D *o = 0;
  321. for( auto obj = objects->getIterator(); obj; obj++ )
  322. {
  323. if( obj->calcHitPoint( worldPos, worldDir, hitPoint ) )
  324. {
  325. if( ( hitPoint - worldPos ).getLengthSq() < dist )
  326. {
  327. dist = ( hitPoint - worldPos ).getLengthSq();
  328. o = obj;
  329. }
  330. }
  331. }
  332. if( o )
  333. o->impuls( worldPos, worldDir );
  334. }
  335. bool Welt2D::tick( double zeit )
  336. {
  337. bool ret = 0;
  338. for( auto obj = objects->getIterator(); obj; obj++ )
  339. {
  340. if( obj.hasNext() && obj->canCollide() )
  341. {
  342. for( auto obj2 = obj.next(); obj2; obj2++ )
  343. {
  344. if( obj2->canCollide() )
  345. obj->handleCollision( obj2 );
  346. }
  347. }
  348. ret |= obj->tick( info, zeit );
  349. }
  350. return ret;
  351. }
  352. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj, int xOffset, int yOffset, const char *kamName )
  353. {
  354. for( auto obj = objects->getIterator(); obj; obj++ )
  355. {
  356. Rect2< float > bnd = obj->getBoundingBox();
  357. Vertex topRight = Vertex( bnd.bottomRight.x, bnd.topLeft.y );
  358. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  359. Mat3< float > km = kamMat * Mat3<float>::translation( Vertex( (float)xOffset, (float)yOffset ) );
  360. bnd.bottomRight = km * bnd.bottomRight;
  361. bnd.topLeft = km * bnd.topLeft;
  362. topRight = km * topRight;
  363. bottomLeft = km * bottomLeft;
  364. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < (float)size.x ) ||
  365. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < (float)size.y ) ||
  366. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < (float)size.x ) ||
  367. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < (float)size.y ) ||
  368. ( topRight.x >= 0 && topRight.x < (float)size.x ) ||
  369. ( topRight.y >= 0 && topRight.y < (float)size.y ) ||
  370. ( bottomLeft.x >= 0 && bottomLeft.x < (float)size.x ) ||
  371. ( bottomLeft.y >= 0 && bottomLeft.y < (float)size.y ) )
  372. obj->render( km, zRObj, kamName );
  373. }
  374. }
  375. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj, const char *kamName )
  376. {
  377. if( !info.hasSize || !info.circular )
  378. {
  379. for( auto obj = objects->getIterator(); obj; obj++ )
  380. {
  381. Rect2< float > bnd = obj->getBoundingBox();
  382. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  383. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  384. bnd.bottomRight = kamMat * bnd.bottomRight;
  385. bnd.topLeft = kamMat * bnd.topLeft;
  386. topRight = kamMat * topRight;
  387. bottomLeft = kamMat * bottomLeft;
  388. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < (float)size.x ) ||
  389. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < (float)size.y ) ||
  390. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < (float)size.x ) ||
  391. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < (float)size.y ) ||
  392. ( topRight.x >= 0 && topRight.x < (float)size.x ) ||
  393. ( topRight.y >= 0 && topRight.y < (float)size.y ) ||
  394. ( bottomLeft.x >= 0 && bottomLeft.x < (float)size.x ) ||
  395. ( bottomLeft.y >= 0 && bottomLeft.y < (float)size.y ) )
  396. obj->render( kamMat, zRObj, kamName );
  397. }
  398. }
  399. else if( info.circular )
  400. {
  401. render( kamMat, size, zRObj, 0, 0, kamName );
  402. render( kamMat, size, zRObj, info.size.x, 0, kamName );
  403. render( kamMat, size, zRObj, 0, info.size.y, kamName );
  404. render( kamMat, size, zRObj, info.size.x, info.size.y, kamName );
  405. render( kamMat, size, zRObj, -info.size.x, 0, kamName );
  406. render( kamMat, size, zRObj, 0, -info.size.y, kamName );
  407. render( kamMat, size, zRObj, -info.size.x, -info.size.y, kamName );
  408. render( kamMat, size, zRObj, -info.size.x, +info.size.y, kamName );
  409. render( kamMat, size, zRObj, +info.size.x, -info.size.y, kamName );
  410. }
  411. }
  412. const WeltInfo &Welt2D::getWorldInfo() const
  413. {
  414. return info;
  415. }
  416. Iterator< Object2D * > Welt2D::getMembers()
  417. {
  418. return objects->getIterator();
  419. }