Beispiel #1
0
bool DemoApp::quickSelect()
{
	// Create RaySceneQuery
	Ogre::Ray mouseRay = mCamera->getCameraToViewportRay(static_cast<float>(mMouse->getMouseState().X.abs)/mMouse->getMouseState().width, static_cast<float>(mMouse->getMouseState().Y.abs)/mMouse->getMouseState().height);

	Ogre::RaySceneQuery * mRaySceneQuery = mSceneMgr->createRayQuery(Ogre::Ray());

	// Set ray
	mRaySceneQuery->setRay(mouseRay);

	mRaySceneQuery->setQueryTypeMask(Ogre::SceneManager::ENTITY_TYPE_MASK);
			
	mRaySceneQuery->setSortByDistance(true);

	// Ray-cast and get first hit
	Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
	Ogre::RaySceneQueryResult::iterator itr = result.begin();

	if((itr != result.end() && itr->movable))
	{
		
		//Get hit location
		Ogre::String name = itr->movable->getName();
		bool found=false;
		if(name=="Box")
		{
			itr++;
			name = itr->movable->getName();
		}

		for(int i=0;i<TANK_LIMIT;i++)
		{
			if(name=="Tank"+std::to_string(i))
			{
				tanks.at(i).selected=true;
				if(tanks.at(i).mSelectionCircle->getParentSceneNode()!=tanks.at(i).tankNode)
					tanks.at(i).tankNode->attachObject(tanks.at(i).mSelectionCircle);
				if(tanks.at(i).mHealthBar->getParentSceneNode()!=tanks.at(i).tankNode)
					tanks.at(i).tankNode->attachObject(tanks.at(i).mHealthBar);
				found=true;	
				tanks.at(i).path2->setVisible(true);
			}
			else if(controlPressed==false)
			{
				tanks.at(i).selected=false;
				if(tanks.at(i).mSelectionCircle->getParentSceneNode()==tanks.at(i).tankNode)
					tanks.at(i).tankNode->detachObject(tanks.at(i).mSelectionCircle);
				if(tanks.at(i).mHealthBar->getParentSceneNode()==tanks.at(i).tankNode)
					tanks.at(i).tankNode->detachObject(tanks.at(i).mHealthBar);
				tanks.at(i).path2->setVisible(false);
			}
		}
		if(found==false)
			return false;
		else return true;
	}
	return false;
}
//-------------------------------------------------------------------------------------
bool RollerCoaster::Querytest()
{
	Ogre::RaySceneQuery *mQuery = mSceneMgr->createRayQuery(Ogre::Ray());
	Ogre::Vector3 direction =trainNode->getPosition()+
		trainNode->_getDerivedOrientation()*Ogre::Vector3::UNIT_Z;
	
	Ogre::Ray objRay( trainNode->getPosition(),direction);

	mQuery->setRay(objRay);

	Ogre::RaySceneQueryResult &result = mQuery->execute();
	Ogre::RaySceneQueryResult::iterator iter = result.begin();

	for (iter; iter!=result.end(); iter++)
	{
		if((*iter).movable->getName()== "mTarget")
		{
			return true;
		}
	}

	mQuery->clearResults();
	return false;
}
Beispiel #3
0
void
System::makeObjectTransparent( const Ogre::Vector3 &start, const Ogre::Vector3 &end, float nNewTrans, float nDelay, IObjectFilter *pFilter )
{
    Ogre::Vector3 vDirction = (start-end);
    Ogre::Real fDistance = vDirction.length();
    Ogre::Ray ray = Ogre::Ray(end,vDirction);
    Ogre::RaySceneQuery* raySceneQuery = getSceneManager()->createRayQuery(Ogre::Ray());
    std::set<Fairy::Object*> setCurrentTransparentObjects;

    raySceneQuery->setRay(ray);
    const Ogre::RaySceneQueryResult& queryResult = raySceneQuery->execute();
    for (Ogre::RaySceneQueryResult::const_iterator it = queryResult.begin(); it != queryResult.end(); ++it)
    {
        Ogre::MovableObject* pMovable = it->movable;
        Ogre::Vector3 vObjectPos = pMovable->getParentNode()->getPosition();
        Ogre::Vector3 vObjectDir = (vObjectPos-end);
        Fairy::ObjectPtr pObject = Fairy::getObjectFromMovable(pMovable);

        if(pObject)
        {
            // 当满足以下条件时设置透明: 1.是StaticEntity类型; 2.相交; 3.允许透明
            if(pObject->getType() == "StaticEntity" && vObjectDir.dotProduct(vDirction) > 0 && static_cast<Fairy::StaticEntityObject*>(pObject.get())->getEnableTransparency())
            {
                Fairy::StaticEntityObject* pModel = static_cast<Fairy::StaticEntityObject*>(pObject.get());
                pModel->setTransparency(0.5f);
                // 删除原有的Object,放到临时的Set中
                m_setTransparencyObjects.erase(pObject.get());
                setCurrentTransparentObjects.insert(pObject.get());
            }
        }
    }

    std::set<Fairy::Object*>::iterator iter = m_setTransparencyObjects.begin();
    std::set<Fairy::Object*>::iterator tempIter;

    while (iter != m_setTransparencyObjects.end())
    {
        tempIter = iter;
        ++iter;
        // 恢复没有遮挡的Object的透明度为0
        Fairy::StaticEntityObject* pModel = static_cast<Fairy::StaticEntityObject*>(*tempIter);
        pModel->setTransparency(0);
        // 删除透明度为0的Object
        if (pModel->getTransparency()==0)
        {
            m_setTransparencyObjects.erase(tempIter);
        }
    }

    // 将遮挡的Object放到m_setTransparencyObjects
    for (std::set<Fairy::Object*>::iterator it = setCurrentTransparentObjects.begin(); it != setCurrentTransparentObjects.end(); ++it)
    {
        m_setTransparencyObjects.insert(*it);
    }

    if(raySceneQuery)
    {
        getSceneManager()->destroyQuery(raySceneQuery);
        raySceneQuery = NULL;
    }
}
Beispiel #4
0
void DemoApp::generatePath()
{
	for(int j = 0; j < TOTAL_NODES; j++)
	{
		if(pathFindingGraph->getContent(j) == 2)
			pathFindingGraph->setContent(j, 0);
	}

	for(int i = 0; i < TANK_LIMIT; i++)
	{
		//if path already exists
		if(tanks.at(i).mCurrentState > 1)
		{
			// reset
			tanks.at(i).mCurrentState = 0;
			tanks.at(i).path2->clear();
		}
			if(playerControlled == i) //player-controlled tank movement
			{
				// Create RaySceneQuery
				Ogre::Ray mouseRay = mCamera->getCameraToViewportRay(static_cast<float>(mMouse->getMouseState().X.abs)/mMouse->getMouseState().width, static_cast<float>(mMouse->getMouseState().Y.abs)/mMouse->getMouseState().height);

				Ogre::RaySceneQuery *mRaySceneQuery = mSceneMgr->createRayQuery(Ogre::Ray());

				//set ray
				mRaySceneQuery->setRay(mouseRay);

				// Ray-cast and get first hit
				Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
				Ogre::RaySceneQueryResult::iterator itr = result.begin();

				// if hit an object
				if((itr != result.end()))
				{
					//Get hit location
					Ogre::Vector3 location = mouseRay.getPoint(itr->distance);

					// if hit the floor
					if((location.y < 0.001 && (selectionMode==1 || selectionMode==2)) || selectionMode==0)
					{
						tanks.at(i).startNode = pathFindingGraph->getNode(tanks.at(i).tankNode->_getDerivedPosition());
						// set goal node
						if(selectionMode==0)
						{
							do
							{
								tanks.at(i).goalNode = (int)rand() % 256;
							}while(pathFindingGraph->getContent(tanks.at(i).goalNode) != 0);
						}
						else
						{
							tanks.at(i).goalNode=pathFindingGraph->getNode(location);
						}
						// check that goal node is not the same as start node
						if(tanks.at(i).goalNode != tanks.at(i).startNode)
						{
							findPath(i);
						}
						else
						{
							resetPath(i);
						}
					}
				}
			}
			else //AI-controlled tank movement
			{
				/*Ogre::Vector3 location; //pre-define location for the if else

				if(tanks.at(i).team == 1)
					location = pathFindingGraph->getPosition((13 + rand() % 16) + rand() % (40 + 1)); //get a random position to move to
				else if(tanks.at(i).team == 2)
					location = pathFindingGraph->getPosition((13 + rand() % 16) * ((rand() % 40 + 1) * 42)); //get a random position to move to
				*/
				int location = ((13 + rand() % 16) + ((rand() % 40 + 1) * 42));
				tanks.at(i).startNode = pathFindingGraph->getNode(tanks.at(i).tankNode->_getDerivedPosition());
				/*
				// set goal node
				if(selectionMode==0)
				{*/
				/*do
				{
					tanks.at(i).goalNode = (int)rand() % 256;
				}while(pathFindingGraph->getContent(tanks.at(i).goalNode) != 0);*/
				/*}
				else
				{
					tanks.at(i).goalNode = pathFindingGraph->getNode(location);
				}*/
				tanks.at(i).goalNode = location;
				//tanks.at(i).goalNode = pathFindingGraph->getNode(location);
				// check that goal node is not the same as start node
				if(tanks.at(i).goalNode != tanks.at(i).startNode)
				{
					findPath(i);
				}
				else
				{
					resetPath(i);
				}
				
			}

			pathFindingGraph->setContent(pathFindingGraph->getNode(tanks.at(i).tankNode->_getDerivedPosition()), 2);
	}
}