tomneo2000
29-09-2009 07:04:22
#include <NxOgre.h>
#include <NxOgreOGRE3D.h>
class Robot
{
protected:
NxOgre::Real mRotateSpeed;
NxOgre::Real mRotateScale;
NxOgre::Real mRangeToDest;
NxOgre::Vec3 mPosition;
NxOgre::Vec3 mDestination;
NxOgre::Vec3 mLastDestination;
OGRE3DKinematicBody* mEntity;
NxOgre::Mesh* mMesh;
OGRE3DRenderSystem* mRenderSystem;
NxOgre::Scene* mScene;
Ogre::String mName;
Ogre::String mMeshName;
NxOgre::RigidBodyDescription mRigidBodyDesc;
public:
Robot(NxOgre::Real RotateSpeed, NxOgre::Vec3 Destination, NxOgre::Vec3 Position, NxOgre::Mesh* Mesh, OGRE3DRenderSystem* RenderSystem,
NxOgre::Scene* Scene, Ogre::String& Name, Ogre::String& MeshName,
NxOgre::RigidBodyDescription& description=NxOgre::RigidBodyDescription());
~Robot();
protected:
void Rotate(Ogre::Real TimeStep);
NxOgre::Real DotProduct(const NxOgre::Vec3& v1, const NxOgre::Vec3& v2);
public:
void SetDestination(NxOgre::Vec3& dest);
void SetName(Ogre::String& name);
void SetScene(NxOgre::Scene* scene);
NxOgre::Vec3 GetDestination();
NxOgre::Vec3 GetLastDestination();
Ogre::String GetName();
NxOgre::Scene* GetScene();
void Update(Ogre::Real TimeStep);
};
#include "Robot.h"
Robot::Robot(NxOgre::Real RotateSpeed, NxOgre::Vec3 Destination, NxOgre::Vec3 Position, NxOgre::Mesh* Mesh, OGRE3DRenderSystem* RenderSystem,
NxOgre::Scene* Scene, Ogre::String& Name, Ogre::String& MeshName, NxOgre::RigidBodyDescription& description)
{
Robot::mRotateSpeed=RotateSpeed;
Robot::mDestination=Destination;
Robot::mLastDestination=mDestination;
Robot::mPosition=Position;
Robot::mMesh=Mesh;
Robot::mRenderSystem=RenderSystem;
Robot::mScene=Scene;
Robot::mName=Name;
Robot::mMeshName=MeshName;
Robot::mRangeToDest=1;
Robot::mRotateScale=0;
Robot::mRigidBodyDesc=description;
Robot::mEntity=mRenderSystem->createKinematicBody(new NxOgre::Convex(mMesh), mPosition, mMeshName, mRigidBodyDesc);
}
Robot::~Robot()
{
if(mRenderSystem)
{
mRenderSystem->destroyKinematicBody(mEntity);
mEntity=NULL;
}
}
NxOgre::Vec3 Robot::GetDestination()
{
return mDestination;
}
NxOgre::Vec3 Robot::GetLastDestination()
{
return mLastDestination;
}
Ogre::String Robot::GetName()
{
return mName;
}
NxOgre::Scene* Robot::GetScene()
{
return mScene;
}
void Robot::SetDestination(NxOgre::Vec3 &dest)
{
if (dest!=mDestination)
{
mDestination=dest;
}
}
void Robot::SetName(Ogre::String &name)
{
if (name!=mName)
{
mName=name;
}
}
void Robot::SetScene(NxOgre::Scene *scene)
{
if(scene!=mScene)
{
mScene=scene;
}
}
NxOgre::Real Robot::DotProduct(const NxOgre::Vec3 &v1, const NxOgre::Vec3 &v2)
{
return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
}
void Robot::Rotate(Ogre::Real TimeStep)
{
//direction of myself
NxOgre::Vec3 direction;
NxOgre::Matrix33 mm=mEntity->getGlobalOrientation();
mEntity->getGlobalOrientationQuat().normalise();
direction.x=mEntity->getGlobalOrientationQuat().x;
direction.y=mEntity->getGlobalOrientationQuat().y;
direction.z=mEntity->getGlobalOrientationQuat().z;
//direction to point clicked
NxOgre::Vec3 directionToDest=mDestination-mEntity->getGlobalPosition();
directionToDest.normalise();
//do dot product and cosine, find out the theta between myself direction vec and direction vec from me to point clicked
NxOgre::Real value=Ogre::Math::Cos(DotProduct(direction, directionToDest));
if (value!=0)
{
if(value>0)
{
mEntity->moveGlobalOrientation(NxOgre::Matrix33(NxOgre::Math::cos(mRotateScale), 0, NxOgre::Math::sin(-mRotateScale), 0, 1, 0,
NxOgre::Math::sin(mRotateScale), 0, NxOgre::Math::cos(mRotateScale)));
mRotateScale+=mRotateSpeed*TimeStep;
}
else if(value<0)
{
mEntity->moveGlobalOrientation(NxOgre::Matrix33(Ogre::Math::Cos(mRotateScale), 0, Ogre::Math::Sin(mRotateScale), 0, 1, 0,
Ogre::Math::Sin(-mRotateScale), 0, Ogre::Math::Cos(mRotateScale)));
mRotateScale+=mRotateSpeed*TimeStep;
}
}
else if (value==0)
{
mLastDestination=mDestination;
}
}
void Robot::Update(Ogre::Real TimeStep)
{
if (mDestination!=mLastDestination)
{
Rotate(TimeStep);
}
}
My body can do the rotation but now i want to make it to rotate smoothly from it's original direction to a point which i give. In NxOgre i can't find any method that can retrieve a body's direction, so i use getGlobalOrientationQuat() instead, but the result is strange. I want to get body's direction because once i get it then i can do dot product to check how much degree left , if the degree==0 then it means to stop rotate(body is facing that point). Anyway , now my body just keep rotating never stop, even i give a point.
I have been thinking to use getGlobalOrientation() to get Matrix3x3 first then retrieve direction from Matrix3x3, but i am not sure it works or not?
Is there any one know how to do it or what function can be used for this situation?