Skip to content

Commit

Permalink
Use a new collision manager
Browse files Browse the repository at this point in the history
..
  • Loading branch information
LiPingjiang committed May 11, 2015
1 parent d9804ac commit 58274e7
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 12 deletions.
56 changes: 56 additions & 0 deletions CollisionManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "CollisionManager.h"

CollisionManager::CollisionManager(std::vector<std::ObjectInfo> *Obs)
{
Objects=Obs;
}
CheckCollisionAnswer CollisionManager::checkCollisionAABB(std::ObjectInfo object)
{
CheckCollisionAnswer answer;
answer.amount=0;
answer.objectName=object.getObjectName();
answer.collided=false;
Ogre::AxisAlignedBox spbox = static_cast<Ogre::SceneNode*>(object.getNode())->_getWorldAABB();
for (std::vector<std::ObjectInfo>::iterator iter = Objects->begin(); iter != Objects->end(); iter++)
{

if(object.getObjectName() != iter->getObjectName() )
{
Ogre::AxisAlignedBox cbbox=static_cast<Ogre::SceneNode*>(iter->getNode())->_getWorldAABB();

if(spbox.intersects(cbbox))//collided
{
answer.amount++;
answer.collided=true;
answer.collidedList.push_back(*iter);
}
}
}
return answer;
}

CheckCollisionAnswer CollisionManager::checkCameraCollision()
{
CheckCollisionAnswer answer;
answer.amount=0;
answer.objectName="camera";
answer.collided=false;

//Ogre::AxisAlignedBox spbox = new Ogre::AxisAlignedBox(
//for (std::vector<std::ObjectInfo>::iterator iter = Objects->begin(); iter != Objects->end(); iter++)
// {

// if(object.getObjectName() != iter->getObjectName() )
// {
// Ogre::AxisAlignedBox cbbox=static_cast<Ogre::SceneNode*>(iter->getNode())->_getWorldAABB();

// if(spbox.intersects(cbbox))//collided
// {
// answer.amount++;
// answer.collided=true;
// answer.collidedList.push_back(*iter);
// }
// }
// }
return answer;
}
31 changes: 31 additions & 0 deletions CollisionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef COLLISIONMANAGER_H
#define COLLISIONMANAGER_H

#include <Ogre.h>
#include "ObjectInfo.h"
#include <vector>

struct CheckCollisionAnswer
{
bool collided;
std::string objectName;
int amount; //how many object have collided with this one
std::vector<std::ObjectInfo> collidedList;
};

class CollisionManager
{
public:
CollisionManager(std::vector<std::ObjectInfo> *Obs);

std::vector<std::ObjectInfo> *Objects;

CheckCollisionAnswer checkCollisionAABB(std::ObjectInfo object);

CheckCollisionAnswer checkCameraCollision();

private:

};

#endif
9 changes: 6 additions & 3 deletions GeneratorFrameListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,13 @@ void GeneratorFrameListener::moveCamera()
Ogre::Vector3 oldPosition=mCamera->getPosition();
mCamera->moveRelative(mTranslateVector);
pSphere->setObserverPosition(mCamera->getPosition());
//later need to detect all the collision between camera and objects
if( pSphere->getObserverDistanceToSurface()<=2.0f)

