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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btDefaultSoftBodySolver.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 "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
17#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
18#include "BulletCollision/CollisionShapes/btCollisionShape.h"
19
20#include "btDefaultSoftBodySolver.h"
21#include "BulletCollision/CollisionShapes/btCapsuleShape.h"
22#include "BulletSoftBody/btSoftBody.h"
23
24btDefaultSoftBodySolver::btDefaultSoftBodySolver()
25{
26 // Initial we will clearly need to update solver constants
27 // For now this is global for the cloths linked with this solver - we should probably make this body specific
28 // for performance in future once we understand more clearly when constants need to be updated
29 m_updateSolverConstants = true;
30}
31
32btDefaultSoftBodySolver::~btDefaultSoftBodySolver()
33{
34}
35
36// In this case the data is already in the soft bodies so there is no need for us to do anything
37 void btDefaultSoftBodySolver::copyBackToSoftBodies(bool bMove)
38{
39}
40
41 void btDefaultSoftBodySolver::optimize(btAlignedObjectArray<btSoftBody *> &softBodies, bool forceUpdate)
42{
43 m_softBodySet.copyFromArray(softBodies);
44}
45
46 void btDefaultSoftBodySolver::updateSoftBodies()
47{
48 for (int i = 0; i < m_softBodySet.size(); i++)
49 {
50 btSoftBody *psb = (btSoftBody *)m_softBodySet[i];
51 if (psb->isActive())
52 {
53 psb->integrateMotion();
54 }
55 }
56} // updateSoftBodies
57
58 bool btDefaultSoftBodySolver::checkInitialized()
59{
60 return true;
61}
62
63 void btDefaultSoftBodySolver::solveConstraints(btScalar solverdt)
64{
65 // Solve constraints for non-solver softbodies
66 for (int i = 0; i < m_softBodySet.size(); ++i)
67 {
68 btSoftBody *psb = static_cast<btSoftBody *>(m_softBodySet[i]);
69 if (psb->isActive())
70 {
71 psb->solveConstraints();
72 }
73 }
74} // btDefaultSoftBodySolver::solveConstraints
75
76 void btDefaultSoftBodySolver::copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
77{
78 // Currently only support CPU output buffers
79 // TODO: check for DX11 buffers. Take all offsets into the same DX11 buffer
80 // and use them together on a single kernel call if possible by setting up a
81 // per-cloth target buffer array for the copy kernel.
82
83 if (vertexBuffer->getBufferType() == btVertexBufferDescriptor::CPU_BUFFER)
84 {
85 const btAlignedObjectArray<btSoftBody::Node> &clothVertices(softBody->m_nodes);
86 int numVertices = clothVertices.size();
87
88 const btCPUVertexBufferDescriptor *cpuVertexBuffer = static_cast<btCPUVertexBufferDescriptor *>(vertexBuffer);
89 float *basePointer = cpuVertexBuffer->getBasePointer();
90
91 if (vertexBuffer->hasVertexPositions())
92 {
93 const int vertexOffset = cpuVertexBuffer->getVertexOffset();
94 const int vertexStride = cpuVertexBuffer->getVertexStride();
95 float *vertexPointer = basePointer + vertexOffset;
96
97 for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
98 {
99 btVector3 position = clothVertices[vertexIndex].m_x;
100 *(vertexPointer + 0) = (float)position.getX();
101 *(vertexPointer + 1) = (float)position.getY();
102 *(vertexPointer + 2) = (float)position.getZ();
103 vertexPointer += vertexStride;
104 }
105 }
106 if (vertexBuffer->hasNormals())
107 {
108 const int normalOffset = cpuVertexBuffer->getNormalOffset();
109 const int normalStride = cpuVertexBuffer->getNormalStride();
110 float *normalPointer = basePointer + normalOffset;
111
112 for (int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex)
113 {
114 btVector3 normal = clothVertices[vertexIndex].m_n;
115 *(normalPointer + 0) = (float)normal.getX();
116 *(normalPointer + 1) = (float)normal.getY();
117 *(normalPointer + 2) = (float)normal.getZ();
118 normalPointer += normalStride;
119 }
120 }
121 }
122} // btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
123
124 void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, btSoftBody *otherSoftBody)
125{
126 softBody->defaultCollisionHandler(otherSoftBody);
127}
128
129// For the default solver just leave the soft body to do its collision processing
130 void btDefaultSoftBodySolver::processCollision(btSoftBody *softBody, const btCollisionObjectWrapper *collisionObjectWrap)
131{
132 softBody->defaultCollisionHandler(collisionObjectWrap);
133} // btDefaultSoftBodySolver::processCollision
134
135 void btDefaultSoftBodySolver::predictMotion(btScalar timeStep)
136{
137 for (int i = 0; i < m_softBodySet.size(); ++i)
138 {
139 btSoftBody *psb = m_softBodySet[i];
140
141 if (psb->isActive())
142 {
143 psb->predictMotion(timeStep);
144 }
145 }
146}
btCapsuleShape.h
btCollisionObject.h
btCollisionShape.h
btDefaultSoftBodySolver.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
btSoftBody.h
btTriangleIndexVertexArray.h
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition btAlignedObjectArray.h:46
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition btAlignedObjectArray.h:142
btAlignedObjectArray::copyFromArray
void copyFromArray(const btAlignedObjectArray &otherArray)
Definition btAlignedObjectArray.h:496
btCPUVertexBufferDescriptor
Definition btSoftBodySolverVertexBuffer.h:103
btCPUVertexBufferDescriptor::getBasePointer
virtual float * getBasePointer() const
Return the base pointer in memory to the first vertex.
Definition btSoftBodySolverVertexBuffer.h:154
btCollisionObject::isActive
bool isActive() const
Definition btCollisionObject.h:306
btDefaultSoftBodySolver::~btDefaultSoftBodySolver
virtual ~btDefaultSoftBodySolver()
Definition btDefaultSoftBodySolver.cpp:32
btDefaultSoftBodySolver::btDefaultSoftBodySolver
btDefaultSoftBodySolver()
Definition btDefaultSoftBodySolver.cpp:24
btDefaultSoftBodySolver::updateSoftBodies
virtual void updateSoftBodies()
Perform necessary per-step updates of soft bodies such as recomputing normals and bounding boxes.
Definition btDefaultSoftBodySolver.cpp:46
btDefaultSoftBodySolver::predictMotion
virtual void predictMotion(btScalar solverdt)
Predict motion of soft bodies into next timestep.
Definition btDefaultSoftBodySolver.cpp:135
btDefaultSoftBodySolver::m_softBodySet
btAlignedObjectArray< btSoftBody * > m_softBodySet
Definition btDefaultSoftBodySolver.h:29
btDefaultSoftBodySolver::processCollision
virtual void processCollision(btSoftBody *, const btCollisionObjectWrapper *)
Definition btDefaultSoftBodySolver.cpp:130
btDefaultSoftBodySolver::optimize
virtual void optimize(btAlignedObjectArray< btSoftBody * > &softBodies, bool forceUpdate=false)
Optimize soft bodies in this solver.
Definition btDefaultSoftBodySolver.cpp:41
btDefaultSoftBodySolver::copySoftBodyToVertexBuffer
virtual void copySoftBodyToVertexBuffer(const btSoftBody *const softBody, btVertexBufferDescriptor *vertexBuffer)
Definition btDefaultSoftBodySolver.cpp:76
btDefaultSoftBodySolver::solveConstraints
virtual void solveConstraints(btScalar solverdt)
Solve constraints for a set of soft bodies.
Definition btDefaultSoftBodySolver.cpp:63
btDefaultSoftBodySolver::m_updateSolverConstants
bool m_updateSolverConstants
Variable to define whether we need to update solver constants on the next iteration.
Definition btDefaultSoftBodySolver.h:27
btDefaultSoftBodySolver::copyBackToSoftBodies
virtual void copyBackToSoftBodies(bool bMove=true)
Copy necessary data back to the original soft body source objects.
Definition btDefaultSoftBodySolver.cpp:37
btDefaultSoftBodySolver::checkInitialized
virtual bool checkInitialized()
Ensure that this solver is initialized.
Definition btDefaultSoftBodySolver.cpp:58
btSoftBody
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
Definition btSoftBody.h:75
btSoftBody::integrateMotion
void integrateMotion()
Definition btSoftBody.cpp:2348
btSoftBody::defaultCollisionHandler
void defaultCollisionHandler(const btCollisionObjectWrapper *pcoWrap)
Definition btSoftBody.cpp:4085
btSoftBody::predictMotion
void predictMotion(btScalar dt)
Definition btSoftBody.cpp:2099
btSoftBody::solveConstraints
void solveConstraints()
Definition btSoftBody.cpp:2208
btSoftBody::m_nodes
tNodeArray m_nodes
Definition btSoftBody.h:814
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition btVector3.h:565
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition btVector3.h:563
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition btVector3.h:561
btVertexBufferDescriptor
Definition btSoftBodySolverVertexBuffer.h:20
btVertexBufferDescriptor::getBufferType
virtual BufferTypes getBufferType() const =0
Return the type of the vertex buffer descriptor.
btVertexBufferDescriptor::hasVertexPositions
virtual bool hasVertexPositions() const
Definition btSoftBodySolverVertexBuffer.h:54
btVertexBufferDescriptor::getNormalOffset
virtual int getNormalOffset() const
Return the vertex offset in floats from the base pointer.
Definition btSoftBodySolverVertexBuffer.h:88
btVertexBufferDescriptor::getVertexOffset
virtual int getVertexOffset() const
Return the vertex offset in floats from the base pointer.
Definition btSoftBodySolverVertexBuffer.h:72
btVertexBufferDescriptor::hasNormals
virtual bool hasNormals() const
Definition btSoftBodySolverVertexBuffer.h:59
btVertexBufferDescriptor::getNormalStride
virtual int getNormalStride() const
Return the vertex stride in number of floats between vertices.
Definition btSoftBodySolverVertexBuffer.h:96
btVertexBufferDescriptor::CPU_BUFFER
@ CPU_BUFFER
Definition btSoftBodySolverVertexBuffer.h:24
btVertexBufferDescriptor::getVertexStride
virtual int getVertexStride() const
Return the vertex stride in number of floats between vertices.
Definition btSoftBodySolverVertexBuffer.h:80
btCollisionObjectWrapper
Definition btCollisionObjectWrapper.h:18