[osg-users] Create a custom StandardManipulator

Florian GOLESTIN florian.golestin at gmail.com
Sat Apr 23 02:08:17 PDT 2016


Hi everyone,

I'm trying to develop my own Camera Manipulator (in order to have a Doom-like view). I've looked at documentations but there are still questions for me.

First question I had was "How to translate an object relatively to its own position (and not to the world)". I'm still looking for a way doing this with Matrix, however, I've found a solution with quaternion :
You can multiply a Quaternion with a Vector to move relatively to the direction. 

For instance: 

Code:

_position += _attitude * translation;



where 

"_position" is a Vec3 representing the current position (e.g. from osg::PositionAttitudeTransform)
"_attitude" is a quaternion representing the current direction (e.g. from osg::PositionAttitudeTransform)
"translation" is the relative translation (as Vec3) to apply


This solution provides me good result but is it a good way?



Then I wanted to create the camera manipulator, so I've extended the class such as:

Header

Code:

class FirstPersonManipulator : public osgGA::StandardManipulator, public osg::PositionAttitudeTransform




Source

Code:

FirstPersonManipulator::FirstPersonManipulator(const osg::Vec3d &eye, const osg::Vec3d &center)
{
  osg::Vec3 up(0.0, 0.0, 1.0);
    
  setTransformation(eye, center, up);

  _homeEye = eye;
  _homeCenter = center;
  _homeUp = up;
  _autoComputeHomePosition = false;
}
  
FirstPersonManipulator::~FirstPersonManipulator(void)
{
}

void  FirstPersonManipulator::setTransformation(const osg::Vec3d &eye, const osg::Quat &rotation)
{

  std::cout << "FirstPersonManipulator::setTransformation: Old Position [" << _position << " X " << _attitude
     << "] New Position: [" <<  eye << " X " << rotation << "]\n";
  this->_position = eye;
  this->_attitude = rotation;
}

void  FirstPersonManipulator::setTransformation(const osg::Vec3d &eye, const osg::Vec3d &center, const osg::Vec3d &up)
{
  osg::Matrixd view;
    
  view.makeLookAt(eye, center, up);

  /* This line is required otherwise the canera seems "head-down" */
  view.makeRotate(osg::DegreesToRadians(90.0), osg::Vec3(1, 0, 0));

    
  std::cout << "FirstPersonManipulator::setTransformation: " << view  << "\n";
    
  this->setTransformation(eye, view.getRotate());
}

void  FirstPersonManipulator::getTransformation(osg::Vec3d &eye, osg::Quat &rotation) const
{
  eye = this->_position;
  rotation = this->_attitude;
}

void  FirstPersonManipulator::getTransformation(osg::Vec3d &eye, osg::Vec3d &center, osg::Vec3d &up) const
{
  osg::Matrixd view;

  view.setTrans(this->_position);
  view.setRotate(this->_attitude);
  view.getLookAt(eye, center, up);
}

void  FirstPersonManipulator::setByMatrix(const osg::Matrixd &matrix)
{
  std::cout << "FirstPersonManipulator::setByMatrix: " << matrix << "\n";
  this->setTransformation(matrix.getTrans(), matrix.getRotate());
}
  
void  FirstPersonManipulator::setByInverseMatrix(const osg::Matrixd &matrix)
{
  std::cout << "FirstPersonManipulator::setByInverseMatrix: " << matrix << "\n";
  setByMatrix(osg::Matrixd::inverse(matrix));
}
    
osg::Matrixd  FirstPersonManipulator::getMatrix() const
{
  osg::Matrixd view;

  view.setTrans(this->_position);
  view.setRotate(this->_attitude);
  std::cout << "FirstPersonManipulator::getMatrix: " << view << "\n";
  return view;
}
  
osg::Matrixd  FirstPersonManipulator::getInverseMatrix() const
{
  std::cout << "FirstPersonManipulator::getInverseMatrix: " << osg::Matrixd::inverse(getMatrix()) << "\n";
  return osg::Matrixd::inverse(getMatrix());
}




Here the camera is correctly positioned and oriented but I had to add this line (in FirstPersonManipulator::setTransformation):

Code:

  /* This line is required otherwise the canera seems "head-down" */
  view.makeRotate(osg::DegreesToRadians(90.0), osg::Vec3(1, 0, 0));



Otherwise the camera seemed to had foot up and head down.

Is the code above correct?
Do you have an idea about the rotation I had to do?


(PS: Sorry I cannot get rid of the 'question-mark '?' - I've replaced all tab and spaces but they still appears)

Thanks,
Florian[/list]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=66937#66937








More information about the osg-users mailing list