bens1404
17-01-2008 00:25:58
hi anybody
i am using ogreopcode collision detection library on my scens loaded with ofusion ,i wanna make a first person camera style ,so
here is my code
main.cpp
i got the collision work but the entity and the collision shape they
dont have the some size and my camera go a bit through wall
beleive me the entity from ofusion got scaled by ofusion node
hope if anybody can help me to fix that,how do i scale the collision
shape to fit the entity node loaded fom ofusion
i am using ogreopcode collision detection library on my scens loaded with ofusion ,i wanna make a first person camera style ,so
here is my code
main.cpp
#include "ExampleApplication.h"
#include "collision.h"
// oScene loader library header file
#include "OgreOSMScene.h"
// Callback handler to post-process created objects
class oSceneCallback : public OSMSceneCallbacks {
public:
// We override the OnCreate method for cameras (See IXMLSceneCallbacks class)
void OnCameraCreate(Ogre::Camera* pCamera, TiXmlElement* pCameraDesc) {
// If a camera of name "FirstCamera" is loaded, it will be set as the default current
if(pCamera->getName() == "FirstCamera")
Ogre::Root::getSingleton().getAutoCreatedWindow()->getViewport(0)->setCamera(pCamera);
}
void OnEntityCreate(Ogre::Entity* pEntity, TiXmlElement* pEntityDesc) {
pEntity->getParentSceneNode()->showBoundingBox(true);
}
};
// Event handler to animate
class oSceneLibDemoFrameListener : public ExampleFrameListener, Collision::CollisionListener
{
protected:
bool mFlagGravity;
Real mTimeSinceLastFrame;
String mLatestCollision;
SceneManager* mSceneMgr;
public:
oSceneLibDemoFrameListener(SceneManager* sceneMgr, RenderWindow* win, Camera* cam,SceneNode* camNode)
: mSceneMgr(sceneMgr), ExampleFrameListener(win, cam,camNode),mFlagGravity(false),
Collision::CollisionListener()
{
}
bool frameStarted(const FrameEvent& evt)
{
if( ExampleFrameListener::frameStarted(evt) == false )
return false;
mTimeSinceLastFrame = evt.timeSinceLastFrame;
return true;
}
//--------------
void moveCamera()
{
Quaternion q = mCamNode->getWorldOrientation();
Vector3 vel = q * mTranslateVector;
Vector3 gravity(0, 0, 0);
if (mFlagGravity) {
// adjust gravity based on fps
gravity = Vector3(0, -100 * mTimeSinceLastFrame, 0);
}
// small enough so we can fly out of open windows
Real radius = 0.3;
if (vel != Vector3::ZERO || mFlagGravity) {
// do the collision
Vector3 pos = Collision::collideAndSlide(mCamNode->getPosition(), vel,
radius, gravity, this);
mCamNode->setPosition(pos);
}
mCamNode->yaw(mRotX, Node::TS_WORLD);
mCamNode->pitch(mRotY);
}
//-------------------------------
void collisionOccurred(String obj)
{
mLatestCollision = "/ Latest collision: " + obj;
}
//------------------------
bool processUnbufferedKeyInput(const FrameEvent& evt)
{
static Real currentTime = 0;
bool ret = ExampleFrameListener::processUnbufferedKeyInput(evt);
if (!ret) return false;
if(mKeyboard->isKeyDown(OIS::KC_G) && mTimeUntilNextToggle <= 0) {
mFlagGravity = !mFlagGravity;
mTimeUntilNextToggle = 1;
}
return true;
}
};
class oSceneLibApplication : public ExampleApplication {
public:
oSceneLibApplication() {}
protected:
OgreOpcode::CollisionContext *mCollisionContext;
void setupCollisions()
{
using namespace OgreOpcode;
new CollisionManager(mSceneMgr);
CollisionManager* colMgr = CollisionManager::getSingletonPtr();
mCollisionContext = colMgr->getDefaultContext();
Details::OgreOpcodeDebugger *debuger = new Details::OgreOpcodeDebugger("entity_alias", mSceneMgr);
// notice that camera sliding does not use collision classes at all
// (it uses COLLTYPE_ALWAYS_EXACT)
//colMgr->addCollClass("gameobject");
//colMgr->addCollType("gameobject", "gameobject", COLLTYPE_IGNORE);
SceneManager::MovableObjectIterator mov_it = mSceneMgr->getMovableObjectIterator("Entity");
while (mov_it.hasMoreElements()) {
MovableObject* movable = mov_it.getNext();
Entity* ent = static_cast<Entity*>(movable);
String entName = ent->getName();
CollisionObject* collObj = mCollisionContext->createObject(entName + "_CollObj");
EntityCollisionShape* collShape = colMgr->createEntityCollisionShape(entName + "_CollShape");
collShape->load(ent);
collObj->setShape(collShape);
mCollisionContext->addObject(collObj);
// this line is needed for some reason (otherwise CollisionObject minv/maxv
// gets strange values and no collisions occur)
// looks like some bounding box is not calculated if objects are not moving
collShape->visualize(debuger);
collObj->update(0);
}
}
// Just override the mandatory create scene method
void createScene(void)
{
// Here is the code that will load the scene file
// A fixed filename "scene.osm" is implemented for this demo
// Create the scene loader
OSMScene oScene;
// Create an oE_Loader Callback object to post-process created objects
oSceneCallback oe_Callback;
// Initialises with the scene to be loaded and the callback if requiered
oScene.initialise("myworld.osm");
// create and setup the scene in the root node
oScene.createScene();
mSceneMgr = oScene.getSceneManager();
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setNearClipDistance(5);
mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCamNode->lookAt(Vector3(0, 0, -300), Node::TS_WORLD);
mCamNode->attachObject(mCamera);
mCamNode->setPosition(0, 15,300);
Ogre::Root::getSingleton().getAutoCreatedWindow()->getViewport(0)->setCamera(mCamera);
setupCollisions();
}
// The scene manager will be created by the Scene Loader lib
void chooseSceneManager(void) {}
void createCamera(void)
{
}
void createViewports(void) {}
// Create new frame listener
void createFrameListener(void)
{
mFrameListener= new oSceneLibDemoFrameListener(mSceneMgr, mWindow, mCamera,mCamNode);
mRoot->addFrameListener(mFrameListener);
}
};
i got the collision work but the entity and the collision shape they
dont have the some size and my camera go a bit through wall
beleive me the entity from ofusion got scaled by ofusion node
hope if anybody can help me to fix that,how do i scale the collision
shape to fit the entity node loaded fom ofusion