SHARE
TWEET

Untitled

a guest Jun 9th, 2013 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Player.h"
  2. #include "TextRenderer.h"
  3. #include <OgreMath.h>
  4. #include <OgreFontManager.h>
  5. #include <OgreManualObject.h>
  6. #include <OgreSimpleRenderable.h>
  7.  
  8. Player::Player(SceneManager* mSceneMgr, RenderWindow* window, OIS::Keyboard* keyboard, OIS::JoyStick* joystick)
  9. {
  10.         this->mSceneMgr = mSceneMgr;
  11.         mWorld = new OgreBulletDynamics::DynamicsWorld(
  12.                 mSceneMgr,
  13.                 AxisAlignedBox(Vector3(-1000, -1000, -1000), Vector3(1000, 1000, 1000)),
  14.                 Vector3(0, -9.81, 0),
  15.                 true,
  16.                 true,
  17.                 100000
  18.         );
  19.         mDebugDrawer = new OgreBulletCollisions::DebugDrawer();
  20.         mDebugDrawer->setDrawWireframe(true);
  21.         mWorld->setDebugDrawer(mDebugDrawer);
  22.         mWorld->setShowDebugShapes(true);
  23.         SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("debugDrawer", Ogre::Vector3::ZERO);
  24.         node->attachObject(static_cast <SimpleRenderable *> (mDebugDrawer));
  25.         OgreBulletCollisions::BoxCollisionShape shape();
  26.         TextRenderer::getSingleton().addTextBox("nigger", "nigger", 10, 10, 100, 20, Ogre::ColourValue::White);
  27.         Ogre::FontManager::getSingleton().getByName("StarWars")->load();
  28.         // Create the camera
  29.         mCamera = mSceneMgr->createCamera("PlayerCam");
  30.  
  31.         mCameraPivot = (SceneNode*)mSceneMgr->getRootSceneNode()->createChildSceneNode();
  32.         mCameraNodeParentYaw = mCameraPivot->createChildSceneNode();
  33.         mCameraNodeParentPitch = mCameraNodeParentYaw->createChildSceneNode();
  34.         mCameraNode = mCameraNodeParentPitch->createChildSceneNode();
  35.         mSceneHead = mCameraPivot->createChildSceneNode();
  36.         mSceneHead->scale(0.1, 0.1, 0.1);
  37.  
  38.         // Position it at 80 in Z direction
  39.         mCameraPivot->setPosition(Ogre::Vector3(0,0,0));
  40.         mCameraNode->setPosition(Ogre::Vector3(0,20,100));
  41.         // Look back along -Z
  42.         mCamera->lookAt(Vector3::ZERO);
  43.         mCamera->setNearClipDistance(5);
  44.  
  45.         mCameraNode->attachObject(mCamera);
  46.         Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
  47.         Ogre::Entity* ogreHead2 = mSceneMgr->createEntity("Head2", "ogrehead.mesh");
  48.  
  49.         Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  50.         headNode->scale(0.4, 0.4, 0.4);
  51.         headNode->attachObject(ogreHead);
  52.         mSceneHead->attachObject(ogreHead2);
  53.         Vector3 size = ogreHead2->getBoundingBox().getSize() * 0.4;
  54.  
  55.         OgreBulletCollisions::BoxCollisionShape *sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(size);
  56.         // and the Bullet rigid body
  57.         mDefaultBody = new OgreBulletDynamics::RigidBody(
  58.            "defaultBoxRigfghfghid",
  59.            mWorld);
  60.         mDefaultBody->setShape(mCameraPivot,
  61.                 sceneBoxShape,
  62.                 0,         // dynamic body restitution
  63.                 1,         // dynamic body friction
  64.                 1.0f,          // dynamic bodymass
  65.                 mSceneHead->getPosition(),      // starting position of the box
  66.                 mCameraPivot->getOrientation());// orientation of the box
  67.         mNumEntitiesInstanced++;
  68.         mDefaultBody->setLinearVelocity(
  69.                   Vector3(10, 0, 10) ); // shooting
  70.  
  71.         Plane plane;
  72.         plane.normal = Ogre::Vector3::UNIT_Y;
  73.         plane.d = 0;
  74.         MeshManager::getSingleton().createPlane("floor", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 450.0f, 450.0f, 10, 10, true, 1, 50.0f, 50.0f, Ogre::Vector3::UNIT_Z);
  75.         Ogre::Entity* planeEnt = mSceneMgr->createEntity("plane", "floor");
  76.         planeEnt->setMaterialName("Examples/BumpyMetal");
  77.         planeEnt->setCastShadows(false);
  78.         SceneNode* grassNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  79.         grassNode->attachObject(planeEnt);
  80.         grassNode->translate(0, -5, 0);
  81.  
  82.         // Create one viewport, entire window
  83.         Ogre::Viewport* vp = window->addViewport(mCamera);
  84.         vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
  85.  
  86.         // Alter the camera aspect ratio to match the viewport
  87.         mCamera->setAspectRatio(
  88.             Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
  89.  
  90.         mKeyboard = keyboard;
  91.         mJoystick = joystick;
  92.  
  93.         ManualObject* cube = new ManualObject("nigger");
  94.         cube->begin("Examples/GrassFloor");
  95.  
  96.         cube->position(0.5,-0.5,1.0);cube->normal(0.408248,-0.816497,0.408248);cube->textureCoord(1,0);
  97.         cube->position(-0.5,-0.5,0.0);cube->normal(-0.408248,-0.816497,-0.408248);cube->textureCoord(0,1);
  98.         cube->position(0.5,-0.5,0.0);cube->normal(0.666667,-0.333333,-0.666667);cube->textureCoord(1,1);
  99.         cube->position(-0.5,-0.5,1.0);cube->normal(-0.666667,-0.333333,0.666667);cube->textureCoord(0,0);
  100.         cube->position(0.5,0.5,1.0);cube->normal(0.666667,0.333333,0.666667);cube->textureCoord(1,0);
  101.         cube->position(-0.5,-0.5,1.0);cube->normal(-0.666667,-0.333333,0.666667);cube->textureCoord(0,1);
  102.         cube->position(0.5,-0.5,1.0);cube->normal(0.408248,-0.816497,0.408248);cube->textureCoord(1,1);
  103.         cube->position(-0.5,0.5,1.0);cube->normal(-0.408248,0.816497,0.408248);cube->textureCoord(0,0);
  104.         cube->position(-0.5,0.5,0.0);cube->normal(-0.666667,0.333333,-0.666667);cube->textureCoord(0,1);
  105.         cube->position(-0.5,-0.5,0.0);cube->normal(-0.408248,-0.816497,-0.408248);cube->textureCoord(1,1);
  106.         cube->position(-0.5,-0.5,1.0);cube->normal(-0.666667,-0.333333,0.666667);cube->textureCoord(1,0);
  107.         cube->position(0.5,-0.5,0.0);cube->normal(0.666667,-0.333333,-0.666667);cube->textureCoord(0,1);
  108.         cube->position(0.5,0.5,0.0);cube->normal(0.408248,0.816497,-0.408248);cube->textureCoord(1,1);
  109.         cube->position(0.5,-0.5,1.0);cube->normal(0.408248,-0.816497,0.408248);cube->textureCoord(0,0);
  110.         cube->position(0.5,-0.5,0.0);cube->normal(0.666667,-0.333333,-0.666667);cube->textureCoord(1,0);
  111.         cube->position(-0.5,-0.5,0.0);cube->normal(-0.408248,-0.816497,-0.408248);cube->textureCoord(0,0);
  112.         cube->position(-0.5,0.5,1.0);cube->normal(-0.408248,0.816497,0.408248);cube->textureCoord(1,0);
  113.         cube->position(0.5,0.5,0.0);cube->normal(0.408248,0.816497,-0.408248);cube->textureCoord(0,1);
  114.         cube->position(-0.5,0.5,0.0);cube->normal(-0.666667,0.333333,-0.666667);cube->textureCoord(1,1);
  115.         cube->position(0.5,0.5,1.0);cube->normal(0.666667,0.333333,0.666667);cube->textureCoord(0,0);
  116.  
  117.         cube->triangle(0,1,2);      cube->triangle(3,1,0);
  118.         cube->triangle(4,5,6);      cube->triangle(4,7,5);
  119.         cube->triangle(8,9,10);      cube->triangle(10,7,8);
  120.         cube->triangle(4,11,12);   cube->triangle(4,13,11);
  121.         cube->triangle(14,8,12);   cube->triangle(14,15,8);
  122.         cube->triangle(16,17,18);   cube->triangle(16,19,17);
  123.         cube->end();
  124.         SceneNode* mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  125.         mNode->setPosition(0,0.5,0);
  126.         mNode->scale(40, 30, 40);
  127.         mNode->attachObject(cube);
  128.  
  129.         mRaySceneQuery = mSceneMgr->createRayQuery(Ray());
  130.  
  131.         OgreBulletCollisions::CollisionShape *Shape;
  132.         Shape = new OgreBulletCollisions::StaticPlaneCollisionShape(Ogre::Vector3(0,1,0), 0); // (normal vector, distance)
  133.         OgreBulletDynamics::RigidBody *defaultPlaneBody = new OgreBulletDynamics::RigidBody(
  134.                 "BasePlane",
  135.                 mWorld);
  136.         defaultPlaneBody->setStaticShape(Shape, 0, 1); // (shape, restitution, friction)
  137.         // push the created objects to the deques
  138.         mShapes.push_back(Shape);
  139.         mBodies.push_back(defaultPlaneBody);
  140.  
  141. }
  142. bool Player::isCollisioning()
  143. {
  144.         Ogre::Ray ray(mSceneHead->getPosition(), Vector3(1));
  145.         mRaySceneQuery->setRay(ray);
  146.         Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
  147.         Ogre::RaySceneQueryResult::iterator itr;
  148.         for (itr = result.begin(); itr != result.end(); itr++) {
  149.                 if (itr->movable->getName().compare(mCameraPivot->getName())!=0 && itr->distance<30/2) {
  150.                         return true;
  151.                 }
  152.         }
  153.         return false;
  154. }
  155. bool Player::isOnGround()
  156. {
  157.         return true;
  158.         /*Ogre::Ray ray(mCameraPivot->getPosition(), Vector3::NEGATIVE_UNIT_Y);
  159.         mRaySceneQuery->setRay(ray);
  160.         Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
  161.         Ogre::RaySceneQueryResult::iterator itr;
  162.         for (itr = result.begin(); itr != result.end(); itr++) {
  163.                 if (itr->movable->getName().compare("Head2")!=0 && itr->distance <= 0) {
  164.                         return true;
  165.                 }
  166.         }
  167.         return false;*/
  168. }
  169. bool Player::isJumping()
  170. {
  171.         return false;
  172. }
  173. void Player::updateWorld(const FrameEvent& evt)
  174. {
  175.         mWorld->stepSimulation(evt.timeSinceLastFrame);
  176. }
  177. bool Player::update(const FrameEvent& evt)
  178. {
  179.         //Need to capture/update each device
  180.         mKeyboard->capture();
  181.   //  mMouse->capture();
  182.  
  183.         if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
  184.         {
  185.                 return false;
  186.         }
  187.  
  188.         Real speed;
  189.         speed = PLAYER_MOVEMENT_SPEED;
  190.  
  191.         if(mKeyboard->isKeyDown(OIS::KC_SPACE) && mDefaultBody->getLinearVelocity().y == 0)
  192.         {
  193.                 mDefaultBody->setLinearVelocity(
  194.                         Vector3(
  195.                                 -50 * Math::Sin(Radian(mCameraNodeParentYaw->getOrientation().getYaw())),
  196.                                 50,
  197.                                 -50 * Math::Cos(Radian(mCameraNodeParentYaw->getOrientation().getYaw()))));
  198.                 mDefaultBody->enableActiveState();
  199.                 if (mKeyboard->isKeyDown(OIS::KC_E))
  200.                 {
  201.                         /*mCurrentJump = new JumpKinetic(
  202.                                         Vector3(
  203.                                                         -50 * Math::Sin(Radian(mCameraNodeParentYaw->getOrientation().getYaw())),
  204.                                                         50,
  205.                                                         -50 * Math::Cos(Radian(mCameraNodeParentYaw->getOrientation().getYaw()))));*/
  206.                 }
  207.                 else
  208.                 {
  209.                         /*mCurrentJump = new JumpKinetic(Vector3(0, 50, 0));*/
  210.                 }
  211.         }
  212.  
  213.         if(mKeyboard->isKeyDown(OIS::KC_B) && mTimeUntilNextToggle <=0)
  214.         {
  215.                 Vector3 size = Vector3::ZERO;   // size of the box
  216.                 // starting position of the box
  217.                 Vector3 position = (mCamera->getDerivedPosition() + mCamera->getDerivedDirection().normalisedCopy() * 10);
  218.                 // create an ordinary, Ogre mesh with texture
  219.                 Entity *entity = mSceneMgr->createEntity(
  220.                    "Box" + StringConverter::toString(mNumEntitiesInstanced),
  221.                    "ogrehead.mesh");
  222.                 entity->setCastShadows(true);
  223.                 // we need the bounding box of the box to be able to set the size of the Bullet-box
  224.                 AxisAlignedBox boundingB = entity->getBoundingBox();
  225.                 size = boundingB.getSize(); size /= 2.0f; // only the half needed
  226.                 size *= 0.96f;   // Bullet margin is a bit bigger so we need a smaller size
  227.                                            // (Bullet 2.76 Physics SDK Manual page 18)
  228.                 SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  229.                 node->attachObject(entity);
  230.                 node->scale(0.4f, 0.4f, 0.4f);   // the cube is too big for us
  231.                 size *= 0.4f;                  // don't forget to scale down the Bullet-box too
  232.                 // after that create the Bullet shape with the calculated size
  233.                 OgreBulletCollisions::BoxCollisionShape *sceneBoxShape = new OgreBulletCollisions::BoxCollisionShape(size);
  234.                 // and the Bullet rigid body
  235.                 OgreBulletDynamics::RigidBody* defaultBody = new OgreBulletDynamics::RigidBody(
  236.                    "defaultBoxRigid" + StringConverter::toString(mNumEntitiesInstanced),
  237.                    mWorld);
  238.                 defaultBody->setShape(   node,
  239.                                            sceneBoxShape,
  240.                                            0,         // dynamic body restitution
  241.                                            1.0f,         // dynamic body friction
  242.                                            1.0f,          // dynamic bodymass
  243.                                            position,      // starting position of the box
  244.                                            Quaternion(0,0,0,1));// orientation of the box
  245.                 mNumEntitiesInstanced++;
  246.                 defaultBody->setLinearVelocity(
  247.                           Vector3(10, 0, 10) ); // shooting speed
  248.                    // push the created objects to the deques
  249.                 mShapes.push_back(sceneBoxShape);
  250.                 mBodies.push_back(defaultBody);
  251.                 mTimeUntilNextToggle = 0.5;
  252.         }
  253.         mTimeUntilNextToggle -= evt.timeSinceLastFrame; // Pacha 4468-1268-6236
  254.  
  255.         if (mKeyboard->isKeyDown(OIS::KC_A))
  256.         {
  257.                 mCameraNodeParentYaw->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(0,1,0)), Node::TS_LOCAL);
  258.         }
  259.         if (mKeyboard->isKeyDown(OIS::KC_D))
  260.         {
  261.                 mCameraNodeParentYaw->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(0,-1,0)), Node::TS_LOCAL);
  262.         }
  263.         if (mKeyboard->isKeyDown(OIS::KC_W))
  264.         {
  265.                 mCameraNodeParentPitch->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(1,0,0)), Node::TS_LOCAL);
  266.         }
  267.         if (mKeyboard->isKeyDown(OIS::KC_S))
  268.         {
  269.                 mCameraNodeParentPitch->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(-1,0,0)), Node::TS_LOCAL);
  270.         }
  271.  
  272.         Quaternion quat = mCameraNodeParentPitch->getOrientation();
  273.         if (quat.getPitch() < CAMERA_PITCH_MAX)
  274.         {
  275.                 mCameraNodeParentPitch->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(1,0,0)), Node::TS_LOCAL);
  276.         }
  277.         else if(quat.getPitch() > CAMERA_PITCH_MIN)
  278.         {
  279.                 mCameraNodeParentPitch->rotate(Quaternion(Degree(100*evt.timeSinceLastFrame), Vector3(-1,0,0)), Node::TS_LOCAL);
  280.         }
  281.  
  282.         //mCamera->lookAt(mCameraPivot->_getDerivedPosition());
  283.  
  284.         Vector3 vector = Vector3::ZERO;
  285.         if (mKeyboard->isKeyDown(OIS::KC_UP))
  286.         {
  287.                 vector.z -= speed*evt.timeSinceLastFrame;
  288.         }
  289.         if (mKeyboard->isKeyDown(OIS::KC_DOWN))
  290.         {
  291.                 vector.z += speed*evt.timeSinceLastFrame;
  292.         }
  293.         if (mKeyboard->isKeyDown(OIS::KC_RIGHT))
  294.         {
  295.                 vector.x += speed*evt.timeSinceLastFrame;
  296.         }
  297.         if (mKeyboard->isKeyDown(OIS::KC_LEFT))
  298.         {
  299.                 vector.x -= speed*evt.timeSinceLastFrame;
  300.         }
  301.  
  302.         mCameraPivot->translate(mCameraNodeParentYaw->getOrientation() * vector, Node::TS_LOCAL);
  303.  
  304.         TextRenderer::getSingleton().printf("nigger", "Y: %f, y: %f", quat.getPitch().valueAngleUnits(), mDefaultBody->getLinearVelocity().y);
  305.   //  mCamera->lookAt(0, 0, 0);
  306.         if (!isOnGround())
  307.         {
  308.                 mCameraPivot->translate(0, -0.01, 0, Node::TS_LOCAL);
  309.         }
  310.  
  311.         return true;
  312. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top