|
楼主 |
发表于 2011-9-30 18:13:47
|
显示全部楼层
回复 4# glk
- #include "stdafx.h"
- #include <osg/Notify>
- #include <osg/MatrixTransform>
- #include <osg/PositionAttitudeTransform>
- #include <osg/Group>
- #include <osg/Geode>
- #include <osg/Node>
- #include <osgUtil/Optimizer>
- #include <osg/LineSegment>
- #include <osgDB/Registry>
- #include <osgDB/ReadFile>
- #include <osgGA/TrackballManipulator>
- #include <osgGA/FlightManipulator>
- #include <osgGA/DriveManipulator>
- #include <osgParticle/ExplosionDebrisEffect>
- #include <osgParticle/ExplosionEffect>
- #include <osgParticle/SmokeEffect>
- #include <osgParticle/FireEffect>
- #include <osgSim/OverlayNode>
- #include <osgViewer/Viewer>
- #include <iostream>
- #include<sstream>
- #include <math.h>
- osg::ref_ptr<osg::Group>m_root;
- double rotz0,rotz;//绕z旋转角度的弧度值,当前位置的角度和目标位置的角度,是通过坐标变换后的新坐标系中确定的旋转角度值
- double posx0,posy0,posz0;//当前位置
- double posx,posy,posz;//目标位置
- osg::ref_ptr<osg::MatrixTransform> carnode;
- //double rotscale;
- double newposx0,newposy0,newposz0;//新坐标系下的当前坐标值
- double newposx,newposy,newposz;//新坐标系下的目标坐标值
- int firstflag ;
- //double rotnum;//新坐标系相对于前坐标系的旋转角度
- //double rott;
- double carrot;//rotz;
- double zbrot;
- osg::Quat rtquat;
- osg::Vec3 rtpos;
- double nowposx,nowposy,nowposz;
- double nowrot;//当前相对世界坐标系的旋转角度,可以赋值给zbrot
- osg::Vec3 vec;
- osg::ref_ptr<osg::Node>node0;
- class keyhandler : public osgGA::GUIEventHandler
- {
- public : virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa,osg::Object*,osg::NodeVisitor*)
- {
- osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
- switch (ea.getEventType())
- {
- case osgGA::GUIEventAdapter::PUSH:
- if(ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
- {
- osgUtil::LineSegmentIntersector::Intersections intersections;
- float x=ea.getX();
- float y=ea.getY();
- if( viewer->computeIntersections(x,y,intersections))
- {
- osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
- posx=hitr->getWorldIntersectPoint().x();
- posy=hitr->getWorldIntersectPoint().y();
- //posy=hitr->getWorldIntersectPoint().x();
- //posx=hitr->getWorldIntersectPoint().y();
- posz=hitr->getWorldIntersectPoint().z();
- double rotscale=0;//每次需要旋转的度数
- double rottime=0;//每次旋转需要的时间
- double transtime=0;//每次移动需要的时间
- //osg::ref_ptr<Matrix> mMatrixWorld;//获得当前小车的世界坐标系中的矩阵
- osg::NodePath np = node0->getParentalNodePaths()[0];
- osg::Matrix matrixWorld = osg::computeLocalToWorld(np);
- rtpos=matrixWorld.getTrans();
- rtquat=matrixWorld.getRotate();
- nowposx=rtpos[0];
- nowposy=rtpos[1];
- nowposz=rtpos[2];
- // rtquat.getRotate(nowrot,vec);
-
- posx0=nowposx;
- posy0=nowposy;
- // zbrot=nowrot;
- //rotz0=nowrot;
- // if(firstflag==0)
- // {
- // rotscale=(-(posx-posx0)/fabs(posx-posx0))*(osg::PI_2-((posy-posy0)/fabs(posy-posy0))*atan(fabs(posy-posy0)/fabs(posx-posx0)));
- // rotz=rotscale+rotz;
- // if(posx-posx0>=0){zbrot=-rotscale;}
- // else{zbrot=-rotscale;};
- //firstflag++;
- // }
- //else
-
- /* newposx=posx*cos(zbrot)-posy*sin(zbrot);
- newposy=posx*sin(zbrot)+posy*cos(zbrot);
- newposx0=posx0*cos(zbrot)-posy0*sin(zbrot);
- newposy0=posx0*sin(zbrot)+posy0*cos(zbrot);
- rotscale=(-(newposx-newposx0)/fabs(newposx-newposx0))*(osg::PI_2-((newposy-newposy0)/fabs(newposy-newposy0))*atan(fabs(newposy-newposy0)/fabs(newposx-newposx0)));
- rotz=rotscale+zbrot;
- if(newposx-newposx0>=0){zbrot=-rotscale+zbrot;}
- else{zbrot=-rotscale+zbrot;};*/
- if(firstflag==0)
- {
- rotscale=(-(posx-posx0)/fabs(posx-posx0))*(osg::PI_2-((posy-posy0)/fabs(posy-posy0))*atan(fabs(posy-posy0)/fabs(posx-posx0)));
- rotz=rotscale+rotz;
- if(posx-posx0>=0){zbrot=-rotscale;}
- else{zbrot=-rotscale;};
- firstflag++;
- }
- else{
- newposx=posx*cos(zbrot)-posy*sin(zbrot);
- newposy=posx*sin(zbrot)+posy*cos(zbrot);
- newposx0=posx0*cos(zbrot)-posy0*sin(zbrot);
- newposy0=posx0*sin(zbrot)+posy0*cos(zbrot);
- rotscale=(-(newposx-newposx0)/fabs(newposx-newposx0))*(osg::PI_2-((newposy-newposy0)/fabs(newposy-newposy0))*atan(fabs(newposy-newposy0)/fabs(newposx-newposx0)));
- rotz=rotscale+rotz;
- if(newposx-newposx0>=0){zbrot=-rotscale+zbrot;}
- else{zbrot=-rotscale+zbrot;};
- };
-
- //rotz=rotz+rotscale;
- rottime=fabs(rotscale);
- transtime=sqrt(pow((posx-posx0),2)+pow((posy-posy0),2));
- osg::ref_ptr<osg::AnimationPath> path =new osg::AnimationPath;
- path->setLoopMode(osg::AnimationPath::NO_LOOPING);
- path->insert(0.0,osg::AnimationPath::ControlPoint(osg::Vec3(posx0,posy0,posz0),osg::Quat(rotz0,osg::Vec3(0.0,0.0,1.0))));
- path->insert(rottime,osg::AnimationPath::ControlPoint(osg::Vec3(posx0,posy0,posz0),osg::Quat(rotz,osg::Vec3(0.0,0.0,1.0))));
- path->insert((rottime+transtime),osg::AnimationPath::ControlPoint(osg::Vec3(posx,posy,posz),osg::Quat(rotz,osg::Vec3(0.0,0.0,1.0))));
- carnode->setUpdateCallback(new osg::AnimationPathCallback(path.get()));
- //carnode->setMatrix(osg::Matrixd::scale(osg::Vec3d(5.0,5.0,5.0)));
- rotz0=rotz;
- //posx0=posx;
- //posy0=posy;
- //posz0=posz;
- }
- }
- }
- return false;
- }
- };
- int main( )
- {
- rotz0=0;rotz=0;
- posx0=0;posy0=0;posz0=0;
- posx=0;posy=0;posz=0;
- firstflag=0;
- zbrot=0;
- // rotnum=0;
- carnode=new osg::MatrixTransform();
- osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer();
- m_root =new osg::Group();
- osg::ref_ptr<osg::Node>node= osgDB::readNodeFile("qiangti.osg");
- m_root->addChild(node.get());
- node0= osgDB::readNodeFile("car.osg");
-
- carnode->addChild(node0.get());
- // carnode->setMatrix(osg::Matrixd::translate(osg::Vec3d(0,0,60.0)));
- m_root->addChild(carnode.get());
- viewer->addEventHandler(new keyhandler());
- viewer->setSceneData(m_root.get());
- viewer->realize();
- viewer->run();
- return 0;
- }
复制代码 |
|