Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bullet1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <btBulletDynamicsCommon.h>
- #include <irrlicht.h>
- #define M_PI (3.14159)
- using namespace irr;
- /*
- To receive events like mouse and keyboard input, or GUI events like "the OK
- button has been clicked", we need an object which is derived from the
- irr::IEventReceiver object. There is only one method to override:
- irr::IEventReceiver::OnEvent(). This method will be called by the engine once
- when an event happens. What we really want to know is whether a key is being
- held down, and so we will remember the current state of each key.
- */
- class MyEventReceiver : public IEventReceiver
- {
- public:
- // This is the one method that we have to implement
- virtual bool OnEvent(const SEvent& event)
- {
- // Remember whether each key is down or up
- if (event.EventType == irr::EET_KEY_INPUT_EVENT)
- KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
- return false;
- }
- // This is used to check whether a key is being held down
- virtual bool IsKeyDown(EKEY_CODE keyCode) const
- {
- return KeyIsDown[keyCode];
- }
- MyEventReceiver()
- {
- for (u32 i = 0; i<KEY_KEY_CODES_COUNT; ++i)
- KeyIsDown[i] = false;
- }
- private:
- // We use this array to store the current state of each key
- bool KeyIsDown[KEY_KEY_CODES_COUNT];
- };
- class DrawMotionState : public btMotionState {
- scene::ISceneNode* sceneNode;
- public:
- DrawMotionState(scene::ISceneNode* sceneNodeIn) {
- sceneNode = sceneNodeIn;
- }
- virtual void getWorldTransform(btTransform &worldTrans) const
- {
- worldTrans.setOrigin(btVector3(
- sceneNode->getPosition().X,
- sceneNode->getPosition().Y,
- sceneNode->getPosition().Z
- ));
- core::quaternion quat = core::quaternion(
- f32(sceneNode->getRotation().X * (2 * M_PI) / 360.),
- f32(sceneNode->getRotation().Y * (2 * M_PI) / 360.),
- f32(sceneNode->getRotation().Z * (2 * M_PI) / 360.)
- );
- worldTrans.setRotation(btQuaternion(quat.X, quat.Y, quat.Z, quat.W));
- }
- virtual void setWorldTransform(const btTransform &worldTrans)
- {
- btVector3 btPosition = worldTrans.getOrigin();
- btQuaternion btRotation = worldTrans.getRotation();
- // std::cout << worldTrans.getOrigin().getX() << std::endl;
- sceneNode->setPosition(core::vector3df(
- btPosition.getX(),
- btPosition.getY(),
- btPosition.getZ()
- ));
- core::quaternion irrlichtRotationQ = core::quaternion(
- btRotation.getX(), btRotation.getY(),
- btRotation.getZ(), btRotation.getW());
- core::vector3df irrlichtRotationV;
- irrlichtRotationQ.toEuler(irrlichtRotationV);
- sceneNode->setRotation(irrlichtRotationV * f32(360. / (2*M_PI)));
- }
- };
- int main()
- {
- //m_guiHelper->resetCamera(0, 0, 0, 0, 0, 0);
- //dist, pitch, yaw, targetPos[0], targetPos[1], targetPos[2]);
- /* Irrlich stuff. */
- MyEventReceiver receiver;
- IrrlichtDevice* device = createDevice(irr::video::EDT_OPENGL,
- core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
- if (device == 0)
- return 1; // could not create selected driver.
- video::IVideoDriver* driver = device->getVideoDriver();
- scene::ISceneManager* smgr = device->getSceneManager();
- /*
- Create the node which will be moved with the WSAD keys. We create a
- sphere node, which is a built-in geometry primitive. We place the node
- at (0,0,30) and assign a texture to it to let it look a little bit more
- interesting. Because we have no dynamic lights in this scene we disable
- lighting for each model (otherwise the models would be black).
- */
- scene::ISceneNode* node = smgr->addSphereSceneNode();
- node->setPosition(core::vector3df(0, 1, 0));
- /*
- To be able to look at and move around in this scene, we create a first
- person shooter style camera and make the mouse cursor invisible.
- */
- scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
- camera->setPosition(core::vector3df(0, 0, 0));
- device->getCursorControl()->setVisible(false);
- if (node)
- {
- node->setPosition(core::vector3df(0, 5, 50));
- node->setMaterialTexture(0, driver->getTexture("water.jpg"));
- node->setMaterialFlag(video::EMF_LIGHTING, false);
- }
- /* Done Irrlicht stuff. */
- btBroadphaseInterface* broadphase = new btDbvtBroadphase();
- btDefaultCollisionConfiguration* collisionConfiguration
- = new btDefaultCollisionConfiguration();
- btCollisionDispatcher* dispatcher
- = new btCollisionDispatcher(collisionConfiguration);
- btSequentialImpulseConstraintSolver* solver
- = new btSequentialImpulseConstraintSolver();
- btDiscreteDynamicsWorld* dynamicsWorld
- = new btDiscreteDynamicsWorld(
- dispatcher,
- broadphase,
- solver,
- collisionConfiguration
- );
- dynamicsWorld->setGravity(btVector3(0, -10, 0));
- /* Start: Simulation. */
- btCollisionShape* fallShape = new btSphereShape(1);
- DrawMotionState fallMotionState = DrawMotionState(node);
- btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(
- 1, &fallMotionState, fallShape);
- btRigidBody fallRigidBody(fallRigidBodyCI);
- fallRigidBody.setLinearVelocity(btVector3(-5, 0, 0));
- fallRigidBody.setAngularVelocity(btVector3(0, 5, 0));
- btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
- btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, nullptr, groundShape);
- btRigidBody groundRigidBody(groundRigidBodyCI);
- btCollisionShape* wall1Shape = new btStaticPlaneShape(btVector3(-1, 0, 0), -40);
- btRigidBody::btRigidBodyConstructionInfo wall1RigidBodyCI(0, nullptr, wall1Shape);
- btRigidBody wall1RigidBody(wall1RigidBodyCI);
- fallRigidBody.setRestitution(btScalar(.8));
- groundRigidBody.setRestitution(btScalar(.8));
- wall1RigidBody.setRestitution(btScalar(.8));
- fallRigidBody.setRollingFriction(.7);
- groundRigidBody.setRollingFriction(.7);
- wall1RigidBody.setRollingFriction(.7);
- fallRigidBody.setFriction(.7);
- groundRigidBody.setFriction(.7);
- dynamicsWorld->addRigidBody(&fallRigidBody);
- dynamicsWorld->addRigidBody(&groundRigidBody);
- dynamicsWorld->addRigidBody(&wall1RigidBody);
- /*
- We have done everything, so lets draw it. We also write the current
- frames per second and the name of the driver to the caption of the
- window.
- */
- int lastFPS = -1;
- // In order to do framerate independent movement, we have to know
- // how long it was since the last frame
- u32 then = device->getTimer()->getTime();
- // This is the movemen speed in units per second.
- const f32 MOVEMENT_SPEED = 5.f;
- const double TIMESTEP = 1 / 60.;
- while (device->run())
- {
- // Work out a frame delta time.
- const u32 now = device->getTimer()->getTime();
- const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
- then = now;
- dynamicsWorld->stepSimulation(frameDeltaTime);
- std::cout << fallRigidBody.getWorldTransform().getOrigin().getX() << std::endl;
- driver->beginScene(true, true, video::SColor(255, 113, 113, 133));
- smgr->drawAll(); // draw the 3d scene
- device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
- driver->endScene();
- int fps = driver->getFPS();
- if (lastFPS != fps)
- {
- core::stringw tmp(L"Movement Example - Irrlicht Engine [");
- tmp += driver->getName();
- tmp += L"] fps: ";
- tmp += fps;
- device->setWindowCaption(tmp.c_str());
- lastFPS = fps;
- }
- double remainder = TIMESTEP - frameDeltaTime;
- if (remainder > 0) {
- device->sleep(u32(remainder * 1000));
- }
- }
- /* End: Simulation. */
- std::cout << "Exiting succesfully...";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement