Advertisement
Guest User

Untitled

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