Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGWalkNavigator.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000,2001 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include "OSGWalkNavigator.h"
00040 
00041 #include <OSGSimpleGeometry.h>
00042 #include <OSGNode.h>
00043 #include <OSGNodeCore.h>
00044 
00045 OSG_USING_NAMESPACE
00046 
00047 
00048 /***************************************************************************\
00049  *                            Description                                  *
00050 \***************************************************************************/
00051 
00058 /*------------------------- constructors ----------------------------------*/
00059 
00063 WalkNavigator::WalkNavigator(): FlyNavigator(),
00064     _ground(NullFC),
00065     _world(NullFC),
00066     _groundDistance(0.75),
00067     _wallDistance(0.1),
00068     _height(0.85),
00069     _width(0.5),
00070     _fatness(0.5)
00071 {
00072     _act = IntersectAction::create();
00073 }
00074 
00075 /*-------------------------- destructors ----------------------------------*/
00076 
00080 WalkNavigator::~WalkNavigator()
00081 {
00082     delete _act;
00083 }
00084 
00085 
00086 /*------------------------------ get --------------------------------------*/
00087 
00088 
00089 /*------------------------------ set --------------------------------------*/
00090 
00091 
00092 void WalkNavigator::setGround(NodePtr &new_ground)
00093 {
00094     _ground=new_ground;
00095 }
00096 
00097 void WalkNavigator::setWorld(NodePtr &new_world)
00098 {
00099     _world=new_world;
00100 }
00101 
00102 void WalkNavigator::setGroundDistance(Real32 groundDistance)
00103 {
00104     _groundDistance=groundDistance;
00105 }
00106 
00107 void WalkNavigator::setMinWallDistance (Real32 wallDistance)
00108 {
00109     _wallDistance=wallDistance;
00110 }
00111     
00112 void WalkNavigator::setPersonDimensions(Real32 height,Real32 width,Real32 fatness)
00113 {
00114     _height  = height;
00115     _width   = width;
00116     _fatness = fatness;
00117 }
00118 
00119 /*---------------------- Walker Transformations ----------------------------*/
00120 
00124 void WalkNavigator::rotate (Real32 deltaX, Real32 deltaY)
00125 {
00126     FlyNavigator::rotate(deltaX, deltaY);
00127 }
00128    
00132 Real32 WalkNavigator::forward(Real32 step)
00133 {
00134     Vec3f lv = _rFrom - _rAt;
00135     lv.normalize();
00136 
00137     Vec3f upn = _vUp;
00138     upn.normalize();
00139 
00140     Vec3f mv = lv - upn.dot(lv)*upn;
00141     mv.normalize();
00142 
00143     //side vector symbolizes shoulders
00144     Vec3f sv = mv;
00145     sv.crossThis(upn);
00146     sv.normalize();
00147 
00148     Pnt3f rFrom = _rFrom + step*mv;
00149     Pnt3f rAt = _rAt + step*mv;
00150 
00151     Real32 dist;
00152     Line line(rFrom, -upn);
00153 
00154     //keep the walker at a constant distance from the ground
00155     _act->setLine(line);
00156     _act->apply(_ground);
00157 
00158     if (_act->didHit()) {
00159         dist = _act->getHitT();
00160         if (dist >= _height) {
00161             rFrom = rFrom + (_groundDistance-dist+_height)*upn;
00162             rAt = rAt + (_groundDistance-dist+_height)*upn;
00163         }
00164         else return 0.0;    //can't jump so high
00165     }
00166 
00167     //finally check if the move is correct or not
00168 
00169     line.setValue(_rFrom, mv);
00170     _act->setLine(line);
00171     _act->apply(_world);
00172 
00173     if (_act->didHit()) {
00174         dist = _act->getHitT();
00175         if (dist <= (rFrom-_rFrom).length()+_fatness+_wallDistance)
00176             return 0.0;     //running against a wall
00177     }
00178 
00179     //move was ok, store new values
00180     _rFrom = rFrom;
00181     _rAt = rAt;
00182     return step;
00183 }
00184 
00188 Real32 WalkNavigator::right(Real32 step)
00189 {
00190 //    Int16 sign = (step >= 0) ? -1 : 1;
00191 //    Real32 angle = 0.19634954f;
00192 //
00193 //    //rotate around the up vector
00194 //    FlyNavigator::rotate(sign*angle, 0);
00195 //    return step;
00196 
00197     Vec3f lv = _rFrom - _rAt;
00198     lv.normalize();
00199 
00200     Vec3f upn = _vUp;
00201     upn.normalize();
00202 
00203     Vec3f mv = lv - upn.dot(lv)*upn;
00204     mv.normalize();
00205 
00206     //side vector symbolizes shoulders
00207     Vec3f sv = mv;
00208     sv.crossThis(upn);
00209     sv.normalize();
00210 
00211     Pnt3f rFrom = _rFrom + step*sv;
00212     Pnt3f rAt = _rAt + step*sv;
00213 
00214     Real32 dist;
00215     Line line(rFrom, -upn);
00216 
00217     //keep the walker at a constant distance from the ground
00218     _act->setLine(line);
00219     _act->apply(_ground);
00220 
00221     if (_act->didHit()) {
00222         dist = _act->getHitT();
00223         if (dist >= _height) {
00224             rFrom = rFrom + (_groundDistance-dist+_height)*upn;
00225             rAt = rAt + (_groundDistance-dist+_height)*upn;
00226         }
00227         else return 0.0;    //can't jump so high
00228     }
00229 
00230     //finally check if the move is correct or not
00231 
00232     line.setValue(_rFrom, sv);
00233     _act->setLine(line);
00234     _act->apply(_world);
00235 
00236     if (_act->didHit()) {
00237         dist = _act->getHitT();
00238         if (dist <= (rFrom-_rFrom).length()+_fatness+_wallDistance)
00239             return 0.0;     //running against a wall
00240     }
00241 
00242     //move was ok, store new values
00243     _rFrom = rFrom;
00244     _rAt = rAt;
00245     return step;
00246 }

Generated on Thu Aug 25 04:12:07 2005 for OpenSG by  doxygen 1.4.3