bool PlanesDataUser::CheckCollision(PVRTVec3 nodeWorldPos, float walkDistance) { JoyStick * pJoyStick = JoyStick::GetJoyStick(); PVRTVec2 vecLeft = pJoyStick->GetVectorLeft(); PVRTVec2 vecRight = pJoyStick->GetVectorRight(); vecLeft.normalize(); vecLeft.y*=-1.0f; PVRTVec2 nodeNewPos(nodeWorldPos.x, nodeWorldPos.z); PVRTVec2 globalTan(0.0f,0.0f); int collisionCount = 0; for (int p=0;p<mNumPlanesData;p++) { PlanesData * pPlanesData = mPlanesData[p]; int numOfPlanes = pPlanesData->GetNumOfPlanes(); for (int n=0;n<numOfPlanes;n++) { Plane * plane = pPlanesData->GetPlane(n); Line2D line; line.p0.x = plane->x1; line.p0.y = plane->z1; line.p1.x = plane->x2; line.p1.y = plane->z2; float dist = Collision::Vec2DistanceFromLine2D(line, nodeNewPos); if (dist < MM(walkDistance)) { PVRTVec2 wall(plane->nx, plane->nz); globalTan+=wall.normalized(); collisionCount++; } } } if (collisionCount > 0) { globalTan/=(float)collisionCount; float deltaAngle = acos(vecLeft.dot(globalTan)); //static char txt[255]={0}; //sprintf(txt,"%f \n",deltaAngle); //OutputDebugStr(txt); if ( deltaAngle >= PVRT_PI*0.5f ) { return true; } } return false; }
JoyStick* JoyStick::createWithFile(const std::string &fileName, float radius) { JoyStick *pRet = new JoyStick(); if (pRet && pRet->initWithFile(fileName, radius)) { pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; return NULL; } }
JoystickManager(InputManager* pInputMgr, EventHandler* pEventHdlr) : _pInputMgr(pInputMgr), _nCurrJoyInd(-1), _dMasterGain(0.5), _bAutoCenter(true) { _bFFFound = false; for( int nJoyInd = 0; nJoyInd < pInputMgr->getNumberOfDevices(OISJoyStick); ++nJoyInd ) { //Create the stick JoyStick* pJoy = (JoyStick*)pInputMgr->createInputObject( OISJoyStick, true ); cout << endl << "Created buffered joystick #" << nJoyInd << " '" << pJoy->vendor() << "' (Id=" << pJoy->getID() << ")"; // Check for FF, and if so, keep the joy and dump FF info ForceFeedback* pFFDev = (ForceFeedback*)pJoy->queryInterface(Interface::ForceFeedback ); if( pFFDev ) { _bFFFound = true; // Keep the joy to play with it. pJoy->setEventCallback(pEventHdlr); _vecJoys.push_back(pJoy); // Keep also the associated FF device _vecFFDev.push_back(pFFDev); // Dump FF supported effects and other info. cout << endl << " * Number of force feedback axes : " << pFFDev->getFFAxesNumber() << endl; const ForceFeedback::SupportedEffectList &lstFFEffects = pFFDev->getSupportedEffects(); if (lstFFEffects.size() > 0) { cout << " * Supported effects :"; ForceFeedback::SupportedEffectList::const_iterator itFFEff; for(itFFEff = lstFFEffects.begin(); itFFEff != lstFFEffects.end(); ++itFFEff) cout << " " << Effect::getEffectTypeName(itFFEff->second); cout << endl << endl; } else cout << "Warning: no supported effect found !" << endl; } else { cout << " (no force feedback support detected) => ignored." << endl << endl; _pInputMgr->destroyInputObject(pJoy); } } }
// Constructor MyFrameListener(RenderWindow* win, Camera* cam, CylindricalEffect *cylEffect, SceneNode* ogreNode) { _cam = cam; _movementspeed = 10.0f; _camAngle = -1*Ogre::Math::HALF_PI; _ogreNode = ogreNode; _walkMagnitude = 0; _turnMagnitude = 0; _orientation = 0.0f; ParamList parameters; unsigned int windowHandle = 0; std::ostringstream windowHandleString; win->getCustomAttribute("WINDOW", &windowHandle); windowHandleString << windowHandle; parameters.insert(std::make_pair("WINDOW", windowHandleString.str())); _inputManager = InputManager::createInputSystem(parameters); // cyl effect initialize _cylEffect = cylEffect; _keyboard = static_cast<Keyboard*>(_inputManager->createInputObject(OISKeyboard, false)); _mouse = static_cast<OIS::Mouse*>(_inputManager->createInputObject(OIS::OISMouse, false)); // Try to create joystick try { _joyStick = static_cast<OIS::JoyStick*>(_inputManager->createInputObject( OIS::OISJoyStick, true )); _joyStick->setEventCallback(this); std::cout << "Successfuly created Joystick"; } catch(...) { std::cout << "Failed to initialize Joystick"; _joyStick = 0; } }
bool DoorsDataUser::CheckCollision(PVRTVec3 nodeWorldPos, float walkDistance) { JoyStick * pJoyStick = JoyStick::GetJoyStick(); PVRTVec2 vecLeft = pJoyStick->GetVectorLeft(); PVRTVec2 vecRight = pJoyStick->GetVectorRight(); vecLeft.normalize(); vecLeft.y*=-1.0f; PVRTVec2 nodeNewPos(nodeWorldPos.x, nodeWorldPos.z); PVRTVec2 globalTan(0.0f,0.0f); int collisionCount = 0; for (int p=0;p<mNumDoorsData;p++) { DoorsData * pDoorsData = mDoorsData[p]; int numOfDoors = pDoorsData->GetNumOfDoors(); for (int d=0;d<numOfDoors;d++) { Door * pDoor = pDoorsData->GetDoor(d); if (pDoor->GetState() == Door::door_state_open) continue; int numOfDoorEdges = pDoor->GetNumOfDoorEdges(); for (int e = 0 ; e< numOfDoorEdges ; e++) { DoorEdge * edge = pDoor->GetDoorEdge(e); Line2D line; line.p0.x = edge->x1; line.p0.y = edge->z1; line.p1.x = edge->x2; line.p1.y = edge->z2; float dist = Collision::Vec2DistanceFromLine2D(line, nodeNewPos); if (dist < MM(walkDistance)) { PVRTVec2 wall(edge->nx, edge->nz); globalTan+=wall.normalized(); collisionCount++; } } } } if (collisionCount > 0) { globalTan/=(float)collisionCount; float deltaAngle = acos(vecLeft.dot(globalTan)); if ( deltaAngle >= PVRT_PI*0.5f ) { return true; } } return false; }
bool frameStarted(const FrameEvent& evt) { Ogre::Vector3 translate(0,0,0); _keyboard->capture(); _mouse -> capture(); if( _joyStick ) _joyStick->capture(); bool walked = false; Ogre::Vector3 SinbadTranslate(0, 0, 0); // If the joystick is not available, use the keyboard as input. if (!_joyStick) { if (_keyboard->isKeyDown(OIS::KC_UP)) { _walkMagnitude = 1; } else if (_keyboard->isKeyDown(OIS::KC_DOWN)) { _walkMagnitude = -1; } else { _walkMagnitude = 0; } if (_keyboard->isKeyDown(OIS::KC_LEFT)) { _turnMagnitude = 1; } else if (_keyboard->isKeyDown(OIS::KC_RIGHT)) { _turnMagnitude = -1; } else { _turnMagnitude = 0; } } // Create the translation vector. SinbadTranslate = _ogreNode->getOrientation().zAxis() * evt.timeSinceLastFrame * _movementspeed * _walkMagnitude; walked = true; // Increment the roation angle. _orientation += evt.timeSinceLastFrame * _turnMagnitude*2; // Now finally apply the rotation and translation. _ogreNode->translate(SinbadTranslate); _ogreNode->setOrientation(Ogre::Quaternion(Ogre::Radian(_orientation), Ogre::Vector3::UNIT_Y)); if (_keyboard->isKeyDown(KC_ESCAPE)) { return false; } if (_keyboard->isKeyDown(KC_W)) translate += Ogre::Vector3(0,0,-1); if (_keyboard->isKeyDown(KC_S)) translate += Ogre::Vector3(0,0,1); if (_keyboard->isKeyDown(KC_A)) translate += Ogre::Vector3(-1,0,0); if (_keyboard->isKeyDown(KC_D)) translate += Ogre::Vector3(1,0,0); _cam->moveRelative(translate*evt.timeSinceLastFrame * _movementspeed); float rotX = _mouse->getMouseState().X.rel * evt.timeSinceLastFrame * -1; float rotY = _mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1; _cam->yaw(Radian(rotX)); _cam->pitch(Radian(rotY)); _cylEffect->update(0.1f); _cam->setPosition(_ogreNode->getPosition()+Ogre::Vector3(20.0f*Ogre::Math::Cos(_camAngle), 10.0f, 20.0f*Ogre::Math::Sin(_camAngle))); _cam->lookAt(_ogreNode->getPosition()); return true; }