OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Create a custom StandardManipulator


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
Flogo
User


Joined: 08 Apr 2016
Posts: 26

PostPosted: Sat Apr 23, 2016 9:07 am    Post subject:
Create a custom StandardManipulator
Reply with quote

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?


Thanks,
Florian
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts the speed of PagedLOD create nodes snowlove General 0 Thu Sep 20, 2018 8:07 am View latest post
No new posts How to create a 3D ray from cursor po... Waaayoff General 2 Mon Jul 16, 2018 3:34 pm View latest post
No new posts How do I create a graphics context on... srinivas General 3 Tue Apr 24, 2018 11:40 am View latest post
No new posts custom file GeekOwl Plugins [osgPlugins] 1 Thu Oct 12, 2017 12:32 pm View latest post
No new posts Passing input to viewer/camera (using... antiro42 General 2 Sat Sep 16, 2017 11:07 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP