diff --git a/Demos/VoronoiFractureDemo/VoronoiFractureDemo.cpp b/Demos/VoronoiFractureDemo/VoronoiFractureDemo.cpp index 174c795c3..ce32fe3b2 100644 --- a/Demos/VoronoiFractureDemo/VoronoiFractureDemo.cpp +++ b/Demos/VoronoiFractureDemo/VoronoiFractureDemo.cpp @@ -37,6 +37,8 @@ Voronoi fracture and shatter code and demo copyright (c) 2011 Alain Ducharme #include "GLDebugDrawer.h" static GLDebugDrawer sDebugDraw; +static bool useGenericConstraint = false; + void VoronoiFractureDemo::attachFixedConstraints() { @@ -91,17 +93,29 @@ void VoronoiFractureDemo::attachFixedConstraints() trA = body0->getWorldTransform().inverse()*globalFrame; trB = body1->getWorldTransform().inverse()*globalFrame; - - btGeneric6DofConstraint* dof6 = new btGeneric6DofConstraint(*body0,*body1,trA,trB,true); - dof6->setOverrideNumSolverIterations(100); - float totalMass = 1.f/body0->getInvMass() + 1.f/body1->getInvMass(); - dof6->setBreakingImpulseThreshold(BREAKING_THRESHOLD*totalMass); + + if (useGenericConstraint) + { + btGeneric6DofConstraint* dof6 = new btGeneric6DofConstraint(*body0,*body1,trA,trB,true); + dof6->setOverrideNumSolverIterations(30); - for (int i=0;i<6;i++) - dof6->setLimit(i,0,0); - getDynamicsWorld()->addConstraint(dof6,true); + + dof6->setBreakingImpulseThreshold(BREAKING_THRESHOLD*totalMass); + + for (int i=0;i<6;i++) + dof6->setLimit(i,0,0); + getDynamicsWorld()->addConstraint(dof6,true); + + } else + { + btFixedConstraint* fixed = new btFixedConstraint(*body0,*body1,trA,trB); + fixed->setBreakingImpulseThreshold(BREAKING_THRESHOLD*totalMass); + fixed ->setOverrideNumSolverIterations(30); + getDynamicsWorld()->addConstraint(fixed,true); + + } } } @@ -509,6 +523,10 @@ void VoronoiFractureDemo::displayCallback(void) { void VoronoiFractureDemo::initPhysics() { + useGenericConstraint = !useGenericConstraint; + printf("useGenericConstraint = %d\n", useGenericConstraint); + + setTexturing(true); setShadows(true); @@ -563,13 +581,33 @@ void VoronoiFractureDemo::initPhysics() m_dynamicsWorld->addRigidBody(body); } + { + btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(10.),btScalar(8.),btScalar(1.))); + btScalar mass(0.); + + //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) + groundShape->calculateLocalInertia(mass,localInertia); + groundTransform.setOrigin(btVector3(0,0,0)); + //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects + btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); + btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); + btRigidBody* body = new btRigidBody(rbInfo); + + //add the body to the dynamics world + m_dynamicsWorld->addRigidBody(body); + } + // ==> Voronoi Shatter Basic Demo: Random Cuboid // Random size cuboid (defined by bounding box max and min) - btVector3 bbmax(btScalar(rand() / btScalar(RAND_MAX)) * 6. +0.5, btScalar(rand() / btScalar(RAND_MAX)) * 6. +0.5, btScalar(rand() / btScalar(RAND_MAX)) * 6. +0.5); + btVector3 bbmax(btScalar(rand() / btScalar(RAND_MAX)) * 12. +0.5, btScalar(rand() / btScalar(RAND_MAX)) * 1. +0.5, btScalar(rand() / btScalar(RAND_MAX)) * 1. +0.5); btVector3 bbmin = -bbmax; // Place it 10 units above ground - btVector3 bbt(0,10,0); + btVector3 bbt(0,15,0); // Use an arbitrary material density for shards (should be consitent/relative with/to rest of RBs in world) btScalar matDensity = 1; // Using random rotation diff --git a/src/BulletDynamics/CMakeLists.txt b/src/BulletDynamics/CMakeLists.txt index f46956079..5a626f851 100644 --- a/src/BulletDynamics/CMakeLists.txt +++ b/src/BulletDynamics/CMakeLists.txt @@ -6,6 +6,7 @@ SET(BulletDynamics_SRCS Character/btKinematicCharacterController.cpp ConstraintSolver/btConeTwistConstraint.cpp ConstraintSolver/btContactConstraint.cpp + ConstraintSolver/btFixedConstraint.cpp ConstraintSolver/btGearConstraint.cpp ConstraintSolver/btGeneric6DofConstraint.cpp ConstraintSolver/btGeneric6DofSpringConstraint.cpp @@ -38,6 +39,7 @@ SET(ConstraintSolver_HDRS ConstraintSolver/btConstraintSolver.h ConstraintSolver/btContactConstraint.h ConstraintSolver/btContactSolverInfo.h + ConstraintSolver/btFixedConstraint.h ConstraintSolver/btGearConstraint.h ConstraintSolver/btGeneric6DofConstraint.h ConstraintSolver/btGeneric6DofSpringConstraint.h diff --git a/src/BulletDynamics/ConstraintSolver/btFixedConstraint.cpp b/src/BulletDynamics/ConstraintSolver/btFixedConstraint.cpp new file mode 100644 index 000000000..99037bbd9 --- /dev/null +++ b/src/BulletDynamics/ConstraintSolver/btFixedConstraint.cpp @@ -0,0 +1,129 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2013 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + + +#include "btFixedConstraint.h" +#include "BulletDynamics/Dynamics/btRigidBody.h" +#include "LinearMath/btTransformUtil.h" +#include + + +btFixedConstraint::btFixedConstraint(btRigidBody& rbA,btRigidBody& rbB, const btTransform& frameInA,const btTransform& frameInB) +:btTypedConstraint(FIXED_CONSTRAINT_TYPE,rbA,rbB) +{ + m_pivotInA = frameInA.getOrigin(); + m_pivotInB = frameInB.getOrigin(); + m_relTargetAB = frameInA.getRotation()*frameInB.getRotation().inverse(); + +} + +btFixedConstraint::~btFixedConstraint () +{ +} + + +void btFixedConstraint::getInfo1 (btConstraintInfo1* info) +{ + info->m_numConstraintRows = 6; + info->nub = 6; +} + +void btFixedConstraint::getInfo2 (btConstraintInfo2* info) +{ + //fix the 3 linear degrees of freedom + + + const btVector3& worldPosA = m_rbA.getCenterOfMassTransform().getOrigin(); + const btMatrix3x3& worldOrnA = m_rbA.getCenterOfMassTransform().getBasis(); + const btVector3& worldPosB= m_rbB.getCenterOfMassTransform().getOrigin(); + const btMatrix3x3& worldOrnB = m_rbB.getCenterOfMassTransform().getBasis(); + + + info->m_J1linearAxis[0] = 1; + info->m_J1linearAxis[info->rowskip+1] = 1; + info->m_J1linearAxis[2*info->rowskip+2] = 1; + + btVector3 a1 = worldOrnA*m_pivotInA; + { + btVector3* angular0 = (btVector3*)(info->m_J1angularAxis); + btVector3* angular1 = (btVector3*)(info->m_J1angularAxis+info->rowskip); + btVector3* angular2 = (btVector3*)(info->m_J1angularAxis+2*info->rowskip); + btVector3 a1neg = -a1; + a1neg.getSkewSymmetricMatrix(angular0,angular1,angular2); + } + + if (info->m_J2linearAxis) + { + info->m_J2linearAxis[0] = -1; + info->m_J2linearAxis[info->rowskip+1] = -1; + info->m_J2linearAxis[2*info->rowskip+2] = -1; + } + + btVector3 a2 = worldOrnB*m_pivotInB; + + { + // btVector3 a2n = -a2; + btVector3* angular0 = (btVector3*)(info->m_J2angularAxis); + btVector3* angular1 = (btVector3*)(info->m_J2angularAxis+info->rowskip); + btVector3* angular2 = (btVector3*)(info->m_J2angularAxis+2*info->rowskip); + a2.getSkewSymmetricMatrix(angular0,angular1,angular2); + } + + // set right hand side for the linear dofs + btScalar k = info->fps * info->erp; + + btVector3 linearError = k*(a2+worldPosB-a1-worldPosA); + int j; + for (j=0; j<3; j++) + { + + + + info->m_constraintError[j*info->rowskip] = linearError[j]; + //printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]); + } + + //fix the 3 angular degrees of freedom + + int start_row = 3; + int s = info->rowskip; + int start_index = start_row * s; + + // 3 rows to make body rotations equal + info->m_J1angularAxis[start_index] = 1; + info->m_J1angularAxis[start_index + s + 1] = 1; + info->m_J1angularAxis[start_index + s*2+2] = 1; + if ( info->m_J2angularAxis) + { + info->m_J2angularAxis[start_index] = -1; + info->m_J2angularAxis[start_index + s+1] = -1; + info->m_J2angularAxis[start_index + s*2+2] = -1; + } + + // set right hand side for the angular dofs + + btVector3 diff; + btScalar angle; + btMatrix3x3 mrelCur = worldOrnA *worldOrnB.inverse(); + btQuaternion qrelCur; + mrelCur.getRotation(qrelCur); + btTransformUtil::calculateDiffAxisAngleQuaternion(m_relTargetAB,qrelCur,diff,angle); + diff*=-angle; + for (j=0; j<3; j++) + { + info->m_constraintError[(3+j)*info->rowskip] = k * diff[j]; + } + +} \ No newline at end of file diff --git a/src/BulletDynamics/ConstraintSolver/btFixedConstraint.h b/src/BulletDynamics/ConstraintSolver/btFixedConstraint.h new file mode 100644 index 000000000..ce8c27731 --- /dev/null +++ b/src/BulletDynamics/ConstraintSolver/btFixedConstraint.h @@ -0,0 +1,49 @@ +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2013 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef BT_FIXED_CONSTRAINT_H +#define BT_FIXED_CONSTRAINT_H + +#include "btTypedConstraint.h" + +ATTRIBUTE_ALIGNED16(class) btFixedConstraint : public btTypedConstraint +{ + btVector3 m_pivotInA; + btVector3 m_pivotInB; + btQuaternion m_relTargetAB; + +public: + btFixedConstraint(btRigidBody& rbA,btRigidBody& rbB, const btTransform& frameInA,const btTransform& frameInB); + + virtual ~btFixedConstraint(); + + + virtual void getInfo1 (btConstraintInfo1* info); + + virtual void getInfo2 (btConstraintInfo2* info); + + virtual void setParam(int num, btScalar value, int axis = -1) + { + btAssert(0); + } + virtual btScalar getParam(int num, int axis = -1) const + { + btAssert(0); + return 0.f; + } + +}; + +#endif //BT_FIXED_CONSTRAINT_H diff --git a/src/BulletDynamics/ConstraintSolver/btTypedConstraint.h b/src/BulletDynamics/ConstraintSolver/btTypedConstraint.h index a9a070060..bbcd470ad 100644 --- a/src/BulletDynamics/ConstraintSolver/btTypedConstraint.h +++ b/src/BulletDynamics/ConstraintSolver/btTypedConstraint.h @@ -43,6 +43,7 @@ enum btTypedConstraintType CONTACT_CONSTRAINT_TYPE, D6_SPRING_CONSTRAINT_TYPE, GEAR_CONSTRAINT_TYPE, + FIXED_CONSTRAINT_TYPE, MAX_CONSTRAINT_TYPE }; diff --git a/src/Makefile.am b/src/Makefile.am index 99d3ef09a..22486bbd5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -317,6 +317,7 @@ libBulletDynamics_la_SOURCES = \ BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp \ BulletDynamics/Dynamics/Bullet-C-API.cpp \ BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp \ + BulletDynamics/ConstraintSolver/btFixedConstraint.cpp \ BulletDynamics/ConstraintSolver/btGearConstraint.cpp \ BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.cpp \ BulletDynamics/ConstraintSolver/btGeneric6DofSpringConstraint.cpp \ diff --git a/src/btBulletDynamicsCommon.h b/src/btBulletDynamicsCommon.h index dbd175c3f..50282bf21 100644 --- a/src/btBulletDynamicsCommon.h +++ b/src/btBulletDynamicsCommon.h @@ -33,6 +33,8 @@ subject to the following restrictions: #include "BulletDynamics/ConstraintSolver/btUniversalConstraint.h" #include "BulletDynamics/ConstraintSolver/btHinge2Constraint.h" #include "BulletDynamics/ConstraintSolver/btGearConstraint.h" +#include "BulletDynamics/ConstraintSolver/btFixedConstraint.h" + #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"