Hi,
I am currently working on a simple CameraManipulator class. This is a simple
CameraManipulator whose aim is to go forward towards a specific direction
(heading). Now I want to play with the roll value, however I noticed that when
I changes roll, it changes in fact heading ! I'm very confuse.
Have you ever encountered such a problem ?
Here is my class :
Code:
class NavManipulator: public osgGA::CameraManipulator
{
class UnknownTransformType {};
private :
bool setting;
Vec3f *pos;
osg::Geode *textGeode;
osgText::Text *config;
osgText::Text *vals;
float heading, pitch, roll, k;
bool is_jumping;;
float jump_distance;
float jump_angle;
float latestZVal;
void setTextSetting()
{
if(setting)
{
config->setCharacterSize(10);
config->setText("Moving:\n key up : forward\n key down :
backward\n key left : turn left\n key right : turn right\n p : go up \n m : go
down\n\nVelocity:\n o : go faster\n l : go slower\n\nOther stuff:\n j : jump\n
t : random teleportation\n c : get position");
config->setAxisAlignment(osgText::Text::SCREEN);
config->setPosition(osg::Vec3(50,180,-1.5));
config->setColor(osg::Vec4(255, 255, 255, 1));
vals->setCharacterSize(10);
vals->setAxisAlignment(osgText::Text::SCREEN);
vals->setPosition(osg::Vec3(50,20,-1.5));
vals->setColor(osg::Vec4(255, 0, 0, 1));
setting = false;
}
}
void getCurrentPosition()
{
stringstream currentPosition;
currentPosition << "Position : (";
currentPosition << std::setprecision(2) << pos->x();
currentPosition << ",";
currentPosition << std::setprecision(2) << pos->y();
currentPosition << ",";
currentPosition << std::setprecision(2) << pos->z();
currentPosition << ")";
std::cerr << currentPosition.str() << std::endl;
vals->setText(currentPosition.str());
}
public :
NavManipulator(osg::Geode *textGeode)
{
setting = true;
this->textGeode = textGeode;
config = new osgText::Text();
vals = new osgText::Text();
textGeode->addDrawable(config);
textGeode->addDrawable(vals);
heading = 270;
pitch = 90;
roll = 0;
pos = new Vec3f(0,0,200);
k = 1;
is_jumping = false;
jump_distance = 0;
jump_angle = 0;
latestZVal = 0;
}
virtual ~NavManipulator()
{
delete pos;
//delete config;
//delete vals;
}
bool handle(const GUIEventAdapter& ea, GUIActionAdapter& us)
{
setTextSetting();
switch(ea.getEventType())
{
case(GUIEventAdapter::KEYDOWN):
{
/* walking */
if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Up )
{
pos->x() += k*sin(-osg::DegreesToRadians(heading));
pos->y() += k*cos(-osg::DegreesToRadians(heading));
return true;
}
if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
{
pos->x() -= k*sin(-osg::DegreesToRadians(heading));
pos->y() -= k*cos(-osg::DegreesToRadians(heading));
return true;
}
if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
{
heading = heading + 10;
return true;
}
if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
{
heading = heading - 10;
return true;
}
if(ea.getKey() == 'p')
{
pos->z() += 10;
return true;
}
if(ea.getKey() == 'm')
{
pos->z() -= 10;
return true;
}
/* speed boost */
if(ea.getKey() == 'o')
{
k *= 2;
return true;
}
if(ea.getKey() == 'l')
{
k /= 2;
return true;
}
if(ea.getKey() == 'c')
{
roll += 10;
getCurrentPosition();
return true;
}
if(ea.getKey() == 'j')
{
is_jumping = true;
jump_distance = 20;
jump_angle = 180;
latestZVal = pos->z();
return true;
}
if(ea.getKey() == 't')
{
pos->x() = 400.0 * ((float)rand() / RAND_MAX);
pos->y() = 400.0 * ((float)rand() / RAND_MAX);
// not to be under the ground
pos->z() = 350.0;
return true;
}
return false;
}
case(GUIEventAdapter::FRAME) :
if(is_jumping)
{
if(jump_angle > 0)
{
pos->x() +=
k*sin(-osg::DegreesToRadians(heading));
pos->y() +=
k*cos(-osg::DegreesToRadians(heading));
pos->z() = latestZVal + jump_distance/2 *
sin(osg::DegreesToRadians(180 - jump_angle));
jump_angle -= 5;
}
else
{
is_jumping = false;
}
}
return true;
default :
return false;
}
}
void setByMatrix(const Matrix& m)
{
}
Matrixd getMatrix() const
{
Matrix matRotate, matTranslate;
matRotate.makeRotate(DegreesToRadians(roll),
Vec3(0,1,0),
DegreesToRadians(pitch),
Vec3(1,0,0),
DegreesToRadians(heading),
Vec3(0,0,1));
matTranslate.makeTranslate(*pos);
return matRotate * matTranslate;
}
void setByInverseMatrix(const Matrixd &m)
{
}
Matrixd getInverseMatrix() const
{
return Matrix::inverse(getMatrix());
}
};
Thank you!
Cheers,
alef
Code:
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44372#44372
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org