if( pSphere->getObserverDistanceToSurface()<=2.0f)//not inside the sphere
{
mCamera->setPosition(oldPosition);
if(!CollisionDetectionManager->checkCameraCollision().collided)
{
mCamera->setPosition(oldPosition);
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions GeneratorFrameListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ D: Step right
#include "OgreTextAreaOverlayElement.h"
#include "OgreFontManager.h"
#include "PSphere.h"
#include "CollisionManager.h"



Expand All @@ -57,33 +58,37 @@ class GeneratorFrameListener: public Ogre::FrameListener, public Ogre::WindowEve
public:
Ogre::SceneNode *RootSceneNode;//use to operate the entity

MOC::CollisionTools *CollisionManager;
//MOC::CollisionTools *CollisionManager;
Ogre::SceneManager *Scene;
PSphere *pSphere;
CollisionManager *CollisionDetectionManager;


GeneratorFrameListener()
{
}

// Constructor takes a RenderWindow because it uses that to determine input context
GeneratorFrameListener(Ogre::RenderWindow* win, Ogre::Camera* cam, PSphere *ps, Ogre::SceneManager *Sc,Ogre::SceneNode *RSN,bool bufferedKeys = false, bool bufferedMouse = false,
GeneratorFrameListener(Ogre::RenderWindow* win, Ogre::Camera* cam, PSphere *ps, Ogre::SceneManager *Sc,Ogre::SceneNode *RSN,CollisionManager *CDM ,bool bufferedKeys = false, bool bufferedMouse = false,
bool bufferedJoy = false ) :
mCamera(cam), mTranslateVector(Ogre::Vector3::ZERO), mCurrentSpeed(0), mWindow(win), mStatsOn(true), mNumScreenShots(0),
mMoveScale(0.0f), mRotScale(0.0f), mTimeUntilNextToggle(0), mFiltering(Ogre::TFO_BILINEAR),
mAniso(1), mSceneDetailIndex(0), mMoveSpeed(100), mRotateSpeed(18), mDebugOverlay(0),
mInputManager(0), mMouse(0), mKeyboard(0), mJoy(0)
{

CollisionDetectionManager = CDM;

RootSceneNode = RSN;//NEW

Scene = Sc;

pSphere = ps;

// init the collision handler
CollisionManager = new MOC::CollisionTools(Scene);
//CollisionManager = new MOC::CollisionTools(Scene);
// set how far we want the camera to be above ground
CollisionManager->setHeightAdjust(5);
//CollisionManager->setHeightAdjust(5);


Ogre::OverlayManager &om = Ogre::OverlayManager::getSingleton();
Expand Down
6 changes: 5 additions & 1 deletion ObjectInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace std {
position = pos;
name = objName;
node = rootNode->getChild(objName);

}

std::string ObjectInfo::getObjectName() {
Expand All @@ -20,5 +21,8 @@ namespace std {
Ogre::Vector3 ObjectInfo::getPosition() {
return position;
}


Ogre::Node *ObjectInfo::getNode(){
return node;
}
}
1 change: 1 addition & 0 deletions ObjectInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace std{
ObjectInfo(Ogre::Vector3 position, const std::string& objName, Ogre::SceneNode *rootNode);
std::string getObjectName();
Ogre::Vector3 getPosition();
Ogre::Node *getNode();

private:
Ogre::Vector3 position;
Expand Down
6 changes: 6 additions & 0 deletions PSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,9 @@ Ogre::Vector3 PSphere::nextPosition(Ogre::Vector3 location, PSphere::Direction d

return newPos;
}


vector<ObjectInfo> *PSphere::getObjects()
{
return &objects;
}
4 changes: 2 additions & 2 deletions PSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class PSphere

void attachMeshOnGround(Ogre::SceneNode *node, Ogre::SceneManager *scene, const std::string &meshName, const std::string &objectName, Ogre::Real latitude, Ogre::Real longitude);



Ogre::MeshPtr getMesh();

void setObserverPosition(Ogre::Vector3 position);
Expand All @@ -50,6 +48,8 @@ class PSphere

Ogre::Vector3 nextPosition(Ogre::Vector3 location, PSphere::Direction dir);

vector<ObjectInfo> *getObjects();

PSphere();

private:
Expand Down
4 changes: 3 additions & 1 deletion initOgre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ void initOgre::CreateFrameListener(PSphere *pSphere){
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
FrameListener= new GeneratorFrameListener(Window, Camera, true, true, true);
#else
FrameListener= new GeneratorFrameListener(Window, Camera,pSphere ,Scene , RootSceneNode);
FrameListener= new GeneratorFrameListener(Window, Camera,pSphere ,Scene , RootSceneNode ,CollisionDetectionManager);
#endif
//FrameListener->showDebugOverlay(true);
Root->addFrameListener(FrameListener);
}

void initOgre::setSceneAndRun(PSphere *planet){

CollisionDetectionManager = new CollisionManager(planet->getObjects());

// Create camera
Camera = Scene->createCamera("VertCamera");
Expand Down
2 changes: 2 additions & 0 deletions initOgre.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "GeneratorFrameListener.h"
#include "PSphere.h"
#include <vector>
#include "CollisionManager.h"

class initOgre{
public:
Expand All @@ -22,6 +23,7 @@ class initOgre{
Ogre::Camera *Camera;
GeneratorFrameListener *FrameListener;
Ogre::OverlaySystem *OverlaySystem;
CollisionManager *CollisionDetectionManager;

void CreateFrameListener(PSphere *pSphere);
};
Expand Down
2 changes: 1 addition & 1 deletion simplexnoise1234.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SimplexNoise1234
// Copyright © 2003-2011, Stefan Gustavson
// Copyright ?2003-2011, Stefan Gustavson
//
// Contact: stegu@itn.liu.se
//
Expand Down

0 comments on commit 58274e7

Please sign in to comment.