Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | None | 0 0
  1. // Bullet1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <btBulletDynamicsCommon.h>
  7.  
  8. #include <irrlicht.h>
  9.  
  10. #define M_PI (3.14159)
  11.  
  12. using namespace irr;
  13.  
  14. /*
  15. To receive events like mouse and keyboard input, or GUI events like "the OK
  16. button has been clicked", we need an object which is derived from the
  17. irr::IEventReceiver object. There is only one method to override:
  18. irr::IEventReceiver::OnEvent(). This method will be called by the engine once
  19. when an event happens. What we really want to know is whether a key is being
  20. held down, and so we will remember the current state of each key.
  21. */
  22. class MyEventReceiver : public IEventReceiver
  23. {
  24. public:
  25.     // This is the one method that we have to implement
  26.     virtual bool OnEvent(const SEvent& event)
  27.     {
  28.         // Remember whether each key is down or up
  29.         if (event.EventType == irr::EET_KEY_INPUT_EVENT)
  30.             KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
  31.  
  32.         return false;
  33.     }
  34.  
  35.     // This is used to check whether a key is being held down
  36.     virtual bool IsKeyDown(EKEY_CODE keyCode) const
  37.     {
  38.         return KeyIsDown[keyCode];
  39.     }
  40.  
  41.     MyEventReceiver()
  42.     {
  43.         for (u32 i = 0; i<KEY_KEY_CODES_COUNT; ++i)
  44.             KeyIsDown[i] = false;
  45.     }
  46.  
  47. private:
  48.     // We use this array to store the current state of each key
  49.     bool KeyIsDown[KEY_KEY_CODES_COUNT];
  50. };
  51.  
  52. class DrawMotionState : public btMotionState {
  53.  
  54.     scene::ISceneNode* sceneNode;
  55. public:
  56.     DrawMotionState(scene::ISceneNode* sceneNodeIn) {
  57.         sceneNode = sceneNodeIn;
  58.     }
  59.  
  60.     virtual void getWorldTransform(btTransform &worldTrans) const
  61.     {      
  62.         worldTrans.setOrigin(btVector3(
  63.             sceneNode->getPosition().X,
  64.             sceneNode->getPosition().Y,
  65.             sceneNode->getPosition().Z
  66.         ));
  67.        
  68.         core::quaternion quat = core::quaternion(
  69.             f32(sceneNode->getRotation().X * (2 * M_PI) / 360.),
  70.             f32(sceneNode->getRotation().Y * (2 * M_PI) / 360.),
  71.             f32(sceneNode->getRotation().Z * (2 * M_PI) / 360.)
  72.         );
  73.         worldTrans.setRotation(btQuaternion(quat.X, quat.Y, quat.Z, quat.W));
  74.     }
  75.  
  76.     virtual void setWorldTransform(const btTransform &worldTrans)
  77.     {
  78.         btVector3 btPosition = worldTrans.getOrigin();
  79.         btQuaternion btRotation = worldTrans.getRotation();    
  80.  
  81.         // std::cout << worldTrans.getOrigin().getX() << std::endl;
  82.  
  83.         sceneNode->setPosition(core::vector3df(
  84.             btPosition.getX(),
  85.             btPosition.getY(),
  86.             btPosition.getZ()
  87.         ));
  88.  
  89.         core::quaternion irrlichtRotationQ = core::quaternion(
  90.             btRotation.getX(), btRotation.getY(),
  91.             btRotation.getZ(), btRotation.getW());
  92.         core::vector3df irrlichtRotationV;
  93.         irrlichtRotationQ.toEuler(irrlichtRotationV);
  94.  
  95.         sceneNode->setRotation(irrlichtRotationV * f32(360. / (2*M_PI)));
  96.     }
  97. };
  98.  
  99. int main()
  100. {
  101.     //m_guiHelper->resetCamera(0, 0, 0, 0, 0, 0);
  102.     //dist, pitch, yaw, targetPos[0], targetPos[1], targetPos[2]);
  103.  
  104.     /* Irrlich stuff. */
  105.     MyEventReceiver receiver;
  106.  
  107.     IrrlichtDevice* device = createDevice(irr::video::EDT_OPENGL,
  108.         core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
  109.  
  110.  
  111.     if (device == 0)
  112.         return 1; // could not create selected driver.
  113.  
  114.     video::IVideoDriver* driver = device->getVideoDriver();
  115.     scene::ISceneManager* smgr = device->getSceneManager();
  116.  
  117.     /*
  118.     Create the node which will be moved with the WSAD keys. We create a
  119.     sphere node, which is a built-in geometry primitive. We place the node
  120.     at (0,0,30) and assign a texture to it to let it look a little bit more
  121.     interesting. Because we have no dynamic lights in this scene we disable
  122.     lighting for each model (otherwise the models would be black).
  123.     */
  124.     scene::ISceneNode* node = smgr->addSphereSceneNode();
  125.     node->setPosition(core::vector3df(0, 1, 0));
  126.  
  127.     /*
  128.     To be able to look at and move around in this scene, we create a first
  129.     person shooter style camera and make the mouse cursor invisible.
  130.     */
  131.     scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
  132.     camera->setPosition(core::vector3df(0, 0, 0));
  133.     device->getCursorControl()->setVisible(false);
  134.  
  135.     if (node)
  136.     {
  137.         node->setPosition(core::vector3df(0, 5, 50));
  138.         node->setMaterialTexture(0, driver->getTexture("water.jpg"));
  139.         node->setMaterialFlag(video::EMF_LIGHTING, false);
  140.     }
  141.  
  142.     /* Done Irrlicht stuff. */
  143.  
  144.     btBroadphaseInterface* broadphase = new btDbvtBroadphase();
  145.     btDefaultCollisionConfiguration* collisionConfiguration
  146.                     = new btDefaultCollisionConfiguration();
  147.     btCollisionDispatcher* dispatcher
  148.                     = new btCollisionDispatcher(collisionConfiguration);
  149.     btSequentialImpulseConstraintSolver* solver
  150.                     = new btSequentialImpulseConstraintSolver();
  151.  
  152.     btDiscreteDynamicsWorld* dynamicsWorld
  153.                     = new btDiscreteDynamicsWorld(
  154.                             dispatcher,
  155.                             broadphase,
  156.                             solver,
  157.                             collisionConfiguration
  158.                     );
  159.    
  160.     dynamicsWorld->setGravity(btVector3(0, -10, 0));
  161.  
  162.     /* Start: Simulation. */
  163.  
  164.     btCollisionShape* fallShape = new btSphereShape(1);
  165.     DrawMotionState fallMotionState = DrawMotionState(node);
  166.  
  167.     btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(
  168.                                         1, &fallMotionState, fallShape);
  169.  
  170.     btRigidBody fallRigidBody(fallRigidBodyCI);
  171.     fallRigidBody.setLinearVelocity(btVector3(-5, 0, 0));
  172.     fallRigidBody.setAngularVelocity(btVector3(0, 5, 0));
  173.  
  174.     btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
  175.     btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, nullptr, groundShape);
  176.  
  177.     btRigidBody groundRigidBody(groundRigidBodyCI);
  178.  
  179.     btCollisionShape* wall1Shape = new btStaticPlaneShape(btVector3(-1, 0, 0), -40);
  180.     btRigidBody::btRigidBodyConstructionInfo wall1RigidBodyCI(0, nullptr, wall1Shape);
  181.  
  182.     btRigidBody wall1RigidBody(wall1RigidBodyCI);
  183.  
  184.     fallRigidBody.setRestitution(btScalar(.8));
  185.     groundRigidBody.setRestitution(btScalar(.8));
  186.     wall1RigidBody.setRestitution(btScalar(.8));
  187.  
  188.     fallRigidBody.setRollingFriction(.7);
  189.     groundRigidBody.setRollingFriction(.7);
  190.     wall1RigidBody.setRollingFriction(.7);
  191.     fallRigidBody.setFriction(.7);
  192.     groundRigidBody.setFriction(.7);
  193.  
  194.     dynamicsWorld->addRigidBody(&fallRigidBody);
  195.     dynamicsWorld->addRigidBody(&groundRigidBody);
  196.     dynamicsWorld->addRigidBody(&wall1RigidBody);
  197.  
  198.     /*
  199.     We have done everything, so lets draw it. We also write the current
  200.     frames per second and the name of the driver to the caption of the
  201.     window.
  202.     */
  203.     int lastFPS = -1;
  204.  
  205.     // In order to do framerate independent movement, we have to know
  206.     // how long it was since the last frame
  207.     u32 then = device->getTimer()->getTime();
  208.  
  209.     // This is the movemen speed in units per second.
  210.     const f32 MOVEMENT_SPEED = 5.f;
  211.  
  212.     const double TIMESTEP = 1 / 60.;
  213.  
  214.     while (device->run())
  215.     {
  216.         // Work out a frame delta time.
  217.         const u32 now = device->getTimer()->getTime();
  218.         const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
  219.         then = now;
  220.  
  221.         dynamicsWorld->stepSimulation(frameDeltaTime);
  222.  
  223.         std::cout << fallRigidBody.getWorldTransform().getOrigin().getX() << std::endl;
  224.        
  225.         driver->beginScene(true, true, video::SColor(255, 113, 113, 133));
  226.  
  227.         smgr->drawAll(); // draw the 3d scene
  228.         device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
  229.  
  230.         driver->endScene();
  231.  
  232.         int fps = driver->getFPS();
  233.  
  234.         if (lastFPS != fps)
  235.         {
  236.             core::stringw tmp(L"Movement Example - Irrlicht Engine [");
  237.             tmp += driver->getName();
  238.             tmp += L"] fps: ";
  239.             tmp += fps;
  240.  
  241.             device->setWindowCaption(tmp.c_str());
  242.             lastFPS = fps;
  243.         }
  244.  
  245.         double remainder = TIMESTEP - frameDeltaTime;
  246.  
  247.         if (remainder > 0) {
  248.             device->sleep(u32(remainder * 1000));
  249.         }
  250.     }      
  251.  
  252.     /* End: Simulation. */
  253.  
  254.     std::cout << "Exiting succesfully...";
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement