| 1 | /*************************************************************************** | 
|---|
| 2 |  | 
|---|
| 3 | This source file is part of OGREBULLET | 
|---|
| 4 | (Object-oriented Graphics Rendering Engine Bullet Wrapper) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10 | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2007 tuan.kuranes@gmail.com (Use it Freely, even Statically, but have to contribute any changes) | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 12 | the terms of the GPL General Public License with runtime exception as published by the Free Software | 
|---|
| 13 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 14 | version. | 
|---|
| 15 |  | 
|---|
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 18 | FOR A PARTICULAR PURPOSE. See the GPL General Public License with runtime exception for more details. | 
|---|
| 19 |  | 
|---|
| 20 | You should have received a copy of the GPL General Public License with runtime exception along with | 
|---|
| 21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 23 | http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | 
|---|
| 24 | ----------------------------------------------------------------------------- | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 | #include "OgreBulletCollisions.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "OgreBulletCollisionsWorld.h" | 
|---|
| 30 | #include "OgreBulletCollisionsRay.h" | 
|---|
| 31 | #include "Utils/OgreBulletConverter.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "OgreBulletCollisionsObject.h" | 
|---|
| 34 | #include "Debug/OgreBulletCollisionsDebugShape.h" | 
|---|
| 35 |  | 
|---|
| 36 | using namespace Ogre; | 
|---|
| 37 | using namespace OgreBulletCollisions; | 
|---|
| 38 |  | 
|---|
| 39 | namespace OgreBulletCollisions | 
|---|
| 40 | { | 
|---|
| 41 |     // ------------------------------------------------------------------------- | 
|---|
| 42 |         CollisionRayResultCallback::CollisionRayResultCallback(const Ogre::Ray &ray,  | 
|---|
| 43 |                                                         CollisionsWorld *world, Ogre::Real max_distance, | 
|---|
| 44 |                                                         bool init): | 
|---|
| 45 |         mRayResultCallback(0), | 
|---|
| 46 |         mWorld(world), | 
|---|
| 47 |         mRay (ray), | 
|---|
| 48 |                 mMaxDistance(max_distance) | 
|---|
| 49 |     { | 
|---|
| 50 |         if (init) | 
|---|
| 51 |         { | 
|---|
| 52 |             //mRay = new btCollisionWorld::RayResultCallback ( | 
|---|
| 53 |             //    OgreBtConverter::to(ray.getOrigin ()),  | 
|---|
| 54 |             //   OgreBtConverter::to(ray.getDirection ())); | 
|---|
| 55 |         } | 
|---|
| 56 |         } | 
|---|
| 57 |     // ------------------------------------------------------------------------- | 
|---|
| 58 |     CollisionRayResultCallback::~CollisionRayResultCallback() | 
|---|
| 59 |     { | 
|---|
| 60 |         if (mRayResultCallback) | 
|---|
| 61 |         { | 
|---|
| 62 |             delete mRayResultCallback; | 
|---|
| 63 |             mRayResultCallback = 0; | 
|---|
| 64 |         } | 
|---|
| 65 |     } | 
|---|
| 66 |     Ogre::Vector3 CollisionRayResultCallback::getRayStartPoint() const | 
|---|
| 67 |     { | 
|---|
| 68 |       return mRay.getOrigin(); | 
|---|
| 69 |     } | 
|---|
| 70 |     // ------------------------------------------------------------------------- | 
|---|
| 71 |     Ogre::Vector3 CollisionRayResultCallback::getRayEndPoint() const | 
|---|
| 72 |     { | 
|---|
| 73 |       return mRay.getPoint(mMaxDistance); | 
|---|
| 74 |     } | 
|---|
| 75 |     // ------------------------------------------------------------------------- | 
|---|
| 76 |     bool  CollisionRayResultCallback::doesCollide() const | 
|---|
| 77 |     { | 
|---|
| 78 |         return mRayResultCallback->hasHit(); | 
|---|
| 79 |     } | 
|---|
| 80 |     // ------------------------------------------------------------------------- | 
|---|
| 81 |     Object  *CollisionClosestRayResultCallback::getCollidedObject () const | 
|---|
| 82 |     {         | 
|---|
| 83 |         return mWorld->findObject(static_cast<btCollisionWorld::ClosestRayResultCallback *> (mRayResultCallback)->m_collisionObject); | 
|---|
| 84 |         } | 
|---|
| 85 |     // ------------------------------------------------------------------------- | 
|---|
| 86 |         CollisionClosestRayResultCallback::CollisionClosestRayResultCallback(const Ogre::Ray &ray, CollisionsWorld *world, Ogre::Real max_distance) : | 
|---|
| 87 |         CollisionRayResultCallback(ray, world, max_distance, false) | 
|---|
| 88 |     { | 
|---|
| 89 |         mRayResultCallback = new btCollisionWorld::ClosestRayResultCallback ( | 
|---|
| 90 |             OgreBtConverter::to(getRayStartPoint ()),  | 
|---|
| 91 |             OgreBtConverter::to(getRayEndPoint ())); | 
|---|
| 92 |     }  | 
|---|
| 93 |     // ------------------------------------------------------------------------- | 
|---|
| 94 |     Vector3 CollisionClosestRayResultCallback::getCollisionPoint() const | 
|---|
| 95 |     { | 
|---|
| 96 |         return BtOgreConverter::to(getBulletClosestRayResultCallback()->m_hitPointWorld); | 
|---|
| 97 |         } | 
|---|
| 98 |         // ------------------------------------------------------------------------- | 
|---|
| 99 |         Vector3 CollisionClosestRayResultCallback::getCollisionNormal() const | 
|---|
| 100 |         { | 
|---|
| 101 |                 return BtOgreConverter::to(getBulletClosestRayResultCallback()->m_hitNormalWorld); | 
|---|
| 102 |         } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|