2 * OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20 * Contains code for a sphere collider.
21 * \file OPC_SphereCollider.h
22 * \author Pierre Terdiman
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 #ifndef __OPC_SPHERECOLLIDER_H__
30 #define __OPC_SPHERECOLLIDER_H__
32 struct OPCODE_API SphereCache : VolumeCache
34 SphereCache() : Center(0.0f,0.0f,0.0f), FatRadius2(0.0f), FatCoeff(1.1f) {}
37 // Cached faces signature
38 Point Center; //!< Sphere used when performing the query resulting in cached faces
39 float FatRadius2; //!< Sphere used when performing the query resulting in cached faces
41 float FatCoeff; //!< mRadius2 multiplier used to create a fat sphere
44 class OPCODE_API SphereCollider : public VolumeCollider
47 // Constructor / Destructor
49 virtual ~SphereCollider();
51 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 * Generic collision query for generic OPCODE models. After the call, access the results:
54 * - with GetContactStatus()
55 * - with GetNbTouchedPrimitives()
56 * - with GetTouchedPrimitives()
58 * \param cache [in/out] a sphere cache
59 * \param sphere [in] collision sphere in local space
60 * \param model [in] Opcode model to collide with
61 * \param worlds [in] sphere's world matrix, or null
62 * \param worldm [in] model's world matrix, or null
63 * \return true if success
64 * \warning SCALE NOT SUPPORTED. The matrices must contain rotation & translation parts only.
66 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 bool Collide(SphereCache& cache, const Sphere& sphere, const Model& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
70 bool Collide(SphereCache& cache, const Sphere& sphere, const AABBTree* tree);
72 // Sphere in model space
73 Point mCenter; //!< Sphere center
74 float mRadius2; //!< Sphere radius squared
76 void _Collide(const AABBCollisionNode* node);
77 void _Collide(const AABBNoLeafNode* node);
78 void _Collide(const AABBQuantizedNode* node);
79 void _Collide(const AABBQuantizedNoLeafNode* node);
80 void _Collide(const AABBTreeNode* node);
81 void _CollideNoPrimitiveTest(const AABBCollisionNode* node);
82 void _CollideNoPrimitiveTest(const AABBNoLeafNode* node);
83 void _CollideNoPrimitiveTest(const AABBQuantizedNode* node);
84 void _CollideNoPrimitiveTest(const AABBQuantizedNoLeafNode* node);
86 inline_ BOOL SphereContainsBox(const Point& bc, const Point& be);
87 inline_ BOOL SphereAABBOverlap(const Point& center, const Point& extents);
88 BOOL SphereTriOverlap(const Point& vert0, const Point& vert1, const Point& vert2);
90 BOOL InitQuery(SphereCache& cache, const Sphere& sphere, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
93 class OPCODE_API HybridSphereCollider : public SphereCollider
96 // Constructor / Destructor
97 HybridSphereCollider();
98 virtual ~HybridSphereCollider();
100 bool Collide(SphereCache& cache, const Sphere& sphere, const HybridModel& model, const Matrix4x4* worlds=null, const Matrix4x4* worldm=null);
102 Container mTouchedBoxes;
105 #endif // __OPC_SPHERECOLLIDER_H__