2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BT_FEATHERSTONE_LINK_COLLIDER_H
|
|
|
|
#define BT_FEATHERSTONE_LINK_COLLIDER_H
|
|
|
|
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
|
|
|
|
|
|
|
|
#include "btMultiBody.h"
|
2018-09-07 16:11:04 +02:00
|
|
|
#include "LinearMath/btSerializer.h"
|
|
|
|
|
|
|
|
#ifdef BT_USE_DOUBLE_PRECISION
|
|
|
|
#define btMultiBodyLinkColliderData btMultiBodyLinkColliderDoubleData
|
|
|
|
#define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderDoubleData"
|
|
|
|
#else
|
|
|
|
#define btMultiBodyLinkColliderData btMultiBodyLinkColliderFloatData
|
|
|
|
#define btMultiBodyLinkColliderDataName "btMultiBodyLinkColliderFloatData"
|
|
|
|
#endif
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
class btMultiBodyLinkCollider : public btCollisionObject
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
//protected:
|
2017-08-01 14:30:58 +02:00
|
|
|
public:
|
|
|
|
btMultiBody* m_multiBody;
|
|
|
|
int m_link;
|
|
|
|
|
2019-06-11 13:18:05 +02:00
|
|
|
virtual ~btMultiBodyLinkCollider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
btMultiBodyLinkCollider(btMultiBody* multiBody, int link)
|
|
|
|
: m_multiBody(multiBody),
|
|
|
|
m_link(link)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_checkCollideWith = true;
|
2017-08-01 14:30:58 +02:00
|
|
|
//we need to remove the 'CF_STATIC_OBJECT' flag, otherwise links/base doesn't merge islands
|
|
|
|
//this means that some constraints might point to bodies that are not in the islands, causing crashes
|
|
|
|
//if (link>=0 || (multiBody && !multiBody->hasFixedBase()))
|
|
|
|
{
|
|
|
|
m_collisionFlags &= (~btCollisionObject::CF_STATIC_OBJECT);
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
//{
|
|
|
|
// m_collisionFlags |= (btCollisionObject::CF_STATIC_OBJECT);
|
|
|
|
//}
|
|
|
|
|
|
|
|
m_internalType = CO_FEATHERSTONE_LINK;
|
|
|
|
}
|
|
|
|
static btMultiBodyLinkCollider* upcast(btCollisionObject* colObj)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
|
2017-08-01 14:30:58 +02:00
|
|
|
return (btMultiBodyLinkCollider*)colObj;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static const btMultiBodyLinkCollider* upcast(const btCollisionObject* colObj)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (colObj->getInternalType() & btCollisionObject::CO_FEATHERSTONE_LINK)
|
2017-08-01 14:30:58 +02:00
|
|
|
return (btMultiBodyLinkCollider*)colObj;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual bool checkCollideWithOverride(const btCollisionObject* co) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const btMultiBodyLinkCollider* other = btMultiBodyLinkCollider::upcast(co);
|
|
|
|
if (!other)
|
|
|
|
return true;
|
|
|
|
if (other->m_multiBody != this->m_multiBody)
|
|
|
|
return true;
|
|
|
|
if (!m_multiBody->hasSelfCollision())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//check if 'link' has collision disabled
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_link >= 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const btMultibodyLink& link = m_multiBody->getLink(this->m_link);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int parent_of_this = m_link;
|
|
|
|
while (1)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (parent_of_this == -1)
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
parent_of_this = m_multiBody->getLink(parent_of_this).m_parent;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (parent_of_this == other->m_link)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
else if (link.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (link.m_parent == other->m_link)
|
2017-08-01 14:30:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (other->m_link >= 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const btMultibodyLink& otherLink = other->m_multiBody->getLink(other->m_link);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int parent_of_other = other->m_link;
|
|
|
|
while (1)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (parent_of_other == -1)
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
parent_of_other = m_multiBody->getLink(parent_of_other).m_parent;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (parent_of_other == this->m_link)
|
2017-08-01 14:30:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
else if (otherLink.m_flags & BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (otherLink.m_parent == this->m_link)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2021-09-29 15:47:08 +02:00
|
|
|
bool isStaticOrKinematic() const
|
|
|
|
{
|
|
|
|
return isStaticOrKinematicObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isKinematic() const
|
|
|
|
{
|
|
|
|
return isKinematicObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDynamicType(int dynamicType)
|
|
|
|
{
|
|
|
|
int oldFlags = getCollisionFlags();
|
|
|
|
oldFlags &= ~(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT);
|
|
|
|
setCollisionFlags(oldFlags | dynamicType);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual int calculateSerializeBufferSize() const;
|
2018-09-07 16:11:04 +02:00
|
|
|
|
|
|
|
///fills the dataBuffer and returns the struct name (and 0 on failure)
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
|
2018-09-07 16:11:04 +02:00
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// clang-format off
|
2018-09-07 16:11:04 +02:00
|
|
|
|
|
|
|
struct btMultiBodyLinkColliderFloatData
|
|
|
|
{
|
|
|
|
btCollisionObjectFloatData m_colObjData;
|
|
|
|
btMultiBodyFloatData *m_multiBody;
|
|
|
|
int m_link;
|
|
|
|
char m_padding[4];
|
2017-08-01 14:30:58 +02:00
|
|
|
};
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
struct btMultiBodyLinkColliderDoubleData
|
|
|
|
{
|
|
|
|
btCollisionObjectDoubleData m_colObjData;
|
|
|
|
btMultiBodyDoubleData *m_multiBody;
|
|
|
|
int m_link;
|
|
|
|
char m_padding[4];
|
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE int btMultiBodyLinkCollider::calculateSerializeBufferSize() const
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
return sizeof(btMultiBodyLinkColliderData);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
SIMD_FORCE_INLINE const char* btMultiBodyLinkCollider::serialize(void* dataBuffer, class btSerializer* serializer) const
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
btMultiBodyLinkColliderData* dataOut = (btMultiBodyLinkColliderData*)dataBuffer;
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionObject::serialize(&dataOut->m_colObjData, serializer);
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
dataOut->m_link = this->m_link;
|
|
|
|
dataOut->m_multiBody = (btMultiBodyData*)serializer->getUniquePointer(m_multiBody);
|
|
|
|
|
|
|
|
// Fill padding with zeros to appease msan.
|
|
|
|
memset(dataOut->m_padding, 0, sizeof(dataOut->m_padding));
|
|
|
|
|
|
|
|
return btMultiBodyLinkColliderDataName;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //BT_FEATHERSTONE_LINK_COLLIDER_H
|