I am trying to develop a normal walkthrough program in OSG . I am trying use
wasd for movement and mouse for rotation .

W-front A-left D-right S-back .

The wads part is done , but when it comes to mouse movement i am facing a
small problem .

What i am trying to do is , i am calculating the change in x co-ordinate and
y co-ordinate of the mouse while it is moving . And i am rotating according
to value of dx and dy .I was able to do it quite sucessfully .. but then i
faced a problem . The problem as that i couldnt rotate more when the
pointer  is at a corner . coz near the corner he cant move further and dx=0
or dy=0 . Thus i wasnt able to move anything :( .

so i thought of restoring the mouse pointer every frame to the center of the
window. For that i used requestWarpPointer() of osgViewer::GraphicsWindow .
But now the program is behaving weirdly ... can u please check it out ..
thanx in advance

----------------------------------------------
Maruti Borker
IIIT Hyderabad
Website:- 
http://students.iiit.ac.in/~maruti<http://students.iiit.ac.in/%7Emaruti>
Blog:- http://marutiborker.wordpress.com
#include <osg/PositionAttitudeTransform>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile> 
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>

// Main Global Variables needed
osgGA::TrackballManipulator *Mn;
osgViewer::GraphicsWindow* gw;
class KeyboardHandler: public osgGA::GUIEventHandler 
{
	public:
		KeyboardHandler() {}
		bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
		{
			if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) 
			{
				if (ea.getKey() == 'w') { // To move forward 
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(0.0,0.0,-1.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;	
				}
				if (ea.getKey() == 'a') { //To move left
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(-1.0,0.0,0.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
				if (ea.getKey() == 's') { //To move backward
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(0.0,0.0,1.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
				if (ea.getKey() == 'd') { //To move right
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(1.0,0.0,0.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
			}
			if(ea.getEventType() == osgGA::GUIEventAdapter::DRAG || ea.getEventType() == osgGA::GUIEventAdapter::MOVE )
			{
				static float px = ea.getXnormalized();
				static float py = ea.getYnormalized();
/*				float dx =ea.getXnormalized();
				float dy =ea.getYnormalized();
				if(dx==400 && dy==400) return false;
				dx-=px;
				dx*=-20;
				dy-=py;
				dy*=20;*/
				float dx = -(ea.getXnormalized()-px)*20;
				float dy = (ea.getYnormalized()-py)*20;
				px = ea.getXnormalized();
				py = ea.getYnormalized();
				osg::Matrixd tempMatrix;
				tempMatrix.makeRotate(
						osg::DegreesToRadians(dx), osg::Vec3(0,1,0), // roll
						osg::DegreesToRadians(dy), osg::Vec3(1,0,0) , // pitch
						osg::DegreesToRadians(0.0), osg::Vec3(0,0,1) );
				tempMatrix=tempMatrix*Mn->getMatrix();
				Mn->setByMatrix(tempMatrix);
			}	
			return false;
		}
};
int main()
{
	osg::Node* tankNode = NULL;
	osg::Group* root = NULL;
//Start
	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
	traits->x = 200;
	traits->y = 200;
	traits->width = 800;
	traits->height = 600;
	traits->windowDecoration = true;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;

	osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
	gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
	gw->useCursor(false);
	gw->requestWarpPointer(400,400);
//End 

	osgViewer::Viewer viewer;
	viewer.getCamera()->setGraphicsContext(gc.get());
	viewer.getCamera()->setViewport(0,0,800,600);
	osg::Vec3 tankPosit; 
	osg::PositionAttitudeTransform* tankXform;

	tankNode = osgDB::readNodeFile("cow.osg");

	root = new osg::Group();
	tankXform = new osg::PositionAttitudeTransform();

	root->addChild(tankXform);
	tankXform->addChild(tankNode);

	tankPosit.set(5,0,0);
	tankXform->setPosition( tankPosit ); 
	Mn= new osgGA::TrackballManipulator();
	viewer.setCameraManipulator(Mn);
	KeyboardHandler *KH=new KeyboardHandler;
	viewer.addEventHandler(KH);
	viewer.setSceneData( root );
	viewer.realize();
	int cnt=0;
	while( !viewer.done() )
	{
		cnt++;
	 	if(cnt==100)
		{
			gw->requestWarpPointer(400,400);
			cnt=0;
		}
	 viewer.frame();
	}
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to