fixed: compile issue
[opensg.git] / Source / System / Window / Utilities / OSGWalkEngine.cpp
blobd936da6b396b05881d03b6fc7e743cbf4faca2b6
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include "OSGConfig.h"
40 #include "OSGBaseTypes.h"
41 #include "OSGMatrix.h"
42 #include "OSGMatrixUtility.h"
44 #include "OSGWalkEngine.h"
45 #include "OSGNavigator.h"
46 #include "OSGNode.h"
47 #include "OSGCamera.h"
48 #include "OSGBackground.h"
50 OSG_USING_NAMESPACE
52 /***************************************************************************\
53 * Description *
54 \***************************************************************************/
56 /*! \class OSG::WalkEngine
57 \ingroup GrpSystemWindowNavigators
59 \dev
61 \enddev
65 WalkEngineTransitPtr
66 WalkEngine::create(void)
68 return WalkEngineTransitPtr(new WalkEngine);
71 /*------------------------------ get --------------------------------------*/
73 /*------------------------------ set --------------------------------------*/
75 void WalkEngine::setGround(Node * const new_ground)
77 _ground=new_ground;
80 void WalkEngine::setWorld(Node * const new_world)
82 _world=new_world;
85 void WalkEngine::setGroundDistance(Real32 groundDistance)
87 _groundDistance=groundDistance;
90 void WalkEngine::setMinWallDistance (Real32 wallDistance)
92 _wallDistance=wallDistance;
95 void WalkEngine::setPersonDimensions(Real32 height,Real32 width,Real32 fatness)
97 _height = height;
98 _width = width;
99 _fatness = fatness;
102 /*---------------------- navigator engine callbacks ------------------------*/
103 void WalkEngine::idle(Int16 buttons, Int16 x, Int16 y, Navigator* nav)
105 if (buttons) moveTo(x, y, nav);
108 void WalkEngine::onViewportChanged(Navigator* nav)
110 Viewport *vp = nav->getViewport();
111 setGround(vp->getRoot());
112 setWorld (vp->getRoot());
115 /*---------------------- Walker Transformations ----------------------------*/
117 /*! makes a rotation
120 void WalkEngine::rotate (Real32 deltaX, Real32 deltaY)
122 Inherited::rotate(deltaX, deltaY);
125 /*! "walks" forward
128 Real32 WalkEngine::forward(Real32 step)
130 Vec3f lv = _rFrom - _rAt;
131 lv.normalize();
133 Vec3f upn = _vUp;
134 upn.normalize();
136 Vec3f mv = lv - upn.dot(lv)*upn;
137 mv.normalize();
139 //side vector symbolizes shoulders
140 Vec3f sv = mv;
141 sv.crossThis(upn);
142 sv.normalize();
144 Pnt3f rFrom = _rFrom + step * mv;
145 Pnt3f rAt = _rAt + step * mv;
147 Real32 dist;
148 Line line(rFrom, -upn);
150 //keep the walker at a constant distance from the ground
151 _act->setLine(line );
152 _act->apply (_ground);
154 if(_act->didHit())
156 dist = _act->getHitT();
157 if(dist >= _height)
159 rFrom = rFrom + (_groundDistance - dist + _height) * upn;
160 rAt = rAt + (_groundDistance - dist + _height) * upn;
162 else return 0.0f; //can't jump so high
165 //finally check if the move is correct or not
167 line.setValue(rFrom, (rFrom - _rFrom));
168 _act->setLine(line );
169 _act->apply (_world);
171 if(_act->didHit())
173 dist = _act->getHitT();
174 if(dist <= _fatness + _wallDistance)
175 return 0.0; //running against a wall
178 //move was ok, store new values
179 _rFrom = rFrom;
180 _rAt = rAt;
182 return step;
185 /*! turns the viewer right or left
188 Real32 WalkEngine::right(Real32 step)
190 // Int16 sign = (step >= 0) ? -1 : 1;
191 // Real32 angle = 0.19634954f;
193 // //rotate around the up vector
194 // FlyNavigator::rotate(sign*angle, 0);
195 // return step;
197 Vec3f lv = _rFrom - _rAt;
198 lv.normalize();
200 Vec3f upn = _vUp;
201 upn.normalize();
203 Vec3f mv = lv - upn.dot(lv)*upn;
204 mv.normalize();
206 //side vector symbolizes shoulders
207 Vec3f sv = mv;
208 sv.crossThis(upn);
209 sv.normalize();
211 Pnt3f rFrom = _rFrom + step * sv;
212 Pnt3f rAt = _rAt + step * sv;
214 Real32 dist;
215 Line line(rFrom, -upn);
217 //keep the walker at a constant distance from the ground
218 _act->setLine(line );
219 _act->apply (_ground);
221 if(_act->didHit())
223 dist = _act->getHitT();
224 if(dist >= _height)
226 rFrom = rFrom + (_groundDistance - dist + _height) * upn;
227 rAt = rAt + (_groundDistance - dist + _height) * upn;
229 else return 0.0; //can't jump so high
232 //finally check if the move is correct or not
234 line.setValue(rFrom, (rFrom - _rFrom));
235 _act->setLine(line );
236 _act->apply (_world);
238 if(_act->didHit())
240 dist = _act->getHitT();
241 if(dist <= _fatness + _wallDistance)
242 return 0.0; //running against a wall
245 //move was ok, store new values
246 _rFrom = rFrom;
247 _rAt = rAt;
249 return step;
252 /*------------------------- constructors ----------------------------------*/
254 WalkEngine::WalkEngine(void) :
255 Inherited(),
256 _ground(NULL),
257 _world(NULL),
258 _groundDistance(0.75f),
259 _wallDistance(0.1f),
260 _height(0.85f),
261 _width(0.5f),
262 _fatness(0.5f),
263 _act(IntersectAction::create())
267 /*-------------------------- destructors ----------------------------------*/
269 WalkEngine::~WalkEngine()
271 _act = NULL;