Bullet Collision Detection & Physics Library: btConvexPlaneCollisionAlgorithm.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btConvexPlaneCollisionAlgorithm.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btConvexPlaneCollisionAlgorithm.h"
17
18#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
19#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
20#include "BulletCollision/CollisionShapes/btConvexShape.h"
21#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
22#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
23
24//#include <stdio.h>
25
26btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm(btPersistentManifold* mf, const btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* col0Wrap, const btCollisionObjectWrapper* col1Wrap, bool isSwapped, int numPerturbationIterations, int minimumPointsPerturbationThreshold)
27 : btCollisionAlgorithm(ci),
28 m_ownManifold(false),
29 m_manifoldPtr(mf),
30 m_isSwapped(isSwapped),
31 m_numPerturbationIterations(numPerturbationIterations),
32 m_minimumPointsPerturbationThreshold(minimumPointsPerturbationThreshold)
33{
34 const btCollisionObjectWrapper* convexObjWrap = m_isSwapped ? col1Wrap : col0Wrap;
35 const btCollisionObjectWrapper* planeObjWrap = m_isSwapped ? col0Wrap : col1Wrap;
36
37 if (!m_manifoldPtr && m_dispatcher->needsCollision(convexObjWrap->getCollisionObject(), planeObjWrap->getCollisionObject()))
38 {
39 m_manifoldPtr = m_dispatcher->getNewManifold(convexObjWrap->getCollisionObject(), planeObjWrap->getCollisionObject());
40 m_ownManifold = true;
41 }
42}
43
44btConvexPlaneCollisionAlgorithm::~btConvexPlaneCollisionAlgorithm()
45{
46 if (m_ownManifold)
47 {
48 if (m_manifoldPtr)
49 m_dispatcher->releaseManifold(m_manifoldPtr);
50 }
51}
52
53 void btConvexPlaneCollisionAlgorithm::collideSingleContact(const btQuaternion& perturbeRot, const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
54{
55 const btCollisionObjectWrapper* convexObjWrap = m_isSwapped ? body1Wrap : body0Wrap;
56 const btCollisionObjectWrapper* planeObjWrap = m_isSwapped ? body0Wrap : body1Wrap;
57
58 btConvexShape* convexShape = (btConvexShape*)convexObjWrap->getCollisionShape();
59 btStaticPlaneShape* planeShape = (btStaticPlaneShape*)planeObjWrap->getCollisionShape();
60
61 bool hasCollision = false;
62 const btVector3& planeNormal = planeShape->getPlaneNormal();
63 const btScalar& planeConstant = planeShape->getPlaneConstant();
64
65 btTransform convexWorldTransform = convexObjWrap->getWorldTransform();
66 btTransform convexInPlaneTrans;
67 convexInPlaneTrans = planeObjWrap->getWorldTransform().inverse() * convexWorldTransform;
68 //now perturbe the convex-world transform
69 convexWorldTransform.getBasis() *= btMatrix3x3(perturbeRot);
70 btTransform planeInConvex;
71 planeInConvex = convexWorldTransform.inverse() * planeObjWrap->getWorldTransform();
72
73 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
74
75 btVector3 vtxInPlane = convexInPlaneTrans(vtx);
76 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
77
78 btVector3 vtxInPlaneProjected = vtxInPlane - distance * planeNormal;
79 btVector3 vtxInPlaneWorld = planeObjWrap->getWorldTransform() * vtxInPlaneProjected;
80
81 hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold();
82 resultOut->setPersistentManifold(m_manifoldPtr);
83 if (hasCollision)
84 {
86 btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
87 btVector3 pOnB = vtxInPlaneWorld;
88 resultOut->addContactPoint(normalOnSurfaceB, pOnB, distance);
89 }
90}
91
92 void btConvexPlaneCollisionAlgorithm::processCollision(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
93{
94 (void)dispatchInfo;
95 if (!m_manifoldPtr)
96 return;
97
98 const btCollisionObjectWrapper* convexObjWrap = m_isSwapped ? body1Wrap : body0Wrap;
99 const btCollisionObjectWrapper* planeObjWrap = m_isSwapped ? body0Wrap : body1Wrap;
100
101 btConvexShape* convexShape = (btConvexShape*)convexObjWrap->getCollisionShape();
102 btStaticPlaneShape* planeShape = (btStaticPlaneShape*)planeObjWrap->getCollisionShape();
103
104 bool hasCollision = false;
105 const btVector3& planeNormal = planeShape->getPlaneNormal();
106 const btScalar& planeConstant = planeShape->getPlaneConstant();
107 btTransform planeInConvex;
108 planeInConvex = convexObjWrap->getWorldTransform().inverse() * planeObjWrap->getWorldTransform();
109 btTransform convexInPlaneTrans;
110 convexInPlaneTrans = planeObjWrap->getWorldTransform().inverse() * convexObjWrap->getWorldTransform();
111
112 btVector3 vtx = convexShape->localGetSupportingVertex(planeInConvex.getBasis() * -planeNormal);
113 btVector3 vtxInPlane = convexInPlaneTrans(vtx);
114 btScalar distance = (planeNormal.dot(vtxInPlane) - planeConstant);
115
116 btVector3 vtxInPlaneProjected = vtxInPlane - distance * planeNormal;
117 btVector3 vtxInPlaneWorld = planeObjWrap->getWorldTransform() * vtxInPlaneProjected;
118
119 hasCollision = distance < m_manifoldPtr->getContactBreakingThreshold()+ resultOut->m_closestPointDistanceThreshold;
120 resultOut->setPersistentManifold(m_manifoldPtr);
121 if (hasCollision)
122 {
124 btVector3 normalOnSurfaceB = planeObjWrap->getWorldTransform().getBasis() * planeNormal;
125 btVector3 pOnB = vtxInPlaneWorld;
126 resultOut->addContactPoint(normalOnSurfaceB, pOnB, distance);
127 }
128
129 //the perturbation algorithm doesn't work well with implicit surfaces such as spheres, cylinder and cones:
130 //they keep on rolling forever because of the additional off-center contact points
131 //so only enable the feature for polyhedral shapes (btBoxShape, btConvexHullShape etc)
132 if (convexShape->isPolyhedral() && resultOut->getPersistentManifold()->getNumContacts() < m_minimumPointsPerturbationThreshold)
133 {
134 btVector3 v0, v1;
135 btPlaneSpace1(planeNormal, v0, v1);
136 //now perform 'm_numPerturbationIterations' collision queries with the perturbated collision objects
137
138 const btScalar angleLimit = 0.125f * SIMD_PI;
139 btScalar perturbeAngle;
140 btScalar radius = convexShape->getAngularMotionDisc();
141 perturbeAngle = gContactBreakingThreshold / radius;
142 if (perturbeAngle > angleLimit)
143 perturbeAngle = angleLimit;
144
145 btQuaternion perturbeRot(v0, perturbeAngle);
146 for (int i = 0; i < m_numPerturbationIterations; i++)
147 {
148 btScalar iterationAngle = i * (SIMD_2_PI / btScalar(m_numPerturbationIterations));
149 btQuaternion rotq(planeNormal, iterationAngle);
150 collideSingleContact(rotq.inverse() * perturbeRot * rotq, body0Wrap, body1Wrap, dispatchInfo, resultOut);
151 }
152 }
153
154 if (m_ownManifold)
155 {
156 if (m_manifoldPtr->getNumContacts())
157 {
158 resultOut->refreshContactPoints();
159 }
160 }
161}
162
163 btScalar btConvexPlaneCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0, btCollisionObject* col1, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
164{
165 (void)resultOut;
166 (void)dispatchInfo;
167 (void)col0;
168 (void)col1;
169
170 //not yet
171 return btScalar(1.);
172}
btCollisionDispatcher.h
btCollisionObjectWrapper.h
btCollisionObject.h
gContactBreakingThreshold
btScalar gContactBreakingThreshold
Definition: btPersistentManifold.cpp:26
btConvexPlaneCollisionAlgorithm.h
btConvexShape.h
SIMD_PI
#define SIMD_PI
Definition: btScalar.h:526
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_2_PI
#define SIMD_2_PI
Definition: btScalar.h:527
btStaticPlaneShape.h
btPlaneSpace1
void btPlaneSpace1(const T &n, T &p, T &q)
Definition: btVector3.h:1251
btCollisionAlgorithm
btCollisionAlgorithm is an collision interface that is compatible with the Broadphase and btDispatche...
Definition: btCollisionAlgorithm.h:54
btCollisionAlgorithm::m_dispatcher
btDispatcher * m_dispatcher
Definition: btCollisionAlgorithm.h:56
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:51
btCollisionShape::getAngularMotionDisc
virtual btScalar getAngularMotionDisc() const
getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of...
Definition: btCollisionShape.cpp:47
btCollisionShape::isPolyhedral
bool isPolyhedral() const
Definition: btCollisionShape.h:60
btConvexPlaneCollisionAlgorithm::~btConvexPlaneCollisionAlgorithm
virtual ~btConvexPlaneCollisionAlgorithm()
Definition: btConvexPlaneCollisionAlgorithm.cpp:44
btConvexPlaneCollisionAlgorithm::m_isSwapped
bool m_isSwapped
Definition: btConvexPlaneCollisionAlgorithm.h:33
btConvexPlaneCollisionAlgorithm::m_numPerturbationIterations
int m_numPerturbationIterations
Definition: btConvexPlaneCollisionAlgorithm.h:34
btConvexPlaneCollisionAlgorithm::m_minimumPointsPerturbationThreshold
int m_minimumPointsPerturbationThreshold
Definition: btConvexPlaneCollisionAlgorithm.h:35
btConvexPlaneCollisionAlgorithm::btConvexPlaneCollisionAlgorithm
btConvexPlaneCollisionAlgorithm(btPersistentManifold *mf, const btCollisionAlgorithmConstructionInfo &ci, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, bool isSwapped, int numPerturbationIterations, int minimumPointsPerturbationThreshold)
Definition: btConvexPlaneCollisionAlgorithm.cpp:26
btConvexPlaneCollisionAlgorithm::collideSingleContact
void collideSingleContact(const btQuaternion &perturbeRot, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
Definition: btConvexPlaneCollisionAlgorithm.cpp:53
btConvexPlaneCollisionAlgorithm::m_manifoldPtr
btPersistentManifold * m_manifoldPtr
Definition: btConvexPlaneCollisionAlgorithm.h:32
btConvexPlaneCollisionAlgorithm::processCollision
virtual void processCollision(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
Definition: btConvexPlaneCollisionAlgorithm.cpp:92
btConvexPlaneCollisionAlgorithm::calculateTimeOfImpact
virtual btScalar calculateTimeOfImpact(btCollisionObject *body0, btCollisionObject *body1, const btDispatcherInfo &dispatchInfo, btManifoldResult *resultOut)
Definition: btConvexPlaneCollisionAlgorithm.cpp:163
btConvexPlaneCollisionAlgorithm::m_ownManifold
bool m_ownManifold
Definition: btConvexPlaneCollisionAlgorithm.h:31
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:33
btConvexShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
btDispatcher::releaseManifold
virtual void releaseManifold(btPersistentManifold *manifold)=0
btDispatcher::needsCollision
virtual bool needsCollision(const btCollisionObject *body0, const btCollisionObject *body1)=0
btDispatcher::getNewManifold
virtual btPersistentManifold * getNewManifold(const btCollisionObject *b0, const btCollisionObject *b1)=0
btManifoldResult
btManifoldResult is a helper class to manage contact results.
Definition: btManifoldResult.h:48
btManifoldResult::getPersistentManifold
const btPersistentManifold * getPersistentManifold() const
Definition: btManifoldResult.h:82
btManifoldResult::setPersistentManifold
void setPersistentManifold(btPersistentManifold *manifoldPtr)
Definition: btManifoldResult.h:77
btManifoldResult::m_closestPointDistanceThreshold
btScalar m_closestPointDistanceThreshold
Definition: btManifoldResult.h:152
btManifoldResult::refreshContactPoints
void refreshContactPoints()
Definition: btManifoldResult.h:105
btManifoldResult::addContactPoint
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
Definition: btManifoldResult.cpp:103
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:50
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:65
btPersistentManifold::getNumContacts
int getNumContacts() const
Definition: btPersistentManifold.h:120
btPersistentManifold::getContactBreakingThreshold
btScalar getContactBreakingThreshold() const
Definition: btPersistentManifold.cpp:242
btQuaternion
The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatr...
Definition: btQuaternion.h:50
btStaticPlaneShape
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane.
Definition: btStaticPlaneShape.h:24
btStaticPlaneShape::getPlaneConstant
const btScalar & getPlaneConstant() const
Definition: btStaticPlaneShape.h:54
btStaticPlaneShape::getPlaneNormal
const btVector3 & getPlaneNormal() const
Definition: btStaticPlaneShape.h:49
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btTransform::inverse
btTransform inverse() const
Return the inverse of this transform.
Definition: btTransform.h:183
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:109
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btCollisionAlgorithmConstructionInfo
Definition: btCollisionAlgorithm.h:33
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:18
btCollisionObjectWrapper::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObjectWrapper.h:46
btCollisionObjectWrapper::getCollisionObject
const btCollisionObject * getCollisionObject() const
Definition: btCollisionObjectWrapper.h:45
btCollisionObjectWrapper::getWorldTransform
const btTransform & getWorldTransform() const
Definition: btCollisionObjectWrapper.h:44
btDispatcherInfo
Definition: btDispatcher.h:31