Guest User

Untitled

a guest
Dec 12th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. #include <Box2D/Box2D.h>
  2. #include <SFML/Graphics.hpp>
  3. #include "CPlayer.h"
  4. #include <string>
  5.  
  6. #define DEGTORAD 0.0174532925199432957f
  7. #define RADTODEG 57.295779513082320876f
  8. #define PIXPERMET 0.02
  9. #define METPERPIX 50
  10.  
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. // KeyPressed bools
  15. bool m_Space = false;
  16. bool m_A = true; bool m_ACanMove = true;
  17. bool m_D = true; bool m_DCanMove = true;
  18.  
  19. // body height
  20. float m_dynamicBodyDef_height_METER = 32.0f * PIXPERMET;
  21.  
  22. // Prototype
  23. void isKeyPressed(b2World &world, b2Body *body1, b2Body *edgeBody);
  24. bool atFinistLine(b2Body &body1);
  25.  
  26. // ContactListener
  27. class MyContactListener : public b2ContactListener
  28. {
  29. void BeginContact(b2Contact* contact)
  30. {
  31. // Bodys
  32. b2Body *bodyA = contact->GetFixtureA()->GetBody();
  33. b2Body *bodyB = contact->GetFixtureB()->GetBody();
  34.  
  35. // Einer der Bodies Spieler?
  36. if ((int)bodyA->GetUserData() == 1)
  37. {
  38. if ((int)bodyB->GetUserData() == 0)
  39. {
  40. m_Space = true;
  41. }
  42. }
  43. else if ((int)bodyB->GetUserData() == 1)
  44. {
  45. if ((int)bodyA->GetUserData() == 0)
  46. m_Space = true;
  47. }
  48. }
  49.  
  50. void EndContact(b2Contact* contact) {}
  51. };
  52.  
  53. int main(int argc, char** argv)
  54. {
  55.  
  56. // Timer
  57. sf::Clock m_Clock;
  58. float m_dt, m_fullTime, m_VelocityFullTime;
  59. // Werte für die Physik
  60. float32 timeStep = 1.0f / 60.0f;
  61. int32 velocityIterations = 10;
  62. int32 positionIterations = 8;
  63.  
  64. // Fenster öffnen
  65. sf::RenderWindow m_MainWindow;
  66. m_MainWindow.create(sf::VideoMode(1920.0f, 1080.0f), "Test Anwendung Box2D", sf::Style::Fullscreen);
  67.  
  68. // Welt erstellen
  69. b2Vec2 m_Gravity(0.0f, 9.81f);
  70. b2World *m_World = new b2World(m_Gravity);
  71. // ContactListener
  72. b2ContactListener *ContactListener = new MyContactListener;
  73. m_World->SetContactListener(ContactListener);
  74.  
  75. // Edge erstellen
  76. // EdgeBody Def
  77. b2BodyDef m_edgeBodyDef;
  78. m_edgeBodyDef.type = b2_staticBody;
  79. m_edgeBodyDef.position.Set(0 * PIXPERMET, 0 * PIXPERMET);
  80. b2Body *m_edgeBody = m_World->CreateBody(&m_edgeBodyDef);
  81. int UserDataEdgeBody = 0;
  82. m_edgeBody->SetUserData((void*)UserDataEdgeBody);
  83. b2Body *m_edgeBodyL = m_World->CreateBody(&m_edgeBodyDef);
  84. b2Body *m_edgeBodyR = m_World->CreateBody(&m_edgeBodyDef);
  85. // Edge Shape
  86. b2EdgeShape m_EdgeShape;
  87. m_EdgeShape.Set(b2Vec2(0.0f * PIXPERMET, (1080.0f) * PIXPERMET),
  88. b2Vec2(1920.0f * PIXPERMET, (1080.0f) * PIXPERMET));
  89. // Fixture Template static erstellen
  90. b2FixtureDef m_edgeFixtureDef;
  91. m_edgeFixtureDef.shape = &m_EdgeShape;
  92. m_edgeBody->CreateFixture(&m_edgeFixtureDef);
  93. m_edgeFixtureDef.density = 0;
  94.  
  95. // EdgeShapeL
  96. m_EdgeShape.Set(b2Vec2((0.0f) * PIXPERMET, 0.0f * PIXPERMET),
  97. b2Vec2((0.0f) * PIXPERMET, 1080.0f * PIXPERMET));
  98. m_edgeFixtureDef.shape = &m_EdgeShape;
  99. m_edgeBodyL->CreateFixture(&m_edgeFixtureDef);
  100.  
  101. // EdgeShapeR
  102. m_EdgeShape.Set(b2Vec2((1920.0f) * PIXPERMET, 0.0f * PIXPERMET),
  103. b2Vec2((1920.0f) * PIXPERMET, 1080.0f * PIXPERMET));
  104. m_edgeFixtureDef.shape = &m_EdgeShape;
  105. m_edgeBodyL->CreateFixture(&m_edgeFixtureDef);
  106.  
  107. // Body Template erstellen
  108. b2BodyDef m_dynamicBodyDef;
  109. m_dynamicBodyDef.type = b2_dynamicBody;
  110. m_dynamicBodyDef.position.Set(64.0f * PIXPERMET, 64.0f * PIXPERMET);
  111.  
  112. // Body (Template1) erstellen
  113. b2Body *m_dynamicBody_1 = m_World->CreateBody(&m_dynamicBodyDef);
  114. int UserDataPlayerBody = 1;
  115. m_dynamicBody_1->SetUserData((void*)UserDataPlayerBody);
  116. m_dynamicBody_1->SetFixedRotation(true);
  117.  
  118. // Shape Template erstellen
  119. b2PolygonShape m_polygoneShape;
  120. m_polygoneShape.SetAsBox(64.0f * PIXPERMET, 64.0f * PIXPERMET);
  121.  
  122. // Fixture Template erstellen
  123. b2FixtureDef m_fixtureDef;
  124. m_fixtureDef.shape = &m_polygoneShape;
  125. m_fixtureDef.density = 70.0f;
  126. m_dynamicBody_1->CreateFixture(&m_fixtureDef);
  127.  
  128. // Gegner BodyTemplate
  129. b2BodyDef m_enemyBodyDef;
  130. m_enemyBodyDef.type = b2_staticBody;
  131. m_enemyBodyDef.position.Set(1000.0f * PIXPERMET, (800.0f) * PIXPERMET);
  132.  
  133. // Body GegnerTemplate 1
  134. b2Body *m_enemyBody = m_World->CreateBody(&m_enemyBodyDef);
  135. int UserDataEnemy = 2;
  136. m_enemyBody->SetUserData((void*)UserDataEnemy);;
  137.  
  138. // Shape
  139. b2PolygonShape m_enemyShape;
  140. m_enemyShape.SetAsBox(64.0f * PIXPERMET, 64.0f * PIXPERMET);
  141.  
  142. // Gegner Fixture Template
  143. b2FixtureDef m_enemyFixtureDef;
  144. m_enemyFixtureDef.shape = &m_enemyShape;
  145. m_enemyBody->CreateFixture(&m_enemyFixtureDef);
  146.  
  147. // Sprites laden
  148. sf::Sprite m_Block, m_Enemy;
  149. sf::Texture m_Block_Texture, m_EnemyTexture;
  150. sf::IntRect m_Block_Rect, m_EnemyRect;
  151.  
  152. m_Block_Texture.loadFromFile("Data/Graphics/block.png");
  153. m_Block_Rect.left = 0.0f;
  154. m_Block_Rect.top = 0.0f;
  155. m_Block_Rect.width = 64.0f;
  156. m_Block_Rect.height = 64.0f;
  157. m_Block.setTexture(m_Block_Texture);
  158. m_Block.setTextureRect(m_Block_Rect);
  159. m_Block.setOrigin(32.0f, 32.0f);
  160. m_EnemyTexture.loadFromFile("Data/Graphics/block.png");
  161. m_EnemyRect.left = 0.0f;
  162. m_EnemyRect.top = 0.0f;
  163. m_EnemyRect.width = 64.0f;
  164. m_EnemyRect.height = 64.0f;
  165. m_Enemy.setTexture(m_EnemyTexture);
  166. m_Enemy.setTextureRect(m_EnemyRect);
  167. m_Enemy.setOrigin(32.0f, 32.0f);
  168.  
  169. // Hintergrund
  170. sf::Sprite m_Background_S;
  171. sf::Texture m_Background_T;
  172. sf::IntRect m_Background_R;
  173.  
  174. m_Background_T.loadFromFile("D:/Projects/Box2DProjects/Box2DTestProgramm/Data/Graphics/Background-Image.png");
  175. m_Background_R.left = 0;
  176. m_Background_R.top = 0;
  177. m_Background_R.width = 512.0f;
  178. m_Background_R.height = 320.0f;
  179. m_Background_S.setTexture(m_Background_T);
  180. m_Background_S.setTextureRect(m_Background_R);
  181. m_Background_S.setScale(sf::Vector2f(3.75, 3.375));
  182. m_Background_S.setPosition(0.0f, 0.0f);
  183.  
  184. // Für dt und Zeit
  185. m_Clock.restart();
  186. m_dt = 0;
  187. m_fullTime = 0;
  188. m_VelocityFullTime = 0;
  189.  
  190. // Main Loop
  191. while (m_MainWindow.isOpen())
  192. {
  193. // Alle SFML Bilder löschen
  194. m_MainWindow.clear();
  195.  
  196. // Spiel beenden?
  197. if (atFinistLine(*m_dynamicBody_1))
  198. m_MainWindow.close();
  199.  
  200. // Background malen
  201. m_MainWindow.draw(m_Background_S);
  202.  
  203. // Events abfragen
  204. sf::Event m_Event;
  205. if (m_MainWindow.pollEvent(m_Event))
  206. {
  207. if (m_Event.type == sf::Event::Closed)
  208. return 0;
  209. }
  210.  
  211. isKeyPressed(*m_World, m_dynamicBody_1, m_edgeBody);
  212.  
  213. // Physik berechnen
  214. if (m_VelocityFullTime >= timeStep)
  215. {
  216. // Zeit resetten
  217. m_VelocityFullTime = 0.0f;
  218. // World Step machen
  219. m_World->Step(timeStep, velocityIterations, positionIterations);
  220. // Spieler Sprite Position setzten
  221. m_Block.setPosition(m_dynamicBody_1->GetPosition().x * METPERPIX, m_dynamicBody_1->GetPosition().y * METPERPIX);
  222. m_Block.setRotation(m_dynamicBody_1->GetAngle() * RADTODEG);
  223. }
  224.  
  225. // Gegner malen
  226. m_Enemy.setPosition(m_enemyBody->GetPosition().x * METPERPIX, m_enemyBody->GetPosition().y * METPERPIX);
  227. m_Enemy.setRotation(m_enemyBody->GetAngle() * RADTODEG);
  228. m_MainWindow.draw(m_Enemy);
  229.  
  230. // Spieler malen
  231. m_MainWindow.draw(m_Block);
  232.  
  233. //cout << m_dynamicBody_1->GetPosition().x * METPERPIX << endl << m_dynamicBody_1->GetPosition().y * METPERPIX << endl;
  234.  
  235. // Alles anzeigen
  236. m_MainWindow.display();
  237.  
  238. // dt setzten
  239. m_dt = m_Clock.restart().asSeconds();
  240. m_fullTime += m_dt;
  241. m_VelocityFullTime += m_dt;
  242.  
  243. }
  244.  
  245. m_MainWindow.close();
  246. }
  247.  
  248.  
  249.  
  250.  
  251.  
  252. // isKeyPressed()
  253. void isKeyPressed(b2World &world, b2Body *body1, b2Body *edgeBody)
  254. {
  255. // Jump
  256. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && m_Space == true)
  257. {
  258. m_Space = false;
  259.  
  260. //float impulse = body1->GetMass() * (-1080 * PIXPERMET);
  261. //body1->ApplyLinearImpulse(b2Vec2(0, impulse), body1->GetWorldCenter(), true);
  262. b2Vec2 velocity = body1->GetLinearVelocity();
  263. velocity.y = -12;
  264. body1->SetLinearVelocity(velocity);
  265. }
  266.  
  267. // Teleport
  268. if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
  269. {
  270. body1->SetTransform(b2Vec2(200 * PIXPERMET, 200 * PIXPERMET), 0);
  271. }
  272.  
  273. // Move right
  274. if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && m_ACanMove)
  275. {
  276. // Force
  277. b2Vec2 velocity = body1->GetLinearVelocity();
  278. float force = 0;
  279. if (velocity.x < 5) force = 100;
  280. // Impulse
  281. body1->ApplyLinearImpulse(b2Vec2(force, 0), body1->GetWorldCenter(), true);
  282. }
  283.  
  284. // Move left
  285. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && m_ACanMove)
  286. {
  287. // Force
  288. b2Vec2 velocity = body1->GetLinearVelocity();
  289. float force = 0;
  290. if (velocity.x > -5) force = -100;
  291. // Impulse
  292. body1->ApplyLinearImpulse(b2Vec2(force, 0), body1->GetWorldCenter(), true);
  293. }
  294. }
  295.  
  296. // atFinishLine()
  297. bool atFinistLine(b2Body &body1)
  298. {
  299. // Ziellinie
  300. float finishLineX1 = 1800.0f;
  301. float finishLineX2 = 1920.0f;
  302. float finishLineY1 = 0.0f;
  303. float finishLineY2 = 1080.0f;
  304.  
  305. // Position
  306. float x, y;
  307. x = body1.GetPosition().x * METPERPIX;
  308. y = body1.GetPosition().y * METPERPIX;
  309.  
  310. // Auf/Über Ziellinie?
  311. if (x > finishLineX1 &&
  312. x < finishLineX2 &&
  313. y > finishLineY1 &&
  314. y < finishLineY2)
  315. {
  316. cout << "Ziel erreicht!" << endl;
  317. return true;
  318. }
  319. else
  320. return false;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment