Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <Box2D/Box2D.h>
  2. #include <SFML/Graphics.hpp>
  3. #include "CPlayer.h"
  4.  
  5. #define DEGTORAD 0.0174532925199432957f
  6. #define RADTODEG 57.295779513082320876f
  7. #define PIXTOMET 50
  8. #define METTOPIX 0.02
  9.  
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. int main(int argc, char** argv)
  14. {
  15.  
  16. // Timer
  17. sf::Clock m_Clock;
  18. float m_dt, m_fullTime, m_VelocityFullTime;
  19.  
  20. // Fenster öffnen
  21. sf::RenderWindow m_MainWindow;
  22. m_MainWindow.create(sf::VideoMode(1920, 1080), "Test Anwendung Box2D", sf::Style::Fullscreen);
  23.  
  24. // Welt erstellen
  25. b2Vec2 m_Gravity(0.0f, 10.0f);
  26. b2World *m_World = new b2World(m_Gravity);
  27.  
  28. // Sprite laden
  29. sf::Sprite m_Block;
  30. sf::Texture m_Block_Texture;
  31. sf::IntRect m_Block_Rect;
  32.  
  33. m_Block_Texture.loadFromFile("Data/Graphics/block.png");
  34. m_Block_Rect.left = 0;
  35. m_Block_Rect.top = 0;
  36. m_Block_Rect.width = 64.0f;
  37. m_Block_Rect.height = 64.0f;
  38. m_Block.setTexture(m_Block_Texture);
  39. m_Block.setTextureRect(m_Block_Rect);
  40. m_Block.setOrigin(32.0f, 32.0f);
  41.  
  42. // Body Template erstellen
  43. b2BodyDef m_dynamicBodyDef;
  44. m_dynamicBodyDef.type = b2_dynamicBody;
  45. m_dynamicBodyDef.position.Set(32 * PIXTOMET, 0 * PIXTOMET);
  46. // Body (Template1) erstellen
  47. b2Body *m_dynamicBody_1 = m_World->CreateBody(&m_dynamicBodyDef);
  48. m_dynamicBody_1->SetGravityScale(1);
  49.  
  50. // Shape Template erstellen
  51. b2PolygonShape m_polygoneShape;
  52. m_polygoneShape.SetAsBox(1.28, 1.28);
  53.  
  54. // Fixture Template erstellen
  55. b2FixtureDef m_fixtureDef;
  56. m_fixtureDef.shape = &m_polygoneShape;
  57. m_fixtureDef.density = 1;
  58.  
  59. m_dynamicBody_1->CreateFixture(&m_fixtureDef);
  60.  
  61. // EdgeBody Def
  62. b2BodyDef m_edgeBodyDef;
  63. m_edgeBodyDef.type = b2_staticBody;
  64. m_edgeBodyDef.position.Set(0, 0);
  65. b2Body *m_edgeBody = m_World->CreateBody(&m_edgeBodyDef);
  66.  
  67. // Edge Shape
  68. b2EdgeShape m_EdgeShape;
  69. m_EdgeShape.Set(b2Vec2(20 * PIXTOMET, 1080 * PIXTOMET), b2Vec2((m_MainWindow.getSize().x - 20) * PIXTOMET, 1080 * PIXTOMET));
  70.  
  71. // Fixture Template static erstellen
  72. b2FixtureDef m_edgeFixtureDef;
  73. m_edgeFixtureDef.shape = &m_EdgeShape;
  74. m_edgeBody->CreateFixture(&m_edgeFixtureDef);
  75.  
  76. float32 timeStep = 1.0f / 60.0f;
  77. int32 velocityIterations = 6;
  78. int32 positionIterations = 2;
  79.  
  80. m_Clock.restart();
  81. m_dt = 0;
  82. m_fullTime = 0;
  83. m_VelocityFullTime = 0;
  84.  
  85. // Main Loop
  86. while (m_MainWindow.isOpen())
  87. {
  88. m_MainWindow.clear();
  89.  
  90. // Events abfragen
  91. sf::Event m_Event;
  92. if (m_MainWindow.pollEvent(m_Event))
  93. {
  94. if (m_Event.type == sf::Event::Closed)
  95. return 0;
  96. }
  97.  
  98. //cout << m_VelocityFullTime << endl;
  99. if (m_VelocityFullTime >= 0.016667f)
  100. {
  101. //cout << "triggerd" << endl;
  102. m_VelocityFullTime = 0;
  103. // World Step machen
  104. m_World->Step(timeStep, velocityIterations, positionIterations);
  105. }
  106.  
  107. m_Block.setPosition(m_dynamicBody_1->GetPosition().x * METTOPIX, m_dynamicBody_1->GetPosition().y);
  108. m_Block.setRotation(m_dynamicBody_1->GetAngle() * RADTODEG);
  109. m_MainWindow.draw(m_Block);
  110.  
  111. cout << m_dynamicBody_1->GetPosition().x * METTOPIX << endl << m_dynamicBody_1->GetPosition().y * METTOPIX << endl;
  112.  
  113. // Alles anzeigen
  114. m_MainWindow.display();
  115.  
  116. // dt setzten
  117. m_dt = m_Clock.restart().asSeconds();
  118. m_fullTime += m_dt;
  119. m_VelocityFullTime += m_dt;
  120.  
  121. }
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement