Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include "main.h"
  2. #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
  3. #include "BulletCollision/CollisionDispatch/btConvex2dConvex2dAlgorithm.h"
  4. #include "BulletCollision/CollisionDispatch/btBox2dBox2dCollisionAlgorithm.h"
  5. #include <SFML/Graphics.hpp>
  6.  
  7. bool initWindow(unsigned int width, unsigned int height) {
  8. LynxGlobalPointers* globalPointers = getGlobalPointers();
  9.  
  10. sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(width, height),
  11. "Tests");
  12.  
  13. globalPointers->window = window;
  14.  
  15. window->setVerticalSyncEnabled(true);
  16.  
  17. return true;
  18. }
  19.  
  20. void initPhysics() {
  21.  
  22. btBroadphaseInterface* broadphase = new btDbvtBroadphase();
  23.  
  24. btDefaultCollisionConfiguration* collisionConfiguration =
  25. new btDefaultCollisionConfiguration();
  26.  
  27. btCollisionDispatcher* dispatcher = new btCollisionDispatcher(
  28. collisionConfiguration);
  29.  
  30. btVoronoiSimplexSolver* simplexSolver = new btVoronoiSimplexSolver();
  31.  
  32. btMinkowskiPenetrationDepthSolver* penetrationSolver =
  33. new btMinkowskiPenetrationDepthSolver();
  34.  
  35. btConvex2dConvex2dAlgorithm::CreateFunc* convexAlgo2d =
  36. new btConvex2dConvex2dAlgorithm::CreateFunc(simplexSolver,
  37. penetrationSolver);
  38.  
  39. btBox2dBox2dCollisionAlgorithm::CreateFunc* boxAlgo2d =
  40. new btBox2dBox2dCollisionAlgorithm::CreateFunc();
  41.  
  42. dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,
  43. CONVEX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
  44. dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,
  45. CONVEX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
  46. dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,
  47. BOX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
  48. dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,
  49. BOX_2D_SHAPE_PROXYTYPE, boxAlgo2d);
  50.  
  51. btSequentialImpulseConstraintSolver* solver =
  52. new btSequentialImpulseConstraintSolver;
  53.  
  54. btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(
  55. dispatcher, broadphase, solver, collisionConfiguration);
  56.  
  57. dynamicsWorld->setGravity(btVector3(0, 10, 0));
  58.  
  59. getGlobalPointers()->dynamicsWorld = dynamicsWorld;
  60.  
  61. }
  62.  
  63. int main() {
  64.  
  65. LynxGlobalPointers* globalPointers = getGlobalPointers();
  66.  
  67. char buffer[UNIX_PATH_MAX + 1];
  68. readlink("/proc/self/exe", buffer, UNIX_PATH_MAX);
  69. globalPointers->dirName = dirname(buffer);
  70.  
  71. if (!initWindow(1024, 768)) {
  72. return 1;
  73. }
  74.  
  75. initPhysics();
  76.  
  77. btDynamicsWorld* dynamicsWorld = globalPointers->dynamicsWorld;
  78.  
  79. sf::Transform sfmlTransform;
  80. sfmlTransform.translate(100, 200);
  81. sfmlTransform.rotate(40);
  82.  
  83. btCollisionShape* boxShape = new btBoxShape(btVector3(16, 16, 1));
  84. btTransform groundPhysicsTransform;
  85. groundPhysicsTransform.setFromOpenGLMatrix(
  86. const_cast<float*>(sfmlTransform.getMatrix()));
  87.  
  88. btDefaultMotionState* groundMotionState = new btDefaultMotionState(
  89. groundPhysicsTransform);
  90. btRigidBody::btRigidBodyConstructionInfo floorRbInfo(0, groundMotionState,
  91. boxShape, btVector3(0, 0, 0));
  92. btRigidBody* groundBody = new btRigidBody(floorRbInfo);
  93.  
  94. dynamicsWorld->addRigidBody(groundBody);
  95.  
  96. btTransform boxPhysicsTransform;
  97.  
  98. sfmlTransform = sf::Transform::Identity;
  99. sfmlTransform.translate(100, 0);
  100. boxPhysicsTransform.setFromOpenGLMatrix(
  101. const_cast<float*>(sfmlTransform.getMatrix()));
  102.  
  103. btVector3 boxInertia;
  104.  
  105. btScalar boxMass = 10;
  106.  
  107. boxShape->calculateLocalInertia(boxMass, boxInertia);
  108.  
  109. btDefaultMotionState* boxMotionState = new btDefaultMotionState(
  110. boxPhysicsTransform);
  111. btRigidBody::btRigidBodyConstructionInfo boxRbInfo(boxMass, boxMotionState,
  112. boxShape, boxInertia);
  113.  
  114. btRigidBody* boxBody = new btRigidBody(boxRbInfo);
  115.  
  116. boxBody->setLinearFactor(btVector3(1, 1, 0));
  117. boxBody->setAngularFactor(btVector3(0, 0, 1));
  118.  
  119. dynamicsWorld->addRigidBody(boxBody);
  120.  
  121. sf::RenderWindow* window = globalPointers->window;
  122.  
  123. sf::RenderStates renderState;
  124.  
  125. sf::Clock clock;
  126.  
  127. sf::Texture texture;
  128. texture.loadFromFile(
  129. "/run/media/sergio/RAZIEL/LynxVania/mods/vanilla/campaigns/demo/rawFiles/images/marble.png");
  130.  
  131. sf::Sprite boxSprite(texture);
  132.  
  133. sf::Sprite groundSprite(texture);
  134.  
  135. while (window->isOpen()) {
  136.  
  137. window->clear();
  138.  
  139. sf::Time elapsed = clock.restart();
  140.  
  141. dynamicsWorld->stepSimulation(elapsed.asMilliseconds(), 10);
  142.  
  143. boxBody->getWorldTransform().getOpenGLMatrix(
  144. const_cast<float*>(sfmlTransform.getMatrix()));
  145.  
  146. window->draw(boxSprite,
  147. sf::RenderStates::Default.transform * sfmlTransform);
  148.  
  149. groundBody->getWorldTransform().getOpenGLMatrix(
  150. const_cast<float*>(sfmlTransform.getMatrix()));
  151.  
  152. window->draw(groundSprite,
  153. sf::RenderStates::Default.transform * sfmlTransform);
  154.  
  155. window->display();
  156.  
  157. }
  158.  
  159. delete window;
  160.  
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement