2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
2022-01-06 22:37:49 +00:00
|
|
|
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btCollisionShape.h"
|
|
|
|
|
|
|
|
#include "btDefaultSoftBodySolver.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btCapsuleShape.h"
|
|
|
|
#include "BulletSoftBody/btSoftBody.h"
|
|
|
|
|
|
|
|
btDefaultSoftBodySolver::btDefaultSoftBodySolver()
|
|
|
|
{
|
|
|
|
// Initial we will clearly need to update solver constants
|
2019-01-03 13:26:51 +00:00
|
|
|
// For now this is global for the cloths linked with this solver - we should probably make this body specific
|
2017-08-01 12:30:58 +00:00
|
|
|
// for performance in future once we understand more clearly when constants need to be updated
|
|
|
|
m_updateSolverConstants = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// In this case the data is already in the soft bodies so there is no need for us to do anything
|
|
|
|
void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btDefaultSoftBodySolver::optimize(btAlignedObjectArray<btSoftBody *> &softBodies, bool forceUpdate)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_softBodySet.copyFromArray(softBodies);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btDefaultSoftBodySolver::updateSoftBodies()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_softBodySet.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btSoftBody *psb = (btSoftBody *)m_softBodySet[i];
|
2017-08-01 12:30:58 +00:00
|
|
|
if (psb->isActive())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
psb->integrateMotion();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
} // updateSoftBodies
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
bool btDefaultSoftBodySolver::checkInitialized()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
void btDefaultSoftBodySolver::solveConstraints(btScalar solverdt)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// Solve constraints for non-solver softbodies
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_softBodySet.size(); ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btSoftBody *psb = static_cast<btSoftBody *>(m_softBodySet[i]);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (psb->isActive())
|
|
|
|
{
|
|
|
|
psb->solveConstraints();
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
} // btDefaultSoftBodySolver::solveConstraints
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// Currently only support CPU output buffers
|
|
|
|
// TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
|
|
|
|
// and use them together on a single kernel call if possible by setting up a
|
|
|
|
// per-cloth target buffer array for the copy kernel.
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
const btAlignedObjectArray<btSoftBody::Node> &clothVertices(softBody->m_nodes);
|
2017-08-01 12:30:58 +00:00
|
|
|
int numVertices = clothVertices.size();
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast<btCPUVertexBufferDescriptor *>(vertexBuffer);
|
|
|
|
float *basePointer = cpuVertexBuffer->getBasePointer();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (vertexBuffer->hasVertexPositions())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const int vertexOffset = cpuVertexBuffer->getVertexOffset();
|
|
|
|
const int vertexStride = cpuVertexBuffer->getVertexStride();
|
|
|
|
float *vertexPointer = basePointer + vertexOffset;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 position = clothVertices[vertexIndex].m_x;
|
|
|
|
*(vertexPointer + 0) = (float)position.getX();
|
|
|
|
*(vertexPointer + 1) = (float)position.getY();
|
|
|
|
*(vertexPointer + 2) = (float)position.getZ();
|
|
|
|
vertexPointer += vertexStride;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (vertexBuffer->hasNormals())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const int normalOffset = cpuVertexBuffer->getNormalOffset();
|
|
|
|
const int normalStride = cpuVertexBuffer->getNormalStride();
|
|
|
|
float *normalPointer = basePointer + normalOffset;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 normal = clothVertices[vertexIndex].m_n;
|
|
|
|
*(normalPointer + 0) = (float)normal.getX();
|
|
|
|
*(normalPointer + 1) = (float)normal.getY();
|
|
|
|
*(normalPointer + 2) = (float)normal.getZ();
|
|
|
|
normalPointer += normalStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
} // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, btSoftBody *otherSoftBody)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
softBody->defaultCollisionHandler(otherSoftBody);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For the default solver just leave the soft body to do its collision processing
|
2019-01-03 13:26:51 +00:00
|
|
|
void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, const btCollisionObjectWrapper *collisionObjectWrap)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
softBody->defaultCollisionHandler(collisionObjectWrap);
|
|
|
|
} // btDefaultSoftBodySolver::processCollision
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2020-01-08 17:05:43 +00:00
|
|
|
void btDefaultSoftBodySolver::predictMotion(btScalar timeStep)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_softBodySet.size(); ++i)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btSoftBody *psb = m_softBodySet[i];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (psb->isActive())
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
psb->predictMotion(timeStep);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|