Welt2D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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::explosion( Vertex worldPos, float intensity, float maxRad )
  230. {
  231. maxRad = maxRad * maxRad;
  232. for( auto obj = objects->getArray(); obj.set; obj++ )
  233. {
  234. if( info.circular && info.hasSize && info.size.x && info.size.y )
  235. {
  236. Vertex offsets[] = {
  237. Vertex( 0, 0 ),
  238. Vertex( (float)info.size.x, 0 ),
  239. Vertex( 0, (float)info.size.y ),
  240. Vertex( (float)info.size.x, (float)info.size.y ),
  241. Vertex( (float)-info.size.x, 0 ),
  242. Vertex( 0, (float)-info.size.y ),
  243. Vertex( (float)-info.size.x, (float)-info.size.y ),
  244. Vertex( (float)-info.size.x, (float)info.size.y ),
  245. Vertex( (float)info.size.x, (float)-info.size.y ) };
  246. Vertex offset;
  247. float minDist = INFINITY;
  248. for( Vertex p : offsets )
  249. {
  250. float dist = ( obj.var->getPosition() - (worldPos - p) ).getLengthSq();
  251. if( dist < minDist )
  252. {
  253. minDist = dist;
  254. offset = p;
  255. }
  256. }
  257. if( ( obj.var->getPosition() - (worldPos - offset) ).getLengthSq() < maxRad )
  258. obj.var->explosion( worldPos - offset, intensity );
  259. }
  260. else if( ( obj.var->getPosition() - worldPos ).getLengthSq() < maxRad )
  261. obj.var->explosion( worldPos, intensity );
  262. }
  263. }
  264. void Welt2D::impuls( Vertex worldPos, Vertex worldDir )
  265. {
  266. Vertex hitPoint;
  267. float dist = INFINITY;
  268. Object2D *o = 0;
  269. for( auto obj = objects->getArray(); obj.set; obj++ )
  270. {
  271. if( obj.var->calcHitPoint( worldPos, worldDir, hitPoint ) )
  272. {
  273. if( ( hitPoint - worldPos ).getLengthSq() < dist )
  274. {
  275. dist = ( hitPoint - worldPos ).getLengthSq();
  276. o = obj.var;
  277. }
  278. }
  279. }
  280. if( o )
  281. o->impuls( worldPos, worldDir );
  282. }
  283. bool Welt2D::tick( double zeit )
  284. {
  285. bool ret = 0;
  286. for( auto obj = objects->getArray(); obj.set; obj++ )
  287. {
  288. if( obj.next )
  289. {
  290. for( auto obj2 = *obj.next; obj2.set; obj2++ )
  291. obj.var->handleCollision( obj2 );
  292. }
  293. ret |= obj.var->tick( info, zeit );
  294. }
  295. return ret;
  296. }
  297. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj, int xOffset, int yOffset )
  298. {
  299. for( auto obj = objects->getArray(); obj.set; obj++ )
  300. {
  301. Rect2< float > bnd = obj.var->getBoundingBox();
  302. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  303. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  304. Mat3< float > km = kamMat * Mat3<float>::translation( Vertex( (float)xOffset, (float)yOffset ) );
  305. bnd.bottomRight = km * bnd.bottomRight;
  306. bnd.topLeft = km * bnd.topLeft;
  307. topRight = km * topRight;
  308. bottomLeft = km * bottomLeft;
  309. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  310. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  311. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  312. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  313. ( topRight.x >= 0 && topRight.x < size.x ) ||
  314. ( topRight.y >= 0 && topRight.y < size.y ) ||
  315. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  316. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  317. obj.var->render( km, zRObj );
  318. }
  319. }
  320. void Welt2D::render( Mat3< float > &kamMat, Punkt size, Bild &zRObj )
  321. {
  322. if( !info.hasSize || !info.circular )
  323. {
  324. for( auto obj = objects->getArray(); obj.set; obj++ )
  325. {
  326. Rect2< float > bnd = obj.var->getBoundingBox();
  327. Vertex topRight = Vertex( bnd.topLeft.y, bnd.bottomRight.x );
  328. Vertex bottomLeft = Vertex( bnd.topLeft.x, bnd.bottomRight.y );
  329. bnd.bottomRight = kamMat * bnd.bottomRight;
  330. bnd.topLeft = kamMat * bnd.topLeft;
  331. topRight = kamMat * topRight;
  332. bottomLeft = kamMat * bottomLeft;
  333. if( ( bnd.bottomRight.x >= 0 && bnd.bottomRight.x < size.x ) ||
  334. ( bnd.bottomRight.y >= 0 && bnd.bottomRight.y < size.y ) ||
  335. ( bnd.topLeft.x >= 0 && bnd.topLeft.x < size.x ) ||
  336. ( bnd.topLeft.y >= 0 && bnd.topLeft.y < size.y ) ||
  337. ( topRight.x >= 0 && topRight.x < size.x ) ||
  338. ( topRight.y >= 0 && topRight.y < size.y ) ||
  339. ( bottomLeft.x >= 0 && bottomLeft.x < size.x ) ||
  340. ( bottomLeft.y >= 0 && bottomLeft.y < size.y ) )
  341. obj.var->render( kamMat, zRObj );
  342. }
  343. }
  344. else if( info.circular )
  345. {
  346. render( kamMat, size, zRObj, 0, 0 );
  347. render( kamMat, size, zRObj, info.size.x, 0 );
  348. render( kamMat, size, zRObj, 0, info.size.y );
  349. render( kamMat, size, zRObj, info.size.x, info.size.y );
  350. render( kamMat, size, zRObj, -info.size.x, 0 );
  351. render( kamMat, size, zRObj, 0, -info.size.y );
  352. render( kamMat, size, zRObj, -info.size.x, -info.size.y );
  353. render( kamMat, size, zRObj, -info.size.x, +info.size.y );
  354. render( kamMat, size, zRObj, +info.size.x, -info.size.y );
  355. }
  356. }
  357. const WeltInfo &Welt2D::getWorldInfo() const
  358. {
  359. return info;
  360. }
  361. Welt2D *Welt2D::getThis()
  362. {
  363. ref++;
  364. return this;
  365. }
  366. Welt2D *Welt2D::release()
  367. {
  368. if( !--ref )
  369. delete this;
  370. return 0;
  371. }