Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
- #include "BulletCollision/CollisionDispatch/btConvex2dConvex2dAlgorithm.h"
- #include "BulletCollision/CollisionDispatch/btBox2dBox2dCollisionAlgorithm.h"
- #include <SFML/Graphics.hpp>
- bool initWindow(unsigned int width, unsigned int height) {
- LynxGlobalPointers* globalPointers = getGlobalPointers();
- sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(width, height),
- "Tests");
- globalPointers->window = window;
- window->setVerticalSyncEnabled(true);
- return true;
- }
- void initPhysics() {
- btBroadphaseInterface* broadphase = new btDbvtBroadphase();
- btDefaultCollisionConfiguration* collisionConfiguration =
- new btDefaultCollisionConfiguration();
- btCollisionDispatcher* dispatcher = new btCollisionDispatcher(
- collisionConfiguration);
- btVoronoiSimplexSolver* simplexSolver = new btVoronoiSimplexSolver();
- btMinkowskiPenetrationDepthSolver* penetrationSolver =
- new btMinkowskiPenetrationDepthSolver();
- btConvex2dConvex2dAlgorithm::CreateFunc* convexAlgo2d =
- new btConvex2dConvex2dAlgorithm::CreateFunc(simplexSolver,
- penetrationSolver);
- btBox2dBox2dCollisionAlgorithm::CreateFunc* boxAlgo2d =
- new btBox2dBox2dCollisionAlgorithm::CreateFunc();
- dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,
- CONVEX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
- dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,
- CONVEX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
- dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,
- BOX_2D_SHAPE_PROXYTYPE, convexAlgo2d);
- dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,
- BOX_2D_SHAPE_PROXYTYPE, boxAlgo2d);
- btSequentialImpulseConstraintSolver* solver =
- new btSequentialImpulseConstraintSolver;
- btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(
- dispatcher, broadphase, solver, collisionConfiguration);
- dynamicsWorld->setGravity(btVector3(0, 10, 0));
- getGlobalPointers()->dynamicsWorld = dynamicsWorld;
- }
- int main() {
- LynxGlobalPointers* globalPointers = getGlobalPointers();
- char buffer[UNIX_PATH_MAX + 1];
- readlink("/proc/self/exe", buffer, UNIX_PATH_MAX);
- globalPointers->dirName = dirname(buffer);
- if (!initWindow(1024, 768)) {
- return 1;
- }
- initPhysics();
- btDynamicsWorld* dynamicsWorld = globalPointers->dynamicsWorld;
- sf::Transform sfmlTransform;
- sfmlTransform.translate(100, 200);
- sfmlTransform.rotate(40);
- btCollisionShape* boxShape = new btBoxShape(btVector3(16, 16, 1));
- btTransform groundPhysicsTransform;
- groundPhysicsTransform.setFromOpenGLMatrix(
- const_cast<float*>(sfmlTransform.getMatrix()));
- btDefaultMotionState* groundMotionState = new btDefaultMotionState(
- groundPhysicsTransform);
- btRigidBody::btRigidBodyConstructionInfo floorRbInfo(0, groundMotionState,
- boxShape, btVector3(0, 0, 0));
- btRigidBody* groundBody = new btRigidBody(floorRbInfo);
- dynamicsWorld->addRigidBody(groundBody);
- btTransform boxPhysicsTransform;
- sfmlTransform = sf::Transform::Identity;
- sfmlTransform.translate(100, 0);
- boxPhysicsTransform.setFromOpenGLMatrix(
- const_cast<float*>(sfmlTransform.getMatrix()));
- btVector3 boxInertia;
- btScalar boxMass = 10;
- boxShape->calculateLocalInertia(boxMass, boxInertia);
- btDefaultMotionState* boxMotionState = new btDefaultMotionState(
- boxPhysicsTransform);
- btRigidBody::btRigidBodyConstructionInfo boxRbInfo(boxMass, boxMotionState,
- boxShape, boxInertia);
- btRigidBody* boxBody = new btRigidBody(boxRbInfo);
- boxBody->setLinearFactor(btVector3(1, 1, 0));
- boxBody->setAngularFactor(btVector3(0, 0, 1));
- dynamicsWorld->addRigidBody(boxBody);
- sf::RenderWindow* window = globalPointers->window;
- sf::RenderStates renderState;
- sf::Clock clock;
- sf::Texture texture;
- texture.loadFromFile(
- "/run/media/sergio/RAZIEL/LynxVania/mods/vanilla/campaigns/demo/rawFiles/images/marble.png");
- sf::Sprite boxSprite(texture);
- sf::Sprite groundSprite(texture);
- while (window->isOpen()) {
- window->clear();
- sf::Time elapsed = clock.restart();
- dynamicsWorld->stepSimulation(elapsed.asMilliseconds(), 10);
- boxBody->getWorldTransform().getOpenGLMatrix(
- const_cast<float*>(sfmlTransform.getMatrix()));
- window->draw(boxSprite,
- sf::RenderStates::Default.transform * sfmlTransform);
- groundBody->getWorldTransform().getOpenGLMatrix(
- const_cast<float*>(sfmlTransform.getMatrix()));
- window->draw(groundSprite,
- sf::RenderStates::Default.transform * sfmlTransform);
- window->display();
- }
- delete window;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement