/* ===========================================================================
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=========================================================================== */
#include "SampleApp.h"
using namespace Ogre;
using namespace MOC;
using namespace std;
SampleApp::SampleApp()
{
mShouldQuit = false;
mRenderWin = 0;
mSceneMgr = 0;
mLMouseDown = mMMouseDown = mRMouseDown = false;
mRotateSpeed = 0.5f;
mMoveSpeed = 25;
mDirection = Vector3::ZERO;
mPitch = 0.0f;
mRotation = 0.0f;
mLastTime = mTimer.getMilliseconds();
mTimer1 = 0.0;
mCollision = true;
};
SampleApp::~SampleApp()
{
delete mCollisionTools;
//delete mArtifexLoader;
};
void SampleApp::Startup()
{
mRoot = new Root("plugins.cfg", "ogre.cfg", "ogre.log");
if (!mRoot->restoreConfig())
{
mShouldQuit = !mRoot->showConfigDialog();
if(mShouldQuit) return;
}
mRoot->saveConfig();
mRoot->initialise(true, "MOC Demo - Minimal Ogre Collision 1.0 beta");
parseResources();
mRenderWin = mRoot->getAutoCreatedWindow();
setupEmptyScene();
loadInputSystem();
Ogre::WindowEventUtilities::addWindowEventListener(mRenderWin, this);
// init the info overlay
//mInfoOverlay = new OgreStatOverlay(mRenderWin);
//mInfoOverlay->showDebugOverlay(true);
// init the loader with the path to your zones
//mArtifexLoader = new ArtifexLoader(this,"../media/");
//// load a zone
//mArtifexLoader->loadZone("testzone");
// init the collision handler
mCollisionTools = new CollisionTools(mSceneMgr/*, mArtifexLoader->mTerrainInfo*/);
// set how far we want the camera to be above ground
mCollisionTools->setHeightAdjust(4.5f);
// place the camera node on the ground
mCollisionTools->calculateY(mCamNode);
};
void SampleApp::Update()
{
Root::getSingleton().renderOneFrame();
Ogre::WindowEventUtilities::messagePump();
InputManager::getSingletonPtr()->capture();
// take time since last loop to adapt movement to system speed
const float timeFactor = ((float)(mTimer.getMilliseconds()-mLastTime)/1000);
mTimer1 -= timeFactor;
// commit the camera movement (if moving and not colliding with any entity)
if (mDirection != Vector3::ZERO)
{
// save last position
Vector3 oldPos = mCamNode->getPosition();
// commit move
mCamNode->translate(mCamera->getOrientation() * mDirection * (mMoveSpeed * timeFactor));
// collision on?
if (mCollision) {
// calculate the new Y position: check vs. terrain & all objects flagged with ENTITY_MASK
// multiple masks possible like e.g. ENTITY_MASK|MY_MASK|ETC_MASK
// doGridCheck casts a 2nd ray, gridWidth=2.0f ogre units away from the exact camera position to
// avoid falling through small wholes or gaps in hangbridges for example.
mCollisionTools->calculateY(mCamNode,true,true,2.0f,ENTITY_MASK);
// check if we are colliding with anything with a collision radius of 2.5 ogre units and we
// set the ray origin -1.0 lower towards the ground to get smaller obstacles too
if (mCollisionTools->collidesWithEntity(oldPos, mCamNode->getPosition(), 2.5f, -1.0f, ENTITY_MASK))
{
// undo move
mCamNode->setPosition(oldPos);
}
} else {
// collision off -> get terrain height
Vector3 pos = mCamNode->getPosition();
float y = mTerrainInfo->getHeightAt(pos.x,pos.z);
mCamNode->setPosition(pos.x,y+4.5f,pos.z);
}
}
// commit the camera rotation (if rotating)
if (mRotation != 0.0f�
- 1
- 2
前往页