cameramanipulatorRace.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *    frankguettler@gmx.de                                                 *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  ***************************************************************************
00023  *                                                                         *
00024  *                                                                         *
00025  *   $Log: cameramanipulatorRace.cpp,v $
00026  *   Revision 1.1.2.1  2006/03/19 13:35:10  robot3
00027  *   race mode now works
00028  *
00029  *
00030  *                                                                         *
00031  ***************************************************************************/
00032 
00033 #include <osg/Notify>
00034 #include "cameramanipulatorRace.h"
00035 #include "mathutils.h"
00036 #include "pos.h"
00037 #include <stdio.h>
00038 
00039 
00040 #define square(x) ((x)*(x))
00041 
00042 namespace lpzrobots {
00043 
00044   using namespace osg;
00045   using namespace osgGA;
00046 
00047   CameraManipulatorRace::CameraManipulatorRace(osg::Node* node,GlobalData& global)
00048     : CameraManipulator(node,global) {}
00049 
00050   CameraManipulatorRace::~CameraManipulatorRace(){}
00051 
00052 
00053   void CameraManipulatorRace::calcMovementByAgent() {
00054     if (watchingAgent!=NULL) {
00055       // manipulate desired eye by the move of the robot
00056       const double* robMove = (watchingAgent->getRobot()->getPosition()-oldPositionOfAgent).toArray();
00057       // attach the robSpeed to desired eye
00058       for (int i=0;i<=2;i++) {
00059         if (!isNaN(robMove[i])) {
00060           desiredEye[i]+=robMove[i];}
00061         else 
00062           std::cout << "NAN exception!" << std::endl;
00063       }
00064       // move behind the robot
00065       // returns the orientation of the robot in matrix style
00066       matrix::Matrix Orientation= (watchingAgent->getRobot()->getOrientation());
00067       Orientation.toTranspose();
00068       // first get the normalized vector of the orientation
00069       double eVecX[3] = {0,1,0};
00070       double eVecY[3] = {1,0,0};
00071       matrix::Matrix normVecX = Orientation * matrix::Matrix(3,1,eVecX);
00072       matrix::Matrix normVecY = Orientation * matrix::Matrix(3,1,eVecY);
00073       // then get the distance between robot and camera
00074       Position robPos = watchingAgent->getRobot()->getPosition();
00075       double distance = sqrt(square(desiredEye[0]-robPos.x)+
00076                              square(desiredEye[1]-robPos.y));
00077       // then new eye = robPos minus normalized vector * distance
00078       desiredEye[0]=robPos.x + distance *normVecX.val(1,0);
00079       desiredEye[1]=robPos.y - distance *normVecY.val(1,0);
00080 
00081       // now do center on the robot (manipulate the view)
00082       // desiredEye is the position of the camera
00083       // calculate the horizontal angle, means pan (view.x)
00084       if (robPos.x-desiredEye[0]!=0) { // division by zero
00085         desiredView[0]= atan((desiredEye[0]-robPos.x)/(robPos.y-desiredEye[1]))
00086           / PI*180.0f+180.0f;
00087         if (desiredEye[1]-robPos.y<0) // we must switch
00088                   desiredView[0]+=180.0f;
00089       }
00090       // calculate the vertical angle
00091       if (robPos.z-desiredEye[2]!=0) { // division by zero
00092         // need dz and sqrt(dx^2+dy^2) for calulation
00093         desiredView[1]=-atan((sqrt(square(desiredEye[0]-robPos.x)+
00094                                   square(desiredEye[1]-robPos.y)))
00095                             /(robPos.z-desiredEye[2]))
00096           / PI*180.0f-90.0f;
00097         if (desiredEye[2]-robPos.z<0) // we must switch
00098           desiredView[1]+=180.0f;
00099       }
00100     }
00101   }
00102 }

Generated on Tue Apr 4 19:05:03 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5