1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of the OGRE Reference Application, a layer built
|
---|
4 | on top of OGRE(Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #ifndef __REFAPP_APPLICATIONOBJECT_H__
|
---|
26 | #define __REFAPP_APPLICATIONOBJECT_H__
|
---|
27 |
|
---|
28 | #include "OgreRefAppPrerequisites.h"
|
---|
29 |
|
---|
30 | namespace OgreRefApp {
|
---|
31 |
|
---|
32 | /** This object is the base class for all discrete objects in the application.
|
---|
33 | @remarks
|
---|
34 | This object holds a reference to the underlying OGRE entity / entities which
|
---|
35 | comprise it, plus links to the additional properties required to make it
|
---|
36 | work in the application world.
|
---|
37 | @remarks
|
---|
38 | It extends the OGRE UserDefinedObject to allow reverse links from Ogre::Entity.
|
---|
39 | Note that this class does not override the UserDefinedObject's getTypeId method
|
---|
40 | because this class is abstract.
|
---|
41 | */
|
---|
42 | class _OgreRefAppExport ApplicationObject : public UserDefinedObject
|
---|
43 | {
|
---|
44 | protected:
|
---|
45 | // Visual component
|
---|
46 | SceneNode* mSceneNode;
|
---|
47 | Entity* mEntity;
|
---|
48 |
|
---|
49 | /// Dynamics properties, must be set up by subclasses if dynamics enabled
|
---|
50 | dBody* mOdeBody;
|
---|
51 | /// Mass parameters
|
---|
52 | dMass mMass;
|
---|
53 |
|
---|
54 |
|
---|
55 | /// Collision proxies, must be set up if collision enabled
|
---|
56 | typedef std::list<dGeom*> CollisionProxyList;
|
---|
57 | CollisionProxyList mCollisionProxies;
|
---|
58 |
|
---|
59 |
|
---|
60 | bool mDynamicsEnabled;
|
---|
61 | bool mReenableIfInteractedWith;
|
---|
62 | bool mCollisionEnabled;
|
---|
63 |
|
---|
64 | Real mBounceCoeffRestitution;
|
---|
65 | Real mBounceVelocityThreshold;
|
---|
66 | Real mSoftness;
|
---|
67 | Real mFriction;
|
---|
68 | Real mLinearVelDisableThreshold;
|
---|
69 | Real mAngularVelDisableThreshold;
|
---|
70 | Real mDisableTime;
|
---|
71 | Real mDisableTimeEnd;
|
---|
72 |
|
---|
73 | // Set up method, must override
|
---|
74 | virtual void setUp(const String& name) = 0;
|
---|
75 | /** Internal method for updating the state of the collision proxies. */
|
---|
76 | virtual void updateCollisionProxies(void);
|
---|
77 |
|
---|
78 | /** Internal method for testing the plane bounded region WorldFragment type. */
|
---|
79 | virtual bool testCollidePlaneBounds(SceneQuery::WorldFragment* wf);
|
---|
80 |
|
---|
81 | /// Internal method for updating the query mask
|
---|
82 | virtual void setEntityQueryFlags(void);
|
---|
83 |
|
---|
84 | public:
|
---|
85 | ApplicationObject(const String& name);
|
---|
86 | virtual ~ApplicationObject();
|
---|
87 |
|
---|
88 | /** Sets the position of this object. */
|
---|
89 | virtual void setPosition(const Vector3& vec);
|
---|
90 | /** Sets the position of this object. */
|
---|
91 | virtual void setPosition(Real x, Real y, Real z);
|
---|
92 | /** Sets the orientation of this object. */
|
---|
93 | virtual void setOrientation(const Quaternion& orientation);
|
---|
94 | /** Gets the current position of this object. */
|
---|
95 | virtual const Vector3& getPosition(void);
|
---|
96 | /** Gets the current orientation of this object. */
|
---|
97 | virtual const Quaternion& getOrientation(void);
|
---|
98 |
|
---|
99 | /// Updates the position of this game object from the simulation
|
---|
100 | virtual void _updateFromDynamics(void);
|
---|
101 |
|
---|
102 | /// Returns whether or not this object is considered for collision.
|
---|
103 | virtual bool isCollisionEnabled(void);
|
---|
104 | /** Returns whether or not this object is physically simulated.
|
---|
105 | @remarks
|
---|
106 | Objects which are not physically simulated only move when their
|
---|
107 | SceneNode is manually altered.
|
---|
108 | */
|
---|
109 | virtual bool isDynamicsEnabled(void);
|
---|
110 | /** Sets whether or not this object is considered for collision.
|
---|
111 | @remarks
|
---|
112 | Objects which have collision enabled must set up an ODE
|
---|
113 | collision proxy as part of their setUp method.
|
---|
114 | */
|
---|
115 |
|
---|
116 | /** Sets the linear and angular velocity thresholds, below which the
|
---|
117 | object will have it's dynamics automatically disabled for performance.
|
---|
118 | @remarks
|
---|
119 | These thresholds are used to speed up the simulation and to make it more
|
---|
120 | stable, by turning off dynamics for objects that appear to be at rest.
|
---|
121 | Otherwise, objects which are supposedly stationary can jitter when involved
|
---|
122 | in large stacks, and can consume unnecessary CPU time. Note that if another
|
---|
123 | object interacts with the disabled object, it will automatically reenable itself.
|
---|
124 | @par
|
---|
125 | If you never want to disable dynamics automatically for this object, just
|
---|
126 | set all the values to 0.
|
---|
127 | @param linearSq The squared linear velocity magnitude threshold
|
---|
128 | @param angularSq The squared angular velocity magnitude threshold
|
---|
129 | @param overTime The number of seconds over which the values must continue to be under
|
---|
130 | this threshold for the dynamics to be disabled. This is to catch cases
|
---|
131 | where the object almost stops moving because of a boundary condition, but
|
---|
132 | would speed up again later (e.g. box teetering on an edge).
|
---|
133 |
|
---|
134 | */
|
---|
135 | virtual void setDynamicsDisableThreshold(Real linearSq, Real angularSq, Real overTime);
|
---|
136 |
|
---|
137 | virtual void setCollisionEnabled(bool enabled);
|
---|
138 | /** Sets whether or not this object is physically simulated at this time.
|
---|
139 | @remarks
|
---|
140 | Objects which are not physically simulated only move when their
|
---|
141 | SceneNode is manually altered. Objects which are physically
|
---|
142 | simulated must set up an ODE body as part of their setUp method.
|
---|
143 | @par
|
---|
144 | You can also use this to temporarily turn off simulation on an object,
|
---|
145 | such that it is not simulated until some other object which IS simulated
|
---|
146 | comes in contact with it, or is attached to it with a joint.
|
---|
147 | @param enabled Specifies whether dynamics is enabled
|
---|
148 | @param reEnableOnInteraction If set to true, this object will reenable if some
|
---|
149 | other dynamically simulated object interacts with it
|
---|
150 | */
|
---|
151 | virtual void setDynamicsEnabled(bool enabled, bool reEnableOnInteraction = false);
|
---|
152 |
|
---|
153 | /** Sets the 'bounciness' of this object.
|
---|
154 | * @remarks
|
---|
155 | * Only applies if this object has both collision and dynamics enabled.
|
---|
156 | * When 2 movable objects collide, the greatest bounce parameters
|
---|
157 | * from both objects apply, so even a non-bouncy object can
|
---|
158 | * bounce if it hits a bouncy surface.
|
---|
159 | * @param restitutionValue Coeeficient of restitution
|
---|
160 | * (0 for no bounce, 1 for perfect bounciness)
|
---|
161 | * @param velocityThreshold Velocity below which no bounce will occur;
|
---|
162 | * this is a dampening value to ensure small velocities do not
|
---|
163 | * cause bounce.
|
---|
164 | */
|
---|
165 | virtual void setBounceParameters(Real restitutionValue, Real velocityThreshold);
|
---|
166 | /** Gets the cefficient of restitution (bounciness) for this object. */
|
---|
167 | virtual Real getBounceRestitutionValue(void);
|
---|
168 | /** Gets the bounce velocity threshold for this object. */
|
---|
169 | virtual Real getBounceVelocityThreshold(void);
|
---|
170 |
|
---|
171 | /** Sets the softness of this object, which determines how much it is allowed to
|
---|
172 | * penetrate other objects.
|
---|
173 | * @remarks
|
---|
174 | * This parameter only has meaning if collision and dynamics are enabled for this object.
|
---|
175 | * @param softness Softness factor (0 is completely hard). Softness will be combined from
|
---|
176 | * both objects involved in a collision to determine how much they will penetrate.
|
---|
177 | */
|
---|
178 | virtual void setSoftness(Real softness);
|
---|
179 | /** Gets the softness factor of this object. */
|
---|
180 | virtual Real getSoftness(void);
|
---|
181 |
|
---|
182 | /** Sets the Coulomb frictional coefficient for this object.
|
---|
183 | @remarks
|
---|
184 | This coefficient affects how much an object will slip when it comes
|
---|
185 | into contact with another object.
|
---|
186 | @param friction The Coulomb friction coefficient, valid from 0 to Math::POS_INFINITY.
|
---|
187 | 0 means no friction, Math::POS_INFINITY means infinite friction ie no slippage.
|
---|
188 | Note that friction between these 2 bounds is more CPU intensive so use with caution.
|
---|
189 | */
|
---|
190 | virtual void setFriction(Real friction);
|
---|
191 | /** Gets the Coulomb frictional coefficient for this object. */
|
---|
192 | virtual Real getFriction(void);
|
---|
193 | /** Adds a linear force to this object, in object space, at the position indicated.
|
---|
194 | @remarks
|
---|
195 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
196 | @param direction The force direction in object coordinates.
|
---|
197 | @param atPosition The position at which the force is to be applied, in object coordinates.
|
---|
198 | */
|
---|
199 | virtual void addForce(const Vector3& direction, const Vector3& atPosition = Vector3::ZERO);
|
---|
200 | /** Adds a linear force to this object, in object space, at the position indicated.
|
---|
201 | @remarks
|
---|
202 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
203 | @param dir_x, dir_y, dir_z The force direction in object coordinates.
|
---|
204 | @param pos_x, pos_y, pos_z The position at which the force is to be applied, in object coordinates.
|
---|
205 | */
|
---|
206 | virtual void addForce(Real dir_x, Real dir_y, Real dir_z,
|
---|
207 | Real pos_x = 0, Real pos_y = 0, Real pos_z = 0);
|
---|
208 | /** Adds a linear force to this object, in world space, at the position indicated.
|
---|
209 | @remarks
|
---|
210 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
211 | @param direction The force direction in world coordinates.
|
---|
212 | @param atPosition The position at which the force is to be applied, in world coordinates.
|
---|
213 | */
|
---|
214 | virtual void addForceWorldSpace(const Vector3& direction, const Vector3& atPosition = Vector3::ZERO);
|
---|
215 | /** Adds a linear force to this object, in world space, at the position indicated.
|
---|
216 | @remarks
|
---|
217 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
218 | @param dir_x, dir_y, dir_z The force direction in world coordinates.
|
---|
219 | @param pos_x, pos_y, pos_z The position at which the force is to be applied, in world coordinates.
|
---|
220 | */
|
---|
221 | virtual void addForceWorldSpace(Real dir_x, Real dir_y, Real dir_z,
|
---|
222 | Real pos_x, Real pos_y, Real pos_z);
|
---|
223 | /** Adds rotational force to this object, in object space.
|
---|
224 | @remarks
|
---|
225 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
226 | @param direction The direction of the torque to apply, in object space. */
|
---|
227 | virtual void addTorque(const Vector3& direction);
|
---|
228 | /** Adds rotational force to this object, in object space.
|
---|
229 | @remarks
|
---|
230 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
231 | @param x, y, z The direction of the torque to apply, in object space. */
|
---|
232 | virtual void addTorque(Real x, Real y, Real z);
|
---|
233 | /** Adds rotational force to this object, in world space.
|
---|
234 | @remarks
|
---|
235 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
236 | @param direction The direction of the torque to apply, in world space. */
|
---|
237 | virtual void addTorqueWorldSpace(const Vector3& direction);
|
---|
238 | /** Adds rotational force to this object, in world space.
|
---|
239 | @remarks
|
---|
240 | All forces are applied, then reset after World::applyDynamics is called.
|
---|
241 | @param x, y, z The direction of the torque to apply, in world space. */
|
---|
242 | virtual void addTorqueWorldSpace(Real x, Real y, Real z);
|
---|
243 |
|
---|
244 | /** Tests to see if there is a detailed collision between this object and the object passed in.
|
---|
245 | @remarks
|
---|
246 | If there is a collision, both objects will be notified and if dynamics are enabled
|
---|
247 | on these objects, physics will be applied automatically.
|
---|
248 | @returns true if collision occurred
|
---|
249 |
|
---|
250 | */
|
---|
251 | virtual bool testCollide(ApplicationObject* otherObj);
|
---|
252 |
|
---|
253 | /** Tests to see if there is a detailed collision between this object and the
|
---|
254 | world fragment passed in.
|
---|
255 | @remarks
|
---|
256 | If there is a collision, the object will be notified and if dynamics are enabled
|
---|
257 | on this object, physics will be applied automatically.
|
---|
258 | @returns true if collision occurred
|
---|
259 | */
|
---|
260 | virtual bool testCollide(SceneQuery::WorldFragment* wf);
|
---|
261 |
|
---|
262 | /** Contains information about a collision; used in the _notifyCollided call. */
|
---|
263 | struct CollisionInfo
|
---|
264 | {
|
---|
265 | /// The position in world coordinates at which the collision occurred
|
---|
266 | Vector3 position;
|
---|
267 | /// The normal in world coordinates of the collision surface
|
---|
268 | Vector3 normal;
|
---|
269 | /// Penetration depth
|
---|
270 | Real penetrationDepth;
|
---|
271 | };
|
---|
272 | /** This method is called automatically if testCollide indicates a real collision.
|
---|
273 | */
|
---|
274 | virtual void _notifyCollided(ApplicationObject* otherObj, const CollisionInfo& info);
|
---|
275 | /** This method is called automatically if testCollide indicates a real collision.
|
---|
276 | */
|
---|
277 | virtual void _notifyCollided(SceneQuery::WorldFragment* wf, const CollisionInfo& info);
|
---|
278 | /** Gets the SceneNode which is being used to represent this object's position in
|
---|
279 | the OGRE world. */
|
---|
280 | SceneNode* getSceneNode(void);
|
---|
281 | /** Gets the Entity which is being used to represent this object in the OGRE world. */
|
---|
282 | Entity* getEntity(void);
|
---|
283 | /** Gets the ODE body used to represent this object's mass and current velocity. */
|
---|
284 | dBody* getOdeBody(void);
|
---|
285 |
|
---|
286 | /** Set the mass parameters of this object to represent a sphere.
|
---|
287 | @remarks
|
---|
288 | This method sets the mass and inertia properties of this object such
|
---|
289 | that it is like a sphere, ie center of gravity at the origin and
|
---|
290 | an even distribution of mass in all directions.
|
---|
291 | @param density Density of the sphere in Kg/m^3
|
---|
292 | @param radius of the sphere mass
|
---|
293 | */
|
---|
294 | void setMassSphere(Real density, Real radius);
|
---|
295 |
|
---|
296 | /** Set the mass parameters of this object to represent a box.
|
---|
297 | @remarks
|
---|
298 | This method sets the mass and inertia properties of this object such
|
---|
299 | that it is like a box.
|
---|
300 | @param density Density of the box in Kg/m^3
|
---|
301 | @param dimensions Width, height and depth of the box.
|
---|
302 | @param orientation Optional orientation of the box.
|
---|
303 | */
|
---|
304 | void setMassBox(Real density, const Vector3& dimensions,
|
---|
305 | const Quaternion& orientation = Quaternion::IDENTITY);
|
---|
306 |
|
---|
307 | /** Set the mass parameters of this object to represent a capped cylinder.
|
---|
308 | @remarks
|
---|
309 | This method sets the mass and inertia properties of this object such
|
---|
310 | that it is like a capped cylinder, by default lying along the Z-axis.
|
---|
311 | @param density Density of the cylinder in Kg/m^3
|
---|
312 | @param length Length of the cylinder
|
---|
313 | @param width Width of the cylinder
|
---|
314 | @param orientation Optional orientation if you wish the cylinder to lay
|
---|
315 | along a different axis from Z.
|
---|
316 | */
|
---|
317 | void setMassCappedCylinder(Real density, Real length, Real width,
|
---|
318 | const Quaternion& orientation = Quaternion::IDENTITY);
|
---|
319 |
|
---|
320 | /** Sets the mass parameters manually, use only if you know how!
|
---|
321 | @param mass Mass in Kg
|
---|
322 | @param center The center of gravity
|
---|
323 | @param inertia The inertia matrix describing distribution of the mass around the body.
|
---|
324 | */
|
---|
325 | void setMassExpert(Real mass, const Vector3 center, const Matrix3 inertia);
|
---|
326 |
|
---|
327 | /** Gets the ODE mass parameters for this object. */
|
---|
328 | const dMass* getOdeMass(void);
|
---|
329 |
|
---|
330 | /** Sets the current linear velocity of this object.
|
---|
331 | @remarks
|
---|
332 | Only applicable if dynamics are enabled for this object. This method is useful
|
---|
333 | for starting an object off at a particular speed rather than applying forces to get
|
---|
334 | it there.
|
---|
335 | */
|
---|
336 | void setLinearVelocity(const Vector3& vel);
|
---|
337 | /** Sets the current linear velocity of this object.
|
---|
338 | @remarks
|
---|
339 | Only applicable if dynamics are enabled for this object. This method is useful
|
---|
340 | for starting an object off at a particular speed rather than applying forces to get
|
---|
341 | it there.
|
---|
342 | */
|
---|
343 | void setLinearVelocity(Real x, Real y, Real z);
|
---|
344 | /** Gets the current linear velocity of this object.
|
---|
345 | @remarks
|
---|
346 | Only applicable if dynamics are enabled for this object.
|
---|
347 | @returns Vector3 representing the velocity in units per second.
|
---|
348 | */
|
---|
349 | const Vector3& getLinearVelocity(void);
|
---|
350 |
|
---|
351 | /** Gets the current angular velocity of this object.
|
---|
352 | @remarks
|
---|
353 | Only applicable if dynamics are enabled for this object.
|
---|
354 | @returns Vector3 representing the angular velocity in units per second around each axis.
|
---|
355 | */
|
---|
356 | const Vector3& getAngularVelocity(void);
|
---|
357 |
|
---|
358 | /** Sets the current angular velocity of this object.
|
---|
359 | @remarks
|
---|
360 | Only applicable if dynamics are enabled for this object. This method is useful
|
---|
361 | for starting an object off rather than applying forces to get
|
---|
362 | it there.
|
---|
363 | */
|
---|
364 | void setAngularVelocity(const Vector3& vel);
|
---|
365 | /** Sets the current angular velocity of this object.
|
---|
366 | @remarks
|
---|
367 | Only applicable if dynamics are enabled for this object. This method is useful
|
---|
368 | for starting an object off rather than applying forces to get
|
---|
369 | it there.
|
---|
370 | */
|
---|
371 | void setAngularVelocity(Real x, Real y, Real z);
|
---|
372 |
|
---|
373 | /** Moves the object along it's local axes.
|
---|
374 | @par
|
---|
375 | This method moves the object by the supplied vector along the
|
---|
376 | local axes of the obect.
|
---|
377 | @param
|
---|
378 | d Vector with x,y,z values representing the translation.
|
---|
379 | */
|
---|
380 | virtual void translate(const Vector3& d);
|
---|
381 | /** Moves the object along it's local axes.
|
---|
382 | @par
|
---|
383 | This method moves the object by the supplied vector along the
|
---|
384 | local axes of the obect.
|
---|
385 | @param x, y z Real x, y and z values representing the translation.
|
---|
386 | */
|
---|
387 | virtual void translate(Real x, Real y, Real z);
|
---|
388 |
|
---|
389 | /** Moves the object along the world axes.
|
---|
390 | @par
|
---|
391 | This method moves the object by the supplied vector along the
|
---|
392 | world axes.
|
---|
393 | @param
|
---|
394 | d Vector with x,y,z values representing the translation.
|
---|
395 | */
|
---|
396 | virtual void translateWorldSpace(const Vector3& d);
|
---|
397 | /** Moves the object along the world axes.
|
---|
398 | @par
|
---|
399 | This method moves the object by the supplied vector along the
|
---|
400 | local axes of the obect.
|
---|
401 | @param x, y z Real x, y and z values representing the translation.
|
---|
402 | */
|
---|
403 | virtual void translateWorldSpace(Real x, Real y, Real z);
|
---|
404 |
|
---|
405 | /** Rotate the object around the local Z-axis.
|
---|
406 | */
|
---|
407 | virtual void roll(const Radian& angle);
|
---|
408 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
409 | inline void roll(Real angleunits) {
|
---|
410 | roll ( Angle(angleunits) );
|
---|
411 | }
|
---|
412 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
413 |
|
---|
414 | /** Rotate the object around the local X-axis.
|
---|
415 | */
|
---|
416 | virtual void pitch(const Radian& angle);
|
---|
417 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
418 | inline void pitch(Real angleunits) {
|
---|
419 | pitch ( Angle(angleunits) );
|
---|
420 | }
|
---|
421 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
422 |
|
---|
423 | /** Rotate the object around the local Y-axis.
|
---|
424 | */
|
---|
425 | virtual void yaw(const Radian& angle);
|
---|
426 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
427 | inline void yaw(Real angleunits) {
|
---|
428 | yaw ( Angle(angleunits) );
|
---|
429 | }
|
---|
430 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
431 |
|
---|
432 | /** Rotate the object around an arbitrary axis.
|
---|
433 | */
|
---|
434 | virtual void rotate(const Vector3& axis, const Radian& angle);
|
---|
435 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
436 | inline void rotate(const Vector3& axis, Real angleunits) {
|
---|
437 | rotate ( axis, Angle(angleunits) );
|
---|
438 | }
|
---|
439 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
440 |
|
---|
441 | /** Rotate the object around an aritrary axis using a Quarternion.
|
---|
442 | */
|
---|
443 | virtual void rotate(const Quaternion& q);
|
---|
444 |
|
---|
445 |
|
---|
446 | };
|
---|
447 |
|
---|
448 |
|
---|
449 | } // namespace
|
---|
450 |
|
---|
451 | #endif
|
---|
452 |
|
---|
453 |
|
---|