Welt2D.cpp 12 KB

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