Welt2D.cpp 13 KB

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