diff --git a/Demos/CMakeLists.txt b/Demos/CMakeLists.txt index e50595d89..c8b6d5fae 100644 --- a/Demos/CMakeLists.txt +++ b/Demos/CMakeLists.txt @@ -13,7 +13,7 @@ IF (USE_GLUT) OpenGL AllBulletDemos ConvexDecompositionDemo CcdPhysicsDemo ConstraintDemo SliderConstraintDemo GenericJointDemo Raytracer RagdollDemo ForkLiftDemo BasicDemo VoronoiFractureDemo FractureDemo Box2dDemo BspDemo MovingConcaveDemo VehicleDemo - UserCollisionAlgorithm CharacterDemo SoftDemo HeightFieldFluidDemo + UserCollisionAlgorithm CharacterDemo SoftDemo CollisionInterfaceDemo ConcaveConvexcastDemo SimplexDemo DynamicControlDemo ConvexHullDistance DoublePrecisionDemo ConcaveDemo CollisionDemo diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.cpp deleted file mode 100644 index 77976d003..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.cpp +++ /dev/null @@ -1,1045 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include -#include "btHfFluid.h" -#include "btHfFluidCollisionShape.h" -#include "btHfFluidBuoyantConvexShape.h" -#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" -#include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h" -#include "BulletCollision/CollisionDispatch/btCollisionWorld.h" -#include "BulletCollision/CollisionDispatch/btManifoldResult.h" -#include "BulletCollision/CollisionShapes/btTriangleShape.h" -#include "BulletDynamics/Dynamics/btRigidBody.h" -#include "../../OpenGL/GLDebugDrawer.h" - -btHfFluid::btHfFluid (btScalar gridCellWidth, int numNodesWidth, int numNodesLength) -{ - m_eta = NULL; - m_temp = NULL; - m_ground = NULL; - m_height[0] = NULL; - m_height[1] = NULL; - m_u[0] = NULL; - m_u[1] = NULL; - m_v[0] = NULL; - m_v[1] = NULL; - m_r[0] = NULL; - m_r[1] = NULL; - m_flags = NULL; - m_fillRatio = NULL; - m_internalType = CO_HF_FLUID; - m_heightIndex = 0; - m_velocityIndex = 0; - m_rIndex = 0; - setGridDimensions (gridCellWidth, numNodesWidth, numNodesLength); - - btScalar maxHeight = 20.0; - m_aabbMin = btVector3 (0.0, 0.0, 0.0); - m_aabbMax = btVector3 (m_gridWidth, maxHeight, m_gridLength); - - setCollisionShape (new btHfFluidCollisionShape (this)); - - m_globalVelocityU = btScalar(0.0f); - m_globalVelocityV = btScalar(0.0f); - m_gravity = btScalar(-10.0f); - - m_volumeDisplacementScale = btScalar(0.5f); - m_horizontalVelocityScale = btScalar(0.5f); - - m_epsEta = btScalar(0.01f); - m_epsHeight = btScalar(0.001f); -} - -btHfFluid::~btHfFluid () -{ - btCollisionShape* collisionShape = getCollisionShape (); - delete collisionShape; - btAlignedFree (m_temp); - btAlignedFree (m_height[0]); - btAlignedFree (m_height[1]); - btAlignedFree (m_ground); - btAlignedFree (m_eta); - btAlignedFree (m_u[0]); - btAlignedFree (m_u[1]); - btAlignedFree (m_v[0]); - btAlignedFree (m_v[1]); - btAlignedFree (m_flags); - btAlignedFree (m_fillRatio); -} - -void btHfFluid::predictMotion(btScalar dt) -{ - transferDisplaced (dt); - - advectEta (dt); - advectVelocityU (dt); - advectVelocityV (dt); - updateHeight (dt); - computeFlagsAndFillRatio (); - updateVelocity (dt); - setReflectBoundaryLeft (); - setReflectBoundaryRight (); - setReflectBoundaryTop (); - setReflectBoundaryBottom (); - debugTests(); - //m_heightIndex = (m_heightIndex + 1) % 2; - //m_velocityIndex = (m_velocityIndex + 1) % 2; -} - -void btHfFluid::prep () -{ - for (int i = 0; i < m_numNodesLength*m_numNodesWidth;i++) - { - m_height[0][i] = m_eta[i] + m_ground[i]; - m_height[1][i] = m_eta[i] + m_ground[i]; - } - computeFlagsAndFillRatio (); -} - -btScalar btHfFluid::widthPos (int i) const -{ - return m_gridCellWidth * i; -} - -btScalar btHfFluid::lengthPos (int j) const -{ -return m_gridCellWidth * j; -} - -int btHfFluid::arrayIndex (int i, int j) const -{ - btAssert (i >= 0); - btAssert (i < m_numNodesWidth); - btAssert (j >= 0); - btAssert (j < m_numNodesLength); - int index = i + (j * m_numNodesWidth); - return index; -} - -int btHfFluid::arrayIndex (btScalar i, btScalar j) const -{ - int ii = (int)i; // truncate / floor - int ij = (int)j; // truncate / floor - - return arrayIndex (ii, ij); -} - -const btScalar* btHfFluid::getHeightArray () const -{ - return m_height[m_heightIndex]; -} - -const btScalar* btHfFluid::getGroundArray () const -{ - return m_ground; -} -const btScalar* btHfFluid::getEtaArray () const -{ - return m_eta; -} -const btScalar* btHfFluid::getVelocityUArray () const -{ - return m_u[m_velocityIndex]; -} -const btScalar* btHfFluid::getVelocityVArray () const -{ - return m_v[m_velocityIndex]; -} - -const bool* btHfFluid::getFlagsArray () const -{ - return m_flags; -} - - btScalar* btHfFluid::getHeightArray () -{ - return m_height[m_heightIndex]; -} - btScalar* btHfFluid::getGroundArray () -{ - return m_ground; -} - btScalar* btHfFluid::getEtaArray () -{ - return m_eta; -} - btScalar* btHfFluid::getVelocityUArray () -{ - return m_u[m_velocityIndex]; -} - btScalar* btHfFluid::getVelocityVArray () -{ - return m_v[m_velocityIndex]; -} - -bool* btHfFluid::getFlagsArray () -{ - return m_flags; -} - -void btHfFluid::setFluidHeight (int x, int y, btScalar height) -{ - int index = arrayIndex (x,y); - setFluidHeight (index, height); -} - -void btHfFluid::setFluidHeight (int index, btScalar height) -{ - m_eta[index] = height; - m_height[m_heightIndex][index] = m_ground[index] + m_eta[index]; - m_flags[index] = true; -} - -void btHfFluid::addFluidHeight (int x, int y, btScalar height) -{ - int index = arrayIndex (x,y); - m_eta[index] += height; - m_height[m_heightIndex][index] = m_ground[index] + m_eta[index]; - m_flags[index] = true; -} - -void btHfFluid::getAabbForColumn (int i, int j, btVector3& aabbMin, btVector3& aabbMax) -{ - btVector3 com = getWorldTransform().getOrigin(); - int sw = arrayIndex (i, j); - int se = arrayIndex (i+1, j); - int nw = arrayIndex (i, j+1); - int ne = arrayIndex (i+1, j+1); - - btScalar h = m_height[m_heightIndex][sw]; - btScalar g = m_ground[sw]; - - aabbMin = btVector3(widthPos (i), g, lengthPos (j)); - aabbMax = btVector3(widthPos (i+1), h, lengthPos (j+1)); - aabbMin += com; - aabbMax += com; -} - -void btHfFluid::foreachGroundTriangle(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) -{ - btVector3 verts[3]; - - btScalar minX, minZ, maxX, maxZ; - int startNodeX, startNodeZ, endNodeX, endNodeZ; - - minX = aabbMin.getX(); - minZ = aabbMin.getZ(); - maxX = aabbMax.getX(); - maxZ = aabbMax.getZ(); - - startNodeX = (int)(minX * m_gridCellWidthInv); - startNodeZ = (int)(minZ * m_gridCellWidthInv); - - endNodeX = (int)(maxX * m_gridCellWidthInv); - endNodeZ = (int)(maxZ * m_gridCellWidthInv); - - endNodeX++; - endNodeZ++; - - startNodeX = btMax (1, startNodeX); - startNodeZ = btMax (1, startNodeZ); - endNodeX = btMin (m_numNodesWidth-1, endNodeX); - endNodeZ = btMin (m_numNodesLength-1, endNodeZ); - -#ifdef __BRUTE__ - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - // triangle 1 - verts[0] = btVector3(widthPos(i), m_ground[arrayIndex(i,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_ground[arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j)], lengthPos(j)); - callback->processTriangle(verts,i,j); - // triangle 2 - verts[0] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_ground[arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j+1)], lengthPos(j+1)); - callback->processTriangle(verts,i,j); - } - } -#else - - for (int i = startNodeX; i < endNodeX; i++) - { - for (int j = startNodeZ; j < endNodeZ; j++) - { - // triangle 1 - verts[0] = btVector3(widthPos(i), m_ground[arrayIndex(i,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_ground[arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j)], lengthPos(j)); - callback->processTriangle(verts,i,j); - // triangle 2 - verts[0] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_ground[arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_ground[arrayIndex(i+1,j+1)], lengthPos(j+1)); - callback->processTriangle(verts,i,j); - } - } -#endif -} - -void btHfFluid::foreachFluidColumn (btHfFluidColumnCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) -{ - btScalar minX, minZ, maxX, maxZ; - int startNodeX, startNodeZ, endNodeX, endNodeZ; - - minX = aabbMin.getX(); - minZ = aabbMin.getZ(); - maxX = aabbMax.getX(); - maxZ = aabbMax.getZ(); - - startNodeX = (int)(minX * m_gridCellWidthInv); - startNodeZ = (int)(minZ * m_gridCellWidthInv); - - endNodeX = (int)(maxX * m_gridCellWidthInv); - endNodeZ = (int)(maxZ * m_gridCellWidthInv); - - endNodeX++; - endNodeZ++; - - startNodeX = btMax (1, startNodeX); - startNodeZ = btMax (1, startNodeZ); - endNodeX = btMin (m_numNodesWidth-2, endNodeX); - endNodeZ = btMin (m_numNodesLength-2, endNodeZ); - - bool r; - for (int i = startNodeX; i < endNodeX; i++) - { - for (int j = startNodeZ; j < endNodeZ; j++) - { - if (m_flags[arrayIndex (i, j)] == false) - continue; - - r = callback->processColumn (this, i, j); - if (!r) - return; - } - } -} - -void btHfFluid::foreachSurfaceTriangle (btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) -{ - btVector3 verts[3]; - - btScalar minX, minZ, maxX, maxZ; - int startNodeX, startNodeZ, endNodeX, endNodeZ; - - minX = aabbMin.getX(); - minZ = aabbMin.getZ(); - maxX = aabbMax.getX(); - maxZ = aabbMax.getZ(); - - startNodeX = (int)(minX * m_gridCellWidthInv); - startNodeZ = (int)(minZ * m_gridCellWidthInv); - - endNodeX = (int)(maxX * m_gridCellWidthInv); - endNodeZ = (int)(maxZ * m_gridCellWidthInv); - - endNodeX++; - endNodeZ++; - - startNodeX = btMax (1, startNodeX); - startNodeZ = btMax (1, startNodeZ); - endNodeX = m_numNodesWidth-1; - endNodeZ = m_numNodesLength-1; - - for (int i = startNodeX; i < endNodeX; i++) - { - for (int j = startNodeZ; j < endNodeZ; j++) - { - if (!m_flags[arrayIndex(i,j)]) - continue; - // triangle 1 - verts[0] = btVector3(widthPos(i), m_height[m_heightIndex][arrayIndex(i,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_height[m_heightIndex][arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_height[m_heightIndex][arrayIndex(i+1,j)], lengthPos(j)); - callback->processTriangle(verts,i,j); - // triangle 2 - verts[0] = btVector3(widthPos(i+1), m_height[m_heightIndex][arrayIndex(i+1,j)], lengthPos(j)); - verts[1] = btVector3(widthPos(i), m_height[m_heightIndex][arrayIndex(i,j+1)], lengthPos(j+1)); - verts[2] = btVector3(widthPos(i+1), m_height[m_heightIndex][arrayIndex(i+1,j+1)], lengthPos(j+1)); - callback->processTriangle(verts,i,j); - } - } -} - -void btHfFluid::setGridDimensions (btScalar gridCellWidth, - int numNodesWidth, int numNodesLength) -{ - m_gridWidth = gridCellWidth * numNodesWidth; - m_gridLength = gridCellWidth * numNodesLength; - m_gridCellWidth = gridCellWidth; - m_numNodesWidth = numNodesWidth; - m_numNodesLength = numNodesLength; - m_gridCellWidthInv = btScalar(1.0) / gridCellWidth; - - allocateArrays (); -} - -btScalar btHfFluid::bilinearInterpolate (const btScalar* array, btScalar iPos, btScalar jPos) -{ - int i = (int)iPos; - int j = (int)jPos; - - btScalar iParam1 = iPos - btScalar(i); - btScalar iParam0 = btScalar(1.0) - iParam1; - - btScalar jParam1 = jPos - btScalar(j); - btScalar jParam0 = btScalar(1.0) - jParam1; - - btScalar SW = array[arrayIndex(i, j)]; - btScalar SE = array[arrayIndex(i+1, j)]; - btScalar NW = array[arrayIndex(i, j+1)]; - btScalar NE = array[arrayIndex(i+1, j+1)]; - - btScalar a = jParam0 * SW + jParam1 * NW; - btScalar b = jParam0 * SE + jParam1 * NE; - return iParam0 * a + iParam1 * b; -} - -btScalar btHfFluid::advect (const btScalar* array, btScalar i, btScalar j, btScalar di, btScalar dj,btScalar dt) -{ - // trace particle backwards in time - btScalar srcI = i - di * dt * m_gridCellWidthInv; - btScalar srcJ = j - dj * dt * m_gridCellWidthInv; - - // srcI and srcJ are indices into the array, - // we need to clamp them to within the domain - srcI = btMax (srcI, btScalar(0.0)); - srcI = btMin (srcI, btScalar(m_numNodesWidth-1)); - srcJ = btMax (srcJ, btScalar(0.0)); - srcJ = btMin (srcJ, btScalar(m_numNodesLength-1)); - - return bilinearInterpolate (array, srcI, srcJ); -} - -void btHfFluid::advectEta (btScalar dt) -{ - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - - if (!m_flags[index]) - continue; - - btScalar u = m_globalVelocityU; - btScalar v = m_globalVelocityV; - - u += (m_u[m_velocityIndex][index]+m_u[m_velocityIndex][index+1]) * btScalar(0.5); - v += (m_v[m_velocityIndex][index]+m_v[m_velocityIndex][index+m_numNodesWidth]) * btScalar(0.5); - - m_temp[index] = advect (m_eta, btScalar(i), btScalar(j), u, v, dt); - } - } - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - m_eta[index] = m_temp[index]; - } - } -} - -void btHfFluid::updateHeight (btScalar dt) -{ - for (int j = 1; j < m_numNodesLength-1; j++) - { - for (int i = 1; i < m_numNodesWidth-1; i++) - { - int index = arrayIndex (i, j); - if (!m_flags[index]) - { - m_height[m_heightIndex][index] = m_ground[index] + m_eta[index]; - continue; - } - btScalar deta = -btScalar(0.5f) * m_eta[index] * dt * m_gridCellWidthInv * ( (m_u[m_velocityIndex][index+1] - m_u[m_velocityIndex][index]) + (m_v[m_velocityIndex][index+m_numNodesWidth] - m_v[m_velocityIndex][index])); - m_eta[index] += deta; - m_height[m_heightIndex][index] = m_ground[index] + btMax(m_eta[index],btScalar(0.0f)); - } - } -} - -void btHfFluid::advectVelocityU (btScalar dt) -{ - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - if (!m_flags[index]) - { - continue; - } - btScalar u = m_globalVelocityU; - btScalar v = m_globalVelocityV; - - u += m_u[m_velocityIndex][index]; - v += (m_v[m_velocityIndex][index]+m_v[m_velocityIndex][index+1]+m_v[m_velocityIndex][index+m_numNodesWidth]+m_v[m_velocityIndex][index+m_numNodesWidth+1]) * btScalar(0.25); - - m_temp[index] = advect (m_u[m_velocityIndex], btScalar(i), btScalar(j), u, v, dt); - } - } - - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - m_u[m_velocityIndex][index] = m_temp[index]; - } - } -} - -void btHfFluid::advectVelocityV (btScalar dt) -{ - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - if (!m_flags[index]) - { - continue; - } - btScalar u = m_globalVelocityU; - btScalar v = m_globalVelocityV; - - u += (m_u[m_velocityIndex][index]+m_u[m_velocityIndex][index+1]+m_u[m_velocityIndex][index+m_numNodesWidth]+m_u[m_velocityIndex][index+m_numNodesWidth+1]) * btScalar(0.25); - v += m_v[m_velocityIndex][index]; - - - m_temp[index] = advect (m_v[m_velocityIndex], btScalar(i), btScalar(j), u, v, dt); - } - } - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - int index = arrayIndex (i, j); - m_v[m_velocityIndex][index] = m_temp[index]; - } - } -} - -void btHfFluid::addDisplaced (int i, int j, btScalar r) -{ - m_r[m_rIndex][arrayIndex(i,j)] += r; -} - -void btHfFluid::transferDisplaced (btScalar dt) -{ - for (int i = 2; i < m_numNodesWidth - 2; i++) - { - for (int j = 2; j < m_numNodesLength - 2; j++) - { - btScalar deltaR = m_r[m_rIndex][arrayIndex(i,j)] - m_r[(m_rIndex+1)%2][arrayIndex(i,j)]; - // deltaR is in volume, but we want to change the height.. - deltaR = deltaR / (m_gridCellWidth * m_gridCellWidth); - deltaR *= m_volumeDisplacementScale; - btScalar qdeltaR = deltaR / btScalar(4.0f); - m_eta[arrayIndex(i-1,j-1)] += qdeltaR; - m_eta[arrayIndex(i-1,j+1)] += qdeltaR; - m_eta[arrayIndex(i+1,j-1)] += qdeltaR; - m_eta[arrayIndex(i+1,j+1)] += qdeltaR; - m_eta[arrayIndex(i,j)] -= deltaR; - // OPTIMIZATION: zero out next frames r value - m_r[(m_rIndex+1)%2][arrayIndex(i,j)] = btScalar(0.0); - } - } - m_rIndex = (m_rIndex + 1) % 2; // flip frame -} - -void btHfFluid::updateVelocity (btScalar dt) -{ - for (int j = 1; j < m_numNodesLength-1; j++) - { - for (int i = 2; i < m_numNodesWidth-1; i++) - { - int index = arrayIndex (i, j); - if (!m_flags[index]) - { - continue; - } - m_u[m_velocityIndex][index] += m_gravity * dt * m_gridCellWidthInv * (m_height[m_heightIndex][index]-m_height[m_heightIndex][index-1]); - } - } - - for (int j = 2; j < m_numNodesLength-1; j++) - { - for (int i = 1; i < m_numNodesWidth-1; i++) - { - int index = arrayIndex (i, j); - if (!m_flags[index]) - { - continue; - } - m_v[m_velocityIndex][index] += m_gravity * dt * m_gridCellWidthInv * (m_height[m_heightIndex][index]-m_height[m_heightIndex][index-m_numNodesWidth]); - } - } -} - -void btHfFluid::setReflectBoundaryLeft () -{ - for (int j = 0; j < m_numNodesLength; j++) - { - int indexL = arrayIndex (0, j); - - m_height[m_heightIndex][indexL] = m_height[m_heightIndex][indexL+1]; - m_u[m_velocityIndex][indexL+1] = btScalar(0.0); - m_v[m_velocityIndex][indexL] = btScalar(0.0); - } -} - -void btHfFluid::setReflectBoundaryRight () -{ - for (int j = 0; j < m_numNodesLength; j++) - { - int indexR = arrayIndex (m_numNodesWidth-1, j); - - m_height[m_heightIndex][indexR] = m_height[m_heightIndex][indexR-1]; - m_u[m_velocityIndex][indexR-1] = btScalar(0.0); - m_v[m_velocityIndex][indexR] = btScalar(0.0); - } -} - -void btHfFluid::setReflectBoundaryBottom () -{ - for (int i = 0; i < m_numNodesWidth; i++) - { - int indexT = arrayIndex (i, 0); - - m_height[m_heightIndex][indexT] = m_height[m_heightIndex][indexT+m_numNodesWidth]; - m_v[m_velocityIndex][indexT+m_numNodesWidth] = btScalar(0.0); - m_u[m_velocityIndex][indexT] = btScalar(0.0); - } -} - -void btHfFluid::setReflectBoundaryTop () -{ - for (int i = 0; i < m_numNodesWidth; i++) - { - int indexB = arrayIndex (i, m_numNodesLength-1); - - m_height[m_heightIndex][indexB] = m_height[m_heightIndex][indexB-m_numNodesWidth]; - m_v[m_velocityIndex][indexB-m_numNodesWidth] = btScalar(0.0); - m_u[m_velocityIndex][indexB] = btScalar(0.0); - } -} - -void btHfFluid::setAbsorbBoundaryLeft (btScalar dt) -{ - for (int j = 0; j < m_numNodesLength; j++) - { - int indexL = arrayIndex (0, j); - - btScalar c = btSqrt(m_eta[indexL+1]*m_gravity); - m_height[m_heightIndex][indexL] = ((m_gridCellWidthInv * m_height[(m_heightIndex+1)%2][indexL+1])+(dt*c*m_height[m_heightIndex][indexL+1]))/(m_gridCellWidthInv + dt * c); - m_u[m_velocityIndex][indexL+1] = btScalar(0.0f); - m_v[m_velocityIndex][indexL+1] = btScalar(0.0); - } -} - -void btHfFluid::setAbsorbBoundaryRight (btScalar dt) -{ -} - -void btHfFluid::setAbsorbBoundaryTop (btScalar dt) -{ -} - -void btHfFluid::setAbsorbBoundaryBottom (btScalar dt) -{ -} - -void btHfFluid::computeFlagsAndFillRatio () -{ - for (int i = 1; i < m_numNodesWidth-1; i++) - { - for (int j = 1; j < m_numNodesLength-1; j++) - { - btScalar h = m_height[m_heightIndex][arrayIndex(i,j)]; - btScalar hMin = computeHmin(i,j); - btScalar hMax = computeHmax(i,j); - btScalar etaMax = computeEtaMax(i,j); - if (h <= hMin && etaMax < m_epsEta) - { - m_flags[arrayIndex(i,j)] = false; - m_fillRatio[arrayIndex(i,j)] = btScalar(0.0f); - } else if (h > hMax) { - m_flags[arrayIndex(i,j)] = true; - m_fillRatio[arrayIndex(i,j)] = btScalar(1.0f); - } else { - m_flags[arrayIndex(i,j)] = true; - m_fillRatio[arrayIndex(i,j)] = (h - hMin)/(hMax - hMin); - } - - } - } -} - -btScalar btHfFluid::computeHmin (int i, int j) -{ - btAssert (i > 0); - btAssert (i < m_numNodesWidth-1); - btAssert (j > 0); - btAssert (j < m_numNodesLength-1); - - btScalar h1 = m_height[m_heightIndex][arrayIndex(i-1,j-1)]; - btScalar h2 = m_height[m_heightIndex][arrayIndex(i-1,j+1)]; - btScalar h3 = m_height[m_heightIndex][arrayIndex(i+1,j-1)]; - btScalar h4 = m_height[m_heightIndex][arrayIndex(i+1,j+1)]; - btScalar h = m_height[m_heightIndex][arrayIndex(i,j)]; - btScalar minh = btMin(h1, btMin(h2, btMin(h3,h4))); - - return (minh + h) * btScalar(0.5f); -} - -btScalar btHfFluid::computeHmax (int i, int j) -{ - btAssert (i > 0); - btAssert (i < m_numNodesWidth-1); - btAssert (j > 0); - btAssert (j < m_numNodesLength-1); - - btScalar h1 = m_height[m_heightIndex][arrayIndex(i-1,j-1)]; - btScalar h2 = m_height[m_heightIndex][arrayIndex(i-1,j+1)]; - btScalar h3 = m_height[m_heightIndex][arrayIndex(i+1,j-1)]; - btScalar h4 = m_height[m_heightIndex][arrayIndex(i+1,j+1)]; - btScalar h = m_height[m_heightIndex][arrayIndex(i,j)]; - btScalar maxh = btMax(h1, btMax(h2, btMax(h3,h4))); - - return (maxh + h) * btScalar(0.5f) + m_epsHeight; -} - -btScalar btHfFluid::computeEtaMax (int i, int j) -{ - btAssert (i > 0); - btAssert (i < m_numNodesWidth-1); - btAssert (j > 0); - btAssert (j < m_numNodesLength-1); - - btScalar eta1 = m_eta[arrayIndex(i-1,j-1)]; - btScalar eta2 = m_eta[arrayIndex(i-1,j+1)]; - btScalar eta3 = m_eta[arrayIndex(i+1,j-1)]; - btScalar eta4 = m_eta[arrayIndex(i+1,j+1)]; - btScalar eta = m_eta[arrayIndex(i,j)]; - btScalar maxeta = btMax(eta1, btMax(eta2, btMax(eta3,eta4))); - - return (maxeta + eta) * btScalar(0.5f); -} - -void btHfFluid::allocateArrays () -{ - if (m_temp) - btAlignedFree (m_temp); - if (m_height[0]) - { - btAlignedFree (m_height[0]); - btAlignedFree (m_height[1]); - } - if (m_ground) - btAlignedFree (m_ground); - if (m_eta) - btAlignedFree (m_eta); - if (m_u[0]) - { - btAlignedFree (m_u[0]); - btAlignedFree (m_u[1]); - } - if (m_v) - { - btAlignedFree (m_v[0]); - btAlignedFree (m_v[1]); - } - if (m_r) - { - btAlignedFree (m_r[0]); - btAlignedFree (m_r[1]); - } - if (m_flags) - btAlignedFree (m_flags); - if (m_fillRatio) - btAlignedFree (m_fillRatio); - - m_heightIndex = 0; - m_velocityIndex = 0; - m_temp = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_height[0] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_height[1] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_ground = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_eta = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_u[0] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_u[1] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_v[0] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_v[1] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_r[0] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_r[1] = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_fillRatio = (btScalar*)btAlignedAlloc (sizeof(btScalar) * m_numNodesWidth * m_numNodesLength, 16); - m_flags = (bool*)btAlignedAlloc (sizeof(bool) * m_numNodesWidth * m_numNodesLength, 16); - - { - int bufferSize = sizeof(btScalar) * m_numNodesWidth * m_numNodesLength; - printf("[HfFluid] Fluid buffer size %d bytes\n", bufferSize); - printf("[HfFluid] Temp %d buffers\n", 1); - printf("[HfFluid] Height %d buffers\n", 2); - printf("[HfFluid] Velocity %d buffers\n", 4); - printf("[HfFluid] Eta %d buffers\n", 1); - printf("[HfFluid] Fill Ratio %d buffers\n", 1); - printf("[HfFluid] ===============================\n"); - printf("[HfFluid] Total %d buffers\n", 9); - printf("[HfFluid] Total %d KB\n", (9 * bufferSize)/1024); - } - for (int i = 0; i < m_numNodesWidth*m_numNodesLength; i++) - { - m_eta[i] = btScalar(0.0); - m_u[0][i] = btScalar(0.0); - m_u[1][i] = btScalar(0.0); - m_v[0][i] = btScalar(0.0); - m_v[1][i] = btScalar(0.0); - m_r[0][i] = btScalar(0.0); - m_r[1][i] = btScalar(0.0); - m_height[0][i] = btScalar(0.0); - m_height[1][i] = btScalar(0.0); - m_ground[i] = btScalar(0.0); - m_flags[i] = false; - m_fillRatio[i] = btScalar(0.0); - m_temp[i] = btScalar(0.0); - } -} - - -void btHfFluid::debugTests () -{ - static btScalar total_volume = btScalar(0.0f); - btScalar new_total_volume = btScalar(0.0f); - for (int i = 0; i < m_numNodesWidth*m_numNodesLength; i++) - { - new_total_volume += m_eta[i] * m_gridCellWidth * m_gridCellWidth; - } - printf("volume = %f volume delta = %f\n", new_total_volume, new_total_volume - total_volume); - total_volume = new_total_volume; -} - -// You can enforce a global velocity at the surface of the fluid -// default: 0.0 and 0.0 -void btHfFluid::setGlobaVelocity (btScalar globalVelocityU, btScalar globalVelocityV) -{ - m_globalVelocityU = globalVelocityU; - m_globalVelocityV = globalVelocityV; -} - -void btHfFluid::getGlobalVelocity (btScalar& globalVelocityU, btScalar& globalVelocityV) const -{ - globalVelocityU = m_globalVelocityU; - globalVelocityV = m_globalVelocityV; -} - -// Control force of gravity, should match physics world -// default: -10.0 -void btHfFluid::setGravity (btScalar gravity) -{ - m_gravity = gravity; -} -btScalar btHfFluid::getGravity () const -{ - return m_gravity; -} - -// When a body is submerged into the fluid, the displaced fluid -// is spread to adjacent cells. You can control the percentage of this -// by setting this value between 0.0 and 1.0 -// default: 0.5 -void btHfFluid::setVolumeDisplacementScale (btScalar volumeDisplacementScale) -{ - m_volumeDisplacementScale = volumeDisplacementScale; -} - -btScalar btHfFluid::getVolumeDisplacementScale () const -{ - return m_volumeDisplacementScale; -} - -// The horizontal velocity of the fluid can influence bodies submerged -// in the fluid. You can control how much influence by setting this -// between 0.0 and 1.0 -// default: 0.5 -void btHfFluid::setHorizontalVelocityScale (btScalar horizontalVelocityScale) -{ - m_horizontalVelocityScale = horizontalVelocityScale; -} - -btScalar btHfFluid::getHorizontalVelocityScale () const -{ - return m_horizontalVelocityScale; -} - -static btScalar rangeOverlap (btScalar lo1, btScalar hi1, btScalar lo2, btScalar hi2, btScalar& loOut, btScalar& hiOut) -{ - if (!(lo1 <= hi2 && lo2 <= hi1)) - return btScalar(0.0f); - - if (lo1 >= lo2 && lo1 <= hi2 && - hi1 >= lo2 && hi1 <= hi2) - { - hiOut = hi1; - loOut = lo1; - return hi1 - lo1; - } else if (lo2 >= lo1 && lo2 <= hi1 && - hi2 >= lo1 && hi2 <= hi1) - { - hiOut = hi2; - loOut = lo2; - return hi2 - lo2; - } else if (hi1 >= lo2 && lo1 <= lo2) { - hiOut = hi1; - loOut = lo2; - return hi1 - lo2; - } else { - hiOut = hi2; - loOut = lo1; - return hi2 - lo1; - } -} - -btHfFluidColumnRigidBodyCallback::btHfFluidColumnRigidBodyCallback (btRigidBody* rigidBody, btIDebugDraw* debugDraw, btScalar density, btScalar floatyness) -{ - m_rigidBody = rigidBody; - m_buoyantShape = (btHfFluidBuoyantConvexShape*)rigidBody->getCollisionShape(); - m_debugDraw = debugDraw; - m_rigidBody->getAabb (m_aabbMin, m_aabbMax); - m_volume = btScalar(0.0f); - m_density = density; - m_floatyness = floatyness; - m_numVoxels = m_buoyantShape->getNumVoxels (); - m_voxelPositionsXformed = (btVector3*)btAlignedAlloc(sizeof(btVector3)*m_numVoxels, 16); - m_voxelSubmerged = (bool*)btAlignedAlloc(sizeof(bool)*m_numVoxels, 16); - for (int i = 0; i < m_numVoxels; i++) - { - btVector3 p = m_buoyantShape->getVoxelPositionsArray()[i]; - p = m_rigidBody->getWorldTransform().getBasis() * p; - p += m_rigidBody->getWorldTransform().getOrigin(); - m_voxelPositionsXformed[i] = p; - m_voxelSubmerged[i] = false; - } -} - -btHfFluidColumnRigidBodyCallback::~btHfFluidColumnRigidBodyCallback () -{ - if (m_voxelPositionsXformed) - btAlignedFree (m_voxelPositionsXformed); - if (m_voxelSubmerged) - btAlignedFree (m_voxelSubmerged); -} - -static bool sphereVsAABB (const btVector3& aabbMin, const btVector3& aabbMax, const btVector3& sphereCenter, const btScalar sphereRadius) -{ - btScalar totalDistance = 0; - - // Accumulate the distance of the sphere's center on each axis - for(int i = 0; i < 3; ++i) { - - // If the sphere's center is outside the aabb, we've got distance on this axis - if(sphereCenter[i] < aabbMin[i]) { - btScalar borderDistance = aabbMin[i] - sphereCenter[i]; - totalDistance += borderDistance * borderDistance; - - } else if(sphereCenter[i] > aabbMax[i]) { - btScalar borderDistance = sphereCenter[i] - aabbMax[i]; - totalDistance += borderDistance * borderDistance; - - } - // Otherwise the sphere's center is within the box on this axis, so the - // distance will be 0 and we do not need to accumulate anything at all - - } - - // If the distance to the box is lower than the sphere's radius, both are overlapping - return (totalDistance <= (sphereRadius * sphereRadius)); -} - -bool btHfFluidColumnRigidBodyCallback::processColumn (btHfFluid* fluid, int w, int l) -{ - btVector3 columnAabbMin,columnAabbMax; - - fluid->getAabbForColumn (w, l, columnAabbMin, columnAabbMax); - - bool applyBuoyancyImpulse = true; - bool applyFluidVelocityImpulse = true; - bool applyFluidDisplace = true; - - btScalar dt = btScalar(1.0f/60.0f); - - btScalar columnVolume = btScalar(0.0f); - - for (int i = 0; i < m_buoyantShape->getNumVoxels(); i++) - { - if (m_voxelSubmerged[i]) - continue; - - if (sphereVsAABB(columnAabbMin, columnAabbMax, m_voxelPositionsXformed[i], m_buoyantShape->getVoxelRadius())) - { - m_voxelSubmerged[i] = true; - btScalar voxelVolume = m_buoyantShape->getVolumePerVoxel(); - columnVolume += voxelVolume; - - btVector3 application_point = m_voxelPositionsXformed[i]; - btVector3 relative_position = application_point - m_rigidBody->getCenterOfMassPosition(); - - if (applyBuoyancyImpulse) - { - btScalar massDisplacedWater = voxelVolume * m_density * m_floatyness; - btScalar force = massDisplacedWater * -fluid->getGravity(); - btScalar impulseMag = force * dt; - btVector3 impulseVec = btVector3(btScalar(0.0f), btScalar(1.0f), btScalar(0.0f)) * impulseMag; -//#define CENTER_IMPULSE_ONLY 1 -#ifdef CENTER_IMPULSE_ONLY - m_rigidBody->applyCentralImpulse (impulseVec); -#else - m_rigidBody->applyImpulse (impulseVec, relative_position); -#endif - } - } - } - - if (columnVolume > btScalar(0.0)) - { - m_volume += columnVolume; - - if (applyFluidDisplace) - { - fluid->addDisplaced (w, l, columnVolume); - } - - if (applyFluidVelocityImpulse) - { - int index = fluid->arrayIndex (w,l); - btScalar u = fluid->getVelocityUArray()[index]; - btScalar v = fluid->getVelocityVArray()[index]; - btVector3 vd = btVector3(u, btScalar(0.0f), v); - btVector3 impulse = vd * dt * fluid->getHorizontalVelocityScale(); - m_rigidBody->applyCentralImpulse (impulse); - } - } - - return true; -} - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.h deleted file mode 100644 index 0017c07a0..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluid.h +++ /dev/null @@ -1,243 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifndef __HFFLUID_H -#define __HFFLUID_H - -#include "BulletCollision/CollisionDispatch/btCollisionObject.h" -#include "BulletCollision/CollisionShapes/btTriangleCallback.h" - -class btPersistentManifold; -class btManifoldResult; - -// FIX AABB calculation for whole btHfFluid shape -// Fix flags and fill ratio -// -> Figure out the constants used in flags and fill ratio code -// Fix volume removal -// add buoyant convex vs. convex / concave -// add buoyant concave support (try bunny model) - -///experimental buyancy fluid demo -class btHfFluid : public btCollisionObject -{ -public: - btHfFluid (btScalar gridCellWidth, int numNodesWidth, int numNodesLength); - - ~btHfFluid (); - - void predictMotion(btScalar dt); - - /* Prep does some initial setup of the height field fluid. - * You should call this at initialization time. - */ - void prep (); - - static const btHfFluid* upcast(const btCollisionObject* colObj) - { - if (colObj->getInternalType()==CO_HF_FLUID) - return (const btHfFluid*)colObj; - return 0; - } - static btHfFluid* upcast(btCollisionObject* colObj) - { - if (colObj->getInternalType()==CO_HF_FLUID) - return (btHfFluid*)colObj; - return 0; - } - - // - // ::btCollisionObject - // - - virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const - { - aabbMin = m_aabbMin; - aabbMax = m_aabbMax; - } - - int getNumNodesWidth () const { return m_numNodesWidth; } - int getNumNodesLength () const { return m_numNodesLength; } - - btScalar getGridCellWidth () const { return m_gridCellWidth; } - btScalar widthPos (int i) const; - btScalar lengthPos (int j) const; - - int arrayIndex (int i, int j) const; - int arrayIndex (btScalar i, btScalar j) const; - int arrayIndex (unsigned int i, unsigned int j) const; - const btScalar* getHeightArray () const; - const btScalar* getGroundArray () const; - const btScalar* getEtaArray () const; - const btScalar* getVelocityUArray () const; - const btScalar* getVelocityVArray () const; - const bool* getFlagsArray () const; - - void setFluidHeight (int x, int y, btScalar height); - void setFluidHeight (int index, btScalar height); - - void addFluidHeight (int x, int y, btScalar height); - void addDisplaced (int i, int j, btScalar r); - - void getAabbForColumn (int x, int y, btVector3& aabbMin, btVector3& aabbMax); - - btScalar* getHeightArray (); - btScalar* getGroundArray (); - btScalar* getEtaArray (); - btScalar* getVelocityUArray (); - btScalar* getVelocityVArray (); - bool* getFlagsArray (); - - void foreachGroundTriangle(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax); - class btHfFluidColumnCallback - { - public: - btHfFluidColumnCallback () {} - - virtual ~btHfFluidColumnCallback () {} - - virtual bool processColumn (btHfFluid* fluid, int w, int l) - { - return true; // keep going - } - }; - - void foreachFluidColumn (btHfFluidColumnCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax); - - void foreachSurfaceTriangle (btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax); -protected: - int m_numNodesWidth; - int m_numNodesLength; - - btScalar m_gridCellWidth; - btScalar m_gridWidth; - btScalar m_gridLength; - - btScalar m_gridCellWidthInv; - - btVector3 m_aabbMin; - btVector3 m_aabbMax; - - void setGridDimensions (btScalar gridCellWidth, - int numNodesWidth, int numNodesLength); - - btScalar bilinearInterpolate (const btScalar* array, btScalar i, btScalar j); - - btScalar advect (const btScalar* array, btScalar i, btScalar j, btScalar di, btScalar dj, btScalar dt); - - void advectEta (btScalar dt); - void updateHeight (btScalar dt); - - void advectVelocityU (btScalar dt); - void advectVelocityV (btScalar dt); - void updateVelocity (btScalar dt); - - void transferDisplaced (btScalar dt); - - void setReflectBoundaryLeft (); - void setReflectBoundaryRight (); - void setReflectBoundaryTop (); - void setReflectBoundaryBottom (); - - void setAbsorbBoundaryLeft (btScalar dt); - void setAbsorbBoundaryRight (btScalar dt); - void setAbsorbBoundaryTop (btScalar dt); - void setAbsorbBoundaryBottom (btScalar dt); - - void computeFlagsAndFillRatio (); - btScalar computeHmin (int i, int j); - btScalar computeHmax (int i, int j); - btScalar computeEtaMax (int i, int j); - - void allocateArrays (); - - void debugTests (); - - btScalar* m_temp; // temp - int m_heightIndex; - btScalar* m_height[2]; - btScalar* m_ground; - btScalar* m_eta; // height - ground - btScalar* m_u[2]; - btScalar* m_v[2]; - int m_rIndex; - btScalar* m_r[2]; - int m_velocityIndex; - bool* m_flags; - btScalar* m_fillRatio; - - // tweakables - btScalar m_globalVelocityU; - btScalar m_globalVelocityV; - btScalar m_gravity; - btScalar m_volumeDisplacementScale; - btScalar m_horizontalVelocityScale; - - btScalar m_epsHeight; - btScalar m_epsEta; -public: - // You can enforce a global velocity at the surface of the fluid - // default: 0.0 and 0.0 - void setGlobaVelocity (btScalar globalVelocityU, btScalar globalVelocityV); - void getGlobalVelocity (btScalar& globalVelocityU, btScalar& globalVelocityV) const; - - // Control force of gravity, should match physics world - // default: -10.0 - void setGravity (btScalar gravity); - btScalar getGravity () const; - - // When a body is submerged into the fluid, the displaced fluid - // is spread to adjacent cells. You can control the percentage of this - // by setting this value between 0.0 and 1.0 - // default: 0.5 - void setVolumeDisplacementScale (btScalar volumeDisplacementScale); - btScalar getVolumeDisplacementScale () const; - - // The horizontal velocity of the fluid can influence bodies submerged - // in the fluid. You can control how much influence by setting this - // between 0.0 and 1.0 - // default: 0.5 - void setHorizontalVelocityScale (btScalar horizontalVelocityScale); - btScalar getHorizontalVelocityScale () const; -}; - -class btRigidBody; -class btIDebugDraw; -class btHfFluidBuoyantConvexShape; - -class btHfFluidColumnRigidBodyCallback : public btHfFluid::btHfFluidColumnCallback -{ -protected: - btRigidBody* m_rigidBody; - btHfFluidBuoyantConvexShape* m_buoyantShape; - btIDebugDraw* m_debugDraw; - int m_numVoxels; - btVector3* m_voxelPositionsXformed; - bool* m_voxelSubmerged; - btVector3 m_aabbMin; - btVector3 m_aabbMax; - btScalar m_volume; - btScalar m_density; - btScalar m_floatyness; -public: - btHfFluidColumnRigidBodyCallback (btRigidBody* rigidBody, btIDebugDraw* debugDraw, btScalar density, btScalar floatyness); - ~btHfFluidColumnRigidBodyCallback (); - bool processColumn (btHfFluid* fluid, int w, int l); - btScalar getVolume () const { return m_volume; } -}; - -#endif - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.cpp deleted file mode 100644 index e222bd61d..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.cpp +++ /dev/null @@ -1,195 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ -#include - -#include "LinearMath/btAabbUtil2.h" -#include "BulletCollision/CollisionShapes/btConvexShape.h" -#include "BulletCollision/CollisionShapes/btSphereShape.h" -#include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h" -#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h" -#include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h" -#include "BulletCollision/NarrowPhaseCollision/btDiscreteCollisionDetectorInterface.h" - -#include "btHfFluidBuoyantConvexShape.h" - -btHfFluidBuoyantConvexShape::btHfFluidBuoyantConvexShape (btConvexShape* convexShape) -{ - m_convexShape = convexShape; - m_shapeType = HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE; - m_radius = btScalar(0.f); - m_numVoxels = 0; - m_voxelPositions = NULL; - m_totalVolume = btScalar(0.0f); - m_floatyness = btScalar(1.5f); -} - -btHfFluidBuoyantConvexShape::~btHfFluidBuoyantConvexShape () -{ - if (m_voxelPositions) - btAlignedFree (m_voxelPositions); -} - -void btHfFluidBuoyantConvexShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const -{ - return m_convexShape->getAabb (t, aabbMin, aabbMax); -} - -void btHfFluidBuoyantConvexShape::setMargin(btScalar margin) -{ - m_convexShape->setMargin (margin); -} - -void btHfFluidBuoyantConvexShape::setLocalScaling(const btVector3& scaling) -{ - m_convexShape->setLocalScaling (scaling); -} - -const char* btHfFluidBuoyantConvexShape::getName() const -{ - return "HF_FLUID_BUOYANT_CONVEX_SHAPE"; -} - -const btVector3& btHfFluidBuoyantConvexShape::getLocalScaling() const -{ - return m_convexShape->getLocalScaling(); -} - -void btHfFluidBuoyantConvexShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const -{ - m_convexShape->calculateLocalInertia (mass, inertia); -} - -btScalar btHfFluidBuoyantConvexShape::getMargin() const -{ - return m_convexShape->getMargin(); -} - -//must be above the machine epsilon -#define REL_ERROR2 btScalar(1.0e-6) -static bool intersect(btVoronoiSimplexSolver* simplexSolver, - const btTransform& transformA, - const btTransform& transformB, - btConvexShape* a, - btConvexShape* b) -{ - - btScalar squaredDistance = SIMD_INFINITY; - btTransform localTransA = transformA; - btTransform localTransB = transformB; - btVector3 positionOffset = (localTransA.getOrigin() + localTransB.getOrigin()) * btScalar(0.5); - localTransA.getOrigin() -= positionOffset; - localTransB.getOrigin() -= positionOffset; - btScalar delta = btScalar(0.); - btVector3 v = btVector3(1.0f, 0.0f, 0.0f); - simplexSolver->reset (); - do - { - btVector3 seperatingAxisInA = (-v)* transformA.getBasis(); - btVector3 seperatingAxisInB = v* transformB.getBasis(); - - btVector3 pInA = a->localGetSupportVertexNonVirtual(seperatingAxisInA); - btVector3 qInB = b->localGetSupportVertexNonVirtual(seperatingAxisInB); - - btVector3 pWorld = localTransA(pInA); - btVector3 qWorld = localTransB(qInB); - - btVector3 w = pWorld - qWorld; - delta = v.dot(w); - - // potential exit, they don't overlap - if ((delta > btScalar(0.0))) - { - return false; - } - - if (simplexSolver->inSimplex (w)) - { - return false; - } - - simplexSolver->addVertex (w, pWorld, qWorld); - - if (!simplexSolver->closest(v)) - { - return false; - } - - btScalar previousSquaredDistance = squaredDistance; - squaredDistance = v.length2(); - - if (previousSquaredDistance - squaredDistance <= SIMD_EPSILON * previousSquaredDistance) - { - return false; - } - } while (!simplexSolver->fullSimplex() && squaredDistance > REL_ERROR2 * simplexSolver->maxVertex()); - - return true; -} - -void btHfFluidBuoyantConvexShape::generateShape (btScalar radius, btScalar gap) -{ - btTransform T; - T.setIdentity (); - btVector3 aabbMin, aabbMax; - getAabb (T, aabbMin, aabbMax); - - m_radius = radius; - m_numVoxels = 0; - - btVoronoiSimplexSolver simplexSolver; - btSphereShape sphereShape(radius); - btVector3* voxelPositions = (btVector3*)btAlignedAlloc (sizeof(btVector3)*MAX_VOXEL_DIMENSION*MAX_VOXEL_DIMENSION*MAX_VOXEL_DIMENSION,16); - for (int i = 0; i < MAX_VOXEL_DIMENSION; i++) - { - for (int j = 0; j < MAX_VOXEL_DIMENSION; j++) - { - for (int k = 0; k < MAX_VOXEL_DIMENSION; k++) - { - btVector3 point; - btTransform sT; - sT.setIdentity (); - - point.setX(aabbMin.getX() + (i * btScalar(2.0f) * radius) + (i * gap)); - point.setY(aabbMin.getY() + (j * btScalar(2.0f) * radius) + (j * gap)); - point.setZ(aabbMin.getZ() + (k * btScalar(2.0f) * radius) + (k * gap)); - - if (TestPointAgainstAabb2(aabbMin, aabbMax, point)) - { - btTransform sT; - sT.setIdentity (); - sT.setOrigin (point); - - if (intersect (&simplexSolver, T, sT, m_convexShape, &sphereShape)) - { - voxelPositions[m_numVoxels] = point; - m_numVoxels++; - } - } - } - } - } - m_voxelPositions = (btVector3*)btAlignedAlloc (sizeof(btVector3)*m_numVoxels, 16); - for (int i = 0; i < m_numVoxels;i++) - { - m_voxelPositions[i] = voxelPositions[i]; - } - btAlignedFree (voxelPositions); - m_volumePerVoxel = btScalar(4.0f)/btScalar(3.0f)*SIMD_PI*radius*radius*radius; - m_totalVolume = m_numVoxels * m_volumePerVoxel; - m_radius = radius; -} - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.h deleted file mode 100644 index 3e25a94fd..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantConvexShape.h +++ /dev/null @@ -1,65 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ -#ifndef __BT_HFFLUID_BUOYANT_CONVEX_SHAPE_H -#define __BT_HFFLUID_BUOYANT_CONVEX_SHAPE_H - -#include "LinearMath/btVector3.h" -#include "BulletCollision/CollisionShapes/btCollisionShape.h" - -#define MAX_VOXEL_DIMENSION 32 - -class btConvexShape; -///experimental buyancy fluid demo -class btHfFluidBuoyantConvexShape : public btCollisionShape -{ -public: - btHfFluidBuoyantConvexShape (btConvexShape* convexShape); - ~btHfFluidBuoyantConvexShape (); - void generateShape (btScalar radius, btScalar gap); - - const btConvexShape* getConvexShape () const - { - return m_convexShape; - } - - virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; - virtual void setMargin(btScalar margin); - virtual btScalar getMargin() const; - virtual void setLocalScaling(const btVector3& scaling); - virtual const btVector3& getLocalScaling() const; - virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const; - virtual const char* getName()const; - - btScalar getVoxelRadius () const { return m_radius; } - btScalar getTotalVolume () const { return m_totalVolume; } - btScalar getVolumePerVoxel () const { return m_volumePerVoxel; } - btScalar getFloatyness () const { return m_floatyness; } - void setFloatyness (btScalar floatyness) { m_floatyness = floatyness; } - int getNumVoxels () const { return m_numVoxels; } - const btVector3* getVoxelPositionsArray() { return m_voxelPositions; } - -protected: - btScalar m_floatyness; - btScalar m_radius; - btScalar m_totalVolume; - btScalar m_volumePerVoxel; - int m_numVoxels; - btVector3* m_voxelPositions; - btConvexShape* m_convexShape; -}; - -#endif diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.cpp deleted file mode 100644 index b1a97487d..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ -#include - -#include "btHfFluidBuoyantShapeCollisionAlgorithm.h" -#include "btHfFluidBuoyantConvexShape.h" -#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" -#include "BulletCollision/CollisionShapes/btSphereShape.h" -#include "BulletCollision/CollisionShapes/btBoxShape.h" - -#include "BulletCollision/CollisionDispatch/btCollisionObject.h" -#include "BulletDynamics/Dynamics/btRigidBody.h" -#include "btHfFluid.h" -#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h" - -btHfFluidBuoyantShapeCollisionAlgorithm::btHfFluidBuoyantShapeCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap,btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) -: btCollisionAlgorithm(ci), m_convexConvexAlgorithm(NULL, ci, col0Wrap, col1Wrap, simplexSolver, pdSolver,0,0) -{ - m_collisionObject0 = col0Wrap->getCollisionObject(); - m_collisionObject1 = col1Wrap->getCollisionObject(); -} - -btHfFluidBuoyantShapeCollisionAlgorithm::~btHfFluidBuoyantShapeCollisionAlgorithm() -{ -} - -void btHfFluidBuoyantShapeCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) -{ - const btHfFluidBuoyantConvexShape* tmpShape0 = (const btHfFluidBuoyantConvexShape*)body0Wrap->getCollisionShape(); - const btHfFluidBuoyantConvexShape* tmpShape1 = (const btHfFluidBuoyantConvexShape*)body1Wrap->getCollisionShape(); - const btConvexShape* convexShape0 = tmpShape0->getConvexShape(); - const btConvexShape* convexShape1 = tmpShape1->getConvexShape(); - - //body0->setCollisionShape (convexShape0); - //body1->setCollisionShape (convexShape1); - - btCollisionObjectWrapper ob0(body0Wrap,convexShape0,body0Wrap->getCollisionObject(),body0Wrap->getWorldTransform()); - btCollisionObjectWrapper ob1(body1Wrap,convexShape1,body1Wrap->getCollisionObject(),body1Wrap->getWorldTransform()); - m_convexConvexAlgorithm.processCollision (&ob0, &ob1, dispatchInfo,resultOut); - - -} - -btScalar btHfFluidBuoyantShapeCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) -{ - btAssert(0); - - btHfFluidBuoyantConvexShape* tmpShape0 = (btHfFluidBuoyantConvexShape*)body0->getCollisionShape(); - btHfFluidBuoyantConvexShape* tmpShape1 = (btHfFluidBuoyantConvexShape*)body1->getCollisionShape(); - const btConvexShape* convexShape0 = tmpShape0->getConvexShape(); - const btConvexShape* convexShape1 = tmpShape1->getConvexShape(); - - - btScalar toi = btScalar(0.0f); - - toi = m_convexConvexAlgorithm.calculateTimeOfImpact (body0, body1, dispatchInfo, resultOut); - - - return toi; -} diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.h deleted file mode 100644 index 853f07239..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.h +++ /dev/null @@ -1,87 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifndef HF_FLUID_BUOYANT_SHAPE_COLLISION_ALGORITHM_H -#define HF_FLUID_BUOYANT_SHAPE_COLLISION_ALGORITHM_H - -#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" -#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" -#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h" -#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" -#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" -#include "BulletCollision/CollisionShapes/btTriangleCallback.h" -#include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h" -#include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h" - -#include "LinearMath/btVector3.h" -class btHfFluid; - -class btConvexConvexAlgorithm; -class btConvexPenetrationDepthSolver; -class btSimplexSolverInterface; - -///experimental buyancy fluid demo -/// btHfFluidBuoyantShapeCollisionAlgorithm provides collision detection between btHfFluidBuoyantConvexShape and btHfFluidBuoyantConvexShape -class btHfFluidBuoyantShapeCollisionAlgorithm : public btCollisionAlgorithm -{ - const btCollisionObject* m_collisionObject0; - const btCollisionObject* m_collisionObject1; - - btConvexConvexAlgorithm m_convexConvexAlgorithm; -public: - - btHfFluidBuoyantShapeCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver); - - virtual ~btHfFluidBuoyantShapeCollisionAlgorithm(); - - virtual void processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); - - virtual btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); - - virtual void getAllContactManifolds(btManifoldArray& manifoldArray) - { - m_convexConvexAlgorithm.getAllContactManifolds (manifoldArray); - } - - - struct CreateFunc :public btCollisionAlgorithmCreateFunc - { - btConvexPenetrationDepthSolver* m_pdSolver; - btSimplexSolverInterface* m_simplexSolver; - - CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) - { - m_simplexSolver = simplexSolver; - m_pdSolver = pdSolver; - } - - virtual ~CreateFunc() {} - virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap) - { - void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btHfFluidBuoyantShapeCollisionAlgorithm)); - if (!m_swapped) - { - return new(mem) btHfFluidBuoyantShapeCollisionAlgorithm(ci,body0Wrap,body1Wrap, m_simplexSolver, m_pdSolver); - } else - { - return new(mem) btHfFluidBuoyantShapeCollisionAlgorithm(ci,body0Wrap,body1Wrap, m_simplexSolver, m_pdSolver); - } - } - }; -}; - -#endif //HF_FLUID_BUOYANT_SHAPE_COLLISION_ALGORITHM_H diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.cpp deleted file mode 100644 index 71820923e..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ -#include "btHfFluidCollisionShape.h" - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.h deleted file mode 100644 index bf24670bf..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidCollisionShape.h +++ /dev/null @@ -1,94 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ -#ifndef BT_HF_FLUID_COLLISION_SHAPE_H -#define BT_HF_FLUID_COLLISION_SHAPE_H - -#include "btHfFluid.h" -#include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h" -#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" -#include "BulletCollision/CollisionShapes/btConvexInternalShape.h" -#include "BulletCollision/CollisionShapes/btConcaveShape.h" - -class btHfFluidCollisionShape : public btConcaveShape -{ - public: - btHfFluid* m_fluid; - - btHfFluidCollisionShape(btHfFluid* backptr) : btConcaveShape () - { - m_shapeType = HFFLUID_SHAPE_PROXYTYPE; - m_fluid=backptr; - } - - virtual ~btHfFluidCollisionShape() - { - - } - - void processAllTriangles(btTriangleCallback* /*callback*/,const btVector3& /*aabbMin*/,const btVector3& /*aabbMax*/) const - { - //not yet - btAssert(0); - } - - ///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t. - virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const - { - /* t should be identity, but better be safe than...fast? */ - btVector3 mins; - btVector3 maxs; - - m_fluid->getAabb (mins, maxs); - - const btVector3 crns[]={t*btVector3(mins.x(),mins.y(),mins.z()), - t*btVector3(maxs.x(),mins.y(),mins.z()), - t*btVector3(maxs.x(),maxs.y(),mins.z()), - t*btVector3(mins.x(),maxs.y(),mins.z()), - t*btVector3(mins.x(),mins.y(),maxs.z()), - t*btVector3(maxs.x(),mins.y(),maxs.z()), - t*btVector3(maxs.x(),maxs.y(),maxs.z()), - t*btVector3(mins.x(),maxs.y(),maxs.z())}; - aabbMin=aabbMax=crns[0]; - for(int i=1;i<8;++i) - { - aabbMin.setMin(crns[i]); - aabbMax.setMax(crns[i]); - } - } - - virtual void setLocalScaling(const btVector3& /*scaling*/) - { - ///na - btAssert(0); - } - virtual const btVector3& getLocalScaling() const - { - static const btVector3 dummy(1,1,1); - return dummy; - } - virtual void calculateLocalInertia(btScalar /*mass*/,btVector3& /*inertia*/) const - { - ///not yet - btAssert(0); - } - virtual const char* getName()const - { - return "HfFluid"; - } -}; - -#endif diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.cpp deleted file mode 100644 index d3497cee6..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include - -#include "btHfFluidRigidCollisionAlgorithm.h" -#include "btHfFluidBuoyantConvexShape.h" -#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" -#include "BulletCollision/CollisionShapes/btSphereShape.h" -#include "BulletCollision/CollisionShapes/btBoxShape.h" -#include "BulletCollision/CollisionDispatch/btCollisionObject.h" -#include "BulletDynamics/Dynamics/btRigidBody.h" -#include "btHfFluid.h" -#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h" - -btHfFluidRigidCollisionAlgorithm::~btHfFluidRigidCollisionAlgorithm() -{ -} - -btHfFluidRigidCollisionAlgorithm::btHfFluidRigidCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, bool isSwapped) -: btCollisionAlgorithm(ci), m_isSwapped(isSwapped), - m_convexTrianglecallback(ci.m_dispatcher1, col0Wrap, col1Wrap, !isSwapped) // we flip the isSwapped because we are hf fluid vs. convex and callback expects convex vs. concave -{ - m_manifoldPtr = m_convexTrianglecallback.m_manifoldPtr; - if (m_isSwapped) - { - m_hfFluid = static_cast(col1Wrap->getCollisionObject()); - m_rigidCollisionObject = static_cast(col0Wrap->getCollisionObject()); - m_manifoldPtr->setBodies(m_hfFluid,m_rigidCollisionObject); - } else { - m_hfFluid = static_cast(col0Wrap->getCollisionObject()); - m_rigidCollisionObject = static_cast(col1Wrap->getCollisionObject()); - m_manifoldPtr->setBodies(m_rigidCollisionObject,m_hfFluid); - } -} - -void btHfFluidRigidCollisionAlgorithm::processGround (const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) -{ - btAssert(0); - //needs fixing after btCollisionObjectWrapper introduction -#if 0 - btScalar triangleMargin = m_rigidCollisionObject->getCollisionShape()->getMargin(); - resultOut->setPersistentManifold(m_manifoldPtr); - // to perform the convex shape vs. ground terrain: - // we pull the convex shape out of the buoyant shape and replace it temporarily - btHfFluidBuoyantConvexShape* tmpShape = (btHfFluidBuoyantConvexShape*)m_rigidCollisionObject->getCollisionShape(); - const btConvexShape* convexShape = ((const btHfFluidBuoyantConvexShape*)tmpShape)->getConvexShape(); - //m_rigidCollisionObject->setCollisionShape (convexShape); - m_convexTrianglecallback.setTimeStepAndCounters (triangleMargin, dispatchInfo, resultOut); - m_hfFluid->foreachGroundTriangle (&m_convexTrianglecallback, m_convexTrianglecallback.getAabbMin(),m_convexTrianglecallback.getAabbMax()); - resultOut->refreshContactPoints(); -#endif - // restore the buoyant shape - //m_rigidCollisionObject->setCollisionShape (tmpShape); -} - -btScalar btHfFluidRigidCollisionAlgorithm::processFluid (const btDispatcherInfo& dispatchInfo, btScalar density, btScalar floatyness) -{ - btAssert(0); - //needs fixing after btCollisionObjectWrapper introduction -#if 0 - btRigidBody* rb = btRigidBody::upcast(m_rigidCollisionObject); - btHfFluidColumnRigidBodyCallback columnCallback (rb, dispatchInfo.m_debugDraw, density, floatyness); - m_hfFluid->foreachFluidColumn (&columnCallback, m_convexTrianglecallback.getAabbMin(), m_convexTrianglecallback.getAabbMax()); - return columnCallback.getVolume (); -#endif - return 0.f; - -} - -void btHfFluidRigidCollisionAlgorithm::applyFluidFriction (btScalar mu, btScalar submerged_percentage) -{ - btAssert(0); - //needs fixing after btCollisionObjectWrapper introduction -#if 0 - - btRigidBody* rb = btRigidBody::upcast(m_rigidCollisionObject); - btScalar dt = btScalar(1.0f/60.0f); - -//#define OLD_WAY -#ifdef OLD_WAY - btScalar radius = btScalar(0.0f); - { - btVector3 aabbMin, aabbMax; - btTransform T; - T.setIdentity(); - rb->getCollisionShape()->getAabb (T, aabbMin, aabbMax); - radius = (aabbMax-aabbMin).length()*btScalar(0.5); - } - btScalar viscosity = btScalar(0.05); - btVector3 force = btScalar(6.0f) * SIMD_PI * viscosity * radius * -rb->getLinearVelocity(); - - btVector3 impulse = force * dt; - rb->applyCentralImpulse (impulse); - - if (true) - { - btVector3 av = rb->getAngularVelocity(); - av *= btScalar(0.99); - rb->setAngularVelocity (av); - } -#else - btScalar scaled_mu = mu * submerged_percentage * btScalar(0.4f); - rb->applyCentralImpulse (dt * scaled_mu * -rb->getLinearVelocity()); - rb->applyTorqueImpulse (dt * scaled_mu * -rb->getAngularVelocity()); -#endif -#endif - -} - -void btHfFluidRigidCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) -{ - btAssert(0); - //needs fixing after btCollisionObjectWrapper introduction -#if 0 - processGround (dispatchInfo, resultOut); - btHfFluidBuoyantConvexShape* buoyantShape = (btHfFluidBuoyantConvexShape*)m_rigidCollisionObject->getCollisionShape(); - btRigidBody* rb = btRigidBody::upcast(m_rigidCollisionObject); - if (rb) - { - btScalar mass = btScalar(1.0f) / rb->getInvMass (); - btScalar volume = buoyantShape->getTotalVolume (); - btScalar density = mass / volume; - btScalar floatyness = buoyantShape->getFloatyness (); - btScalar submerged_volume = processFluid (dispatchInfo, density, floatyness); - if (submerged_volume > btScalar(0.0)) - { - btScalar submerged_percentage = submerged_volume/buoyantShape->getTotalVolume(); - //printf("%f\n", submerged_percentage); - btScalar mu = btScalar(6.0f); - applyFluidFriction (mu, submerged_percentage); - } - } -#endif - -} - -btScalar btHfFluidRigidCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) -{ - return btScalar(1.0); -} diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.h deleted file mode 100644 index b03051a04..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionAlgorithm.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifndef HF_FLUID_RIGID_COLLISION_ALGORITHM_H -#define HF_FLUID_RIGID_COLLISION_ALGORITHM_H - -#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" -#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" -#include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h" -#include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" -#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" -#include "BulletCollision/CollisionShapes/btTriangleCallback.h" -#include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h" - -#include "LinearMath/btVector3.h" -class btHfFluid; - -///experimental buyancy fluid demo -/// btHfFluidRigidCollisionAlgorithm provides collision detection between btHfFluid and btRigidBody -class btHfFluidRigidCollisionAlgorithm : public btCollisionAlgorithm -{ - btPersistentManifold* m_manifoldPtr; - - const btHfFluid* m_hfFluid; - const btCollisionObject* m_rigidCollisionObject; - - ///for rigid versus fluid (instead of fluid versus rigid), we use this swapped boolean - bool m_isSwapped; - - btConvexTriangleCallback m_convexTrianglecallback; - - void processGround (const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); - void applyFluidFriction (btScalar mu, btScalar submerged_percentage); - btScalar processFluid (const btDispatcherInfo& dispatchInfo, btScalar density, btScalar floatyness); -public: - - btHfFluidRigidCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, bool isSwapped); - - virtual ~btHfFluidRigidCollisionAlgorithm(); - - virtual void processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); - - virtual btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); - - virtual void getAllContactManifolds(btManifoldArray& manifoldArray) - { - manifoldArray.push_back (m_manifoldPtr); - } - - - struct CreateFunc :public btCollisionAlgorithmCreateFunc - { - virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap) - { - void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btHfFluidRigidCollisionAlgorithm)); - if (!m_swapped) - { - return new(mem) btHfFluidRigidCollisionAlgorithm(ci,body0Wrap,body1Wrap,false); - } else - { - return new(mem) btHfFluidRigidCollisionAlgorithm(ci,body0Wrap,body1Wrap,true); - } - } - }; -}; - -#endif //HF_FLUID_RIGID_COLLISION_ALGORITHM_H diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.cpp deleted file mode 100644 index f828ec130..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include "btHfFluidRigidCollisionConfiguration.h" -#include "btHfFluidRigidCollisionAlgorithm.h" -#include "btHfFluidBuoyantShapeCollisionAlgorithm.h" -#include "LinearMath/btPoolAllocator.h" - -btHfFluidRigidCollisionConfiguration::btHfFluidRigidCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo) -:btDefaultCollisionConfiguration(constructionInfo) -{ - void* mem; - - mem = btAlignedAlloc(sizeof(btHfFluidRigidCollisionAlgorithm::CreateFunc),16); - m_hfFluidRigidConvexCreateFunc = new(mem) btHfFluidRigidCollisionAlgorithm::CreateFunc; - - mem = btAlignedAlloc(sizeof(btHfFluidRigidCollisionAlgorithm::CreateFunc),16); - m_swappedHfFluidRigidConvexCreateFunc = new(mem) btHfFluidRigidCollisionAlgorithm::CreateFunc; - m_swappedHfFluidRigidConvexCreateFunc->m_swapped = true; - - mem = btAlignedAlloc(sizeof(btHfFluidBuoyantShapeCollisionAlgorithm::CreateFunc),16); - m_hfFluidBuoyantShapeCollisionCreateFunc = new(mem) btHfFluidBuoyantShapeCollisionAlgorithm::CreateFunc(m_simplexSolver, m_pdSolver); - - if (m_ownsCollisionAlgorithmPool && m_collisionAlgorithmPool) - { - int curElemSize = m_collisionAlgorithmPool->getElementSize(); - ///calculate maximum element size, big enough to fit any collision algorithm in the memory pool - - int maxSize0 = sizeof(btHfFluidRigidCollisionAlgorithm); - int maxSize1 = 0; - int maxSize2 = 0; - - int collisionAlgorithmMaxElementSize = btMax(maxSize0,maxSize1); - collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize2); - - if (collisionAlgorithmMaxElementSize > curElemSize) - { - m_collisionAlgorithmPool->~btPoolAllocator(); - btAlignedFree(m_collisionAlgorithmPool); - void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16); - m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,constructionInfo.m_defaultMaxCollisionAlgorithmPoolSize); - } - } -} - -btHfFluidRigidCollisionConfiguration::~btHfFluidRigidCollisionConfiguration() -{ - m_hfFluidRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc(); - m_swappedHfFluidRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc(); - btAlignedFree(m_hfFluidRigidConvexCreateFunc); - btAlignedFree(m_swappedHfFluidRigidConvexCreateFunc); -} - -btCollisionAlgorithmCreateFunc* btHfFluidRigidCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1) -{ - if ((proxyType0 == HFFLUID_SHAPE_PROXYTYPE) && (proxyType1 == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE)) - { - return m_hfFluidRigidConvexCreateFunc; - } - - if ((proxyType0 == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE) && (proxyType1 == HFFLUID_SHAPE_PROXYTYPE)) - { - return m_swappedHfFluidRigidConvexCreateFunc; - } - - if ((proxyType0 == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE) && (proxyType1 == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE)) - { - return m_hfFluidBuoyantShapeCollisionCreateFunc; - } - - ///fallback to the regular rigid collision shape - return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0,proxyType1); -} - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.h deleted file mode 100644 index 7860d586a..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidCollisionConfiguration.h +++ /dev/null @@ -1,47 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifndef BT_HFFLUID_RIGID_COLLISION_CONFIGURATION -#define BT_HFFLUID_RIGID_COLLISION_CONFIGURATION - -#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h" - -class btVoronoiSimplexSolver; -class btGjkEpaPenetrationDepthSolver; - -///experimental buyancy fluid demo -///btSoftBodyRigidBodyCollisionConfiguration add softbody interaction on top of btDefaultCollisionConfiguration -class btHfFluidRigidCollisionConfiguration : public btDefaultCollisionConfiguration -{ - //default CreationFunctions, filling the m_doubleDispatch table - btCollisionAlgorithmCreateFunc* m_hfFluidRigidConvexCreateFunc; - btCollisionAlgorithmCreateFunc* m_swappedHfFluidRigidConvexCreateFunc; - btCollisionAlgorithmCreateFunc* m_hfFluidBuoyantShapeCollisionCreateFunc; - -public: - - btHfFluidRigidCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo = btDefaultCollisionConstructionInfo()); - - virtual ~btHfFluidRigidCollisionConfiguration(); - - ///creation of soft-soft and soft-rigid, and otherwise fallback to base class implementation - virtual btCollisionAlgorithmCreateFunc* getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1); - -}; - -#endif //BT_HFFLUID_RIGID_COLLISION_CONFIGURATION - diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.cpp b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.cpp deleted file mode 100644 index f11438418..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.cpp +++ /dev/null @@ -1,273 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include -#include "LinearMath/btQuickprof.h" -#include "LinearMath/btIDebugDraw.h" -#include "BulletCollision/CollisionShapes/btCollisionShape.h" - -// height field fluid -#include "btHfFluid.h" -#include "btHfFluidBuoyantConvexShape.h" -#include "btHfFluidRigidDynamicsWorld.h" - - - - -btHfFluidRigidDynamicsWorld::btHfFluidRigidDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration) -:btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration) -{ - m_drawMode = DRAWMODE_NORMAL; - m_bodyDrawMode = BODY_DRAWMODE_NORMAL; -} - -btHfFluidRigidDynamicsWorld::~btHfFluidRigidDynamicsWorld() -{ - -} - -void btHfFluidRigidDynamicsWorld::predictUnconstraintMotion(btScalar timeStep) -{ - btDiscreteDynamicsWorld::predictUnconstraintMotion( timeStep); - - for ( int i=0;ipredictMotion(timeStep); - } -} - -void btHfFluidRigidDynamicsWorld::internalSingleStepSimulation( btScalar timeStep) -{ - btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep ); - - updateFluids (timeStep); - - solveFluidConstraints (timeStep); -} - -void btHfFluidRigidDynamicsWorld::updateFluids(btScalar timeStep) -{ - BT_PROFILE("updateFluids"); - - for ( int i=0;ipredictMotion (timeStep); - } -} - -void btHfFluidRigidDynamicsWorld::solveFluidConstraints(btScalar timeStep) -{ - BT_PROFILE("solve Fluid Contacts"); - -#if 0 - if(m_hfFluids.size()) - { - btHfFluid::solveClusters(m_hfFluids); - } - - for(int i=0;isolveConstraints(); - } -#endif -} - -void btHfFluidRigidDynamicsWorld::addHfFluid(btHfFluid* body) -{ - m_hfFluids.push_back(body); - - btCollisionWorld::addCollisionObject(body, - btBroadphaseProxy::DefaultFilter, - btBroadphaseProxy::AllFilter); - -} - -void btHfFluidRigidDynamicsWorld::removeHfFluid(btHfFluid* body) -{ - m_hfFluids.remove(body); - - btCollisionWorld::removeCollisionObject(body); -} - -void btHfFluidRigidDynamicsWorld::drawHfFluidGround (btIDebugDraw* debugDraw, btHfFluid* fluid) -{ - const btScalar* ground = fluid->getGroundArray (); - btVector3 com = fluid->getWorldTransform().getOrigin(); - btVector3 color = btVector3(btScalar(0.13f), btScalar(0.13f), btScalar(0.0)); - for (int i = 1; i < fluid->getNumNodesWidth()-1; i++) - { - for (int j = 1; j < fluid->getNumNodesLength()-1; j++) - { - int sw = fluid->arrayIndex (i, j); - int se = fluid->arrayIndex (i+1, j); - int nw = fluid->arrayIndex (i, j+1); - int ne = fluid->arrayIndex (i+1, j+1); - btVector3 swV = btVector3 (fluid->widthPos (i), ground[sw], fluid->lengthPos (j)); - btVector3 seV = btVector3 (fluid->widthPos (i+1), ground[se], fluid->lengthPos (j)); - btVector3 nwV = btVector3 (fluid->widthPos (i), ground[nw], fluid->lengthPos (j+1)); - btVector3 neV = btVector3 (fluid->widthPos (i+1), ground[ne], fluid->lengthPos (j+1)); - debugDraw->drawTriangle (swV+com, seV+com, nwV+com, color, btScalar(1.0f)); - debugDraw->drawTriangle (seV+com, neV+com, nwV+com, color, btScalar(1.0f)); - } - } -} - -void btHfFluidRigidDynamicsWorld::drawHfFluidVelocity (btIDebugDraw* debugDraw, btHfFluid* fluid) -{ - btScalar alpha(0.7f); - const btScalar* height = fluid->getHeightArray (); - btVector3 com = fluid->getWorldTransform().getOrigin(); - btVector3 red = btVector3(btScalar(1.0f), btScalar(0.0f), btScalar(0.0)); - btVector3 green = btVector3(btScalar(0.0f), btScalar(1.0f), btScalar(0.0)); - const bool* flags = fluid->getFlagsArray (); - for (int i = 1; i < fluid->getNumNodesWidth()-1; i++) - { - for (int j = 1; j < fluid->getNumNodesLength()-1; j++) - { - int index = fluid->arrayIndex (i, j); - if (!flags[index]) - continue; - btVector3 from = btVector3 (fluid->widthPos (i), height[index]+btScalar(0.1f), fluid->lengthPos (j)); - btVector3 velocity; - velocity.setX (fluid->getVelocityUArray()[index]); - velocity.setY (btScalar(0.0f)); - velocity.setZ (fluid->getVelocityVArray()[index]); - velocity.normalize(); - btVector3 to = from + velocity; - - debugDraw->drawLine (from+com, to+com, red, green); - } - } -} - -void btHfFluidRigidDynamicsWorld::drawHfFluidBuoyantConvexShape (btIDebugDraw* debugDrawer, btCollisionObject* object, btHfFluidBuoyantConvexShape* buoyantShape, int voxelDraw) -{ - if (voxelDraw) - { - btTransform xform = object->getWorldTransform(); - for (int i = 0; i < buoyantShape->getNumVoxels(); i++) - { - btVector3 p = buoyantShape->getVoxelPositionsArray()[i]; - p = xform.getBasis() * p; - p += xform.getOrigin(); - debugDrawer->drawSphere (p, buoyantShape->getVoxelRadius(), btVector3(1.0, 0.0, 0.0)); - } - } else { - btVector3 color(btScalar(255.),btScalar(255.),btScalar(255.)); - switch(object->getActivationState()) - { - case ACTIVE_TAG: - color = btVector3(btScalar(255.),btScalar(255.),btScalar(255.)); break; - case ISLAND_SLEEPING: - color = btVector3(btScalar(0.),btScalar(255.),btScalar(0.));break; - case WANTS_DEACTIVATION: - color = btVector3(btScalar(0.),btScalar(255.),btScalar(255.));break; - case DISABLE_DEACTIVATION: - color = btVector3(btScalar(255.),btScalar(0.),btScalar(0.));break; - case DISABLE_SIMULATION: - color = btVector3(btScalar(255.),btScalar(255.),btScalar(0.));break; - default: - { - color = btVector3(btScalar(255.),btScalar(0.),btScalar(0.)); - } - }; - - const btConvexShape* convexShape = ((const btHfFluidBuoyantConvexShape*)object->getCollisionShape())->getConvexShape(); - debugDrawObject(object->getWorldTransform(),(btCollisionShape*)convexShape,color); - } -} - -void btHfFluidRigidDynamicsWorld::drawHfFluidNormal (btIDebugDraw* debugDraw, btHfFluid* fluid) -{ - int colIndex = 0; - btVector3 col[2]; - col[0] = btVector3(btScalar(0.0f), btScalar(0.0f), btScalar(1.0)); - col[1] = btVector3(btScalar(0.0f), btScalar(0.5f), btScalar(0.5)); - btScalar alpha(0.7f); - const btScalar* height = fluid->getHeightArray (); - const btScalar* eta = fluid->getEtaArray (); - const btScalar* ground = fluid->getGroundArray (); - btVector3 com = fluid->getWorldTransform().getOrigin(); - const bool* flags = fluid->getFlagsArray (); - for (int i = 0; i < fluid->getNumNodesWidth()-1; i++) - { - for (int j = 0; j < fluid->getNumNodesLength()-1; j++) - { - int sw = fluid->arrayIndex (i, j); - int se = fluid->arrayIndex (i+1, j); - int nw = fluid->arrayIndex (i, j+1); - int ne = fluid->arrayIndex (i+1, j+1); - - btScalar h = eta[sw]; - btScalar g = ground[sw]; - - if (h < btScalar(0.05f)) - continue; - - if (h <= btScalar(0.01f)) - continue; - - btVector3 boxMin = btVector3(fluid->widthPos (i), g, fluid->lengthPos(j)); - btVector3 boxMax = btVector3(fluid->widthPos(i+1), g+h, fluid->lengthPos(j+1)); - boxMin += com; - boxMax += com; - - debugDraw->drawBox (boxMin, boxMax, btVector3(btScalar(0.0f), btScalar(0.0f), btScalar(1.0f))); - } - } -} - -void btHfFluidRigidDynamicsWorld::debugDrawWorld() -{ - if (getDebugDrawer()) - { - int i; - for ( i=0;im_hfFluids.size();i++) - { - btHfFluid* phh=(btHfFluid*)this->m_hfFluids[i]; - switch (m_drawMode) - { - case DRAWMODE_NORMAL: - drawHfFluidGround (m_debugDrawer, phh); - //drawHfFluidNormal (m_debugDrawer, phh); - break; - case DRAWMODE_VELOCITY: - drawHfFluidGround (m_debugDrawer, phh); - //drawHfFluidNormal (m_debugDrawer, phh); - drawHfFluidVelocity (m_debugDrawer, phh); - break; - default: - btAssert (0); - break; - } - } - for (i = 0; i < this->m_collisionObjects.size(); i++) - { - btCollisionShape* shape = m_collisionObjects[i]->getCollisionShape(); - if (shape->getShapeType() == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE) - { - btHfFluidBuoyantConvexShape* buoyantShape = (btHfFluidBuoyantConvexShape*)shape; - drawHfFluidBuoyantConvexShape (m_debugDrawer, m_collisionObjects[i], buoyantShape, m_bodyDrawMode); - } - } - } - btDiscreteDynamicsWorld::debugDrawWorld(); -} diff --git a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.h b/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.h deleted file mode 100644 index 39fb2ebfa..000000000 --- a/Demos/HeightFieldFluidDemo/BulletHfFluid/btHfFluidRigidDynamicsWorld.h +++ /dev/null @@ -1,92 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" - -#ifndef BT_HFFLUID_RIGID_DYNAMICS_WORLD_H -#define BT_HFFLUID_RIGID_DYNAMICS_WORLD_H - -class btHfFluid; -typedef btAlignedObjectArray btHfFluidArray; - -#define DRAWMODE_NORMAL 0 -#define DRAWMODE_VELOCITY 1 -#define DRAWMODE_MAX 2 - -#define BODY_DRAWMODE_NORMAL 0 -#define BODY_DRAWMODE_VOXEL 1 -#define BODY_DRAWMODE_MAX 2 - -class btHfFluidBuoyantConvexShape; - -///experimental buyancy fluid demo -class btHfFluidRigidDynamicsWorld : public btDiscreteDynamicsWorld -{ - - btHfFluidArray m_hfFluids; - int m_drawMode; - int m_bodyDrawMode; -protected: - - virtual void predictUnconstraintMotion(btScalar timeStep); - - virtual void internalSingleStepSimulation( btScalar timeStep); - - void updateFluids(btScalar timeStep); - - void solveFluidConstraints(btScalar timeStep); - - virtual void debugDrawWorld(); - - void drawHfFluidGround (btIDebugDraw* debugDraw, btHfFluid* fluid); - void drawHfFluidVelocity (btIDebugDraw* debugDraw, btHfFluid* fluid); - void drawHfFluidBuoyantConvexShape (btIDebugDraw* debugDrawer, btCollisionObject* object, btHfFluidBuoyantConvexShape* buoyantShape, int voxelDraw); - void drawHfFluidNormal (btIDebugDraw* debugDraw, btHfFluid* fluid); -public: - - btHfFluidRigidDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration); - - virtual ~btHfFluidRigidDynamicsWorld(); - - - void addHfFluid(btHfFluid* fluid); - - void removeHfFluid(btHfFluid* fluid); - - void setDrawMode (int drawMode) - { - m_drawMode = drawMode; - } - - void setBodyDrawMode (int bodyDrawMode) - { - m_bodyDrawMode = bodyDrawMode; - } - - btHfFluidArray& getHfFluidArray() - { - return m_hfFluids; - } - - const btHfFluidArray& getHfFluidArray() const - { - return m_hfFluids; - } - -}; - -#endif //BT_HFFLUID_RIGID_DYNAMICS_WORLD_H diff --git a/Demos/HeightFieldFluidDemo/CMakeLists.txt b/Demos/HeightFieldFluidDemo/CMakeLists.txt deleted file mode 100644 index 2a2855061..000000000 --- a/Demos/HeightFieldFluidDemo/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -# This is basically the overall name of the project in Visual Studio this is the name of the Solution File - - -# For every executable you have with a main method you should have an add_executable line below. -# For every add executable line you should list every .cpp and .h file you have associated with that executable. - - - - -# You shouldn't have to modify anything below this line -######################################################## - - - -INCLUDE_DIRECTORIES( -${BULLET_PHYSICS_SOURCE_DIR}/src ${BULLET_PHYSICS_SOURCE_DIR}/Demos/OpenGL -) - -LINK_LIBRARIES( -OpenGLSupport BulletDynamics BulletCollision LinearMath ${GLUT_glut_LIBRARY} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} -) - -ADD_EXECUTABLE(AppHfFluidDemo - HfFluidDemo_GL_ShapeDrawer.cpp - BulletHfFluid/btHfFluid.cpp - BulletHfFluid/btHfFluidBuoyantConvexShape.cpp - BulletHfFluid/btHfFluidBuoyantShapeCollisionAlgorithm.cpp - BulletHfFluid/btHfFluidCollisionShape.cpp - BulletHfFluid/btHfFluidRigidCollisionAlgorithm.cpp - BulletHfFluid/btHfFluidRigidCollisionConfiguration.cpp - BulletHfFluid/btHfFluidRigidDynamicsWorld.cpp - main.cpp - HfFluidDemo.cpp -) - diff --git a/Demos/HeightFieldFluidDemo/HfFluidDemo.cpp b/Demos/HeightFieldFluidDemo/HfFluidDemo.cpp deleted file mode 100644 index 1c07777e6..000000000 --- a/Demos/HeightFieldFluidDemo/HfFluidDemo.cpp +++ /dev/null @@ -1,1457 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include "btBulletDynamicsCommon.h" - -#include "BulletHfFluid/btHfFluidRigidDynamicsWorld.h" -#include "BulletHfFluid/btHfFluid.h" -#include "BulletHfFluid/btHfFluidRigidCollisionConfiguration.h" -#include "BulletHfFluid/btHfFluidBuoyantConvexShape.h" -#include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h" -#include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h" -#include "LinearMath/btQuickprof.h" -#include "LinearMath/btIDebugDraw.h" -#include "LinearMath/btRandom.h" -#include //printf debugging -#include "LinearMath/btConvexHull.h" - -#include "HfFluidDemo.h" -#include "GL_ShapeDrawer.h" -#include "HfFluidDemo_GL_ShapeDrawer.h" - -#include "GlutStuff.h" - -extern float eye[3]; -extern int glutScreenWidth; -extern int glutScreenHeight; - -const int maxProxies = 32766; -const int maxOverlap = 65535; - -static btVector3* gGroundVertices=0; -static int* gGroundIndices=0; -static btBvhTriangleMeshShape* trimeshShape =0; -static btRigidBody* staticBody = 0; -static float waveheight = 5.f; - -const float TRIANGLE_SIZE=8.f; - -#define ARRAY_SIZE_X 1 -#define ARRAY_SIZE_Y 1 -#define ARRAY_SIZE_Z 1 - -//maximum number of objects (and allow user to shoot additional boxes) -#define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024) - -#define START_POS_X 5 -#define START_POS_Y -5 -#define START_POS_Z 3 - -unsigned int current_draw_mode=DRAWMODE_NORMAL; -unsigned int current_body_draw_mode = 0; -unsigned current_demo=0; - -void Init_Floatyness (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(0.25), 100, 100); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-10.0), btScalar(-5.0), btScalar(-10.0)); - fluid->setWorldTransform (xform); - fluid->setHorizontalVelocityScale (btScalar(0.0f)); - fluid->setVolumeDisplacementScale (btScalar(0.0f)); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->setFluidHeight(i, btScalar(5.0f)); - } - - fluid->prep (); - - const int numObjects = 5; - btScalar floatyness = btScalar(1.0f); - btScalar dfloatyness = btScalar(0.25f); - btScalar start_x = btScalar(-5.0f); - btScalar step_x = btScalar(3.0f); - btScalar start_z = btScalar(-5.0f); - for (int i = 0; i < numObjects; i++) - { - //btConvexShape* colShape = new btBoxShape(btVector3(1.0, 1.0, 1.0)); - btConvexShape* colShape = new btSphereShape(btScalar(1.)); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - buoyantShape->setFloatyness (floatyness + dfloatyness * i); - fluidDemo->m_collisionShapes.push_back (buoyantShape); - - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - btVector3 localInertia(0,0,0); - colShape->calculateLocalInertia(mass,localInertia); - - btVector3 origin = btVector3(step_x * i + start_x, 7.5f, start_z); - startTransform.setOrigin(origin); - - //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects - btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); - btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,buoyantShape,localInertia); - btRigidBody* body = new btRigidBody(rbInfo); - fluidDemo->getHfFluidDynamicsWorld()->addRigidBody(body); - } - floatyness = btScalar(2.0f); - start_z = btScalar(5.0f); - for (int i = 0; i < numObjects; i++) - { - //btConvexShape* colShape = new btBoxShape(btVector3(1.0, 1.0, 1.0)); - btConvexShape* colShape = new btSphereShape(btScalar(1.)); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - buoyantShape->setFloatyness (floatyness + dfloatyness * i); - fluidDemo->m_collisionShapes.push_back (buoyantShape); - - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - btVector3 localInertia(0,0,0); - colShape->calculateLocalInertia(mass,localInertia); - - btVector3 origin = btVector3(step_x * i + start_x, -4.0f, start_z); - startTransform.setOrigin(origin); - - //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects - btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); - btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,buoyantShape,localInertia); - btRigidBody* body = new btRigidBody(rbInfo); - fluidDemo->getHfFluidDynamicsWorld()->addRigidBody(body); - } -} - -void Init_Bowl (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0), 50, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-10.0), btScalar(-5.0), btScalar(-10.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - btScalar* ground = fluid->getGroundArray(); - btScalar* eta = fluid->getEtaArray(); - btScalar amplitude = btScalar(200.0); - for (int i = 0; i < fluid->getNumNodesWidth(); i++) - { - btScalar x = btScalar(i - fluid->getNumNodesWidth()/2)/btScalar(fluid->getNumNodesWidth()*2); - btScalar xh = amplitude * (x * x) + btScalar(5.0); - for (int j = 0; j < fluid->getNumNodesLength(); j++) - { - btScalar y = btScalar(j - fluid->getNumNodesLength()/2)/btScalar(fluid->getNumNodesLength()*2); - btScalar yh = amplitude * (y * y) + btScalar(5.0); - btScalar gHeight = btMax(xh,yh); - int index = fluid->arrayIndex (i, j); - ground[index] = gHeight; - btScalar wHeight = btScalar(0.0f); - if (gHeight > 14.0) - { - wHeight = btScalar(0.0f); - } else { - wHeight = btScalar(14.0f) - gHeight; - } - eta[index] = wHeight; } - } - - fluid->prep (); - - { - //create a few dynamic rigidbodies - // Re-using the same collision is better for memory usage and performance - - btConvexShape* colShape = new btBoxShape(btVector3(1,1,1)); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); - fluidDemo->m_collisionShapes.push_back(colShape); - fluidDemo->m_collisionShapes.push_back (buoyantShape); - - /// Create Dynamic Objects - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - bool isDynamic = (mass != 0.f); - - btVector3 localInertia(0,0,0); - if (isDynamic) - colShape->calculateLocalInertia(mass,localInertia); - - float start_x = START_POS_X - ARRAY_SIZE_X/2; - float start_y = START_POS_Y; - float start_z = START_POS_Z - ARRAY_SIZE_Z/2; - - for (int k=0;kgetHfFluidDynamicsWorld()->addRigidBody(body); - } - } - } - } -} - -void Init_Drops (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(0.5), 50, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-10.0), btScalar(-5.0), btScalar(-10.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->setFluidHeight(i, btScalar(5.0f)); - } - - fluid->prep (); - { - //create a few dynamic rigidbodies - // Re-using the same collision is better for memory usage and performance - - btConvexShape* colShape = new btBoxShape(btVector3(5,0.5,5)); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); - fluidDemo->m_collisionShapes.push_back(colShape); - fluidDemo->m_collisionShapes.push_back (buoyantShape); - - /// Create Dynamic Objects - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - bool isDynamic = (mass != 0.f); - - btVector3 localInertia(0,0,0); - if (isDynamic) - colShape->calculateLocalInertia(mass,localInertia); - - float start_x = START_POS_X - ARRAY_SIZE_X/2; - float start_y = START_POS_Y; - float start_z = START_POS_Z - ARRAY_SIZE_Z/2; - - for (int k=0;kgetHfFluidDynamicsWorld()->addRigidBody(body); - } - } - } - } -} - -void Init_Wave (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0f), 75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->getEtaArray()[i] = btScalar(10.0f); - } - - for (int i = 1; i < fluid->getNumNodesWidth()-1; i++) - { - fluid->getEtaArray()[fluid->arrayIndex (i, fluid->getNumNodesLength()/2-1)] = btScalar (2.0); - fluid->getEtaArray()[fluid->arrayIndex (i, fluid->getNumNodesLength()/2)] = btScalar (2.0); - fluid->getEtaArray()[fluid->arrayIndex (i, fluid->getNumNodesLength()/2+1)] = btScalar (2.0); - } - - fluid->prep (); -} - -void Init_RandomDrops (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0),75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->getEtaArray()[i] = btScalar(0.0f); - } - fluid->prep (); -} - -void Init_FillPool (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - const int gridLength = 50; - const int gridWidth = 50; - fluid = new btHfFluid (btScalar(1.0), gridLength, gridWidth); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-20.0), btScalar(-5.0), btScalar(-20.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - btScalar* ground = fluid->getGroundArray(); - btScalar* eta = fluid->getEtaArray(); - - const btScalar poolEdgeHeight = btScalar(10.0f); - const btScalar poolBottomHeight = btScalar(1.0f); - const btScalar poolPourerHeight = btScalar(6.0f); - for (int j = 1; j < fluid->getNumNodesLength()-1; j++) - { - for (int i = 1; i < fluid->getNumNodesWidth()-1; i++) - { - int index = fluid->arrayIndex (i, j); - // pool edge - if (j == 1 || i == 1 || j == fluid->getNumNodesLength()-2 || i == fluid->getNumNodesWidth()-2) - { - ground[index] = poolEdgeHeight; - continue; - } - if (j > 35) - { - if (i <= 25 || i >= 30) - { - ground[index] = poolEdgeHeight; - } else { - ground[index] = poolPourerHeight; - } - continue; - } - ground[index] = poolBottomHeight; - //eta[index] = btScalar(3.0f); - } - } - fluid->prep (); - - { - btConvexShape* colShape = new btBoxShape(btVector3(btScalar(1.), btScalar(1.), btScalar(1.))); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - fluidDemo->m_collisionShapes.push_back(colShape); - fluidDemo->m_collisionShapes.push_back(buoyantShape); - - /// Create Dynamic Objects - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - bool isDynamic = (mass != 0.f); - - btVector3 localInertia(0,0,0); - if (isDynamic) - colShape->calculateLocalInertia(mass,localInertia); - - int gridSize = 2; - btScalar startPosX = btScalar(-10.0f); - btScalar startPosY = btScalar(2.0f); - btScalar startPosZ = btScalar(-10.f); - float start_x = startPosX - gridSize/2; - float start_y = startPosY; - float start_z = startPosZ - gridSize/2; - - for (int k=0;kgetHfFluidDynamicsWorld()->addRigidBody(body); - } - } - } - } -} - -void Run_FillPool (HfFluidDemo* fluidDemo) -{ - static btScalar dtSinceLastDrop = btScalar(0.0f); - btScalar dt = btScalar(1.0/60.); - btHfFluidArray& fluids = fluidDemo->getHfFluidDynamicsWorld ()->getHfFluidArray (); - btHfFluid* fluid = fluids[0]; - - for (int i = 26; i < 30; i++) - { - fluid->setFluidHeight (i, fluid->getNumNodesLength()-3, btScalar(3.0f)); - } -} - - -void Run_RandomDrops (HfFluidDemo* fluidDemo) -{ - static btScalar dtSinceLastDrop = btScalar(0.0f); - btScalar dt = btScalar(1.0/60.); - - - btHfFluidArray& fluids = fluidDemo->getHfFluidDynamicsWorld ()->getHfFluidArray (); - btHfFluid* fluid = fluids[0]; - - if (dtSinceLastDrop > btScalar(0.5f)) - { - dtSinceLastDrop = btScalar(0.0f); - int randomXNode = GEN_rand () % (fluid->getNumNodesWidth()-2); - int randomZNode = GEN_rand () % (fluid->getNumNodesLength()-2); - if (randomXNode <= 1) - randomXNode = 2; - if (randomZNode <= 1) - randomZNode = 2; - - btScalar* eta = fluid->getEtaArray (); - btScalar* height = fluid->getHeightArray (); - const btScalar* ground = fluid->getGroundArray (); - bool* flags = fluid->getFlagsArray(); - int index = fluid->arrayIndex (randomXNode, randomZNode); - eta[index] += btScalar(4.5f); - eta[index-1] += btScalar(2.25f); - eta[index+1] += btScalar(2.25f); - eta[index+fluid->getNumNodesWidth()] += btScalar(2.25f); - eta[index-fluid->getNumNodesWidth()] += btScalar(2.25f); - height[index] = eta[index] + ground[index]; - height[index-1] = eta[index-1] + ground[index-1]; - height[index+1] = eta[index+1] + ground[index+1]; - height[index+fluid->getNumNodesWidth()] = eta[index+fluid->getNumNodesWidth()] + ground[index+fluid->getNumNodesWidth()]; - height[index-fluid->getNumNodesWidth()] = eta[index-fluid->getNumNodesWidth()] + ground[index-fluid->getNumNodesWidth()]; - flags[index] = true; - flags[index-1] = true; - flags[index+1] = true; - flags[index+fluid->getNumNodesWidth()] = true; - flags[index-fluid->getNumNodesWidth()] = true; - } else { - dtSinceLastDrop += dt; - } -} - -void Init_Fill (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0f), 75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->getEtaArray()[i] = btScalar(0.0f); - } - fluid->prep (); -} - -void Run_Fill (HfFluidDemo* fluidDemo) -{ - static btScalar dtSinceLastDrop = btScalar(0.0f); - btScalar dt = btScalar(1.0/60.); - - btHfFluidArray& fluids = fluidDemo->getHfFluidDynamicsWorld ()->getHfFluidArray (); - btHfFluid* fluid = fluids[0]; - - if (dtSinceLastDrop > btScalar(0.25f)) - { - dtSinceLastDrop = btScalar(0.0f); - - btScalar* eta = fluid->getEtaArray (); - btScalar* velocityU = fluid->getVelocityUArray (); - btScalar* velocityV = fluid->getVelocityVArray (); - btScalar* height = fluid->getHeightArray (); - const btScalar* ground = fluid->getGroundArray (); - bool* flags = fluid->getFlagsArray(); - int index = fluid->arrayIndex (fluid->getNumNodesWidth()/2, fluid->getNumNodesLength()/2); - eta[index] += btScalar(4.5f); - eta[index-1] += btScalar(2.25f); - eta[index+1] += btScalar(2.25f); - eta[index+fluid->getNumNodesWidth()] += btScalar(2.25f); - eta[index-fluid->getNumNodesWidth()] += btScalar(2.25f); - - velocityU[index] = btScalar(0.0f); - velocityU[index-1] = btScalar(-10.0f); - velocityU[index+1] = btScalar(10.0f); - velocityU[index+fluid->getNumNodesWidth()] = btScalar(0.0f); - velocityU[index-fluid->getNumNodesWidth()] = btScalar(0.0f); - - velocityV[index] = btScalar(0.0f); - velocityV[index-1] = btScalar(0.0f); - velocityV[index+1] = btScalar(0.0f); - velocityV[index+fluid->getNumNodesWidth()] = btScalar(10.0f); - velocityV[index-fluid->getNumNodesWidth()] = btScalar(-10.0f); - - height[index] = eta[index] + ground[index]; - height[index-1] = eta[index-1] + ground[index-1]; - height[index+1] = eta[index+1] + ground[index+1]; - height[index+fluid->getNumNodesWidth()] = eta[index+fluid->getNumNodesWidth()] + ground[index+fluid->getNumNodesWidth()]; - height[index-fluid->getNumNodesWidth()] = eta[index-fluid->getNumNodesWidth()] + ground[index-fluid->getNumNodesWidth()]; - flags[index] = true; - flags[index-1] = true; - flags[index+1] = true; - flags[index+fluid->getNumNodesWidth()] = true; - flags[index-fluid->getNumNodesWidth()] = true; - } else { - dtSinceLastDrop += dt; - } - -} - -void Init_BlockWave (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0), 75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - btScalar* eta = fluid->getEtaArray (); - - for (int i = 0; i < fluid->getNumNodesLength() * fluid->getNumNodesWidth(); i++) - { - eta[i] = btScalar(12.0f); - } - - for (int i = fluid->getNumNodesWidth()/8; i < fluid->getNumNodesWidth()/4; i++) - { - for (int j = fluid->getNumNodesLength()/8; j < fluid->getNumNodesLength()/4; j++) - { - int index = fluid->arrayIndex(i, j); - eta[index] = btScalar(4.0f); - } - } - fluid->prep (); - - { - btConvexShape* colShape = new btSphereShape(btScalar(1.)); - btHfFluidBuoyantConvexShape* buoyantShape = new btHfFluidBuoyantConvexShape(colShape); - buoyantShape->generateShape (btScalar(0.25f), btScalar(0.05f)); - fluidDemo->m_collisionShapes.push_back(buoyantShape); - fluidDemo->m_collisionShapes.push_back(colShape); - - /// Create Dynamic Objects - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - bool isDynamic = (mass != 0.f); - - btVector3 localInertia(0,0,0); - if (isDynamic) - colShape->calculateLocalInertia(mass,localInertia); - - int gridSize = 2; - btScalar startPosX = btScalar(-10.0f); - btScalar startPosY = btScalar(2.0f); - btScalar startPosZ = btScalar(-10.f); - float start_x = startPosX - gridSize/2; - float start_y = startPosY; - float start_z = startPosZ - gridSize/2; - - for (int k=0;kgetHfFluidDynamicsWorld()->addRigidBody(body); - } - } - } - } -} - -void Init_Ground (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0f),75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - btScalar* eta = fluid->getEtaArray (); - - for (int i = 0; i < fluid->getNumNodesLength() * fluid->getNumNodesWidth(); i++) - { - eta[i] = btScalar(4.0f); - } - - btScalar* ground = fluid->getGroundArray (); - for (int i = 0; i < fluid->getNumNodesWidth(); i++) - { - for (int j = 0; j < fluid->getNumNodesLength(); j++) - { - int index = fluid->arrayIndex (i, j); - - if (j <= fluid->getNumNodesLength()/2) - { - - ground[index] = btScalar(5.0f); - } else if (j > (fluid->getNumNodesLength()/8*6)) { - ground[index] = btScalar(0.0f); - } else { - ground[index] = btScalar(6.5f); - } - - if (j <= fluid->getNumNodesLength()/4 && j > fluid->getNumNodesLength()/8) - { - eta[index] = btScalar(8.0f); - } else if (j <= fluid->getNumNodesLength()/8) - { - eta[index] = btScalar(20.0f); - } else { - eta[index] = btScalar(0.0f); - } - - } - } - fluid->prep (); -} - -void Init_Ground2 (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0f), 75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - btScalar* eta = fluid->getEtaArray (); - - for (int i = 0; i < fluid->getNumNodesLength() * fluid->getNumNodesWidth(); i++) - { - eta[i] = btScalar(4.0f); - } - - btScalar* ground = fluid->getGroundArray (); - for (int i = 0; i < fluid->getNumNodesWidth(); i++) - { - for (int j = 0; j < fluid->getNumNodesLength(); j++) - { - int index = fluid->arrayIndex (i, j); - - ground[index] = (btScalar(j)/fluid->getNumNodesLength()-1)*btScalar(8.0f); - } - } - - for (int i = 0; i < fluid->getNumNodesLength() * fluid->getNumNodesWidth(); i++) - { - eta[i] = btScalar(2.0f); - } - - for (int i = fluid->getNumNodesWidth()/8; i < fluid->getNumNodesWidth()/4; i++) - { - for (int j = fluid->getNumNodesLength()/8; j < fluid->getNumNodesLength()/4; j++) - { - int index = fluid->arrayIndex(i, j); - eta[index] = btScalar(8.0f); - } - } - fluid->prep (); -} - -void Init_Fill2 (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0), 100, 100); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->getEtaArray()[i] = btScalar(0.0f); - } - fluid->prep (); -} - -void Run_Fill2 (HfFluidDemo* fluidDemo) -{ - static btScalar dtSinceLastDrop = btScalar(0.0f); - btScalar dt = btScalar(1.0/60.); - - btHfFluidArray& fluids = fluidDemo->getHfFluidDynamicsWorld ()->getHfFluidArray (); - btHfFluid* fluid = fluids[0]; - - if (dtSinceLastDrop > btScalar(0.25f)) - { - dtSinceLastDrop = btScalar(0.0f); - - btScalar* eta = fluid->getEtaArray (); - btScalar* velocityU = fluid->getVelocityUArray (); - btScalar* velocityV = fluid->getVelocityVArray (); - btScalar* height = fluid->getHeightArray (); - const btScalar* ground = fluid->getGroundArray (); - bool* flags = fluid->getFlagsArray(); - - for (int i = 1; i < fluid->getNumNodesWidth()-1; i++) - { - int index = fluid->arrayIndex (i, 1); - eta[index] += btScalar(3.0f); - velocityU[index] = btScalar(4.0f); - height[index] = ground[index] + eta[index]; - flags[index] = true; - } - } else { - dtSinceLastDrop += dt; - } - -} - -void Init_MovingPour (HfFluidDemo* fluidDemo) -{ - btHfFluid* fluid = NULL; - - fluid = new btHfFluid (btScalar(1.0),75, 50); - btTransform xform; - xform.setIdentity (); - xform.getOrigin() = btVector3(btScalar(-50.0), btScalar(-5.0), btScalar(-50.0)); - fluid->setWorldTransform (xform); - fluidDemo->getHfFluidDynamicsWorld()->addHfFluid (fluid); - - for (int i = 0; i < fluid->getNumNodesLength()*fluid->getNumNodesWidth(); i++) - { - fluid->getEtaArray()[i] = btScalar(5.0f); - } - - - fluid->prep (); - { - //create a few dynamic rigidbodies - // Re-using the same collision is better for memory usage and performance - - btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1)); - //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); - fluidDemo->m_collisionShapes.push_back(colShape); - - /// Create Dynamic Objects - btTransform startTransform; - startTransform.setIdentity(); - - btScalar mass(1.f); - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - bool isDynamic = (mass != 0.f); - - btVector3 localInertia(0,0,0); - if (isDynamic) - colShape->calculateLocalInertia(mass,localInertia); - - float start_x = START_POS_X - ARRAY_SIZE_X/2; - float start_y = START_POS_Y; - float start_z = START_POS_Z - ARRAY_SIZE_Z/2; - - for (int k=0;kgetHfFluidDynamicsWorld()->addRigidBody(body); - } - } - } - } -} - -void Run_MovingPour(HfFluidDemo* fluidDemo) -{ - static btScalar dtSinceLastDrop = btScalar(0.0f); - static btScalar x = 4; - static btScalar z = 4; - static btScalar dx = btScalar(20.0f); - static btScalar dz = btScalar(30.0f); - btScalar dt = btScalar(1.0/60.); - - btHfFluidArray& fluids = fluidDemo->getHfFluidDynamicsWorld ()->getHfFluidArray (); - btHfFluid* fluid = fluids[0]; - - int minX = 2; - int minZ = 2; - int maxX = fluid->getNumNodesWidth() - 2; - int maxZ = fluid->getNumNodesLength() - 2; - - x += dx * dt; - - if (x <= minX) - { - dx *= btScalar(-1.0f); - x = minX; - } else if (x >= maxX) { - dx *= btScalar(-1.0f); - x = maxX; - } - z += dz * dt; - - if (z <= minZ) - { - dz *= btScalar(-1.0f); - z = minZ; - } else if (z >= maxZ) { - dz *= btScalar(-1.0f); - z = maxZ; - } - - const btScalar dropHeight = btScalar(3.0f); - - { - int iX = (int)x; - int iZ = (int)z; - fluid->addFluidHeight (iX,iZ, dropHeight); - //fluid->addFluidHeight (x, z+1, dropHeight); - //fluid->addFluidHeight (x+1, z, dropHeight); - //fluid->addFluidHeight (x+1, z+1, dropHeight); - } -} - -#define NUM_DEMOS 12 - -void (*demo_run_functions[NUM_DEMOS])(HfFluidDemo*)= -{ - NULL, // Run_Floatyness - NULL, // Run_Bowl - Run_FillPool, //Run_FillPool - NULL, // Run_Drops - NULL, // Run_Wave - Run_RandomDrops, - Run_Fill, - Run_Fill2, - NULL, // Run_BlockWave - NULL, // Run_Ground - NULL, // Run_Ground2 - Run_MovingPour, -}; -void (*demo_init_functions[NUM_DEMOS])(HfFluidDemo*)= -{ - Init_Floatyness, - Init_Bowl, - Init_FillPool, - Init_Drops, - Init_Wave, - Init_RandomDrops, - Init_Fill, - Init_Fill2, - Init_BlockWave, - Init_Ground, - Init_Ground2, - Init_MovingPour, -}; - -btScalar g_ele_array[NUM_DEMOS] = { - btScalar(10), - btScalar(45), - btScalar(35), - btScalar(35), - btScalar(10), - btScalar(10), - btScalar(35), - btScalar(45), - btScalar(35), - btScalar(20), - btScalar(20), -}; - -btScalar g_azi_array[NUM_DEMOS] = { - btScalar(0), - btScalar(55), - btScalar(245), - btScalar(270), - btScalar(55), - btScalar(55), - btScalar(180), - btScalar(205), - btScalar(255), - btScalar(305), - btScalar(305), -}; - -btScalar g_cameraDistance_array[NUM_DEMOS] = { - btScalar(20), - btScalar(29), - btScalar(43), - btScalar(26), - btScalar(77), - btScalar(77), - btScalar(77), - btScalar(32), - btScalar(62), - btScalar(70), - btScalar(70), -}; - -#ifdef _DEBUG -const int gNumObjects = 1; -#else -const int gNumObjects = 1;//try this in release mode: 3000. never go above 16384, unless you increate maxNumObjects value in DemoApplication.cp -#endif - -const int maxNumObjects = 32760; - -#define CUBE_HALF_EXTENTS 1.5 -#define EXTRA_HEIGHT -10.f - -// -void HfFluidDemo::createStack( btCollisionShape* boxShape, float halfCubeSize, int size, float zPos ) -{ - btTransform trans; - trans.setIdentity(); - - for(int i=0; igenerateShape (btScalar(0.25f), btScalar(0.05f)); - m_shootBoxShape = buoyantShape; - } -} - -//////////////////////////////////// - -extern int gNumManifold; -extern int gOverlappingPairs; - -void HfFluidDemo::clientMoveAndDisplay() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); - - - float dt = 1.0/60.; - - if (m_dynamicsWorld) - { - if (demo_run_functions[current_demo]) - { - demo_run_functions[current_demo](this); - } - } - - if (m_dynamicsWorld) - { - if(m_drag) - { - const int x=m_lastmousepos[0]; - const int y=m_lastmousepos[1]; - const btVector3 rayFrom=m_cameraPosition; - const btVector3 rayTo=getRayTo(x,y); - const btVector3 rayDir=(rayTo-rayFrom).normalized(); - const btVector3 N=(m_cameraTargetPosition-m_cameraPosition).normalized(); - const btScalar O=btDot(m_impact,N); - const btScalar den=btDot(N,rayDir); - if((den*den)>0) - { - const btScalar num=O-btDot(N,rayFrom); - const btScalar hit=num/den; - if((hit>0)&&(hit<1500)) - { - m_goal=rayFrom+rayDir*hit; - } - } - btVector3 delta; - static const btScalar maxdrag=10; - if(delta.length2()>(maxdrag*maxdrag)) - { - delta=delta.normalized()*maxdrag; - } - } - -#define FIXED_STEP -#ifdef FIXED_STEP - m_dynamicsWorld->stepSimulation(dt=1.0f/60.f,0); - -#else - //during idle mode, just run 1 simulation step maximum, otherwise 4 at max - int maxSimSubSteps = m_idle ? 1 : 4; - //if (m_idle) - // dt = 1.0/420.f; - - int numSimSteps; - numSimSteps = m_dynamicsWorld->stepSimulation(dt); - -#ifdef VERBOSE_TIMESTEPPING_CONSOLEOUTPUT - if (!numSimSteps) - printf("Interpolated transforms\n"); - else - { - if (numSimSteps > maxSimSubSteps) - { - //detect dropping frames - printf("Dropped (%i) simulation steps out of %i\n",numSimSteps - maxSimSubSteps,numSimSteps); - } else - { - printf("Simulated (%i) steps\n",numSimSteps); - } - } -#endif //VERBOSE_TIMESTEPPING_CONSOLEOUTPUT - -#endif - - //optional but useful: debug drawing - - } - -#ifdef USE_QUICKPROF - btProfiler::beginBlock("render"); -#endif //USE_QUICKPROF - - renderme(); - - //render the graphics objects, with center of mass shift - - updateCamera(); - - - -#ifdef USE_QUICKPROF - btProfiler::endBlock("render"); -#endif - glFlush(); - //some additional debugging info -#ifdef PRINT_CONTACT_STATISTICS - printf("num manifolds: %i\n",gNumManifold); - printf("num gOverlappingPairs: %i\n",gOverlappingPairs); - printf("num gTotalContactPoints : %i\n",gTotalContactPoints ); -#endif //PRINT_CONTACT_STATISTICS - - //gTotalContactPoints = 0; - glutSwapBuffers(); - -} - - - -void HfFluidDemo::displayCallback(void) { - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - - renderme(); - - glFlush(); - glutSwapBuffers(); -} - - - - -void HfFluidDemo::clientResetScene() -{ - DemoApplication::clientResetScene(); - /* Clean up */ - for(int i=m_dynamicsWorld->getNumCollisionObjects()-1;i>0;i--) - { - btCollisionObject* obj=m_dynamicsWorld->getCollisionObjectArray()[i]; - btRigidBody* body=btRigidBody::upcast(obj); - if(body&&body->getMotionState()) - { - delete body->getMotionState(); - } - while(m_dynamicsWorld->getNumConstraints()) - { - btTypedConstraint* pc=m_dynamicsWorld->getConstraint(0); - m_dynamicsWorld->removeConstraint(pc); - delete pc; - } - btHfFluid* hfFluid = btHfFluid::upcast(obj); - if (hfFluid) - { - getHfFluidDynamicsWorld()->removeHfFluid(hfFluid); - } else - { - m_dynamicsWorld->removeCollisionObject(obj); - } - delete obj; - } - - /* Init */ - - - m_autocam = false; - m_raycast = false; - m_cutting = false; - printf("current_demo = %d\n", current_demo); - m_azi = g_azi_array[current_demo]; - m_ele = g_ele_array[current_demo]; - m_cameraDistance = g_cameraDistance_array[current_demo]; - updateCamera(); - demo_init_functions[current_demo](this); -} - -void HfFluidDemo::renderme() -{ - btIDebugDraw* idraw=m_dynamicsWorld->getDebugDrawer(); - - m_dynamicsWorld->debugDrawWorld(); - - /* Bodies */ - btVector3 ps(0,0,0); - int nps=0; - - DemoApplication::renderme(); -} - - -void HfFluidDemo::keyboardCallback(unsigned char key, int x, int y) -{ - switch(key) - { - case ']': - current_demo = (current_demo+1)%NUM_DEMOS; - clientResetScene(); - break; - case '[': - current_demo = (current_demo-1)%NUM_DEMOS; - clientResetScene(); - break; - case '.': - current_draw_mode = (current_draw_mode+1) % DRAWMODE_MAX; - getHfFluidDynamicsWorld()->setDrawMode (current_draw_mode); - break; - case 'v': - current_body_draw_mode = (current_body_draw_mode+1) % BODY_DRAWMODE_MAX; - getHfFluidDynamicsWorld()->setBodyDrawMode (current_body_draw_mode); - break; - default: - DemoApplication::keyboardCallback(key,x,y); - break; - } -} - -// -void HfFluidDemo::mouseMotionFunc(int x,int y) -{ - DemoApplication::mouseMotionFunc(x,y); -} - -// -void HfFluidDemo::mouseFunc(int button, int state, int x, int y) -{ -if(button==0) - { - switch(state) - { - case 0: - { - DemoApplication::mouseFunc(button,state,x,y); - } - break; - case 1: - DemoApplication::mouseFunc(button,state,x,y); - break; - } - } - else - { - DemoApplication::mouseFunc(button,state,x,y); - } -} - - -void HfFluidDemo::initPhysics() -{ -///create concave ground mesh - - btCollisionShape* groundShape = 0; - bool useConcaveMesh = false;//not ready yet true; - - if (useConcaveMesh) - { - int i; - int j; - - const int NUM_VERTS_X = 30; - const int NUM_VERTS_Y = 30; - const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y; - const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1); - - gGroundVertices = new btVector3[totalVerts]; - gGroundIndices = new int[totalTriangles*3]; - - btScalar offset(-50); - - for ( i=0;iaddChildShape(localTransform,cylinderShape); - btQuaternion orn(btVector3(0,1,0),SIMD_PI); - localTransform.setRotation(orn); - cylinderCompound->addChildShape(localTransform,cylinderShape); - - m_collisionShapes.push_back(cylinderCompound); - - - m_dispatcher=0; - - /* FIXME: Register new collision algorithm */ - ///register some softbody collision algorithms on top of the default btDefaultCollisionConfiguration - m_collisionConfiguration = new btHfFluidRigidCollisionConfiguration(); - - - m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); - - //////////////////////////// - ///Register HfFluid versus rigidbody collision algorithm - - - btVector3 worldAabbMin(-1000,-1000,-1000); - btVector3 worldAabbMax(1000,1000,1000); - - m_broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); - - btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver(); - - m_solver = solver; - - btDiscreteDynamicsWorld* world = new btHfFluidRigidDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); - m_dynamicsWorld = world; - - - m_dynamicsWorld->getDispatchInfo().m_enableSPU = true; - m_dynamicsWorld->setGravity(btVector3(0,-10,0)); - - btTransform tr; - tr.setIdentity(); - tr.setOrigin(btVector3(0,-12,0)); - - - - localCreateRigidBody(0.f,tr,m_collisionShapes[0]); - - - // clientResetScene(); - - clientResetScene(); -} - - - - - - -void HfFluidDemo::exitPhysics() -{ - - //cleanup in the reverse order of creation/initialization - - //remove the rigidbodies from the dynamics world and delete them - int i; - for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) - { - btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; - btRigidBody* body = btRigidBody::upcast(obj); - if (body && body->getMotionState()) - { - delete body->getMotionState(); - } - m_dynamicsWorld->removeCollisionObject( obj ); - delete obj; - } - - //delete collision shapes - for (int j=0;j m_FluidRigidCollisionAlgorithms; - - - bool m_autocam; - bool m_cutting; - bool m_raycast; - btScalar m_animtime; - btClock m_clock; - int m_lastmousepos[2]; - btVector3 m_impact; - btVector3 m_goal; - bool m_drag; - - - //keep the collision shapes, for deletion/cleanup - btAlignedObjectArray m_collisionShapes; - - btBroadphaseInterface* m_broadphase; - - btCollisionDispatcher* m_dispatcher; - - - btConstraintSolver* m_solver; - - btCollisionAlgorithmCreateFunc* m_boxBoxCF; - - btDefaultCollisionConfiguration* m_collisionConfiguration; - -public: - - void initPhysics(); - - void exitPhysics(); - - HfFluidDemo (); - - virtual ~HfFluidDemo() - { - exitPhysics(); - } - - virtual void setDrawClusters(bool drawClusters) - { - - } - - virtual void setShootBoxShape (); - - virtual void clientMoveAndDisplay(); - - virtual void displayCallback(); - - void createStack( btCollisionShape* boxShape, float halfCubeSize, int size, float zPos ); - - static DemoApplication* Create() - { - HfFluidDemo* demo = new HfFluidDemo; - demo->myinit(); - demo->initPhysics(); - return demo; - } - - virtual const btHfFluidRigidDynamicsWorld* getHfFluidDynamicsWorld() const - { - ///just make it a btSoftRigidDynamicsWorld please - ///or we will add type checking - return (btHfFluidRigidDynamicsWorld*) m_dynamicsWorld; - } - - virtual btHfFluidRigidDynamicsWorld* getHfFluidDynamicsWorld() - { - ///just make it a btSoftRigidDynamicsWorld please - ///or we will add type checking - return (btHfFluidRigidDynamicsWorld*) m_dynamicsWorld; - } - - // - void clientResetScene(); - void renderme(); - void keyboardCallback(unsigned char key, int x, int y); - void mouseFunc(int button, int state, int x, int y); - void mouseMotionFunc(int x,int y); - -}; - -#define MACRO_SOFT_DEMO(a) class HfFluidDemo##a : public HfFluidDemo\ -{\ -public:\ - static DemoApplication* Create()\ - {\ - HfFluidDemo* demo = new HfFluidDemo##a;\ - extern unsigned int current_demo;\ - current_demo=a;\ - demo->myinit();\ - demo->initPhysics();\ - return demo;\ - }\ -}; - - -MACRO_SOFT_DEMO(0) //Init_Drops -MACRO_SOFT_DEMO(1) //Init_Wave -MACRO_SOFT_DEMO(2) //Init_RandomDrops -MACRO_SOFT_DEMO(3) - -#endif //CCD_PHYSICS_DEMO_H - - - - - diff --git a/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.cpp b/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.cpp deleted file mode 100644 index 0f7087776..000000000 --- a/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.cpp +++ /dev/null @@ -1,606 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifdef _WIN32 //needed for glut.h -#include -#endif - -//think different -#if defined(__APPLE__) && !defined (VMDMESA) -#include -#include -#include -#else -#include -#endif - -#include "GlutStuff.h" -#include "HfFluidDemo_GL_ShapeDrawer.h" - -#include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h" -#include "BulletCollision/CollisionShapes/btTriangleMeshShape.h" -#include "BulletCollision/CollisionShapes/btBoxShape.h" -#include "BulletCollision/CollisionShapes/btSphereShape.h" -#include "BulletCollision/CollisionShapes/btConeShape.h" -#include "BulletCollision/CollisionShapes/btCylinderShape.h" -#include "BulletCollision/CollisionShapes/btTetrahedronShape.h" -#include "BulletCollision/CollisionShapes/btCompoundShape.h" -#include "BulletCollision/CollisionShapes/btCapsuleShape.h" -#include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" -#include "BulletCollision/CollisionShapes/btUniformScalingShape.h" -#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h" -/// -#include "BulletCollision/CollisionShapes/btShapeHull.h" -#include "BulletHfFluid/btHfFluidBuoyantConvexShape.h" -#include "BulletHfFluid/btHfFluid.h" -#include "BulletHfFluid/btHfFluidCollisionShape.h" - -#include "LinearMath/btTransformUtil.h" - - -#include "LinearMath/btIDebugDraw.h" -//for debugmodes -#include //printf debugging - -#include - -using namespace std; - - -class GlDrawcallback : public btTriangleCallback -{ - -public: - - bool m_wireframe; - - GlDrawcallback() - :m_wireframe(false) - { - } - - virtual void processTriangle(btVector3* triangle,int partId, int triangleIndex) - { - - (void)triangleIndex; - (void)partId; - - - if (m_wireframe) - { - glBegin(GL_LINES); - glColor3f(1, 0, 0); - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glColor3f(0, 1, 0); - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glColor3f(0, 0, 1); - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glEnd(); - } else - { - glBegin(GL_TRIANGLES); - //glColor3f(1, 1, 1); - - - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glEnd(); - } - } -}; - -class TriangleGlDrawcallback : public btInternalTriangleIndexCallback -{ -public: - virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex) - { - (void)triangleIndex; - (void)partId; - - - glBegin(GL_TRIANGLES);//LINES); - glColor3f(1, 0, 0); - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glColor3f(0, 1, 0); - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ()); - glColor3f(0, 0, 1); - glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ()); - glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ()); - glEnd(); - } -}; - - - -void HfFluidDemo_GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color,int debugMode,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax) -{ - - - glPushMatrix(); - btglMultMatrix(m); - - if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE) - { - const btUniformScalingShape* scalingShape = static_cast(shape); - const btConvexShape* convexShape = scalingShape->getChildShape(); - float scalingFactor = (float)scalingShape->getUniformScalingFactor(); - { - btScalar tmpScaling[4][4]={{scalingFactor,0,0,0}, - {0,scalingFactor,0,0}, - {0,0,scalingFactor,0}, - {0,0,0,1}}; - - drawOpenGL( (btScalar*)tmpScaling,convexShape,color,debugMode,worldBoundsMin,worldBoundsMax); - } - glPopMatrix(); - return; - } - - if (shape->getShapeType() == HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE) - { - const btConvexShape* convexShape = ((btHfFluidBuoyantConvexShape*)shape)->getConvexShape(); - btTransform I; - I.setIdentity(); - btScalar mat[16]; - I.getOpenGLMatrix (&mat[0]); - drawOpenGL (mat, convexShape, color, debugMode, worldBoundsMin, worldBoundsMax); - return; - } - - if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) - { - const btCompoundShape* compoundShape = static_cast(shape); - for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--) - { - btTransform childTrans = compoundShape->getChildTransform(i); - const btCollisionShape* colShape = compoundShape->getChildShape(i); - btScalar childMat[16]; - childTrans.getOpenGLMatrix(childMat); - drawOpenGL(childMat,colShape,color,debugMode,worldBoundsMin,worldBoundsMax); - - } - - } else - { - if(m_textureenabled&&(!m_textureinitialized)) - { - GLubyte* image=new GLubyte[256*256*3]; - for(int y=0;y<256;++y) - { - const int t=y>>4; - GLubyte* pi=image+y*256*3; - for(int x=0;x<256;++x) - { - const int s=x>>4; - const GLubyte b=180; - GLubyte c=b+((s+t&1)&1)*(255-b); - pi[0]=pi[1]=pi[2]=c;pi+=3; - } - } - glGenTextures(1,(GLuint*)&m_texturehandle); - glBindTexture(GL_TEXTURE_2D,m_texturehandle); - glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); - gluBuild2DMipmaps(GL_TEXTURE_2D,3,256,256,GL_RGB,GL_UNSIGNED_BYTE,image); - delete[] image; - - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glScalef(0.025,0.025,0.025); - - static const GLfloat planex[]={1,0,0,0}; - static const GLfloat planey[]={0,1,0,0}; - static const GLfloat planez[]={0,0,1,0}; - glTexGenfv(GL_S,GL_OBJECT_PLANE,planex); - glTexGenfv(GL_T,GL_OBJECT_PLANE,planez); - glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR); - glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR); - glEnable(GL_TEXTURE_GEN_S); - glEnable(GL_TEXTURE_GEN_T); - glEnable(GL_TEXTURE_GEN_R); - m_textureinitialized=true; - } - //drawCoordSystem(); - - //glPushMatrix(); - glEnable(GL_COLOR_MATERIAL); - if(m_textureenabled) - { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D,m_texturehandle); - } else - { - glDisable(GL_TEXTURE_2D); - } - - - glColor3f(color.x(),color.y(), color.z()); - - bool useWireframeFallback = true; - - if (!(debugMode & btIDebugDraw::DBG_DrawWireframe)) - { - ///you can comment out any of the specific cases, and use the default - ///the benefit of 'default' is that it approximates the actual collision shape including collision margin - int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape->getShapeType(); - switch (shapetype) - { - case BOX_SHAPE_PROXYTYPE: - { - const btBoxShape* boxShape = static_cast(shape); - btVector3 halfExtent = boxShape->getHalfExtentsWithMargin(); - glScaled(2*halfExtent[0], 2*halfExtent[1], 2*halfExtent[2]); - glutSolidCube(1.0); - useWireframeFallback = false; - break; - } - - - case SPHERE_SHAPE_PROXYTYPE: - { - const btSphereShape* sphereShape = static_cast(shape); - float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin - glutSolidSphere(radius,10,10); - useWireframeFallback = false; - break; - } - - case CONE_SHAPE_PROXYTYPE: - { - const btConeShape* coneShape = static_cast(shape); - int upIndex = coneShape->getConeUpIndex(); - float radius = coneShape->getRadius();//+coneShape->getMargin(); - float height = coneShape->getHeight();//+coneShape->getMargin(); - switch (upIndex) - { - case 0: - glRotatef(90.0, 0.0, 1.0, 0.0); - break; - case 1: - glRotatef(-90.0, 1.0, 0.0, 0.0); - break; - case 2: - break; - default: - { - } - }; - - glTranslatef(0.0, 0.0, -0.5*height); - glutSolidCone(radius,height,10,10); - useWireframeFallback = false; - break; - - } - - - case STATIC_PLANE_PROXYTYPE: - { - const btStaticPlaneShape* staticPlaneShape = static_cast(shape); - btScalar planeConst = staticPlaneShape->getPlaneConstant(); - const btVector3& planeNormal = staticPlaneShape->getPlaneNormal(); - btVector3 planeOrigin = planeNormal * planeConst; - btVector3 vec0,vec1; - btPlaneSpace1(planeNormal,vec0,vec1); - btScalar vecLen = 100.f; - btVector3 pt0 = planeOrigin + vec0*vecLen; - btVector3 pt1 = planeOrigin - vec0*vecLen; - btVector3 pt2 = planeOrigin + vec1*vecLen; - btVector3 pt3 = planeOrigin - vec1*vecLen; - glBegin(GL_LINES); - glVertex3f(pt0.getX(),pt0.getY(),pt0.getZ()); - glVertex3f(pt1.getX(),pt1.getY(),pt1.getZ()); - glVertex3f(pt2.getX(),pt2.getY(),pt2.getZ()); - glVertex3f(pt3.getX(),pt3.getY(),pt3.getZ()); - glEnd(); - - - break; - - } - - case CYLINDER_SHAPE_PROXYTYPE: - { - const btCylinderShape* cylinder = static_cast(shape); - int upAxis = cylinder->getUpAxis(); - - - float radius = cylinder->getRadius(); - float halfHeight = cylinder->getHalfExtentsWithMargin()[upAxis]; - - drawCylinder(radius,halfHeight,upAxis); - - break; - } - - default: - { - - - if (shape->isConvex()) - { - ShapeCache* sc=cache((btConvexShape*)shape); - - //if (shape->getUserPointer()) - { - //glutSolidCube(1.0); - btShapeHull* hull = &sc->m_shapehull/*(btShapeHull*)shape->getUserPointer()*/; - - - if (hull->numTriangles () > 0) - { - int index = 0; - const unsigned int* idx = hull->getIndexPointer(); - const btVector3* vtx = hull->getVertexPointer(); - - glBegin (GL_TRIANGLES); - - for (int i = 0; i < hull->numTriangles (); i++) - { - int i1 = index++; - int i2 = index++; - int i3 = index++; - btAssert(i1 < hull->numIndices () && - i2 < hull->numIndices () && - i3 < hull->numIndices ()); - - int index1 = idx[i1]; - int index2 = idx[i2]; - int index3 = idx[i3]; - btAssert(index1 < hull->numVertices () && - index2 < hull->numVertices () && - index3 < hull->numVertices ()); - - btVector3 v1 = vtx[index1]; - btVector3 v2 = vtx[index2]; - btVector3 v3 = vtx[index3]; - btVector3 normal = (v3-v1).cross(v2-v1); - normal.normalize (); - - glNormal3f(normal.getX(),normal.getY(),normal.getZ()); - glVertex3f (v1.x(), v1.y(), v1.z()); - glVertex3f (v2.x(), v2.y(), v2.z()); - glVertex3f (v3.x(), v3.y(), v3.z()); - - } - glEnd (); - - } - } - } - } - } - - } - - - - - /// for polyhedral shapes - if (debugMode==btIDebugDraw::DBG_DrawFeaturesText && (shape->isPolyhedral())) - { - btPolyhedralConvexShape* polyshape = (btPolyhedralConvexShape*) shape; - - { - glRasterPos3f(0.0, 0.0, 0.0); - //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),polyshape->getExtraDebugInfo()); - - glColor3f(1.f, 1.f, 1.f); - int i; - for (i=0;igetNumVertices();i++) - { - btVector3 vtx; - polyshape->getVertex(i,vtx); - glRasterPos3f(vtx.x(), vtx.y(), vtx.z()); - char buf[12]; - sprintf(buf," %d",i); -// BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); - } - - for (i=0;igetNumPlanes();i++) - { - btVector3 normal; - btVector3 vtx; - polyshape->getPlane(normal,vtx,i); - btScalar d = vtx.dot(normal); - - glRasterPos3f(normal.x()*d, normal.y()*d, normal.z()*d); - char buf[12]; - sprintf(buf," plane %d",i); -// BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); - - } - } - - } - - -#ifdef USE_DISPLAY_LISTS - - if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE) - { - GLuint dlist = OGL_get_displaylist_for_shape((btCollisionShape * )shape); - if (dlist) - { - glCallList(dlist); - } - else - { -#else - if (shape->isConcave())//>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE) - // if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE) - { - btConcaveShape* concaveMesh = (btConcaveShape*) shape; - - GlDrawcallback drawCallback; - drawCallback.m_wireframe = (debugMode & btIDebugDraw::DBG_DrawWireframe)!=0; - - concaveMesh->processAllTriangles(&drawCallback,worldBoundsMin,worldBoundsMax); - - } - if (shape->getShapeType() == HFFLUID_SHAPE_PROXYTYPE) - { - btHfFluidCollisionShape* hfFluidShape = (btHfFluidCollisionShape*)shape; - btHfFluid* fluid = hfFluidShape->m_fluid; - - GlDrawcallback drawCallback; - drawCallback.m_wireframe = (debugMode & btIDebugDraw::DBG_DrawWireframe) != 0; - fluid->foreachSurfaceTriangle (&drawCallback, worldBoundsMin, worldBoundsMax); - } -#endif - -#ifdef USE_DISPLAY_LISTS - } -} -#endif - -/* -if (shape->getShapeType() == CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE) -{ -btConvexTriangleMeshShape* convexMesh = (btConvexTriangleMeshShape*) shape; - -//todo: pass camera for some culling -btVector3 aabbMax(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT)); -btVector3 aabbMin(-btScalar(BT_LARGE_FLOAT),-btScalar(BT_LARGE_FLOAT),-btScalar(BT_LARGE_FLOAT)); -TriangleGlDrawcallback drawCallback; -convexMesh->getMeshInterface()->InternalProcessAllTriangles(&drawCallback,aabbMin,aabbMax); - -} -*/ - - - -glDisable(GL_DEPTH_TEST); -glRasterPos3f(0,0,0);//mvtx.x(), vtx.y(), vtx.z()); -if (debugMode&btIDebugDraw::DBG_DrawText) -{ -// BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),shape->getName()); -} - -if (debugMode& btIDebugDraw::DBG_DrawFeaturesText) -{ - //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),shape->getExtraDebugInfo()); -} -glEnable(GL_DEPTH_TEST); - -// glPopMatrix(); -if(m_textureenabled) glDisable(GL_TEXTURE_2D); - } - glPopMatrix(); - -} - -// -void HfFluidDemo_GL_ShapeDrawer::drawShadow(btScalar* m,const btVector3& extrusion,const btCollisionShape* shape,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax) -{ - glPushMatrix(); - btglMultMatrix(m); - if(shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE) - { - const btUniformScalingShape* scalingShape = static_cast(shape); - const btConvexShape* convexShape = scalingShape->getChildShape(); - float scalingFactor = (float)scalingShape->getUniformScalingFactor(); - btScalar tmpScaling[4][4]={ {scalingFactor,0,0,0}, - {0,scalingFactor,0,0}, - {0,0,scalingFactor,0}, - {0,0,0,1}}; - drawShadow((btScalar*)tmpScaling,extrusion,convexShape,worldBoundsMin,worldBoundsMax); - glPopMatrix(); - return; - } - else if(shape->getShapeType()==COMPOUND_SHAPE_PROXYTYPE) - { - const btCompoundShape* compoundShape = static_cast(shape); - for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--) - { - btTransform childTrans = compoundShape->getChildTransform(i); - const btCollisionShape* colShape = compoundShape->getChildShape(i); - btScalar childMat[16]; - childTrans.getOpenGLMatrix(childMat); - drawShadow(childMat,extrusion*childTrans.getBasis(),colShape,worldBoundsMin,worldBoundsMax); - } - } - else - { - bool useWireframeFallback = true; - if (shape->isConvex()) - { - ShapeCache* sc=cache((btConvexShape*)shape); - btShapeHull* hull =&sc->m_shapehull; - glBegin(GL_QUADS); - for(int i=0;im_edges.size();++i) - { - const btScalar d=btDot(sc->m_edges[i].n[0],extrusion); - if((d*btDot(sc->m_edges[i].n[1],extrusion))<0) - { - const int q= d<0?1:0; - const btVector3& a= hull->getVertexPointer()[sc->m_edges[i].v[q]]; - const btVector3& b= hull->getVertexPointer()[sc->m_edges[i].v[1-q]]; - glVertex3f(a[0],a[1],a[2]); - glVertex3f(b[0],b[1],b[2]); - glVertex3f(b[0]+extrusion[0],b[1]+extrusion[1],b[2]+extrusion[2]); - glVertex3f(a[0]+extrusion[0],a[1]+extrusion[1],a[2]+extrusion[2]); - } - } - glEnd(); - } - - } - - - - - if (shape->isConcave())//>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE) - // if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE) - { - btConcaveShape* concaveMesh = (btConcaveShape*) shape; - - GlDrawcallback drawCallback; - drawCallback.m_wireframe = false; - - concaveMesh->processAllTriangles(&drawCallback,worldBoundsMin,worldBoundsMax); - - } - glPopMatrix(); - -} - -// -HfFluidDemo_GL_ShapeDrawer::HfFluidDemo_GL_ShapeDrawer() -{ - m_texturehandle = 0; - m_textureenabled = true; - m_textureinitialized = false; -} - -HfFluidDemo_GL_ShapeDrawer::~HfFluidDemo_GL_ShapeDrawer() -{ -} - diff --git a/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.h b/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.h deleted file mode 100644 index 6d4a2a045..000000000 --- a/Demos/HeightFieldFluidDemo/HfFluidDemo_GL_ShapeDrawer.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#ifndef HFFLUID_GL_SHAPE_DRAWER_H -#define HFFLUID_GL_SHAPE_DRAWER_H - -#include "GL_ShapeDrawer.h" - -///experimental buyancy fluid demo -/// OpenGL shape drawing -class HfFluidDemo_GL_ShapeDrawer : public GL_ShapeDrawer -{ - public: - HfFluidDemo_GL_ShapeDrawer(); - - virtual ~HfFluidDemo_GL_ShapeDrawer(); - - ///drawOpenGL might allocate temporary memoty, stores pointer in shape userpointer - virtual void drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color,int debugMode,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax); - virtual void drawShadow(btScalar* m, const btVector3& extrusion,const btCollisionShape* shape,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax); -}; - -#endif //HFFLUID_GL_SHAPE_DRAWER_H diff --git a/Demos/HeightFieldFluidDemo/main.cpp b/Demos/HeightFieldFluidDemo/main.cpp deleted file mode 100644 index eb72aad9d..000000000 --- a/Demos/HeightFieldFluidDemo/main.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -Bullet Continuous Collision Detection and Physics Library -Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.com - -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. - -Experimental Buoyancy fluid demo written by John McCutchan -*/ - -#include "HfFluidDemo.h" -#include "GlutStuff.h" -#include "GLDebugDrawer.h" -#include "btBulletDynamicsCommon.h" - -GLDebugDrawer gDebugDrawer; - -int main(int argc,char** argv) -{ - - HfFluidDemo* fluidDemo = new HfFluidDemo(); - - fluidDemo->initPhysics(); - fluidDemo->getDynamicsWorld()->setDebugDrawer(&gDebugDrawer); - - - glutmain(argc, argv,640,480,"Bullet Physics Demo. http://bulletphysics.com",fluidDemo); - - delete fluidDemo; - return 0; - -} diff --git a/src/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h b/src/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h index 80e05a4f2..f343ab992 100644 --- a/src/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h +++ b/src/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h @@ -107,6 +107,12 @@ public: #endif // SIMD_FORCE_INLINE int getNumContacts() const { return m_cachedPoints;} + /// the setNumContacts API is usually not used, except when you gather/fill all contacts manually + void setNumContacts(int cachedPoints) + { + m_cachedPoints = cachedPoints; + } + SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const { @@ -128,6 +134,19 @@ public: return m_contactProcessingThreshold; } + void setContactBreakingThreshold(btScalar contactBreakingThreshold) + { + m_contactBreakingThreshold = contactBreakingThreshold; + } + + void setContactProcessingThreshold(btScalar contactProcessingThreshold) + { + m_contactProcessingThreshold = contactProcessingThreshold; + } + + + + int getCacheEntry(const btManifoldPoint& newPoint) const; int addManifoldPoint( const btManifoldPoint& newPoint); diff --git a/src/BulletDynamics/ConstraintSolver/btContactSolverInfo.h b/src/BulletDynamics/ConstraintSolver/btContactSolverInfo.h index 44e5554c6..7ddc25ecc 100644 --- a/src/BulletDynamics/ConstraintSolver/btContactSolverInfo.h +++ b/src/BulletDynamics/ConstraintSolver/btContactSolverInfo.h @@ -49,6 +49,7 @@ struct btContactSolverInfoData btScalar m_globalCfm;//constraint force mixing int m_splitImpulse; btScalar m_splitImpulsePenetrationThreshold; + btScalar m_splitImpulseTurnErp; btScalar m_linearSlop; btScalar m_warmstartingFactor; @@ -73,12 +74,13 @@ struct btContactSolverInfo : public btContactSolverInfoData m_restitution = btScalar(0.); m_maxErrorReduction = btScalar(20.); m_numIterations = 10; - m_erp = btScalar(0.2); + m_erp = btScalar(0.1); m_erp2 = btScalar(0.1); m_globalCfm = btScalar(0.); m_sor = btScalar(1.); m_splitImpulse = true; m_splitImpulsePenetrationThreshold = -.04f; + m_splitImpulseTurnErp = 0.1f; m_linearSlop = btScalar(0.0); m_warmstartingFactor=btScalar(0.85); //m_solverMode = SOLVER_USE_WARMSTARTING | SOLVER_SIMD | SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION|SOLVER_USE_2_FRICTION_DIRECTIONS|SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;// | SOLVER_RANDMIZE_ORDER; diff --git a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp index 08c8ef304..f02cee114 100644 --- a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp +++ b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp @@ -1317,7 +1317,7 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyFinish(btCo btRigidBody* body = m_tmpSolverBodyPool[i].m_originalBody; if (body) { - m_tmpSolverBodyPool[i].writebackVelocityAndTransform(infoGlobal.m_timeStep); + m_tmpSolverBodyPool[i].writebackVelocityAndTransform(infoGlobal.m_timeStep, infoGlobal.m_splitImpulseTurnErp); m_tmpSolverBodyPool[i].m_originalBody->setLinearVelocity(m_tmpSolverBodyPool[i].m_linearVelocity); m_tmpSolverBodyPool[i].m_originalBody->setAngularVelocity(m_tmpSolverBodyPool[i].m_angularVelocity); m_tmpSolverBodyPool[i].m_originalBody->setWorldTransform(m_tmpSolverBodyPool[i].m_worldTransform); diff --git a/src/BulletDynamics/ConstraintSolver/btSolverBody.h b/src/BulletDynamics/ConstraintSolver/btSolverBody.h index 75cfe44ad..b027acbf6 100644 --- a/src/BulletDynamics/ConstraintSolver/btSolverBody.h +++ b/src/BulletDynamics/ConstraintSolver/btSolverBody.h @@ -263,7 +263,7 @@ ATTRIBUTE_ALIGNED64 (struct) btSolverBody } - void writebackVelocityAndTransform(btScalar timeStep) + void writebackVelocityAndTransform(btScalar timeStep, btScalar splitImpulseTurnErp) { (void) timeStep; if (m_originalBody) @@ -276,7 +276,7 @@ ATTRIBUTE_ALIGNED64 (struct) btSolverBody if (m_pushVelocity[0]!=0.f || m_pushVelocity[1]!=0 || m_pushVelocity[2]!=0 || m_turnVelocity[0]!=0.f || m_turnVelocity[1]!=0 || m_turnVelocity[2]!=0) { btQuaternion orn = m_worldTransform.getRotation(); - btTransformUtil::integrateTransform(m_worldTransform,m_pushVelocity,m_turnVelocity,timeStep,newTransform); + btTransformUtil::integrateTransform(m_worldTransform,m_pushVelocity,m_turnVelocity*splitImpulseTurnErp,timeStep,newTransform); m_worldTransform = newTransform; } //m_worldTransform.setRotation(orn);