Welt2D.cpp 12 KB

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