History: BloodyMess Tutorial 5 Source
Source of version: 2 (current)
Copy to clipboard
This is the source for ((BloodyMess Tutorial 5)).
{CODE(wrap="1", colors="c++")}#include "ExampleApplication.h"
#include <NxOgre.h>
#include <NxOgreOGRE3D.h>
class BloodyMessTutorial5Listener : public ExampleFrameListener
{
public:
BloodyMessTutorial5Listener(RenderWindow *win, Camera *cam, OGRE3DRenderSystem* pRenderSystem, OGRE3DKinematicBody* pKB)
: ExampleFrameListener(win, cam)
{
mTimeController = NxOgre::TimeController::getSingleton();
mRenderSystem = pRenderSystem;
mKB = pKB;
mYPos = mKB->getGlobalPosition().y;
mDown = false;
}
bool frameStarted(const FrameEvent& evt)
{
mTimeController->advance(evt.timeSinceLastFrame);
if(mDown)
mYPos -= 2.5f * evt.timeSinceLastFrame;
else
mYPos += 2.5f * evt.timeSinceLastFrame;
// Order the KinematicBody to move to the specified position
mKB->moveGlobalPosition(NxOgre::Vec3(0, mYPos, 0));
if(mYPos > 20)
mDown = true;
else if(mYPos < 0)
mDown = false;
// Space key hit?
if(mKeyboard->isKeyDown(OIS::KC_SPACE) && mTimeUntilNextToggle <= 0)
{
// Create a new Cube and apply some force to it
mCube = mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(mCamera->getPosition().x,
mCamera->getPosition().y, mCamera->getPosition().z), "cube.1m.mesh");
mCube->addForce(NxOgre::Vec3(mCamera->getDirection().x * 10000, mCamera->getDirection().y * 10000,
mCamera->getDirection().z * 10000), NxOgre::Enums::ForceMode_Force, true);
mTimeUntilNextToggle = 0.2;
}
return ExampleFrameListener::frameStarted(evt);
}
protected:
NxOgre::TimeController* mTimeController;
OGRE3DRenderSystem* mRenderSystem;
OGRE3DKinematicBody* mKB;
OGRE3DBody* mCube;
NxOgre::Real mYPos;
bool mDown;
};
class BloodyMessTutorial5 : public ExampleApplication
{
protected:
NxOgre::World* mWorld;
NxOgre::Scene* mScene;
NxOgre::TimeController* mTimeController;
OGRE3DRenderSystem* mRenderSystem;
OGRE3DKinematicBody* mKB;
void createScene()
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5f, 0.5f, 0.5f));
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20, 80, 50);
// Position the camera
mCamera->setPosition(0, 20, 40);
mCamera->lookAt(0, 10, 0);
// Create the world
mWorld = NxOgre::World::createWorld();
// Create scene description
NxOgre::SceneDescription sceneDesc;
sceneDesc.mGravity = NxOgre::Vec3(0, -9.8f, 0);
sceneDesc.mName = "DemoScene";
// Create scene
mScene = mWorld->createScene(sceneDesc);
// Set some physical scene values
mScene->getMaterial(0)->setStaticFriction(0.5);
mScene->getMaterial(0)->setDynamicFriction(0.5);
mScene->getMaterial(0)->setRestitution(0.1);
// Create render system
mRenderSystem = new OGRE3DRenderSystem(mScene);
//Create time controller
mTimeController = NxOgre::TimeController::getSingleton();
// Add objects
mKB = mRenderSystem->createKinematicBody(new NxOgre::Box(12, 1, 12), NxOgre::Vec3(0, 12, 0), "Cube.mesh");
mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(0, 14, 0), "cube.1m.mesh");
}
// Create a new frame listener
void createFrameListener()
{
mFrameListener = new BloodyMessTutorial5Listener(mWindow, mCamera, mRenderSystem, mKB);
mRoot->addFrameListener(mFrameListener);
}
};
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
BloodyMessTutorial5 app;
try {
app.go();
} catch(Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(),
"An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occurred: " << e.getFullDescription();
#endif
}
return 0;
}
#ifdef __cplusplus
}
#endif{CODE}
{INCLUDE(page="NxOgre tpl")}{INCLUDE}