Code Snippet - Bloodymess tutorial 2 on Buggy Swires Branch

WhiteZero

28-03-2013 11:56:40

Hello,

Just going to share this snippet of code, all I did was take the Bloodymess tutorial 2 and implement it on the latest branch with the help of the Buggyswires tutorial download and a few searches on the forum for some old threads. Hope this is helpful:

This code was built for:
Ogre 1.7.4 SDK: link
Win32 Project, and Visual Studio 2010: link
Nxogre-buggyswires: link
Critter-buggyswires: link

**Note this is a very quick and unpolished merge of two tutorials. If new comers find this useful then I'll clean it up when I have the time. It's all for just a single file, so no need to worry about splitting this between different header and cpp files.




#include "ExampleApplication.h"

#include "NxOgre.h"
#include "Critter.h"

class BloodyMessTutorial2Listener : public ExampleFrameListener
{
public:
BloodyMessTutorial2Listener(RenderWindow *win, Camera *cam)
: ExampleFrameListener(win, cam)
{
mWorld = NxOgre::World::getSingleton();
}

bool frameStarted(const FrameEvent& evt)
{

mWorld->advance(evt.timeSinceLastFrame);

// Don't let the camera go underground.
if (mCamera->getPosition().y < 0.5f)
{
Ogre::Vector3 pos = mCamera->getPosition();
pos.y = 0.5f;
mCamera->setPosition(pos);
}
return ExampleFrameListener::frameStarted(evt);
}

protected:
NxOgre::World* mWorld;
};
class BloodyMessTutorial2 : public ExampleApplication
{

public:
NxOgre::World* mWorld;
NxOgre::Scene* mScene;


Critter::RenderSystem* mRenderSystem;
Critter::Body* mBody;
/*
float mLastTimeStep;
NxOgre::Material* mDefaultMaterial;
*/
Critter::Body* mCube;
Critter::Body* mCube2;

protected:

void createScene()
{
//Create world
mWorld = NxOgre::World::createWorld();

NxOgre::SceneDescription sceneDesc;
sceneDesc.mGravity = NxOgre::Vec3(0, -9.8f, 0);
sceneDesc.mName = "BloodyMessTutorial2";

mScene = mWorld->createScene(sceneDesc);

mScene->getMaterial(0)->setStaticFriction(0.5);
mScene->getMaterial(0)->setDynamicFriction(0.5);
mScene->getMaterial(0)->setRestitution(0.1);


// Create the rendersystem.
mRenderSystem = new Critter::RenderSystem(mScene, mSceneMgr);

//////////////

//mCube = mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(0, 20, 0), "cube.1m.mesh");

// Plane creation
mScene->createSceneGeometry(NxOgre::PlaneGeometryDescription());

// Create floor plane (Ogre)
MovablePlane *plane = new MovablePlane("Plane");
plane->d = 0;
plane->normal = Vector3::UNIT_Y;
Ogre::MeshManager::getSingleton().createPlane("PlaneMesh",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*plane, 120, 120, 1, 1, true, 1, 3, 3, Vector3::UNIT_Z);
Entity *planeEnt = mSceneMgr->createEntity("PlaneEntity", "PlaneMesh");
planeEnt->setMaterialName("Examples/GrassFloor");

Ogre::SceneNode* mPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mPlaneNode->attachObject(planeEnt);

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, 80);
mCamera->lookAt(0, 20, 0);


//Cube 1
mCube = mRenderSystem->createBody(NxOgre::BoxDescription(1,1,1), NxOgre::Vec3(0,30,0), "cube.1m.mesh");

//Cube 2

mCube2 = mRenderSystem->createBody(NxOgre::BoxDescription(1,1,1), NxOgre::Vec3(20,35,0), "cube.1m.mesh");

mCube2->addForce(NxOgre::Vec3(-800, -200, 0));

// mScene->createSceneGeometry(new NxOgre::PlaneGeometry(0, NxOgre::Vec3(0, 1, 0)), Matrix44_Identity);
}




// Create a new frame listener
void createFrameListener()
{
mFrameListener = new BloodyMessTutorial2Listener(mWindow, mCamera);
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
BloodyMessTutorial2 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


All this scene creates is a plane, where two boxes start mid air. Both Cube1 and Cube2 are effected by gravity in the scene. However cube2 has ->addForce applied to it, which causes it to colide with cube1 sending them both crashing to the side. Play around with the NxOgre::Vec3(#,#,#) values for different results or add more forces to the cubes.

mCube = mRenderSystem>createBody(NxOgre::BoxDescription(1,1,1),NxOgre::Vec3(0,30,0), "cube.1m.mesh");