PixelLightAPI  .
Sensor.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Sensor.h                                       *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight 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 Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLPHYSICS_SENSOR_H__
00024 #define __PLPHYSICS_SENSOR_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Namespace                                             ]
00030 //[-------------------------------------------------------]
00031 #include <PLMath/Vector3.h>
00032 #include "PLPhysics/Element.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLPhysics {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Forward declarations                                  ]
00043 //[-------------------------------------------------------]
00044 class Body;
00045 
00046 
00047 //[-------------------------------------------------------]
00048 //[ Classes                                               ]
00049 //[-------------------------------------------------------]
00050 /**
00051 *  @brief
00052 *    Abstract PL physics sensor base class
00053 *
00054 *  @remarks
00055 *    Can for instance be used for simple collision detection.
00056 */
00057 class Sensor : public Element {
00058 
00059 
00060     //[-------------------------------------------------------]
00061     //[ Public definitions                                    ]
00062     //[-------------------------------------------------------]
00063     public:
00064         /**
00065         *  @brief
00066         *   Flags
00067         */
00068         enum EFlags {
00069             ClosestBody = 1<<0  /**< A performance hint that we are looking for the closest body */
00070         };
00071 
00072 
00073     //[-------------------------------------------------------]
00074     //[ Public structures                                     ]
00075     //[-------------------------------------------------------]
00076     public:
00077         /**
00078         *  @brief
00079         *    Holds information about a detected body
00080         */
00081         struct BodyInfo {
00082             Body            *pBody;         /**< Detected physics body (always valid!) */
00083             float            fDistance;     /**< Distance (if supported by the sensor) */
00084             PLMath::Vector3  vNormal;       /**< Hit normal (if supported by the sensor) */
00085             int              nCollisionID;  /**< Collision ID (if supported by the sensor) */
00086         };
00087 
00088 
00089     //[-------------------------------------------------------]
00090     //[ Public functions                                      ]
00091     //[-------------------------------------------------------]
00092     public:
00093         /**
00094         *  @brief
00095         *    Destructor
00096         */
00097         PLPHYSICS_API virtual ~Sensor();
00098 
00099         /**
00100         *  @brief
00101         *    Returns the flags
00102         *
00103         *  @return
00104         *    Flags (see EFlags)
00105         */
00106         PLPHYSICS_API PLCore::uint32 GetFlags() const;
00107 
00108         /**
00109         *  @brief
00110         *    Sets the flags
00111         *
00112         *  @param[in] nFlags
00113         *    Flags (see EFlags)
00114         */
00115         PLPHYSICS_API void SetFlags(PLCore::uint32 nFlags = 0);
00116 
00117         /**
00118         *  @brief
00119         *    Clears the list of hit bodies
00120         */
00121         PLPHYSICS_API void ClearHitList();
00122 
00123         /**
00124         *  @brief
00125         *    Returns the number of hit physics bodies
00126         *
00127         *  @return
00128         *    The number of hit physics bodies
00129         */
00130         PLPHYSICS_API PLCore::uint32 GetNumOfHitBodies() const;
00131 
00132         /**
00133         *  @brief
00134         *    Returns a hit physics body by index
00135         *
00136         *  @param[in] nIndex
00137         *    Index of the hit physics body
00138         *
00139         *  @return
00140         *    The hit physics body information, a null pointer on error (do NOT store a pointer on it!)
00141         */
00142         PLPHYSICS_API const BodyInfo *GetHitBody(PLCore::uint32 nIndex) const;
00143 
00144         /**
00145         *  @brief
00146         *    Returns the closest hit physics body
00147         *
00148         *  @return
00149         *    The closest hit physics body information, a null pointer on error (do NOT store a pointer on it!)
00150         */
00151         PLPHYSICS_API const BodyInfo *GetClosestBody() const;
00152 
00153 
00154     //[-------------------------------------------------------]
00155     //[ Public virtual Sensor functions                       ]
00156     //[-------------------------------------------------------]
00157     public:
00158         /**
00159         *  @brief
00160         *    Performs an immediate sensor check
00161         *
00162         *  @return
00163         *    The number of hit physics bodies
00164         */
00165         PLPHYSICS_API virtual PLCore::uint32 Check();
00166 
00167 
00168     //[-------------------------------------------------------]
00169     //[ Public virtual Element functions                      ]
00170     //[-------------------------------------------------------]
00171     public:
00172         PLPHYSICS_API virtual bool IsBody() const override;
00173         PLPHYSICS_API virtual bool IsJoint() const override;
00174         PLPHYSICS_API virtual bool IsSensor() const override;
00175 
00176 
00177     //[-------------------------------------------------------]
00178     //[ Protected functions                                   ]
00179     //[-------------------------------------------------------]
00180     protected:
00181         /**
00182         *  @brief
00183         *    Constructor
00184         *
00185         *  @param[in] cWorld
00186         *    World this sensor is in
00187         *  @param[in] nFlags
00188         *    Flags (see EFlags)
00189         */
00190         PLPHYSICS_API Sensor(World &cWorld, PLCore::uint32 nFlags = 0);
00191 
00192         /**
00193         *  @brief
00194         *    Returns a free body information
00195         *
00196         *  @return
00197         *    Free body information
00198         *
00199         *  @note
00200         *    - Use FreeBodyInfo() if you no longer need this body information
00201         */
00202         PLPHYSICS_API BodyInfo &GetFreeBodyInfo();
00203 
00204         /**
00205         *  @brief
00206         *    Frees a body information
00207         *
00208         *  @param[in] cBodyInfo
00209         *    Body information to free
00210         */
00211         PLPHYSICS_API void FreeBodyInfo(BodyInfo &cBodyInfo);
00212 
00213 
00214     //[-------------------------------------------------------]
00215     //[ Protected virtual Sensor functions                    ]
00216     //[-------------------------------------------------------]
00217     protected:
00218         /**
00219         *  @brief
00220         *    Sensor callback function
00221         *
00222         *  @param[in] cBody
00223         *    Detected physics body
00224         *  @param[in] fDistance
00225         *    Distance
00226         *  @param[in] vNormal
00227         *    Hit normal
00228         *  @param[in] nCollisionID
00229         *    Collision ID
00230         *
00231         *  @return
00232         *    'true' to continue the sensor check, 'false' if stop it right now
00233         *
00234         *  @remarks
00235         *    The default implementation just adds the found body into a result list.
00236         */
00237         PLPHYSICS_API virtual bool Callback(Body &cBody, float fDistance, const PLMath::Vector3 &vNormal, int nCollisionID);
00238 
00239 
00240     //[-------------------------------------------------------]
00241     //[ Private data                                          ]
00242     //[-------------------------------------------------------]
00243     private:
00244         PLCore::Array<BodyInfo*>  m_lstBodyInfo;        /**< List of detected body information */
00245         BodyInfo                 *m_pClosedBodyInfo;    /**< Closest body information, can be a null pointer */
00246         PLCore::uint32            m_nFlags;             /**< Flags (see EFlags) */
00247 
00248 
00249 };
00250 
00251 
00252 //[-------------------------------------------------------]
00253 //[ Namespace                                             ]
00254 //[-------------------------------------------------------]
00255 } // PLPhysics
00256 
00257 
00258 #endif // __PLPHYSICS_SENSOR_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:59
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported