diff options
Diffstat (limited to 'source/gameengine/Physics')
22 files changed, 2285 insertions, 391 deletions
diff --git a/source/gameengine/Physics/BlOde/OdePhysicsController.h b/source/gameengine/Physics/BlOde/OdePhysicsController.h index d3eb443ed3a..925f5b6686a 100644 --- a/source/gameengine/Physics/BlOde/OdePhysicsController.h +++ b/source/gameengine/Physics/BlOde/OdePhysicsController.h @@ -111,6 +111,8 @@ public: virtual void calcXform(){} virtual void SetMargin(float margin) {} virtual float GetMargin() const {return 0.f;} + virtual float GetRadius() const {return 0.f;} + virtual void SetRadius(float margin) {} // clientinfo for raycasts for example virtual void* getNewClientInfo() { return m_clientInfo;} diff --git a/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp b/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp index a04560aaf09..2e8ee31058f 100644 --- a/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp +++ b/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp @@ -198,7 +198,7 @@ int ODEPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl } -void ODEPhysicsEnvironment::removeConstraint(int constraintid) +void ODEPhysicsEnvironment::removeConstraint(void *constraintid) { if (constraintid) { @@ -206,8 +206,7 @@ void ODEPhysicsEnvironment::removeConstraint(int constraintid) } } -PHY_IPhysicsController* ODEPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ) +PHY_IPhysicsController* ODEPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallback &filterCallback,float fromX,float fromY,float fromZ, float toX,float toY,float toZ) { //m_OdeWorld diff --git a/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h b/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h index ec1b7702ffd..dcc87d614c0 100644 --- a/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h +++ b/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h @@ -54,8 +54,7 @@ public: float axisX,float axisY,float axisZ); virtual void removeConstraint(void * constraintid); - virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ); + virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback,float fromX,float fromY,float fromZ, float toX,float toY,float toZ); //gamelogic callbacks @@ -65,6 +64,7 @@ public: { } virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl) {} + virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl) {} virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) {return 0;} virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight) { return 0;} diff --git a/source/gameengine/Physics/BlOde/SConscript b/source/gameengine/Physics/BlOde/SConscript index 0ffeb1dad62..90e949d2d86 100644 --- a/source/gameengine/Physics/BlOde/SConscript +++ b/source/gameengine/Physics/BlOde/SConscript @@ -5,11 +5,11 @@ Import ('library_env') phy_ode_env = library_env.Copy () source_files = ['OdePhysicsController.cpp', - 'OdePhysicsEnvironment.cpp'] + 'OdePhysicsEnvironment.cpp'] phy_ode_env.Append (CPPPATH=['.', - '../common', - ]) + '../common', + ]) phy_ode_env.Append (CPPPATH=user_options_dict['ODE_INCLUDE']) phy_ode_env.Library (target='#'+user_options_dict['BUILD_DIR']+'/lib/PHY_Ode', source=source_files) diff --git a/source/gameengine/Physics/Bullet/CMakeLists.txt b/source/gameengine/Physics/Bullet/CMakeLists.txt index b610fd1bbb0..6c733786caf 100644 --- a/source/gameengine/Physics/Bullet/CMakeLists.txt +++ b/source/gameengine/Physics/Bullet/CMakeLists.txt @@ -30,6 +30,10 @@ SET(INC . ../common ../../../../extern/bullet2/src + ../../../../intern/moto/include + ../../../kernel/gen_system + ../../../../intern/string + ../../Rasterizer ) BLENDERLIB(bf_bullet "${SRC}" "${INC}") diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp index 5a45ce020cc..c9c30c1b450 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp +++ b/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp @@ -15,10 +15,18 @@ subject to the following restrictions: #include "CcdPhysicsController.h" #include "btBulletDynamicsCommon.h" - +#include "BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h" #include "PHY_IMotionState.h" #include "CcdPhysicsEnvironment.h" +#include "RAS_MeshObject.h" +#include "BulletSoftBody/btSoftBody.h" +#include "BulletSoftBody//btSoftBodyInternals.h" +#include "BulletSoftBody/btSoftBodyHelpers.h" +#include "LinearMath/btConvexHull.h" +#include "BulletCollision/Gimpact/btGImpactShape.h" + +#include "BulletSoftBody/btSoftRigidDynamicsWorld.h" class BP_Proxy; @@ -36,24 +44,40 @@ float gAngularSleepingTreshold = 1.0f; btVector3 startVel(0,0,0);//-10000); + CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci) :m_cci(ci) { + m_prototypeTransformInitialized = false; + m_softbodyMappingDone = false; m_collisionDelay = 0; m_newClientInfo = 0; - + m_registerCount = 0; + m_softBodyTransformInitialized = false; + m_parentCtrl = 0; + // copy pointers locally to allow smart release m_MotionState = ci.m_MotionState; + m_collisionShape = ci.m_collisionShape; + // apply scaling before creating rigid body + m_collisionShape->setLocalScaling(m_cci.m_scaling); + if (m_cci.m_mass) + m_collisionShape->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor); + // shape info is shared, increment ref count + m_shapeInfo = ci.m_shapeInfo; + if (m_shapeInfo) + m_shapeInfo->AddRef(); + m_bulletMotionState = 0; CreateRigidbody(); - - #ifdef WIN32 - if (m_body->getInvMass()) - m_body->setLinearVelocity(startVel); - #endif +///??? +#ifdef WIN32 + if (GetRigidBody() && !GetRigidBody()->isStaticObject()) + GetRigidBody()->setLinearVelocity(startVel); +#endif } @@ -105,34 +129,395 @@ public: }; -void CcdPhysicsController::CreateRigidbody() +btRigidBody* CcdPhysicsController::GetRigidBody() +{ + return btRigidBody::upcast(m_object); +} +btCollisionObject* CcdPhysicsController::GetCollisionObject() +{ + return m_object; +} +btSoftBody* CcdPhysicsController::GetSoftBody() { + return btSoftBody::upcast(m_object); +} + +#include "BulletSoftBody/btSoftBodyHelpers.h" + - btTransform trans = GetTransformFromMotionState(m_MotionState); +void CcdPhysicsController::CreateRigidbody() +{ + + //btTransform trans = GetTransformFromMotionState(m_MotionState); m_bulletMotionState = new BlenderBulletMotionState(m_MotionState); - m_body = new btRigidBody(m_cci.m_mass, - m_bulletMotionState, - m_cci.m_collisionShape, - m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor, - m_cci.m_linearDamping,m_cci.m_angularDamping, - m_cci.m_friction,m_cci.m_restitution); + ///either create a btCollisionObject, btRigidBody or btSoftBody + + //create a collision object + + int shapeType = m_cci.m_collisionShape ? m_cci.m_collisionShape->getShapeType() : 0; + + //disable soft body until first sneak preview is ready + if (m_cci.m_bSoft && m_cci.m_collisionShape && + (shapeType == CONVEX_HULL_SHAPE_PROXYTYPE)| + (shapeType == TRIANGLE_MESH_SHAPE_PROXYTYPE) | + (shapeType == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)) + { + btRigidBody::btRigidBodyConstructionInfo rbci(m_cci.m_mass,m_bulletMotionState,m_collisionShape,m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor); + rbci.m_linearDamping = m_cci.m_linearDamping; + rbci.m_angularDamping = m_cci.m_angularDamping; + rbci.m_friction = m_cci.m_friction; + rbci.m_restitution = m_cci.m_restitution; + + + int nodecount = 0; + + int numtriangles = 1; + + btVector3 p(0,0,0);// = getOrigin(); + btScalar h = 1.f; + + btSoftRigidDynamicsWorld* softDynaWorld = (btSoftRigidDynamicsWorld*)m_cci.m_physicsEnv->getDynamicsWorld(); + + PHY__Vector3 grav; + grav[0] = softDynaWorld->getGravity().getX(); + grav[1] = softDynaWorld->getGravity().getY(); + grav[2] = softDynaWorld->getGravity().getZ(); + softDynaWorld->getWorldInfo().m_gravity.setValue(grav[0],grav[1],grav[2]); //?? + + + //btSoftBody* psb=btSoftBodyHelpers::CreateRope(sbi, btVector3(-10,0,i*0.25),btVector3(10,0,i*0.25), 16,1+2); + + btSoftBody* psb = 0; + + if (m_cci.m_collisionShape->getShapeType() == CONVEX_HULL_SHAPE_PROXYTYPE) + { + btConvexHullShape* convexHull = (btConvexHullShape* )m_cci.m_collisionShape; + + //psb = btSoftBodyHelpers::CreateFromConvexHull(sbi,&transformedVertices[0],convexHull->getNumPoints()); + + { + int nvertices = convexHull->getNumPoints(); + const btVector3* vertices = convexHull->getPoints(); + btSoftBodyWorldInfo& worldInfo = softDynaWorld->getWorldInfo(); + + HullDesc hdsc(QF_TRIANGLES,nvertices,vertices); + HullResult hres; + HullLibrary hlib;/*??*/ + hdsc.mMaxVertices=nvertices; + hlib.CreateConvexHull(hdsc,hres); + + psb=new btSoftBody(&worldInfo,(int)hres.mNumOutputVertices, + &hres.m_OutputVertices[0],0); + for(int i=0;i<(int)hres.mNumFaces;++i) + { + const int idx[]={ hres.m_Indices[i*3+0], + hres.m_Indices[i*3+1], + hres.m_Indices[i*3+2]}; + if(idx[0]<idx[1]) psb->appendLink( idx[0],idx[1]); + if(idx[1]<idx[2]) psb->appendLink( idx[1],idx[2]); + if(idx[2]<idx[0]) psb->appendLink( idx[2],idx[0]); + psb->appendFace(idx[0],idx[1],idx[2]); + } + + + + hlib.ReleaseResult(hres); + + + } + + + + + + + } else + { + + btSoftBodyWorldInfo& sbi= softDynaWorld->getWorldInfo(); + + if (m_cci.m_collisionShape->getShapeType() ==SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE) + { + btScaledBvhTriangleMeshShape* scaledtrimeshshape = (btScaledBvhTriangleMeshShape*) m_cci.m_collisionShape; + btBvhTriangleMeshShape* trimeshshape = scaledtrimeshshape->getChildShape(); + + ///only deal with meshes that have 1 sub part/component, for now + if (trimeshshape->getMeshInterface()->getNumSubParts()==1) + { + unsigned char* vertexBase; + PHY_ScalarType vertexType; + int numverts; + int vertexstride; + unsigned char* indexbase; + int indexstride; + int numtris; + PHY_ScalarType indexType; + trimeshshape->getMeshInterface()->getLockedVertexIndexBase(&vertexBase,numverts,vertexType,vertexstride,&indexbase,indexstride,numtris,indexType); + + psb = btSoftBodyHelpers::CreateFromTriMesh(sbi,(const btScalar*)vertexBase,(const int*)indexbase,numtris); + } + } else + { + btBvhTriangleMeshShape* trimeshshape = (btBvhTriangleMeshShape*) m_cci.m_collisionShape; + ///only deal with meshes that have 1 sub part/component, for now + if (trimeshshape->getMeshInterface()->getNumSubParts()==1) + { + unsigned char* vertexBase; + PHY_ScalarType vertexType; + int numverts; + int vertexstride; + unsigned char* indexbase; + int indexstride; + int numtris; + PHY_ScalarType indexType; + trimeshshape->getMeshInterface()->getLockedVertexIndexBase(&vertexBase,numverts,vertexType,vertexstride,&indexbase,indexstride,numtris,indexType); + + psb = btSoftBodyHelpers::CreateFromTriMesh(sbi,(const btScalar*)vertexBase,(const int*)indexbase,numtris); + } + + + //psb = btSoftBodyHelpers::CreateFromTriMesh(sbi,&pts[0].getX(),triangles,numtriangles); + } + + } + + m_object = psb; + + //psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS;//btSoftBody::fCollision::CL_SS+ btSoftBody::fCollision::CL_RS; + + //psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS + btSoftBody::fCollision::VF_SS;//CL_SS; + + + //btSoftBody::Material* pm=psb->appendMaterial(); + btSoftBody::Material* pm=psb->m_materials[0]; + pm->m_kLST = m_cci.m_soft_linStiff; + pm->m_kAST = m_cci.m_soft_angStiff; + pm->m_kVST = m_cci.m_soft_volume; + psb->m_cfg.collisions = 0; + + if (m_cci.m_soft_collisionflags & CCD_BSB_COL_CL_RS) + { + psb->m_cfg.collisions += btSoftBody::fCollision::CL_RS; + } else + { + psb->m_cfg.collisions += btSoftBody::fCollision::SDF_RS; + } + if (m_cci.m_soft_collisionflags & CCD_BSB_COL_CL_SS) + { + psb->m_cfg.collisions += btSoftBody::fCollision::CL_SS; + } else + { + psb->m_cfg.collisions += btSoftBody::fCollision::VF_SS; + } + + + psb->m_cfg.kSRHR_CL = m_cci.m_soft_kSRHR_CL; /* Soft vs rigid hardness [0,1] (cluster only) */ + psb->m_cfg.kSKHR_CL = m_cci.m_soft_kSKHR_CL; /* Soft vs kinetic hardness [0,1] (cluster only) */ + psb->m_cfg.kSSHR_CL = m_cci.m_soft_kSSHR_CL; /* Soft vs soft hardness [0,1] (cluster only) */ + psb->m_cfg.kSR_SPLT_CL = m_cci.m_soft_kSR_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + + psb->m_cfg.kSK_SPLT_CL = m_cci.m_soft_kSK_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + psb->m_cfg.kSS_SPLT_CL = m_cci.m_soft_kSS_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + psb->m_cfg.kVCF = m_cci.m_soft_kVCF; /* Velocities correction factor (Baumgarte) */ + psb->m_cfg.kDP = m_cci.m_soft_kDP; /* Damping coefficient [0,1] */ + + psb->m_cfg.kDG = m_cci.m_soft_kDG; /* Drag coefficient [0,+inf] */ + psb->m_cfg.kLF = m_cci.m_soft_kLF; /* Lift coefficient [0,+inf] */ + psb->m_cfg.kPR = m_cci.m_soft_kPR; /* Pressure coefficient [-inf,+inf] */ + psb->m_cfg.kVC = m_cci.m_soft_kVC; /* Volume conversation coefficient [0,+inf] */ + + psb->m_cfg.kDF = m_cci.m_soft_kDF; /* Dynamic friction coefficient [0,1] */ + psb->m_cfg.kMT = m_cci.m_soft_kMT; /* Pose matching coefficient [0,1] */ + psb->m_cfg.kCHR = m_cci.m_soft_kCHR; /* Rigid contacts hardness [0,1] */ + psb->m_cfg.kKHR = m_cci.m_soft_kKHR; /* Kinetic contacts hardness [0,1] */ + + psb->m_cfg.kSHR = m_cci.m_soft_kSHR; /* Soft contacts hardness [0,1] */ + psb->m_cfg.kAHR = m_cci.m_soft_kAHR; /* Anchors hardness [0,1] */ + + + + if (m_cci.m_gamesoftFlag & CCD_BSB_BENDING_CONSTRAINTS)//OB_SB_GOAL) + { + psb->generateBendingConstraints(2,pm); + } + + psb->m_cfg.piterations = m_cci.m_soft_piterations; + psb->m_cfg.viterations = m_cci.m_soft_viterations; + psb->m_cfg.diterations = m_cci.m_soft_diterations; + psb->m_cfg.citerations = m_cci.m_soft_citerations; + + if (m_cci.m_gamesoftFlag & CCD_BSB_SHAPE_MATCHING)//OB_SB_GOAL) + { + psb->setPose(false,true);// + } else + { + psb->setPose(true,false); + } + + + + psb->randomizeConstraints(); + if (m_cci.m_soft_collisionflags & (CCD_BSB_COL_CL_RS+CCD_BSB_COL_CL_SS)) + { + psb->generateClusters(m_cci.m_soft_numclusteriterations); + } + +// psb->activate(); +// psb->setActivationState(1); +// psb->setDeactivationTime(1.f); + + //psb->m_materials[0]->m_kLST = 0.1+(i/(btScalar)(n-1))*0.9; + psb->setTotalMass(m_cci.m_mass); + + psb->setCollisionFlags(0); + + ///create a mapping between graphics mesh vertices and soft body vertices + { + RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh(); + + if (rasMesh && !m_softbodyMappingDone) + { + + //printf("apply\n"); + RAS_MeshSlot::iterator it; + RAS_MeshMaterial *mmat; + RAS_MeshSlot *slot; + size_t i; + + //for each material + for (int m=0;m<rasMesh->NumMaterials();m++) + { + // The vertex cache can only be updated for this deformer: + // Duplicated objects with more than one ploymaterial (=multiple mesh slot per object) + // share the same mesh (=the same cache). As the rendering is done per polymaterial + // cycling through the objects, the entire mesh cache cannot be updated in one shot. + mmat = rasMesh->GetMeshMaterial(m); + + slot = mmat->m_baseslot; + for(slot->begin(it); !slot->end(it); slot->next(it)) + { + int index = 0; + for(i=it.startvertex; i<it.endvertex; i++,index++) + { + RAS_TexVert* vertex = &it.vertex[i]; + + + //search closest index, and store it in vertex + vertex->setSoftBodyIndex(0); + btScalar maxDistSqr = 1e30; + btSoftBody::tNodeArray& nodes(psb->m_nodes); + btVector3 xyz = btVector3(vertex->getXYZ()[0],vertex->getXYZ()[1],vertex->getXYZ()[2]); + for (int n=0;n<nodes.size();n++) + { + btScalar distSqr = (nodes[n].m_x - xyz).length2(); + if (distSqr<maxDistSqr) + { + maxDistSqr = distSqr; + + vertex->setSoftBodyIndex(n); + } + } + } + } + } + } + } + + m_softbodyMappingDone = true; + + + + + + +// m_object->setCollisionShape(rbci.m_collisionShape); + btTransform startTrans; + + if (rbci.m_motionState) + { + rbci.m_motionState->getWorldTransform(startTrans); + } else + { + startTrans = rbci.m_startWorldTransform; + } + //startTrans.setIdentity(); + + //m_object->setWorldTransform(startTrans); + //m_object->setInterpolationWorldTransform(startTrans); + m_MotionState->setWorldPosition(startTrans.getOrigin().getX(),startTrans.getOrigin().getY(),startTrans.getOrigin().getZ()); + m_MotionState->setWorldOrientation(0,0,0,1); + + if (!m_prototypeTransformInitialized) + { + m_prototypeTransformInitialized = true; + m_softBodyTransformInitialized = true; + GetSoftBody()->transform(startTrans); + } + +// btVector3 wp = m_softBody->getWorldTransform().getOrigin(); +// MT_Point3 center(wp.getX(),wp.getY(),wp.getZ()); +// m_gameobj->NodeSetWorldPosition(center); + + + } else + { + btRigidBody::btRigidBodyConstructionInfo rbci(m_cci.m_mass,m_bulletMotionState,m_collisionShape,m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor); + rbci.m_linearDamping = m_cci.m_linearDamping; + rbci.m_angularDamping = m_cci.m_angularDamping; + rbci.m_friction = m_cci.m_friction; + rbci.m_restitution = m_cci.m_restitution; + m_object = new btRigidBody(rbci); + } + // // init the rigidbody properly // //setMassProps this also sets collisionFlags //convert collision flags! + //special case: a near/radar sensor controller should not be defined static or it will + //generate loads of static-static collision messages on the console + if ((m_cci.m_collisionFilterGroup & CcdConstructionInfo::SensorFilter) != 0) + { + // reset the flags that have been set so far + GetCollisionObject()->setCollisionFlags(0); + } + GetCollisionObject()->setCollisionFlags(m_object->getCollisionFlags() | m_cci.m_collisionFlags); + btRigidBody* body = GetRigidBody(); - m_body->setCollisionFlags(m_body->getCollisionFlags() | m_cci.m_collisionFlags); - - m_body->setGravity( m_cci.m_gravity); - m_body->setDamping(m_cci.m_linearDamping, m_cci.m_angularDamping); + if (body) + { + body->setGravity( m_cci.m_gravity); + body->setDamping(m_cci.m_linearDamping, m_cci.m_angularDamping); + if (!m_cci.m_bRigid) + { + body->setAngularFactor(0.f); + } + } + if (m_object && m_cci.m_do_anisotropic) + { + m_object->setAnisotropicFriction(m_cci.m_anisotropicFriction); + } + +} + +static void DeleteBulletShape(btCollisionShape* shape) +{ + if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE) + { + // shapes based on meshes use an interface that contains the vertices. + btTriangleMeshShape* meshShape = static_cast<btTriangleMeshShape*>(shape); + btStridingMeshInterface* meshInterface = meshShape->getMeshInterface(); + if (meshInterface) + delete meshInterface; + } + delete shape; } CcdPhysicsController::~CcdPhysicsController() @@ -141,12 +526,35 @@ CcdPhysicsController::~CcdPhysicsController() if (m_cci.m_physicsEnv) m_cci.m_physicsEnv->removeCcdPhysicsController(this); - delete m_MotionState; + if (m_MotionState) + delete m_MotionState; if (m_bulletMotionState) delete m_bulletMotionState; - delete m_body; + delete m_object; + + if (m_collisionShape) + { + // collision shape is always unique to the controller, can delete it here + if (m_collisionShape->isCompound()) + { + // bullet does not delete the child shape, must do it here + btCompoundShape* compoundShape = (btCompoundShape*)m_collisionShape; + int numChild = compoundShape->getNumChildShapes(); + for (int i=numChild-1 ; i >= 0; i--) + { + btCollisionShape* childShape = compoundShape->getChildShape(i); + DeleteBulletShape(childShape); + } + } + DeleteBulletShape(m_collisionShape); + } + if (m_shapeInfo) + { + m_shapeInfo->Release(); + } } + /** SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding') */ @@ -154,12 +562,26 @@ bool CcdPhysicsController::SynchronizeMotionStates(float time) { //sync non-static to motionstate, and static from motionstate (todo: add kinematic etc.) - if (!m_body->isStaticObject()) + btSoftBody* sb = GetSoftBody(); + if (sb) { - const btVector3& worldPos = m_body->getCenterOfMassPosition(); + btVector3 aabbMin,aabbMax; + sb->getAabb(aabbMin,aabbMax); + btVector3 worldPos = (aabbMax+aabbMin)*0.5f; + m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]); + m_MotionState->calculateWorldTransformations(); + return true; + } + + btRigidBody* body = GetRigidBody(); + + if (body && !body->isStaticObject()) + { + + const btVector3& worldPos = body->getCenterOfMassPosition(); m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]); - const btQuaternion& worldquat = m_body->getOrientation(); + const btQuaternion& worldquat = body->getOrientation(); m_MotionState->setWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]); m_MotionState->calculateWorldTransformations(); @@ -178,7 +600,7 @@ bool CcdPhysicsController::SynchronizeMotionStates(float time) btTransform oldTrans = m_body->getCenterOfMassTransform(); btTransform newTrans(worldquat,worldPos); - m_body->setCenterOfMassTransform(newTrans); + SetCenterOfMassTransform(newTrans); //need to keep track of previous position for friction effects... m_MotionState->calculateWorldTransformations(); @@ -206,13 +628,41 @@ void CcdPhysicsController::WriteDynamicsToMotionState() // controller replication void CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl) { + + m_softBodyTransformInitialized=false; m_MotionState = motionstate; + m_registerCount = 0; + m_collisionShape = NULL; - + // always create a new shape to avoid scaling bug + if (m_shapeInfo) + { + m_shapeInfo->AddRef(); + m_collisionShape = m_shapeInfo->CreateBulletShape(); - m_body = 0; + if (m_collisionShape) + { + // new shape has no scaling, apply initial scaling + m_collisionShape->setMargin(m_cci.m_margin); + m_collisionShape->setLocalScaling(m_cci.m_scaling); + + if (m_cci.m_mass) + m_collisionShape->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor); + } + } + + m_object = 0; CreateRigidbody(); - + + btRigidBody* body = GetRigidBody(); + + if (body) + { + if (m_cci.m_mass) + { + body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor); + } + } m_cci.m_physicsEnv->addCcdPhysicsController(this); @@ -248,60 +698,95 @@ void CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionsta } + +void CcdPhysicsController::SetCenterOfMassTransform(btTransform& xform) +{ + btRigidBody* body = GetRigidBody(); + if (body) + { + body->setCenterOfMassTransform(xform); + } else + { + //either collision object or soft body? + if (GetSoftBody()) + { + + } else + { + + if (m_object->isStaticOrKinematicObject()) + { + m_object->setInterpolationWorldTransform(m_object->getWorldTransform()); + } else + { + m_object->setInterpolationWorldTransform(xform); + } + if (body) + { + body->setInterpolationLinearVelocity(body->getLinearVelocity()); + body->setInterpolationAngularVelocity(body->getAngularVelocity()); + body->updateInertiaTensor(); + } + m_object->setWorldTransform(xform); + } + } +} + // kinematic methods void CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local) { - if (m_body) + if (m_object) { - m_body->activate(true); - if (m_body->isStaticObject()) + m_object->activate(true); + if (m_object->isStaticObject()) { - m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); } + btRigidBody* body = GetRigidBody(); btVector3 dloc(dlocX,dlocY,dlocZ); - btTransform xform = m_body->getCenterOfMassTransform(); - + btTransform xform = m_object->getWorldTransform(); + if (local) { dloc = xform.getBasis()*dloc; } xform.setOrigin(xform.getOrigin() + dloc); - m_body->setCenterOfMassTransform(xform); + SetCenterOfMassTransform(xform); } } void CcdPhysicsController::RelativeRotate(const float rotval[9],bool local) { - if (m_body ) + if (m_object) { - m_body->activate(true); - if (m_body->isStaticObject()) + m_object->activate(true); + if (m_object->isStaticObject()) { - m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); } - btMatrix3x3 drotmat( rotval[0],rotval[1],rotval[2], - rotval[4],rotval[5],rotval[6], - rotval[8],rotval[9],rotval[10]); + btMatrix3x3 drotmat( rotval[0],rotval[4],rotval[8], + rotval[1],rotval[5],rotval[9], + rotval[2],rotval[6],rotval[10]); btMatrix3x3 currentOrn; GetWorldOrientation(currentOrn); - btTransform xform = m_body->getCenterOfMassTransform(); - + btTransform xform = m_object->getWorldTransform(); + xform.setBasis(xform.getBasis()*(local ? drotmat : (currentOrn.inverse() * drotmat * currentOrn))); - m_body->setCenterOfMassTransform(xform); + SetCenterOfMassTransform(xform); } - } + void CcdPhysicsController::GetWorldOrientation(btMatrix3x3& mat) { float orn[4]; @@ -312,7 +797,7 @@ void CcdPhysicsController::GetWorldOrientation(btMatrix3x3& mat) void CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal) { - btQuaternion q = m_body->getCenterOfMassTransform().getRotation(); + btQuaternion q = m_object->getWorldTransform().getRotation(); quatImag0 = q[0]; quatImag1 = q[1]; quatImag2 = q[2]; @@ -320,33 +805,75 @@ void CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,flo } void CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal) { - m_body->activate(true); - if (m_body->isStaticObject()) + if (m_object) { - m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + m_object->activate(true); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } + // not required + //m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal); + btTransform xform = m_object->getWorldTransform(); + xform.setRotation(btQuaternion(quatImag0,quatImag1,quatImag2,quatReal)); + SetCenterOfMassTransform(xform); + // not required + //m_bulletMotionState->setWorldTransform(xform); + + + } - m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal); - btTransform xform = m_body->getCenterOfMassTransform(); - xform.setRotation(btQuaternion(quatImag0,quatImag1,quatImag2,quatReal)); - m_body->setCenterOfMassTransform(xform); - m_bulletMotionState->setWorldTransform(xform); +} + +void CcdPhysicsController::setWorldOrientation(const btMatrix3x3& orn) +{ + if (m_object) + { + m_object->activate(true); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } + // not required + //m_MotionState->setWorldOrientation(quatImag0,quatImag1,quatImag2,quatReal); + btTransform xform = m_object->getWorldTransform(); + xform.setBasis(orn); + SetCenterOfMassTransform(xform); + // not required + //m_bulletMotionState->setWorldTransform(xform); + //only once! + if (!m_softBodyTransformInitialized && GetSoftBody()) + { + m_softbodyStartTrans.setBasis(orn); + xform.setOrigin(m_softbodyStartTrans.getOrigin()); + GetSoftBody()->transform(xform); + m_softBodyTransformInitialized = true; + } + + } } void CcdPhysicsController::setPosition(float posX,float posY,float posZ) { - m_body->activate(true); - if (m_body->isStaticObject()) + if (m_object) { - m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + m_object->activate(true); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } + // not required, this function is only used to update the physic controller + //m_MotionState->setWorldPosition(posX,posY,posZ); + btTransform xform = m_object->getWorldTransform(); + xform.setOrigin(btVector3(posX,posY,posZ)); + SetCenterOfMassTransform(xform); + if (!m_softBodyTransformInitialized) + m_softbodyStartTrans.setOrigin(xform.getOrigin()); + // not required + //m_bulletMotionState->setWorldTransform(xform); } - - m_MotionState->setWorldPosition(posX,posY,posZ); - btTransform xform = m_body->getCenterOfMassTransform(); - xform.setOrigin(btVector3(posX,posY,posZ)); - m_body->setCenterOfMassTransform(xform); - m_bulletMotionState->setWorldTransform(xform); } @@ -356,7 +883,7 @@ void CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvel void CcdPhysicsController::getPosition(PHY__Vector3& pos) const { - const btTransform& xform = m_body->getCenterOfMassTransform(); + const btTransform& xform = m_object->getWorldTransform(); pos[0] = xform.getOrigin().x(); pos[1] = xform.getOrigin().y(); pos[2] = xform.getOrigin().z(); @@ -370,15 +897,16 @@ void CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ) { m_cci.m_scaling = btVector3(scaleX,scaleY,scaleZ); - if (m_body && m_body->getCollisionShape()) + if (m_object && m_object->getCollisionShape()) { - m_body->getCollisionShape()->setLocalScaling(m_cci.m_scaling); + m_object->getCollisionShape()->setLocalScaling(m_cci.m_scaling); //printf("no inertia recalc for fixed objects with mass=0\n"); - if (m_cci.m_mass) + btRigidBody* body = GetRigidBody(); + if (body && m_cci.m_mass) { - m_body->getCollisionShape()->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor); - m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor); + body->getCollisionShape()->calculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor); + body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor); } } @@ -389,51 +917,81 @@ void CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ) void CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local) { btVector3 torque(torqueX,torqueY,torqueZ); - btTransform xform = m_body->getCenterOfMassTransform(); - if (torque.length2() > (SIMD_EPSILON*SIMD_EPSILON)) - { - m_body->activate(); - } - if (local) + btTransform xform = m_object->getWorldTransform(); + + + if (m_object && torque.length2() > (SIMD_EPSILON*SIMD_EPSILON)) { - torque = xform.getBasis()*torque; + btRigidBody* body = GetRigidBody(); + m_object->activate(); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } + if (local) + { + torque = xform.getBasis()*torque; + } + if (body) + body->applyTorque(torque); } - m_body->applyTorque(torque); } void CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local) { btVector3 force(forceX,forceY,forceZ); - if (force.length2() > (SIMD_EPSILON*SIMD_EPSILON)) - { - m_body->activate(); - } - - btTransform xform = m_body->getCenterOfMassTransform(); - if (local) + if (m_object && force.length2() > (SIMD_EPSILON*SIMD_EPSILON)) { - force = xform.getBasis()*force; + m_object->activate(); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } + + { + btTransform xform = m_object->getWorldTransform(); + + if (local) + { + force = xform.getBasis()*force; + } + btRigidBody* body = GetRigidBody(); + if (body) + body->applyCentralForce(force); + btSoftBody* soft = GetSoftBody(); + if (soft) + { + // the force is applied on each node, must reduce it in the same extend + if (soft->m_nodes.size() > 0) + force /= soft->m_nodes.size(); + soft->addForce(force); + } + } } - m_body->applyCentralForce(force); } void CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local) { btVector3 angvel(ang_velX,ang_velY,ang_velZ); - if (angvel.length2() > (SIMD_EPSILON*SIMD_EPSILON)) - { - m_body->activate(true); - } - + if (m_object && angvel.length2() > (SIMD_EPSILON*SIMD_EPSILON)) { - btTransform xform = m_body->getCenterOfMassTransform(); - if (local) + m_object->activate(true); + if (m_object->isStaticObject()) { - angvel = xform.getBasis()*angvel; - } + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } else + { + btTransform xform = m_object->getWorldTransform(); + if (local) + { + angvel = xform.getBasis()*angvel; + } + btRigidBody* body = GetRigidBody(); + if (body) + body->setAngularVelocity(angvel); - m_body->setAngularVelocity(angvel); + } } } @@ -441,33 +999,53 @@ void CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,floa { btVector3 linVel(lin_velX,lin_velY,lin_velZ); - if (linVel.length2() > (SIMD_EPSILON*SIMD_EPSILON)) - { - m_body->activate(true); - } - + if (m_object/* && linVel.length2() > (SIMD_EPSILON*SIMD_EPSILON)*/) { - btTransform xform = m_body->getCenterOfMassTransform(); - if (local) + m_object->activate(true); + if (m_object->isStaticObject()) { - linVel = xform.getBasis()*linVel; + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + return; + } + + btSoftBody* soft = GetSoftBody(); + if (soft) + { + if (local) + { + linVel = m_softbodyStartTrans.getBasis()*linVel; + } + soft->setVelocity(linVel); + } else + { + btTransform xform = m_object->getWorldTransform(); + if (local) + { + linVel = xform.getBasis()*linVel; + } + btRigidBody* body = GetRigidBody(); + if (body) + body->setLinearVelocity(linVel); } - m_body->setLinearVelocity(linVel); } } void CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ) { btVector3 impulse(impulseX,impulseY,impulseZ); - if (impulse.length2() > (SIMD_EPSILON*SIMD_EPSILON)) + if (m_object && impulse.length2() > (SIMD_EPSILON*SIMD_EPSILON)) { - m_body->activate(); + m_object->activate(); + if (m_object->isStaticObject()) + { + m_object->setCollisionFlags(m_object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); + } btVector3 pos(attachX,attachY,attachZ); - - m_body->activate(); - - m_body->applyImpulse(impulse,pos); + btRigidBody* body = GetRigidBody(); + if (body) + body->applyImpulse(impulse,pos); + } } @@ -477,29 +1055,55 @@ void CcdPhysicsController::SetActive(bool active) // reading out information from physics void CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ) { - const btVector3& linvel = this->m_body->getLinearVelocity(); - linvX = linvel.x(); - linvY = linvel.y(); - linvZ = linvel.z(); + btRigidBody* body = GetRigidBody(); + if (body) + { + const btVector3& linvel = body->getLinearVelocity(); + linvX = linvel.x(); + linvY = linvel.y(); + linvZ = linvel.z(); + } else + { + linvX = 0.f; + linvY = 0.f; + linvZ = 0.f; + } } void CcdPhysicsController::GetAngularVelocity(float& angVelX,float& angVelY,float& angVelZ) { - const btVector3& angvel= m_body->getAngularVelocity(); - angVelX = angvel.x(); - angVelY = angvel.y(); - angVelZ = angvel.z(); + btRigidBody* body = GetRigidBody(); + if (body) + { + const btVector3& angvel= body->getAngularVelocity(); + angVelX = angvel.x(); + angVelY = angvel.y(); + angVelZ = angvel.z(); + } else + { + angVelX = 0.f; + angVelY = 0.f; + angVelZ = 0.f; + } } void CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ) { btVector3 pos(posX,posY,posZ); - btVector3 rel_pos = pos-m_body->getCenterOfMassPosition(); - btVector3 linvel = m_body->getVelocityInLocalPoint(rel_pos); - linvX = linvel.x(); - linvY = linvel.y(); - linvZ = linvel.z(); + btRigidBody* body = GetRigidBody(); + if (body) + { + btVector3 linvel = body->getVelocityInLocalPoint(pos); + linvX = linvel.x(); + linvY = linvel.y(); + linvZ = linvel.z(); + } else + { + linvX = 0.f; + linvY = 0.f; + linvZ = 0.f; + } } void CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ) { @@ -510,11 +1114,15 @@ void CcdPhysicsController::setRigidBody(bool rigid) { if (!rigid) { - //fake it for now - btVector3 inertia = m_body->getInvInertiaDiagLocal(); - inertia[1] = 0.f; - m_body->setInvInertiaDiagLocal(inertia); - m_body->updateInertiaTensor(); + btRigidBody* body = GetRigidBody(); + if (body) + { + //fake it for now + btVector3 inertia = body->getInvInertiaDiagLocal(); + inertia[1] = 0.f; + body->setInvInertiaDiagLocal(inertia); + body->updateInertiaTensor(); + } } } @@ -531,40 +1139,52 @@ void CcdPhysicsController::setNewClientInfo(void* clientinfo) void CcdPhysicsController::UpdateDeactivation(float timeStep) { - m_body->updateDeactivation( timeStep); + btRigidBody* body = GetRigidBody(); + if (body) + { + body->updateDeactivation( timeStep); + } } bool CcdPhysicsController::wantsSleeping() { - - return m_body->wantsSleeping(); + btRigidBody* body = GetRigidBody(); + if (body) + { + return body->wantsSleeping(); + } + //check it out + return true; } PHY_IPhysicsController* CcdPhysicsController::GetReplica() { - //very experimental, shape sharing is not implemented yet. - //just support btSphereShape/ConeShape for now - + // This is used only to replicate Near and Radar sensor controllers + // The replication of object physics controller is done in KX_BulletPhysicsController::GetReplica() CcdConstructionInfo cinfo = m_cci; - if (cinfo.m_collisionShape) + if (m_shapeInfo) + { + // This situation does not normally happen + cinfo.m_collisionShape = m_shapeInfo->CreateBulletShape(); + } + else if (m_collisionShape) { - switch (cinfo.m_collisionShape->getShapeType()) + switch (m_collisionShape->getShapeType()) { case SPHERE_SHAPE_PROXYTYPE: { - btSphereShape* orgShape = (btSphereShape*)cinfo.m_collisionShape; + btSphereShape* orgShape = (btSphereShape*)m_collisionShape; cinfo.m_collisionShape = new btSphereShape(*orgShape); break; } - case CONE_SHAPE_PROXYTYPE: + case CONE_SHAPE_PROXYTYPE: { - btConeShape* orgShape = (btConeShape*)cinfo.m_collisionShape; + btConeShape* orgShape = (btConeShape*)m_collisionShape; cinfo.m_collisionShape = new btConeShape(*orgShape); break; } - default: { return 0; @@ -573,6 +1193,7 @@ PHY_IPhysicsController* CcdPhysicsController::GetReplica() } cinfo.m_MotionState = new DefaultMotionState(); + cinfo.m_shapeInfo = m_shapeInfo; CcdPhysicsController* replica = new CcdPhysicsController(cinfo); return replica; @@ -634,3 +1255,273 @@ void DefaultMotionState::calculateWorldTransformations() } +// Shape constructor +std::map<RAS_MeshObject*, CcdShapeConstructionInfo*> CcdShapeConstructionInfo::m_meshShapeMap; + +CcdShapeConstructionInfo* CcdShapeConstructionInfo::FindMesh(RAS_MeshObject* mesh, bool polytope) +{ + if (polytope) + // not yet supported + return NULL; + + std::map<RAS_MeshObject*,CcdShapeConstructionInfo*>::const_iterator mit = m_meshShapeMap.find(mesh); + if (mit != m_meshShapeMap.end()) + return mit->second; + return NULL; +} + +bool CcdShapeConstructionInfo::SetMesh(RAS_MeshObject* meshobj, bool polytope,bool useGimpact) +{ + m_useGimpact = useGimpact; + + // assume no shape information + // no support for dynamic change of shape yet + assert(m_meshObject == NULL); + m_shapeType = PHY_SHAPE_NONE; + m_vertexArray.clear(); + m_polygonIndexArray.clear(); + m_meshObject = NULL; + + if (!meshobj) + return false; + + // Mesh has no polygons! + int numpolys = meshobj->NumPolygons(); + if (!numpolys) + { + return false; + } + + // check that we have at least one colliding polygon + int numvalidpolys = 0; + + for (int p=0; p<numpolys; p++) + { + RAS_Polygon* poly = meshobj->GetPolygon(p); + + // only add polygons that have the collisionflag set + if (poly->IsCollider()) + { + numvalidpolys++; + break; + } + } + + // No collision polygons + if (numvalidpolys < 1) + return false; + + m_shapeType = (polytope) ? PHY_SHAPE_POLYTOPE : PHY_SHAPE_MESH; + + numvalidpolys = 0; + + for (int p2=0; p2<numpolys; p2++) + { + RAS_Polygon* poly = meshobj->GetPolygon(p2); + + // only add polygons that have the collisionflag set + if (poly->IsCollider()) + { + //Bullet can raycast any shape, so + if (polytope) + { + for (int i=0;i<poly->VertexCount();i++) + { + const float* vtx = poly->GetVertex(i)->getXYZ(); + btPoint3 point(vtx[0],vtx[1],vtx[2]); + //avoid duplicates (could better directly use vertex offsets, rather than a vertex compare) + bool found = false; + for (int j=0;j<m_vertexArray.size();j++) + { + if (m_vertexArray[j]==point) + { + found = true; + break; + } + } + if (!found) + m_vertexArray.push_back(point); + + numvalidpolys++; + } + } else + { + { + const float* vtx = poly->GetVertex(2)->getXYZ(); + btPoint3 vertex0(vtx[0],vtx[1],vtx[2]); + + vtx = poly->GetVertex(1)->getXYZ(); + btPoint3 vertex1(vtx[0],vtx[1],vtx[2]); + + vtx = poly->GetVertex(0)->getXYZ(); + btPoint3 vertex2(vtx[0],vtx[1],vtx[2]); + + m_vertexArray.push_back(vertex0); + m_vertexArray.push_back(vertex1); + m_vertexArray.push_back(vertex2); + m_polygonIndexArray.push_back(p2); + numvalidpolys++; + } + if (poly->VertexCount() == 4) + { + const float* vtx = poly->GetVertex(3)->getXYZ(); + btPoint3 vertex0(vtx[0],vtx[1],vtx[2]); + + vtx = poly->GetVertex(2)->getXYZ(); + btPoint3 vertex1(vtx[0],vtx[1],vtx[2]); + + vtx = poly->GetVertex(0)->getXYZ(); + btPoint3 vertex2(vtx[0],vtx[1],vtx[2]); + + m_vertexArray.push_back(vertex0); + m_vertexArray.push_back(vertex1); + m_vertexArray.push_back(vertex2); + m_polygonIndexArray.push_back(p2); + numvalidpolys++; + } + } + } + } + + if (!numvalidpolys) + { + // should not happen + m_shapeType = PHY_SHAPE_NONE; + return false; + } + m_meshObject = meshobj; + if (!polytope) + { + // triangle shape can be shared, store the mesh object in the map + m_meshShapeMap.insert(std::pair<RAS_MeshObject*,CcdShapeConstructionInfo*>(meshobj,this)); + } + return true; +} + +btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape() +{ + btCollisionShape* collisionShape = 0; + btTriangleMeshShape* concaveShape = 0; + btTriangleMesh* collisionMeshData = 0; + btCompoundShape* compoundShape = 0; + CcdShapeConstructionInfo* nextShapeInfo; + + switch (m_shapeType) + { + case PHY_SHAPE_NONE: + break; + + case PHY_SHAPE_BOX: + collisionShape = new btBoxShape(m_halfExtend); + break; + + case PHY_SHAPE_SPHERE: + collisionShape = new btSphereShape(m_radius); + break; + + case PHY_SHAPE_CYLINDER: + collisionShape = new btCylinderShapeZ(m_halfExtend); + break; + + case PHY_SHAPE_CONE: + collisionShape = new btConeShapeZ(m_radius, m_height); + break; + + case PHY_SHAPE_POLYTOPE: + collisionShape = new btConvexHullShape(&m_vertexArray.begin()->getX(), m_vertexArray.size()); + break; + + case PHY_SHAPE_MESH: + // Let's use the latest btScaledBvhTriangleMeshShape: it allows true sharing of + // triangle mesh information between duplicates => drastic performance increase when + // duplicating complex mesh objects. + // BUT it causes a small performance decrease when sharing is not required: + // 9 multiplications/additions and one function call for each triangle that passes the mid phase filtering + // One possible optimization is to use directly the btBvhTriangleMeshShape when the scale is 1,1,1 + // and btScaledBvhTriangleMeshShape otherwise. + if (m_useGimpact) + { + collisionMeshData = new btTriangleMesh(); + + + // m_vertexArray is necessarily a multiple of 3 + for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); ) + { + collisionMeshData->addTriangle(*it++,*it++,*it++); + } + btGImpactMeshShape* gimpactShape = new btGImpactMeshShape(collisionMeshData); + + collisionShape = gimpactShape; + gimpactShape->updateBound(); + + } else + { + if (!m_unscaledShape) + { + collisionMeshData = new btTriangleMesh(true,false); + collisionMeshData->m_weldingThreshold = m_weldingThreshold; + + // m_vertexArray is necessarily a multiple of 3 + for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); ) + { + collisionMeshData->addTriangle(*it++,*it++,*it++); + } + // this shape will be shared and not deleted until shapeInfo is deleted + m_unscaledShape = new btBvhTriangleMeshShape( collisionMeshData, true ); + m_unscaledShape->recalcLocalAabb(); + } + collisionShape = new btScaledBvhTriangleMeshShape(m_unscaledShape, btVector3(1.0f,1.0f,1.0f)); + } + break; + + case PHY_SHAPE_COMPOUND: + if (m_shapeArray.size() > 0) + { + compoundShape = new btCompoundShape(); + for (std::vector<CcdShapeConstructionInfo*>::iterator sit = m_shapeArray.begin(); + sit != m_shapeArray.end(); + sit++) + { + collisionShape = (*sit)->CreateBulletShape(); + if (collisionShape) + { + collisionShape->setLocalScaling((*sit)->m_childScale); + compoundShape->addChildShape((*sit)->m_childTrans, collisionShape); + } + } + collisionShape = compoundShape; + } + } + return collisionShape; +} + +void CcdShapeConstructionInfo::AddShape(CcdShapeConstructionInfo* shapeInfo) +{ + m_shapeArray.push_back(shapeInfo); +} + +CcdShapeConstructionInfo::~CcdShapeConstructionInfo() +{ + for (std::vector<CcdShapeConstructionInfo*>::iterator sit = m_shapeArray.begin(); + sit != m_shapeArray.end(); + sit++) + { + (*sit)->Release(); + } + m_shapeArray.clear(); + if (m_unscaledShape) + { + DeleteBulletShape(m_unscaledShape); + } + m_vertexArray.clear(); + if (m_shapeType == PHY_SHAPE_MESH && m_meshObject != NULL) + { + std::map<RAS_MeshObject*,CcdShapeConstructionInfo*>::iterator mit = m_meshShapeMap.find(m_meshObject); + if (mit != m_meshShapeMap.end() && mit->second == this) + { + m_meshShapeMap.erase(mit); + } + } +} + + diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.h b/source/gameengine/Physics/Bullet/CcdPhysicsController.h index 11fef56401f..054ec91122a 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsController.h +++ b/source/gameengine/Physics/Bullet/CcdPhysicsController.h @@ -17,11 +17,15 @@ subject to the following restrictions: #ifndef BULLET2_PHYSICSCONTROLLER_H #define BULLET2_PHYSICSCONTROLLER_H +#include <vector> +#include <map> + #include "PHY_IPhysicsController.h" /// PHY_IPhysicsController is the abstract simplified Interface to a physical object. /// It contains the IMotionState and IDeformableMesh Interfaces. #include "btBulletDynamicsCommon.h" +#include "LinearMath/btTransform.h" #include "PHY_IMotionState.h" @@ -31,8 +35,119 @@ extern float gAngularSleepingTreshold; extern bool gDisableDeactivation; class CcdPhysicsEnvironment; class btMotionState; +class RAS_MeshObject; +class btCollisionShape; + + +#define CCD_BSB_SHAPE_MATCHING 2 +#define CCD_BSB_BENDING_CONSTRAINTS 8 +#define CCD_BSB_AERO_VPOINT 16 /* aero model, Vertex normals are oriented toward velocity*/ +#define CCD_BSB_AERO_VTWOSIDE 32 /* aero model, Vertex normals are flipped to match velocity */ + +/* BulletSoftBody.collisionflags */ +#define CCD_BSB_COL_SDF_RS 2 /* SDF based rigid vs soft */ +#define CCD_BSB_COL_CL_RS 4 /* Cluster based rigid vs soft */ +#define CCD_BSB_COL_CL_SS 8 /* Cluster based soft vs soft */ +#define CCD_BSB_COL_VF_SS 16 /* Vertex/Face based soft vs soft */ + + + +// Shape contructor +// It contains all the information needed to create a simple bullet shape at runtime +class CcdShapeConstructionInfo +{ +public: + + + static CcdShapeConstructionInfo* FindMesh(RAS_MeshObject* mesh, bool polytope); + + CcdShapeConstructionInfo() : + m_shapeType(PHY_SHAPE_NONE), + m_radius(1.0), + m_height(1.0), + m_halfExtend(0.f,0.f,0.f), + m_childScale(1.0f,1.0f,1.0f), + m_refCount(1), + m_meshObject(NULL), + m_unscaledShape(NULL), + m_useGimpact(false), + m_weldingThreshold(0.f) + { + m_childTrans.setIdentity(); + } + + ~CcdShapeConstructionInfo(); + + CcdShapeConstructionInfo* AddRef() + { + m_refCount++; + return this; + } + int Release() + { + if (--m_refCount > 0) + return m_refCount; + delete this; + return 0; + } + void AddShape(CcdShapeConstructionInfo* shapeInfo); + + btTriangleMeshShape* GetMeshShape(void) + { + return m_unscaledShape; + } + CcdShapeConstructionInfo* GetChildShape(int i) + { + if (i < 0 || i >= m_shapeArray.size()) + return NULL; + + return m_shapeArray.at(i); + } + + bool SetMesh(RAS_MeshObject* mesh, bool polytope,bool useGimpact); + RAS_MeshObject* GetMesh(void) + { + return m_meshObject; + } + + btCollisionShape* CreateBulletShape(); + + // member variables + PHY_ShapeType m_shapeType; + btScalar m_radius; + btScalar m_height; + btVector3 m_halfExtend; + btTransform m_childTrans; + btVector3 m_childScale; + std::vector<btPoint3> m_vertexArray; // Contains both vertex array for polytope shape and + // triangle array for concave mesh shape. + // In this case a triangle is made of 3 consecutive points + std::vector<int> m_polygonIndexArray; // Contains the array of polygon index in the + // original mesh that correspond to shape triangles. + // only set for concave mesh shape. + + void setVertexWeldingThreshold(float threshold) + { + m_weldingThreshold = threshold; + } + float getVertexWeldingThreshold() const + { + return m_weldingThreshold; + } +protected: + static std::map<RAS_MeshObject*, CcdShapeConstructionInfo*> m_meshShapeMap; + int m_refCount; // this class is shared between replicas + // keep track of users so that we can release it + RAS_MeshObject* m_meshObject; // Keep a pointer to the original mesh + btBvhTriangleMeshShape* m_unscaledShape;// holds the shared unscale BVH mesh shape, + // the actual shape is of type btScaledBvhTriangleMeshShape + std::vector<CcdShapeConstructionInfo*> m_shapeArray; // for compound shapes + bool m_useGimpact; //use gimpact for concave dynamic/moving collision detection + float m_weldingThreshold; //welding closeby vertices together can improve softbody stability etc. + +}; struct CcdConstructionInfo { @@ -46,24 +161,34 @@ struct CcdConstructionInfo StaticFilter = 2, KinematicFilter = 4, DebrisFilter = 8, - AllFilter = DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter, + SensorFilter = 16, + AllFilter = DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorFilter, }; CcdConstructionInfo() - : m_gravity(0,0,0), + : m_localInertiaTensor(1.f, 1.f, 1.f), + m_gravity(0,0,0), m_scaling(1.f,1.f,1.f), m_mass(0.f), m_restitution(0.1f), m_friction(0.5f), m_linearDamping(0.1f), m_angularDamping(0.1f), + m_margin(0.06f), + m_gamesoftFlag(0), m_collisionFlags(0), + m_bRigid(false), + m_bSoft(false), m_collisionFilterGroup(DefaultFilter), m_collisionFilterMask(AllFilter), + m_collisionShape(0), m_MotionState(0), + m_shapeInfo(0), m_physicsEnv(0), - m_inertiaFactor(1.f) + m_inertiaFactor(1.f), + m_do_anisotropic(false), + m_anisotropicFriction(1.f,1.f,1.f) { } @@ -75,7 +200,50 @@ struct CcdConstructionInfo btScalar m_friction; btScalar m_linearDamping; btScalar m_angularDamping; + btScalar m_margin; + + //////////////////// + int m_gamesoftFlag; + float m_soft_linStiff; /* linear stiffness 0..1 */ + float m_soft_angStiff; /* angular stiffness 0..1 */ + float m_soft_volume; /* volume preservation 0..1 */ + + int m_soft_viterations; /* Velocities solver iterations */ + int m_soft_piterations; /* Positions solver iterations */ + int m_soft_diterations; /* Drift solver iterations */ + int m_soft_citerations; /* Cluster solver iterations */ + + float m_soft_kSRHR_CL; /* Soft vs rigid hardness [0,1] (cluster only) */ + float m_soft_kSKHR_CL; /* Soft vs kinetic hardness [0,1] (cluster only) */ + float m_soft_kSSHR_CL; /* Soft vs soft hardness [0,1] (cluster only) */ + float m_soft_kSR_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + + float m_soft_kSK_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + float m_soft_kSS_SPLT_CL; /* Soft vs rigid impulse split [0,1] (cluster only) */ + float m_soft_kVCF; /* Velocities correction factor (Baumgarte) */ + float m_soft_kDP; /* Damping coefficient [0,1] */ + + float m_soft_kDG; /* Drag coefficient [0,+inf] */ + float m_soft_kLF; /* Lift coefficient [0,+inf] */ + float m_soft_kPR; /* Pressure coefficient [-inf,+inf] */ + float m_soft_kVC; /* Volume conversation coefficient [0,+inf] */ + + float m_soft_kDF; /* Dynamic friction coefficient [0,1] */ + float m_soft_kMT; /* Pose matching coefficient [0,1] */ + float m_soft_kCHR; /* Rigid contacts hardness [0,1] */ + float m_soft_kKHR; /* Kinetic contacts hardness [0,1] */ + + float m_soft_kSHR; /* Soft contacts hardness [0,1] */ + float m_soft_kAHR; /* Anchors hardness [0,1] */ + int m_soft_collisionflags; /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */ + int m_soft_numclusteriterations; /* number of iterations to refine collision clusters*/ +/////////////////// + + + int m_collisionFlags; + bool m_bRigid; + bool m_bSoft; ///optional use of collision group/mask: ///only collision with object goups that match the collision mask. @@ -85,32 +253,73 @@ struct CcdConstructionInfo short int m_collisionFilterGroup; short int m_collisionFilterMask; - - btCollisionShape* m_collisionShape; - class PHY_IMotionState* m_MotionState; + ///these pointers are used as argument passing for the CcdPhysicsController constructor + ///and not anymore after that + class btCollisionShape* m_collisionShape; + class PHY_IMotionState* m_MotionState; + class CcdShapeConstructionInfo* m_shapeInfo; CcdPhysicsEnvironment* m_physicsEnv; //needed for self-replication float m_inertiaFactor;//tweak the inertia (hooked up to Blender 'formfactor' + bool m_do_anisotropic; + btVector3 m_anisotropicFriction; + + bool m_do_fh; ///< Should the object have a linear Fh spring? + bool m_do_rot_fh; ///< Should the object have an angular Fh spring? + btScalar m_fh_spring; ///< Spring constant (both linear and angular) + btScalar m_fh_damping; ///< Damping factor (linear and angular) in range [0, 1] + btScalar m_fh_distance; ///< The range above the surface where Fh is active. + bool m_fh_normal; ///< Should the object slide off slopes? + float m_radius;//for fh backwards compatibility + }; class btRigidBody; +class btCollisionObject; +class btSoftBody; ///CcdPhysicsController is a physics object that supports continuous collision detection and time of impact based physics resolution. class CcdPhysicsController : public PHY_IPhysicsController { - btRigidBody* m_body; - class PHY_IMotionState* m_MotionState; + + btCollisionObject* m_object; + + + class PHY_IMotionState* m_MotionState; btMotionState* m_bulletMotionState; + class btCollisionShape* m_collisionShape; + class CcdShapeConstructionInfo* m_shapeInfo; + friend class CcdPhysicsEnvironment; // needed when updating the controller + + //some book keeping for replication + bool m_softbodyMappingDone; + bool m_softBodyTransformInitialized; + bool m_prototypeTransformInitialized; + btTransform m_softbodyStartTrans; - void* m_newClientInfo; + void* m_newClientInfo; + int m_registerCount; // needed when multiple sensors use the same controller CcdConstructionInfo m_cci;//needed for replication + + CcdPhysicsController* m_parentCtrl; + void GetWorldOrientation(btMatrix3x3& mat); void CreateRigidbody(); + bool Register() { + return (m_registerCount++ == 0) ? true : false; + } + bool Unregister() { + return (--m_registerCount == 0) ? true : false; + } + + protected: + void setWorldOrientation(const btMatrix3x3& mat); + public: int m_collisionDelay; @@ -120,11 +329,24 @@ class CcdPhysicsController : public PHY_IPhysicsController virtual ~CcdPhysicsController(); + CcdConstructionInfo& getConstructionInfo() + { + return m_cci; + } + const CcdConstructionInfo& getConstructionInfo() const + { + return m_cci; + } + - btRigidBody* GetRigidBody() { return m_body;} + btRigidBody* GetRigidBody(); + btCollisionObject* GetCollisionObject(); + btSoftBody* GetSoftBody(); + + CcdShapeConstructionInfo* GetShapeInfo() { return m_shapeInfo; } btCollisionShape* GetCollisionShape() { - return m_body->getCollisionShape(); + return m_object->getCollisionShape(); } //////////////////////////////////// // PHY_IPhysicsController interface @@ -190,16 +412,38 @@ class CcdPhysicsController : public PHY_IPhysicsController return m_cci.m_collisionFilterMask; } - virtual void calcXform() {} ; - virtual void SetMargin(float margin) {}; - virtual float GetMargin() const {return 0.f;}; + virtual void SetMargin(float margin) + { + if (m_collisionShape) + m_collisionShape->setMargin(btScalar(margin)); + } + virtual float GetMargin() const + { + return (m_collisionShape) ? m_collisionShape->getMargin() : 0.f; + } + virtual float GetRadius() const + { + // this is not the actual shape radius, it's only used for Fh support + return m_cci.m_radius; + } + virtual void SetRadius(float margin) + { + if (m_collisionShape && m_collisionShape->getShapeType() == SPHERE_SHAPE_PROXYTYPE) + { + btSphereShape* sphereShape = static_cast<btSphereShape*>(m_collisionShape); + sphereShape->setUnscaledRadius(margin); + } + m_cci.m_radius = margin; + } bool wantsSleeping(); void UpdateDeactivation(float timeStep); + void SetCenterOfMassTransform(btTransform& xform); + static btTransform GetTransformFromMotionState(PHY_IMotionState* motionState); void setAabb(const btVector3& aabbMin,const btVector3& aabbMax); @@ -215,7 +459,28 @@ class CcdPhysicsController : public PHY_IPhysicsController return m_MotionState; } + class CcdPhysicsEnvironment* GetPhysicsEnvironment() + { + return m_cci.m_physicsEnv; + } + void setParentCtrl(CcdPhysicsController* parentCtrl) + { + m_parentCtrl = parentCtrl; + } + + CcdPhysicsController* getParentCtrl() + { + return m_parentCtrl; + } + + const CcdPhysicsController* getParentCtrl() const + { + return m_parentCtrl; + } + + + }; diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp index 99c3e5f77c7..4fe35630784 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp +++ b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp @@ -23,6 +23,9 @@ subject to the following restrictions: #include "btBulletDynamicsCommon.h" #include "LinearMath/btIDebugDraw.h" #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h" +#include "BulletSoftBody/btSoftRigidDynamicsWorld.h" +#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h" +#include "BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h" //profiling/timings #include "LinearMath/btQuickprof.h" @@ -30,6 +33,7 @@ subject to the following restrictions: #include "PHY_IMotionState.h" +#define CCD_CONSTRAINT_DISABLE_LINKED_COLLISION 0x80 bool useIslands = true; @@ -53,6 +57,7 @@ void DrawRasterizerLine(const float* from,const float* to,int color); #include <stdio.h> +#include <string.h> // for memset #ifdef NEW_BULLET_VEHICLE_SUPPORT class WrapperVehicle : public PHY_IVehicle @@ -251,6 +256,22 @@ public: }; #endif //NEW_BULLET_VEHICLE_SUPPORT +class CcdOverlapFilterCallBack : public btOverlapFilterCallback +{ +private: + class CcdPhysicsEnvironment* m_physEnv; +public: + CcdOverlapFilterCallBack(CcdPhysicsEnvironment* env) : + m_physEnv(env) + { + } + virtual ~CcdOverlapFilterCallBack() + { + } + // return true when pairs need collision + virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const; +}; + void CcdPhysicsEnvironment::setDebugDrawer(btIDebugDraw* debugDrawer) { @@ -302,33 +323,40 @@ m_numTimeSubSteps(1), m_ccdMode(0), m_solverType(-1), m_profileTimings(0), -m_enableSatCollisionDetection(false) +m_enableSatCollisionDetection(false), +m_solver(NULL), +m_ownPairCache(NULL), +m_ownDispatcher(NULL), +m_filterCallback(NULL) { for (int i=0;i<PHY_NUM_RESPONSE;i++) { m_triggerCallbacks[i] = 0; } - if (!dispatcher) - dispatcher = new btCollisionDispatcher(); +// m_collisionConfiguration = new btDefaultCollisionConfiguration(); + m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration(); - if(!pairCache) + if (!dispatcher) { - - //todo: calculate/let user specify this world sizes - btVector3 worldMin(-10000,-10000,-10000); - btVector3 worldMax(10000,10000,10000); - - pairCache = new btAxisSweep3(worldMin,worldMax); - - //broadphase = new btSimpleBroadphase(); + btCollisionDispatcher* disp = new btCollisionDispatcher(m_collisionConfiguration); + dispatcher = disp; + btGImpactCollisionAlgorithm::registerAlgorithm(disp); + m_ownDispatcher = dispatcher; } + //m_broadphase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000)); + //m_broadphase = new btSimpleBroadphase(); + m_broadphase = new btDbvtBroadphase(); + + m_filterCallback = new CcdOverlapFilterCallBack(this); + m_broadphase->getOverlappingPairCache()->setOverlapFilterCallback(m_filterCallback); setSolverType(1);//issues with quickstep and memory allocations +// m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,m_broadphase,m_solver,m_collisionConfiguration); + m_dynamicsWorld = new btSoftRigidDynamicsWorld(dispatcher,m_broadphase,m_solver,m_collisionConfiguration); - m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,pairCache,new btSequentialImpulseConstraintSolver()); m_debugDrawer = 0; m_gravity = btVector3(0.f,-10.f,0.f); m_dynamicsWorld->setGravity(m_gravity); @@ -339,23 +367,42 @@ m_enableSatCollisionDetection(false) void CcdPhysicsEnvironment::addCcdPhysicsController(CcdPhysicsController* ctrl) { btRigidBody* body = ctrl->GetRigidBody(); + btCollisionObject* obj = ctrl->GetCollisionObject(); //this m_userPointer is just used for triggers, see CallbackTriggers - body->setUserPointer(ctrl); + obj->setUserPointer(ctrl); + if (body) + body->setGravity( m_gravity ); - body->setGravity( m_gravity ); - m_controllers.push_back(ctrl); + m_controllers.insert(ctrl); - m_dynamicsWorld->addRigidBody(body); - if (body->isStaticOrKinematicObject()) + if (body) + { + //use explicit group/filter for finer control over collision in bullet => near/radar sensor + m_dynamicsWorld->addRigidBody(body, ctrl->GetCollisionFilterGroup(), ctrl->GetCollisionFilterMask()); + } else + { + if (ctrl->GetSoftBody()) + { + btSoftBody* softBody = ctrl->GetSoftBody(); + m_dynamicsWorld->addSoftBody(softBody); + } else + { + if (obj->getCollisionShape()) + { + m_dynamicsWorld->addCollisionObject(obj,ctrl->GetCollisionFilterGroup(), ctrl->GetCollisionFilterMask()); + } + } + } + if (obj->isStaticOrKinematicObject()) { - body->setActivationState(ISLAND_SLEEPING); + obj->setActivationState(ISLAND_SLEEPING); } //CollisionObject(body,ctrl->GetCollisionFilterGroup(),ctrl->GetCollisionFilterMask()); - assert(body->getBroadphaseHandle()); + assert(obj->getBroadphaseHandle()); btBroadphaseInterface* scene = getBroadphase(); @@ -364,7 +411,7 @@ void CcdPhysicsEnvironment::addCcdPhysicsController(CcdPhysicsController* ctrl) assert(shapeinterface); - const btTransform& t = ctrl->GetRigidBody()->getCenterOfMassTransform(); + const btTransform& t = ctrl->GetCollisionObject()->getWorldTransform(); btPoint3 minAabb,maxAabb; @@ -376,69 +423,123 @@ void CcdPhysicsEnvironment::addCcdPhysicsController(CcdPhysicsController* ctrl) //extent it with the motion - btVector3 linMotion = body->getLinearVelocity()*timeStep; - - float maxAabbx = maxAabb.getX(); - float maxAabby = maxAabb.getY(); - float maxAabbz = maxAabb.getZ(); - float minAabbx = minAabb.getX(); - float minAabby = minAabb.getY(); - float minAabbz = minAabb.getZ(); - - if (linMotion.x() > 0.f) - maxAabbx += linMotion.x(); - else - minAabbx += linMotion.x(); - if (linMotion.y() > 0.f) - maxAabby += linMotion.y(); - else - minAabby += linMotion.y(); - if (linMotion.z() > 0.f) - maxAabbz += linMotion.z(); - else - minAabbz += linMotion.z(); - - - minAabb = btVector3(minAabbx,minAabby,minAabbz); - maxAabb = btVector3(maxAabbx,maxAabby,maxAabbz); - + if (body) + { + btVector3 linMotion = body->getLinearVelocity()*timeStep; + + float maxAabbx = maxAabb.getX(); + float maxAabby = maxAabb.getY(); + float maxAabbz = maxAabb.getZ(); + float minAabbx = minAabb.getX(); + float minAabby = minAabb.getY(); + float minAabbz = minAabb.getZ(); + + if (linMotion.x() > 0.f) + maxAabbx += linMotion.x(); + else + minAabbx += linMotion.x(); + if (linMotion.y() > 0.f) + maxAabby += linMotion.y(); + else + minAabby += linMotion.y(); + if (linMotion.z() > 0.f) + maxAabbz += linMotion.z(); + else + minAabbz += linMotion.z(); + + + minAabb = btVector3(minAabbx,minAabby,minAabbz); + maxAabb = btVector3(maxAabbx,maxAabby,maxAabbz); + } } + + void CcdPhysicsEnvironment::removeCcdPhysicsController(CcdPhysicsController* ctrl) { - //also remove constraint - - - - m_dynamicsWorld->removeRigidBody(ctrl->GetRigidBody()); - - + btRigidBody* body = ctrl->GetRigidBody(); + if (body) + { + m_dynamicsWorld->removeRigidBody(ctrl->GetRigidBody()); + } else { - std::vector<CcdPhysicsController*>::iterator i = - std::find(m_controllers.begin(), m_controllers.end(), ctrl); - if (!(i == m_controllers.end())) + //if a softbody + if (ctrl->GetSoftBody()) { - std::swap(*i, m_controllers.back()); - m_controllers.pop_back(); + m_dynamicsWorld->removeSoftBody(ctrl->GetSoftBody()); + } else + { + m_dynamicsWorld->removeCollisionObject(ctrl->GetCollisionObject()); } } + m_controllers.erase(ctrl); + + if (ctrl->m_registerCount != 0) + printf("Warning: removing controller with non-zero m_registerCount: %d\n", ctrl->m_registerCount); //remove it from the triggers + m_triggerControllers.erase(ctrl); +} + +void CcdPhysicsEnvironment::updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask) +{ + // this function is used when the collisionning group of a controller is changed + // remove and add the collistioning object + btRigidBody* body = ctrl->GetRigidBody(); + btCollisionObject* obj = ctrl->GetCollisionObject(); + if (obj) { - std::vector<CcdPhysicsController*>::iterator i = - std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl); - if (!(i == m_triggerControllers.end())) + btVector3 inertia(0.0,0.0,0.0); + m_dynamicsWorld->removeCollisionObject(obj); + obj->setCollisionFlags(newCollisionFlags); + if (body) { - std::swap(*i, m_triggerControllers.back()); - m_triggerControllers.pop_back(); + if (newMass) + body->getCollisionShape()->calculateLocalInertia(newMass, inertia); + body->setMassProps(newMass, inertia); } + m_dynamicsWorld->addCollisionObject(obj, newCollisionGroup, newCollisionMask); } + // to avoid nasty interaction, we must update the property of the controller as well + ctrl->m_cci.m_mass = newMass; + ctrl->m_cci.m_collisionFilterGroup = newCollisionGroup; + ctrl->m_cci.m_collisionFilterMask = newCollisionMask; + ctrl->m_cci.m_collisionFlags = newCollisionFlags; +} +void CcdPhysicsEnvironment::enableCcdPhysicsController(CcdPhysicsController* ctrl) +{ + if (m_controllers.insert(ctrl).second) + { + btCollisionObject* obj = ctrl->GetCollisionObject(); + obj->setUserPointer(ctrl); + m_dynamicsWorld->addCollisionObject(obj, + ctrl->GetCollisionFilterGroup(), ctrl->GetCollisionFilterMask()); + } +} +void CcdPhysicsEnvironment::disableCcdPhysicsController(CcdPhysicsController* ctrl) +{ + if (m_controllers.erase(ctrl)) + { + btRigidBody* body = ctrl->GetRigidBody(); + if (body) + { + m_dynamicsWorld->removeRigidBody(body); + } else + { + if (ctrl->GetSoftBody()) + { + } else + { + m_dynamicsWorld->removeCollisionObject(body); + } + } + } } @@ -450,25 +551,31 @@ void CcdPhysicsEnvironment::beginFrame() bool CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep) { + std::set<CcdPhysicsController*>::iterator it; + int i; - int i,numCtrl = GetNumControllers(); - for (i=0;i<numCtrl;i++) + for (it=m_controllers.begin(); it!=m_controllers.end(); it++) { - CcdPhysicsController* ctrl = GetPhysicsController(i); - ctrl->SynchronizeMotionStates(timeStep); + (*it)->SynchronizeMotionStates(timeStep); } + processFhSprings(curTime,timeStep); + float subStep = timeStep / float(m_numTimeSubSteps); for (i=0;i<m_numTimeSubSteps;i++) { - m_dynamicsWorld->stepSimulation(subStep,0);//perform always a full simulation step +// m_dynamicsWorld->stepSimulation(subStep,20,1./240.);//perform always a full simulation step + m_dynamicsWorld->stepSimulation(subStep,0);//perform always a full simulation step } - numCtrl = GetNumControllers(); - for (i=0;i<numCtrl;i++) + for (it=m_controllers.begin(); it!=m_controllers.end(); it++) { - CcdPhysicsController* ctrl = GetPhysicsController(i); - ctrl->SynchronizeMotionStates(timeStep); + (*it)->SynchronizeMotionStates(timeStep); + } + + for (it=m_controllers.begin(); it!=m_controllers.end(); it++) + { + (*it)->SynchronizeMotionStates(timeStep); } for (i=0;i<m_wrapperVehicles.size();i++) @@ -477,11 +584,181 @@ bool CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep) veh->SyncWheels(); } + if (m_dynamicsWorld->getDebugDrawer() && m_dynamicsWorld->getDebugDrawer()->getDebugMode() >0) + m_dynamicsWorld->debugDrawWorld(); + + CallbackTriggers(); return true; } +class ClosestRayResultCallbackNotMe : public btCollisionWorld::ClosestRayResultCallback +{ + btCollisionObject* m_owner; + btCollisionObject* m_parent; + +public: + ClosestRayResultCallbackNotMe(const btVector3& rayFromWorld,const btVector3& rayToWorld,btCollisionObject* owner,btCollisionObject* parent) + :btCollisionWorld::ClosestRayResultCallback(rayFromWorld,rayToWorld), + m_owner(owner) + { + + } + + virtual bool needsCollision(btBroadphaseProxy* proxy0) const + { + //don't collide with self + if (proxy0->m_clientObject == m_owner) + return false; + + if (proxy0->m_clientObject == m_parent) + return false; + + return btCollisionWorld::ClosestRayResultCallback::needsCollision(proxy0); + } + +}; + +void CcdPhysicsEnvironment::processFhSprings(double curTime,float timeStep) +{ + std::set<CcdPhysicsController*>::iterator it; + + for (it=m_controllers.begin(); it!=m_controllers.end(); it++) + { + CcdPhysicsController* ctrl = (*it); + btRigidBody* body = ctrl->GetRigidBody(); + + if (body && (ctrl->getConstructionInfo().m_do_fh || ctrl->getConstructionInfo().m_do_rot_fh)) + { + //printf("has Fh or RotFh\n"); + //re-implement SM_FhObject.cpp using btCollisionWorld::rayTest and info from ctrl->getConstructionInfo() + //send a ray from {0.0, 0.0, 0.0} towards {0.0, 0.0, -10.0}, in local coordinates + + + CcdPhysicsController* parentCtrl = ctrl->getParentCtrl(); + btRigidBody* parentBody = parentCtrl?parentCtrl->GetRigidBody() : 0; + btRigidBody* cl_object = parentBody ? parentBody : body; + + if (body->isStaticOrKinematicObject()) + continue; + + btVector3 rayDirLocal(0,0,-10); + + //m_dynamicsWorld + //ctrl->GetRigidBody(); + btVector3 rayFromWorld = body->getCenterOfMassPosition(); + //btVector3 rayToWorld = rayFromWorld + body->getCenterOfMassTransform().getBasis() * rayDirLocal; + //ray always points down the z axis in world space... + btVector3 rayToWorld = rayFromWorld + rayDirLocal; + + ClosestRayResultCallbackNotMe resultCallback(rayFromWorld,rayToWorld,body,parentBody); + + m_dynamicsWorld->rayTest(rayFromWorld,rayToWorld,resultCallback); + if (resultCallback.hasHit()) + { + //we hit this one: resultCallback.m_collisionObject; + CcdPhysicsController* controller = static_cast<CcdPhysicsController*>(resultCallback.m_collisionObject->getUserPointer()); + + if (controller) + { + if (controller->getConstructionInfo().m_fh_distance < SIMD_EPSILON) + continue; + + btRigidBody* hit_object = controller->GetRigidBody(); + if (!hit_object) + continue; + + CcdConstructionInfo& hitObjShapeProps = controller->getConstructionInfo(); + + float distance = resultCallback.m_closestHitFraction*rayDirLocal.length()-ctrl->getConstructionInfo().m_radius; + if (distance >= hitObjShapeProps.m_fh_distance) + continue; + + + + //btVector3 ray_dir = cl_object->getCenterOfMassTransform().getBasis()* rayDirLocal.normalized(); + btVector3 ray_dir = rayDirLocal.normalized(); + btVector3 normal = resultCallback.m_hitNormalWorld; + normal.normalize(); + + + if (ctrl->getConstructionInfo().m_do_fh) + { + btVector3 lspot = cl_object->getCenterOfMassPosition() + + rayDirLocal * resultCallback.m_closestHitFraction; + + + + + lspot -= hit_object->getCenterOfMassPosition(); + btVector3 rel_vel = cl_object->getLinearVelocity() - hit_object->getVelocityInLocalPoint(lspot); + btScalar rel_vel_ray = ray_dir.dot(rel_vel); + btScalar spring_extent = 1.0 - distance / hitObjShapeProps.m_fh_distance; + + btScalar i_spring = spring_extent * hitObjShapeProps.m_fh_spring; + btScalar i_damp = rel_vel_ray * hitObjShapeProps.m_fh_damping; + + cl_object->setLinearVelocity(cl_object->getLinearVelocity() + (-(i_spring + i_damp) * ray_dir)); + if (hitObjShapeProps.m_fh_normal) + { + cl_object->setLinearVelocity(cl_object->getLinearVelocity()+(i_spring + i_damp) *(normal - normal.dot(ray_dir) * ray_dir)); + } + + btVector3 lateral = rel_vel - rel_vel_ray * ray_dir; + + + if (ctrl->getConstructionInfo().m_do_anisotropic) { + //Bullet basis contains no scaling/shear etc. + const btMatrix3x3& lcs = cl_object->getCenterOfMassTransform().getBasis(); + btVector3 loc_lateral = lateral * lcs; + const btVector3& friction_scaling = cl_object->getAnisotropicFriction(); + loc_lateral *= friction_scaling; + lateral = lcs * loc_lateral; + } + + btScalar rel_vel_lateral = lateral.length(); + + if (rel_vel_lateral > SIMD_EPSILON) { + btScalar friction_factor = hit_object->getFriction();//cl_object->getFriction(); + + btScalar max_friction = friction_factor * btMax(btScalar(0.0), i_spring); + + btScalar rel_mom_lateral = rel_vel_lateral / cl_object->getInvMass(); + + btVector3 friction = (rel_mom_lateral > max_friction) ? + -lateral * (max_friction / rel_vel_lateral) : + -lateral; + + cl_object->applyCentralImpulse(friction); + } + } + + + if (ctrl->getConstructionInfo().m_do_rot_fh) { + btVector3 up2 = cl_object->getWorldTransform().getBasis().getColumn(2); + + btVector3 t_spring = up2.cross(normal) * hitObjShapeProps.m_fh_spring; + btVector3 ang_vel = cl_object->getAngularVelocity(); + + // only rotations that tilt relative to the normal are damped + ang_vel -= ang_vel.dot(normal) * normal; + + btVector3 t_damp = ang_vel * hitObjShapeProps.m_fh_damping; + + cl_object->setAngularVelocity(cl_object->getAngularVelocity() + (t_spring - t_damp)); + } + + } + + + } + + + } + } + +} void CcdPhysicsEnvironment::setDebugMode(int debugMode) { @@ -537,7 +814,7 @@ void CcdPhysicsEnvironment::setSolverDamping(float damping) void CcdPhysicsEnvironment::setLinearAirDamping(float damping) { - gLinearAirDamping = damping; + //gLinearAirDamping = damping; } void CcdPhysicsEnvironment::setUseEpa(bool epa) @@ -556,7 +833,7 @@ void CcdPhysicsEnvironment::setSolverType(int solverType) { m_solver = new btSequentialImpulseConstraintSolver(); - +// ((btSequentialImpulseConstraintSolver*)m_solver)->setSolverMode(btSequentialImpulseConstraintSolver::SOLVER_USE_WARMSTARTING | btSequentialImpulseConstraintSolver::SOLVER_RANDMIZE_ORDER); break; } } @@ -577,7 +854,13 @@ void CcdPhysicsEnvironment::setSolverType(int solverType) - +void CcdPhysicsEnvironment::getGravity(PHY__Vector3& grav) +{ + const btVector3& gravity = m_dynamicsWorld->getGravity(); + grav[0] = gravity.getX(); + grav[1] = gravity.getY(); + grav[2] = gravity.getZ(); +} void CcdPhysicsEnvironment::setGravity(float x,float y,float z) @@ -600,13 +883,16 @@ int CcdPhysicsEnvironment::createUniversalD6Constraint( const btVector3& linearMinLimits, const btVector3& linearMaxLimits, const btVector3& angularMinLimits, - const btVector3& angularMaxLimits + const btVector3& angularMaxLimits,int flags ) { + bool disableCollisionBetweenLinkedBodies = (0!=(flags & CCD_CONSTRAINT_DISABLE_LINKED_COLLISION)); + //we could either add some logic to recognize ball-socket and hinge, or let that up to the user //perhaps some warning or hint that hinge/ball-socket is more efficient? + btGeneric6DofConstraint* genericConstraint = 0; CcdPhysicsController* ctrl0 = (CcdPhysicsController*) ctrlRef; CcdPhysicsController* ctrl1 = (CcdPhysicsController*) ctrlOther; @@ -618,9 +904,10 @@ int CcdPhysicsEnvironment::createUniversalD6Constraint( { + bool useReferenceFrameA = true; genericConstraint = new btGeneric6DofConstraint( *rb0,*rb1, - frameInA,frameInB); + frameInA,frameInB,useReferenceFrameA); genericConstraint->setLinearLowerLimit(linearMinLimits); genericConstraint->setLinearUpperLimit(linearMaxLimits); genericConstraint->setAngularLowerLimit(angularMinLimits); @@ -635,7 +922,7 @@ int CcdPhysicsEnvironment::createUniversalD6Constraint( if (genericConstraint) { // m_constraints.push_back(genericConstraint); - m_dynamicsWorld->addConstraint(genericConstraint); + m_dynamicsWorld->addConstraint(genericConstraint,disableCollisionBetweenLinkedBodies); genericConstraint->setUserConstraintId(gConstraintUid++); genericConstraint->setUserConstraintType(PHY_GENERIC_6DOF_CONSTRAINT); @@ -668,35 +955,54 @@ void CcdPhysicsEnvironment::removeConstraint(int constraintId) struct FilterClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback { - PHY_IPhysicsController* m_ignoreClient; + PHY_IRayCastFilterCallback& m_phyRayFilter; + const btCollisionShape* m_hitTriangleShape; + int m_hitTriangleIndex; - FilterClosestRayResultCallback (PHY_IPhysicsController* ignoreClient,const btVector3& rayFrom,const btVector3& rayTo) + FilterClosestRayResultCallback (PHY_IRayCastFilterCallback& phyRayFilter,const btVector3& rayFrom,const btVector3& rayTo) : btCollisionWorld::ClosestRayResultCallback(rayFrom,rayTo), - m_ignoreClient(ignoreClient) + m_phyRayFilter(phyRayFilter), + m_hitTriangleShape(NULL), + m_hitTriangleIndex(0) { - } virtual ~FilterClosestRayResultCallback() { } - virtual float AddSingleResult( btCollisionWorld::LocalRayResult& rayResult) + virtual bool needsCollision(btBroadphaseProxy* proxy0) const + { + if (!(proxy0->m_collisionFilterGroup & m_collisionFilterMask)) + return false; + if (!(m_collisionFilterGroup & proxy0->m_collisionFilterMask)) + return false; + btCollisionObject* object = (btCollisionObject*)proxy0->m_clientObject; + CcdPhysicsController* phyCtrl = static_cast<CcdPhysicsController*>(object->getUserPointer()); + if (phyCtrl == m_phyRayFilter.m_ignoreController) + return false; + return m_phyRayFilter.needBroadphaseRayCast(phyCtrl); + } + + virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace) { CcdPhysicsController* curHit = static_cast<CcdPhysicsController*>(rayResult.m_collisionObject->getUserPointer()); - //ignore client... - if (curHit != m_ignoreClient) - { - //if valid - return ClosestRayResultCallback::AddSingleResult(rayResult); + // save shape information as ClosestRayResultCallback::AddSingleResult() does not do it + if (rayResult.m_localShapeInfo) + { + m_hitTriangleShape = rayResult.m_collisionObject->getCollisionShape(); + m_hitTriangleIndex = rayResult.m_localShapeInfo->m_triangleIndex; + } else + { + m_hitTriangleShape = NULL; + m_hitTriangleIndex = 0; } - return m_closestHitFraction; + return ClosestRayResultCallback::addSingleResult(rayResult,normalInWorldSpace); } }; -PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ) +PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ) { @@ -710,19 +1016,101 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IPhysicsController* i //Either Ray Cast with or without filtering //btCollisionWorld::ClosestRayResultCallback rayCallback(rayFrom,rayTo); - FilterClosestRayResultCallback rayCallback(ignoreClient,rayFrom,rayTo); + FilterClosestRayResultCallback rayCallback(filterCallback,rayFrom,rayTo); + + PHY_RayCastResult result; + memset(&result, 0, sizeof(result)); - PHY_IPhysicsController* nearestHit = 0; + // don't collision with sensor object + rayCallback.m_collisionFilterMask = CcdConstructionInfo::AllFilter ^ CcdConstructionInfo::SensorFilter; + //, ,filterCallback.m_faceNormal); m_dynamicsWorld->rayTest(rayFrom,rayTo,rayCallback); - if (rayCallback.HasHit()) + if (rayCallback.hasHit()) { - nearestHit = static_cast<CcdPhysicsController*>(rayCallback.m_collisionObject->getUserPointer()); - hitX = rayCallback.m_hitPointWorld.getX(); - hitY = rayCallback.m_hitPointWorld.getY(); - hitZ = rayCallback.m_hitPointWorld.getZ(); + CcdPhysicsController* controller = static_cast<CcdPhysicsController*>(rayCallback.m_collisionObject->getUserPointer()); + result.m_controller = controller; + result.m_hitPoint[0] = rayCallback.m_hitPointWorld.getX(); + result.m_hitPoint[1] = rayCallback.m_hitPointWorld.getY(); + result.m_hitPoint[2] = rayCallback.m_hitPointWorld.getZ(); + + if (rayCallback.m_hitTriangleShape != NULL) + { + // identify the mesh polygon + CcdShapeConstructionInfo* shapeInfo = controller->m_shapeInfo; + if (shapeInfo) + { + btCollisionShape* shape = controller->GetCollisionObject()->getCollisionShape(); + if (shape->isCompound()) + { + btCompoundShape* compoundShape = (btCompoundShape*)shape; + CcdShapeConstructionInfo* compoundShapeInfo = shapeInfo; + // need to search which sub-shape has been hit + for (int i=0; i<compoundShape->getNumChildShapes(); i++) + { + shapeInfo = compoundShapeInfo->GetChildShape(i); + shape=compoundShape->getChildShape(i); + if (shape == rayCallback.m_hitTriangleShape) + break; + } + } + if (shape == rayCallback.m_hitTriangleShape && + rayCallback.m_hitTriangleIndex < shapeInfo->m_polygonIndexArray.size()) + { + result.m_meshObject = shapeInfo->GetMesh(); + result.m_polygon = shapeInfo->m_polygonIndexArray.at(rayCallback.m_hitTriangleIndex); + // Bullet returns the normal from "outside". + // If the user requests the real normal, compute it now + if (filterCallback.m_faceNormal) + { + // mesh shapes are shared and stored in the shapeInfo + btTriangleMeshShape* triangleShape = shapeInfo->GetMeshShape(); + if (triangleShape) + { + // this code is copied from Bullet + btVector3 triangle[3]; + const unsigned char *vertexbase; + int numverts; + PHY_ScalarType type; + int stride; + const unsigned char *indexbase; + int indexstride; + int numfaces; + PHY_ScalarType indicestype; + btStridingMeshInterface* meshInterface = triangleShape->getMeshInterface(); + + meshInterface->getLockedReadOnlyVertexIndexBase( + &vertexbase, + numverts, + type, + stride, + &indexbase, + indexstride, + numfaces, + indicestype, + 0); + + unsigned int* gfxbase = (unsigned int*)(indexbase+rayCallback.m_hitTriangleIndex*indexstride); + const btVector3& meshScaling = shape->getLocalScaling(); + for (int j=2;j>=0;j--) + { + int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:gfxbase[j]; + + btScalar* graphicsbase = (btScalar*)(vertexbase+graphicsindex*stride); + + triangle[j] = btVector3(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ()); + } + meshInterface->unLockReadOnlyVertexBase(0); + btVector3 triangleNormal; + triangleNormal = (triangle[1]-triangle[0]).cross(triangle[2]-triangle[0]); + rayCallback.m_hitNormalWorld = rayCallback.m_collisionObject->getWorldTransform().getBasis()*triangleNormal; + } + } + } + } + } if (rayCallback.m_hitNormalWorld.length2() > (SIMD_EPSILON*SIMD_EPSILON)) { rayCallback.m_hitNormalWorld.normalize(); @@ -730,14 +1118,14 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::rayTest(PHY_IPhysicsController* i { rayCallback.m_hitNormalWorld.setValue(1,0,0); } - normalX = rayCallback.m_hitNormalWorld.getX(); - normalY = rayCallback.m_hitNormalWorld.getY(); - normalZ = rayCallback.m_hitNormalWorld.getZ(); - + result.m_hitNormal[0] = rayCallback.m_hitNormalWorld.getX(); + result.m_hitNormal[1] = rayCallback.m_hitNormalWorld.getY(); + result.m_hitNormal[2] = rayCallback.m_hitNormalWorld.getZ(); + filterCallback.reportHit(&result); } - return nearestHit; + return result.m_controller; } @@ -760,6 +1148,13 @@ btBroadphaseInterface* CcdPhysicsEnvironment::getBroadphase() return m_dynamicsWorld->getBroadphase(); } +btDispatcher* CcdPhysicsEnvironment::getDispatcher() +{ + return m_dynamicsWorld->getDispatcher(); +} + + + @@ -778,22 +1173,27 @@ CcdPhysicsEnvironment::~CcdPhysicsEnvironment() delete m_dynamicsWorld; + if (NULL != m_ownPairCache) + delete m_ownPairCache; -} - + if (NULL != m_ownDispatcher) + delete m_ownDispatcher; -int CcdPhysicsEnvironment::GetNumControllers() -{ - return m_controllers.size(); -} + if (NULL != m_solver) + delete m_solver; + if (NULL != m_debugDrawer) + delete m_debugDrawer; -CcdPhysicsController* CcdPhysicsEnvironment::GetPhysicsController( int index) -{ - return m_controllers[index]; -} + if (NULL != m_filterCallback) + delete m_filterCallback; + if (NULL != m_collisionConfiguration) + delete m_collisionConfiguration; + if (NULL != m_broadphase) + delete m_broadphase; +} void CcdPhysicsEnvironment::setConstraintParam(int constraintId,int param,float value0,float value1) @@ -805,7 +1205,7 @@ void CcdPhysicsEnvironment::setConstraintParam(int constraintId,int param,float { //param = 1..12, min0,max0,min1,max1...min6,max6 btGeneric6DofConstraint* genCons = (btGeneric6DofConstraint*)typedConstraint; - genCons->SetLimit(param,value0,value1); + genCons->setLimit(param,value0,value1); break; }; default: @@ -835,15 +1235,18 @@ void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl) { CcdPhysicsController* ctrl1 = (CcdPhysicsController* )ctrl; - std::vector<CcdPhysicsController*>::iterator i = - std::find(m_controllers.begin(), m_controllers.end(), ctrl); - if ((i == m_controllers.end())) - { - addCcdPhysicsController(ctrl1); - } + // addSensor() is a "light" function for bullet because it is used + // dynamically when the sensor is activated. Use enableCcdPhysicsController() instead + //if (m_controllers.insert(ctrl1).second) + //{ + // addCcdPhysicsController(ctrl1); + //} + enableCcdPhysicsController(ctrl1); + + //Collision filter/mask is now set at the time of the creation of the controller //force collision detection with everything, including static objects (might hurt performance!) - ctrl1->GetRigidBody()->getBroadphaseHandle()->m_collisionFilterMask = btBroadphaseProxy::AllFilter; - ctrl1->GetRigidBody()->getBroadphaseHandle()->m_collisionFilterGroup = btBroadphaseProxy::AllFilter; + //ctrl1->GetRigidBody()->getBroadphaseHandle()->m_collisionFilterMask = btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::SensorTrigger; + //ctrl1->GetRigidBody()->getBroadphaseHandle()->m_collisionFilterGroup = btBroadphaseProxy::SensorTrigger; //todo: make this 'sensor'! requestCollisionCallback(ctrl); @@ -852,21 +1255,19 @@ void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl) void CcdPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl) { - std::vector<CcdPhysicsController*>::iterator i = - std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl); - if (!(i == m_triggerControllers.end())) - { - std::swap(*i, m_triggerControllers.back()); - m_triggerControllers.pop_back(); - } + CcdPhysicsController* ccdCtrl = (CcdPhysicsController*)ctrl; + if (ccdCtrl->Unregister()) + m_triggerControllers.erase(ccdCtrl); } void CcdPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl) { removeCollisionCallback(ctrl); - //printf("removeSensor\n"); + + disableCcdPhysicsController((CcdPhysicsController*)ctrl); } + void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user) { /* printf("addTouchCallback\n(response class = %i)\n",response_class); @@ -903,11 +1304,10 @@ void CcdPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctr { CcdPhysicsController* ccdCtrl = static_cast<CcdPhysicsController*>(ctrl); - //printf("requestCollisionCallback\n"); - m_triggerControllers.push_back(ccdCtrl); + if (ccdCtrl->Register()) + m_triggerControllers.insert(ccdCtrl); } - void CcdPhysicsEnvironment::CallbackTriggers() { @@ -916,13 +1316,16 @@ void CcdPhysicsEnvironment::CallbackTriggers() if (m_triggerCallbacks[PHY_OBJECT_RESPONSE] || (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawContactPoints))) { //walk over all overlapping pairs, and if one of the involved bodies is registered for trigger callback, perform callback - int numManifolds = m_dynamicsWorld->getDispatcher()->getNumManifolds(); + btDispatcher* dispatcher = m_dynamicsWorld->getDispatcher(); + int numManifolds = dispatcher->getNumManifolds(); for (int i=0;i<numManifolds;i++) { - btPersistentManifold* manifold = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i); + btPersistentManifold* manifold = dispatcher->getManifoldByIndexInternal(i); int numContacts = manifold->getNumContacts(); if (numContacts) { + btRigidBody* rb0 = static_cast<btRigidBody*>(manifold->getBody0()); + btRigidBody* rb1 = static_cast<btRigidBody*>(manifold->getBody1()); if (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawContactPoints)) { for (int j=0;j<numContacts;j++) @@ -933,18 +1336,17 @@ void CcdPhysicsEnvironment::CallbackTriggers() m_debugDrawer->drawContactPoint(cp.m_positionWorldOnB,cp.m_normalWorldOnB,cp.getDistance(),cp.getLifeTime(),color); } } - btRigidBody* obj0 = static_cast<btRigidBody* >(manifold->getBody0()); - btRigidBody* obj1 = static_cast<btRigidBody* >(manifold->getBody1()); + btRigidBody* obj0 = rb0; + btRigidBody* obj1 = rb1; //m_internalOwner is set in 'addPhysicsController' CcdPhysicsController* ctrl0 = static_cast<CcdPhysicsController*>(obj0->getUserPointer()); CcdPhysicsController* ctrl1 = static_cast<CcdPhysicsController*>(obj1->getUserPointer()); - std::vector<CcdPhysicsController*>::iterator i = - std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl0); + std::set<CcdPhysicsController*>::const_iterator i = m_triggerControllers.find(ctrl0); if (i == m_triggerControllers.end()) { - i = std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl1); + i = m_triggerControllers.find(ctrl1); } if (!(i == m_triggerControllers.end())) @@ -952,6 +1354,15 @@ void CcdPhysicsEnvironment::CallbackTriggers() m_triggerCallbacks[PHY_OBJECT_RESPONSE](m_triggerCallbacksUserPtrs[PHY_OBJECT_RESPONSE], ctrl0,ctrl1,0); } + // Bullet does not refresh the manifold contact point for object without contact response + // may need to remove this when a newer Bullet version is integrated + if (!dispatcher->needsResponse(rb0, rb1)) + { + // Refresh algorithm fails sometimes when there is penetration + // (usuall the case with ghost and sensor objects) + // Let's just clear the manifold, in any case, it is recomputed on each frame. + manifold->clearManifold(); //refreshContactPoints(rb0->getCenterOfMassTransform(),rb1->getCenterOfMassTransform()); + } } } @@ -962,9 +1373,47 @@ void CcdPhysicsEnvironment::CallbackTriggers() } - - - +// This call back is called before a pair is added in the cache +// Handy to remove objects that must be ignored by sensors +bool CcdOverlapFilterCallBack::needBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const +{ + btCollisionObject *colObj0, *colObj1; + CcdPhysicsController *sensorCtrl, *objCtrl; + bool collides; + // first check the filters + collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0; + collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask); + if (!collides) + return false; + + // additional check for sensor object + if (proxy0->m_collisionFilterGroup & btBroadphaseProxy::SensorTrigger) + { + // this is a sensor object, the other one can't be a sensor object because + // they exclude each other in the above test + assert(!(proxy1->m_collisionFilterGroup & btBroadphaseProxy::SensorTrigger)); + colObj0 = (btCollisionObject*)proxy0->m_clientObject; + colObj1 = (btCollisionObject*)proxy1->m_clientObject; + } + else if (proxy1->m_collisionFilterGroup & btBroadphaseProxy::SensorTrigger) + { + colObj0 = (btCollisionObject*)proxy1->m_clientObject; + colObj1 = (btCollisionObject*)proxy0->m_clientObject; + } + else + { + return true; + } + if (!colObj0 || !colObj1) + return false; + sensorCtrl = static_cast<CcdPhysicsController*>(colObj0->getUserPointer()); + objCtrl = static_cast<CcdPhysicsController*>(colObj1->getUserPointer()); + if (m_physEnv->m_triggerCallbacks[PHY_BROADPH_RESPONSE]) + { + return m_physEnv->m_triggerCallbacks[PHY_BROADPH_RESPONSE](m_physEnv->m_triggerCallbacksUserPtrs[PHY_BROADPH_RESPONSE], sensorCtrl, objCtrl, 0); + } + return true; +} #ifdef NEW_BULLET_VEHICLE_SUPPORT @@ -998,29 +1447,58 @@ PHY_IPhysicsController* CcdPhysicsEnvironment::CreateSphereController(float radi { CcdConstructionInfo cinfo; + // memory leak! The shape is not deleted by Bullet and we cannot add it to the KX_Scene.m_shapes list cinfo.m_collisionShape = new btSphereShape(radius); cinfo.m_MotionState = 0; cinfo.m_physicsEnv = this; - cinfo.m_collisionFlags |= btCollisionObject::CF_NO_CONTACT_RESPONSE | btCollisionObject::CF_KINEMATIC_OBJECT; + // declare this object as Dyamic rather then static!! + // The reason as it is designed to detect all type of object, including static object + // It would cause static-static message to be printed on the console otherwise + cinfo.m_collisionFlags |= btCollisionObject::CF_NO_CONTACT_RESPONSE/* | btCollisionObject::CF_KINEMATIC_OBJECT*/; DefaultMotionState* motionState = new DefaultMotionState(); cinfo.m_MotionState = motionState; + // we will add later the possibility to select the filter from option + cinfo.m_collisionFilterMask = CcdConstructionInfo::AllFilter ^ CcdConstructionInfo::SensorFilter; + cinfo.m_collisionFilterGroup = CcdConstructionInfo::SensorFilter; motionState->m_worldTransform.setIdentity(); motionState->m_worldTransform.setOrigin(btVector3(position[0],position[1],position[2])); CcdPhysicsController* sphereController = new CcdPhysicsController(cinfo); - return sphereController; } +int findClosestNode(btSoftBody* sb,const btVector3& worldPoint); +int findClosestNode(btSoftBody* sb,const btVector3& worldPoint) +{ + int node = -1; + + btSoftBody::tNodeArray& nodes(sb->m_nodes); + float maxDistSqr = 1e30f; + + for (int n=0;n<nodes.size();n++) + { + btScalar distSqr = (nodes[n].m_x - worldPoint).length2(); + if (distSqr<maxDistSqr) + { + maxDistSqr = distSqr; + node = n; + } + } + return node; +} + int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl0,class PHY_IPhysicsController* ctrl1,PHY_ConstraintType type, float pivotX,float pivotY,float pivotZ, float axisX,float axisY,float axisZ, float axis1X,float axis1Y,float axis1Z, - float axis2X,float axis2Y,float axis2Z + float axis2X,float axis2Y,float axis2Z,int flags ) { + bool disableCollisionBetweenLinkedBodies = (0!=(flags & CCD_CONSTRAINT_DISABLE_LINKED_COLLISION)); + + CcdPhysicsController* c0 = (CcdPhysicsController*)ctrl0; CcdPhysicsController* c1 = (CcdPhysicsController*)ctrl1; @@ -1028,14 +1506,166 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl btRigidBody* rb0 = c0 ? c0->GetRigidBody() : 0; btRigidBody* rb1 = c1 ? c1->GetRigidBody() : 0; + + + bool rb0static = rb0 ? rb0->isStaticOrKinematicObject() : true; bool rb1static = rb1 ? rb1->isStaticOrKinematicObject() : true; + + btCollisionObject* colObj0 = c0->GetCollisionObject(); + if (!colObj0) + { + return 0; + } + + btVector3 pivotInA(pivotX,pivotY,pivotZ); + + //it might be a soft body, let's try + btSoftBody* sb0 = c0 ? c0->GetSoftBody() : 0; + btSoftBody* sb1 = c1 ? c1->GetSoftBody() : 0; + if (sb0 && sb1) + { + //not between two soft bodies? + return 0; + } + + if (sb0) + { + //either cluster or node attach, let's find closest node first + //the soft body doesn't have a 'real' world transform, so get its initial world transform for now + btVector3 pivotPointSoftWorld = sb0->m_initialWorldTransform(pivotInA); + int node=findClosestNode(sb0,pivotPointSoftWorld); + if (node >=0) + { + bool clusterconstaint = false; +/* + switch (type) + { + case PHY_LINEHINGE_CONSTRAINT: + { + if (sb0->clusterCount() && rb1) + { + btSoftBody::LJoint::Specs ls; + ls.erp=0.5f; + ls.position=sb0->clusterCom(0); + sb0->appendLinearJoint(ls,rb1); + clusterconstaint = true; + break; + } + } + case PHY_GENERIC_6DOF_CONSTRAINT: + { + if (sb0->clusterCount() && rb1) + { + btSoftBody::AJoint::Specs as; + as.erp = 1; + as.cfm = 1; + as.axis.setValue(axisX,axisY,axisZ); + sb0->appendAngularJoint(as,rb1); + clusterconstaint = true; + break; + } + + break; + } + default: + { + + } + }; + */ + + if (!clusterconstaint) + { + if (rb1) + { + sb0->appendAnchor(node,rb1,disableCollisionBetweenLinkedBodies); + } else + { + sb0->setMass(node,0.f); + } + } + + + } + return 0;//can't remove soft body anchors yet + } + + if (sb1) + { + btVector3 pivotPointAWorld = colObj0->getWorldTransform()(pivotInA); + int node=findClosestNode(sb1,pivotPointAWorld); + if (node >=0) + { + bool clusterconstaint = false; + + /* + switch (type) + { + case PHY_LINEHINGE_CONSTRAINT: + { + if (sb1->clusterCount() && rb0) + { + btSoftBody::LJoint::Specs ls; + ls.erp=0.5f; + ls.position=sb1->clusterCom(0); + sb1->appendLinearJoint(ls,rb0); + clusterconstaint = true; + break; + } + } + case PHY_GENERIC_6DOF_CONSTRAINT: + { + if (sb1->clusterCount() && rb0) + { + btSoftBody::AJoint::Specs as; + as.erp = 1; + as.cfm = 1; + as.axis.setValue(axisX,axisY,axisZ); + sb1->appendAngularJoint(as,rb0); + clusterconstaint = true; + break; + } + + break; + } + default: + { + + + } + };*/ + + + if (!clusterconstaint) + { + if (rb0) + { + sb1->appendAnchor(node,rb0,disableCollisionBetweenLinkedBodies); + } else + { + sb1->setMass(node,0.f); + } + } + + + } + return 0;//can't remove soft body anchors yet + } + if (rb0static && rb1static) + { + return 0; + } + - btVector3 pivotInA(pivotX,pivotY,pivotZ); + if (!rb0) + return 0; + + btVector3 pivotInB = rb1 ? rb1->getCenterOfMassTransform().inverse()(rb0->getCenterOfMassTransform()(pivotInA)) : rb0->getCenterOfMassTransform() * pivotInA; btVector3 axisInA(axisX,axisY,axisZ); @@ -1060,7 +1690,7 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl pivotInA); } - m_dynamicsWorld->addConstraint(p2p); + m_dynamicsWorld->addConstraint(p2p,disableCollisionBetweenLinkedBodies); // m_constraints.push_back(p2p); p2p->setUserConstraintId(gConstraintUid++); @@ -1096,10 +1726,11 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl btTransform globalFrameA = rb0->getCenterOfMassTransform() * frameInA; frameInB = inv * globalFrameA; - + bool useReferenceFrameA = true; + genericConstraint = new btGeneric6DofConstraint( *rb0,*rb1, - frameInA,frameInB); + frameInA,frameInB,useReferenceFrameA); } else @@ -1120,15 +1751,16 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl ///frameInB in worldspace frameInB = rb0->getCenterOfMassTransform() * frameInA; + bool useReferenceFrameA = true; genericConstraint = new btGeneric6DofConstraint( *rb0,s_fixedObject2, - frameInA,frameInB); + frameInA,frameInB,useReferenceFrameA); } if (genericConstraint) { //m_constraints.push_back(genericConstraint); - m_dynamicsWorld->addConstraint(genericConstraint); + m_dynamicsWorld->addConstraint(genericConstraint,disableCollisionBetweenLinkedBodies); genericConstraint->setUserConstraintId(gConstraintUid++); genericConstraint->setUserConstraintType(type); //64 bit systems can't cast pointer to int. could use size_t instead. @@ -1194,7 +1826,7 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl if (coneTwistContraint) { //m_constraints.push_back(genericConstraint); - m_dynamicsWorld->addConstraint(coneTwistContraint); + m_dynamicsWorld->addConstraint(coneTwistContraint,disableCollisionBetweenLinkedBodies); coneTwistContraint->setUserConstraintId(gConstraintUid++); coneTwistContraint->setUserConstraintType(type); //64 bit systems can't cast pointer to int. could use size_t instead. @@ -1233,7 +1865,7 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl hinge->setAngularOnly(angularOnly); //m_constraints.push_back(hinge); - m_dynamicsWorld->addConstraint(hinge); + m_dynamicsWorld->addConstraint(hinge,disableCollisionBetweenLinkedBodies); hinge->setUserConstraintId(gConstraintUid++); hinge->setUserConstraintType(type); //64 bit systems can't cast pointer to int. could use size_t instead. @@ -1275,12 +1907,19 @@ int CcdPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl PHY_IPhysicsController* CcdPhysicsEnvironment::CreateConeController(float coneradius,float coneheight) { CcdConstructionInfo cinfo; + + // we don't need a CcdShapeConstructionInfo for this shape: + // it is simple enough for the standard copy constructor (see CcdPhysicsController::GetReplica) cinfo.m_collisionShape = new btConeShape(coneradius,coneheight); cinfo.m_MotionState = 0; cinfo.m_physicsEnv = this; cinfo.m_collisionFlags |= btCollisionObject::CF_NO_CONTACT_RESPONSE; DefaultMotionState* motionState = new DefaultMotionState(); cinfo.m_MotionState = motionState; + + // we will add later the possibility to select the filter from option + cinfo.m_collisionFilterMask = CcdConstructionInfo::AllFilter ^ CcdConstructionInfo::SensorFilter; + cinfo.m_collisionFilterGroup = CcdConstructionInfo::SensorFilter; motionState->m_worldTransform.setIdentity(); // motionState->m_worldTransform.setOrigin(btVector3(position[0],position[1],position[2])); diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h index 66a6ed59c17..74384dd8cf2 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h +++ b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h @@ -18,6 +18,7 @@ subject to the following restrictions: #include "PHY_IPhysicsEnvironment.h" #include <vector> +#include <set> class CcdPhysicsController; #include "LinearMath/btVector3.h" #include "LinearMath/btTransform.h" @@ -42,6 +43,7 @@ class btBroadphaseInterface; class btOverlappingPairCache; class btIDebugDraw; class PHY_IVehicle; +class CcdOverlapFilterCallBack; /// CcdPhysicsEnvironment is an experimental mainloop for physics simulation using optional continuous collision detection. /// Physics Environment takes care of stepping the simulation and is a container for physics entities. @@ -49,12 +51,15 @@ class PHY_IVehicle; /// A derived class may be able to 'construct' entities by loading and/or converting class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment { + friend class CcdOverlapFilterCallBack; btVector3 m_gravity; - - protected: btIDebugDraw* m_debugDrawer; + + class btDefaultCollisionConfiguration* m_collisionConfiguration; + class btBroadphaseInterface* m_broadphase; + //solver iterations int m_numIterations; @@ -69,6 +74,7 @@ protected: btContactSolverInfo m_solverInfo; + void processFhSprings(double curTime,float timeStep); public: CcdPhysicsEnvironment(btDispatcher* dispatcher=0, btOverlappingPairCache* pairCache=0); @@ -114,12 +120,14 @@ protected: virtual void setDebugMode(int debugMode); virtual void setGravity(float x,float y,float z); + virtual void getGravity(PHY__Vector3& grav); + virtual int createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type, float pivotX,float pivotY,float pivotZ, float axisX,float axisY,float axisZ, float axis1X=0,float axis1Y=0,float axis1Z=0, - float axis2X=0,float axis2Y=0,float axis2Z=0 + float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0 ); @@ -131,7 +139,7 @@ protected: const btVector3& linearMinLimits, const btVector3& linearMaxLimits, const btVector3& angularMinLimits, - const btVector3& angularMaxLimits + const btVector3& angularMaxLimits,int flags ); virtual void setConstraintParam(int constraintId,int param,float value,float value1); @@ -156,8 +164,7 @@ protected: btTypedConstraint* getConstraintById(int constraintId); - virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ); + virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ); //Methods for gamelogic collision/physics callbacks @@ -166,7 +173,7 @@ protected: virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user); virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl); virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl); - + //These two methods are used *solely* to create controllers for Near/Radar sensor! Don't use for anything else virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position); virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight); @@ -183,10 +190,15 @@ protected: void removeCcdPhysicsController(CcdPhysicsController* ctrl); + void updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask); + + void disableCcdPhysicsController(CcdPhysicsController* ctrl); + + void enableCcdPhysicsController(CcdPhysicsController* ctrl); + btBroadphaseInterface* getBroadphase(); - - + btDispatcher* getDispatcher(); bool IsSatCollisionDetectionEnabled() const @@ -200,18 +212,15 @@ protected: } - int GetNumControllers(); - - CcdPhysicsController* GetPhysicsController( int index); - - - const btPersistentManifold* GetManifold(int index) const; void SyncMotionStates(float timeStep); - + class btSoftRigidDynamicsWorld* getDynamicsWorld() + { + return m_dynamicsWorld; + } class btConstraintSolver* GetConstraintSolver(); @@ -220,19 +229,31 @@ protected: - std::vector<CcdPhysicsController*> m_controllers; + std::set<CcdPhysicsController*> m_controllers; - std::vector<CcdPhysicsController*> m_triggerControllers; + std::set<CcdPhysicsController*> m_triggerControllers; PHY_ResponseCallback m_triggerCallbacks[PHY_NUM_RESPONSE]; void* m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE]; std::vector<WrapperVehicle*> m_wrapperVehicles; - class btDynamicsWorld* m_dynamicsWorld; + //use explicit btSoftRigidDynamicsWorld/btDiscreteDynamicsWorld* so that we have access to + //btDiscreteDynamicsWorld::addRigidBody(body,filter,group) + //so that we can set the body collision filter/group at the time of creation + //and not afterwards (breaks the collision system for radar/near sensor) + //Ideally we would like to have access to this function from the btDynamicsWorld interface + //class btDynamicsWorld* m_dynamicsWorld; + class btSoftRigidDynamicsWorld* m_dynamicsWorld; class btConstraintSolver* m_solver; + class btOverlappingPairCache* m_ownPairCache; + + class CcdOverlapFilterCallBack* m_filterCallback; + + class btDispatcher* m_ownDispatcher; + bool m_scalingPropagated; diff --git a/source/gameengine/Physics/Bullet/Makefile b/source/gameengine/Physics/Bullet/Makefile index 49259d0a67c..d5570e75833 100644 --- a/source/gameengine/Physics/Bullet/Makefile +++ b/source/gameengine/Physics/Bullet/Makefile @@ -37,5 +37,10 @@ CCFLAGS += $(LEVEL_1_CPP_WARNINGS) CPPFLAGS += -I$(NAN_BULLET2)/include CPPFLAGS += -I$(NAN_GUARDEDALLOC)/include +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I$(NAN_MOTO)/include +CPPFLAGS += -I../../../kernel/gen_system CPPFLAGS += -I../../Physics/common CPPFLAGS += -I../../Physics/Dummy +CPPFLAGS += -I../../Rasterizer + diff --git a/source/gameengine/Physics/Bullet/SConscript b/source/gameengine/Physics/Bullet/SConscript index 04abca6e4a5..138ae493013 100644 --- a/source/gameengine/Physics/Bullet/SConscript +++ b/source/gameengine/Physics/Bullet/SConscript @@ -3,13 +3,13 @@ Import ('env') sources = 'CcdPhysicsEnvironment.cpp CcdPhysicsController.cpp' -incs = '. ../common' +incs = '. ../common #source/kernel/gen_system #intern/string #intern/moto/include #source/gameengine/Rasterizer' incs += ' ' + env['BF_BULLET_INC'] cflags = [] if env['OURPLATFORM']=='win32-vc': - cflags.append('/GR') - cflags.append('/O2') + cflags.append('/GR') + cflags.append('/O2') env.BlenderLib ( 'bf_bullet', Split(sources), Split(incs), [], libtype=['game','player'], priority=[15,80] ) diff --git a/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.cpp b/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.cpp index f512d44c9f2..ba196b5cf55 100644 --- a/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.cpp +++ b/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.cpp @@ -92,8 +92,7 @@ void DummyPhysicsEnvironment::setGravity(float x,float y,float z) int DummyPhysicsEnvironment::createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type, float pivotX,float pivotY,float pivotZ,float axisX,float axisY,float axisZ, float axis1X,float axis1Y,float axis1Z, - float axis2X,float axis2Y,float axis2Z - + float axis2X,float axis2Y,float axis2Z,int flag ) { @@ -109,8 +108,7 @@ void DummyPhysicsEnvironment::removeConstraint(int constraintid) } } -PHY_IPhysicsController* DummyPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ) +PHY_IPhysicsController* DummyPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallback &filterCallback,float fromX,float fromY,float fromZ, float toX,float toY,float toZ) { //collision detection / raytesting return NULL; diff --git a/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h b/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h index f0761618e4e..a92b1e7f4a6 100644 --- a/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h +++ b/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h @@ -58,7 +58,7 @@ public: float pivotX,float pivotY,float pivotZ, float axisX,float axisY,float axisZ, float axis1X=0,float axis1Y=0,float axis1Z=0, - float axis2X=0,float axis2Y=0,float axis2Z=0 + float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0 ); virtual void removeConstraint(int constraintid); @@ -69,8 +69,7 @@ public: return 0; } - virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ); + virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ); //gamelogic callbacks @@ -80,6 +79,7 @@ public: { } virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl) {} + virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl) {} virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) {return 0;} virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight) { return 0;} diff --git a/source/gameengine/Physics/Sumo/Fuzzics/include/SM_Callback.h b/source/gameengine/Physics/Sumo/Fuzzics/include/SM_Callback.h index a43ddbec483..42b5ab48ab6 100644 --- a/source/gameengine/Physics/Sumo/Fuzzics/include/SM_Callback.h +++ b/source/gameengine/Physics/Sumo/Fuzzics/include/SM_Callback.h @@ -4,6 +4,7 @@ class SM_Callback { public: virtual void do_me() = 0; + virtual ~SM_Callback() {} }; #endif diff --git a/source/gameengine/Physics/Sumo/Fuzzics/src/SM_Scene.cpp b/source/gameengine/Physics/Sumo/Fuzzics/src/SM_Scene.cpp index b5bf67b14ea..f0791bbf89f 100644 --- a/source/gameengine/Physics/Sumo/Fuzzics/src/SM_Scene.cpp +++ b/source/gameengine/Physics/Sumo/Fuzzics/src/SM_Scene.cpp @@ -98,12 +98,17 @@ void SM_Scene::addTouchCallback(int response_class, DT_ResponseCallback callback void SM_Scene::addSensor(SM_Object& object) { - object.calcXform(); - m_objectList.push_back(&object); - DT_AddObject(m_scene, object.getObjectHandle()); - DT_SetResponseClass(m_respTable, object.getObjectHandle(), m_ResponseClass[SENSOR_RESPONSE]); - DT_SetResponseClass(m_secondaryRespTable, object.getObjectHandle(), m_secondaryResponseClass[SENSOR_RESPONSE]); - DT_SetResponseClass(m_fixRespTable, object.getObjectHandle(), m_fixResponseClass[SENSOR_RESPONSE]); + T_ObjectList::iterator i = + std::find(m_objectList.begin(), m_objectList.end(), &object); + if (i == m_objectList.end()) + { + object.calcXform(); + m_objectList.push_back(&object); + DT_AddObject(m_scene, object.getObjectHandle()); + DT_SetResponseClass(m_respTable, object.getObjectHandle(), m_ResponseClass[SENSOR_RESPONSE]); + DT_SetResponseClass(m_secondaryRespTable, object.getObjectHandle(), m_secondaryResponseClass [SENSOR_RESPONSE]); + DT_SetResponseClass(m_fixRespTable, object.getObjectHandle(), m_fixResponseClass[SENSOR_RESPONSE]); + } } void SM_Scene::add(SM_Object& object) { diff --git a/source/gameengine/Physics/Sumo/SConscript b/source/gameengine/Physics/Sumo/SConscript index b5900919321..fdf6fdafec2 100644 --- a/source/gameengine/Physics/Sumo/SConscript +++ b/source/gameengine/Physics/Sumo/SConscript @@ -1,22 +1,21 @@ #!/usr/bin/python Import ('env') -sumoenv = env.Clone() sources = ['SumoPHYCallbackBridge.cpp', - 'SumoPhysicsController.cpp', - 'SumoPhysicsEnvironment.cpp', - 'Fuzzics/src/SM_FhObject.cpp', - 'Fuzzics/src/SM_Object.cpp', - 'Fuzzics/src/SM_Scene.cpp', - 'Fuzzics/src/SM_MotionState.cpp' - ] + 'SumoPhysicsController.cpp', + 'SumoPhysicsEnvironment.cpp', + 'Fuzzics/src/SM_FhObject.cpp', + 'Fuzzics/src/SM_Object.cpp', + 'Fuzzics/src/SM_Scene.cpp', + 'Fuzzics/src/SM_MotionState.cpp' + ] incs =['.', '../common', 'Fuzzics/include', '#/intern/moto/include' ] -incs += [sumoenv['BF_SOLID_INC']] +incs += [env['BF_SOLID_INC']] cflags = [] if env['OURPLATFORM']=='win32-vc': diff --git a/source/gameengine/Physics/Sumo/SumoPhysicsController.h b/source/gameengine/Physics/Sumo/SumoPhysicsController.h index 95f01b45f2b..d8ee54935d7 100644 --- a/source/gameengine/Physics/Sumo/SumoPhysicsController.h +++ b/source/gameengine/Physics/Sumo/SumoPhysicsController.h @@ -120,6 +120,7 @@ public: virtual void SetMargin(float margin) ; virtual float GetMargin() const; virtual float GetRadius() const ; + virtual void SetRadius(float margin) { SetMargin(margin); } // clientinfo for raycasts for example diff --git a/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp b/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp index f324b979fb9..3be5e027345 100644 --- a/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp +++ b/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp @@ -26,6 +26,7 @@ * * ***** END GPL LICENSE BLOCK ***** */ +#include <string.h> // memset #include "SumoPhysicsEnvironment.h" #include "PHY_IMotionState.h" #include "SumoPhysicsController.h" @@ -110,8 +111,8 @@ int SumoPhysicsEnvironment::createConstraint( float pivotX,float pivotY,float pivotZ, float axisX,float axisY,float axisZ, float axis1X,float axis1Y,float axis1Z, - float axis2X,float axis2Y,float axis2Z - + float axis2X,float axis2Y,float axis2Z, + int flag ) { int constraintid = 0; @@ -125,37 +126,35 @@ void SumoPhysicsEnvironment::removeConstraint(int constraintid) } } -PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClientCtrl, +PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, - float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ, - float& normalX,float& normalY,float& normalZ) + float toX,float toY,float toZ) { - SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (ignoreClientCtrl); + SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (filterCallback.m_ignoreController); //collision detection / raytesting MT_Point3 hit, normal; - PHY_IPhysicsController *ret = 0; + PHY_RayCastResult result; SM_Object* sm_ignore = 0; if (ignoreCtr) sm_ignore = ignoreCtr->GetSumoObject(); + memset(&result, 0, sizeof(result)); SM_Object* smOb = m_sumoScene->rayTest(sm_ignore,MT_Point3(fromX, fromY, fromZ),MT_Point3(toX, toY, toZ), hit, normal); if (smOb) { - ret = (PHY_IPhysicsController *) smOb->getPhysicsClientObject(); + result.m_controller = (PHY_IPhysicsController *) smOb->getPhysicsClientObject(); + result.m_hitPoint[0] = hit[0]; + result.m_hitPoint[1] = hit[1]; + result.m_hitPoint[2] = hit[2]; + result.m_hitNormal[0] = normal[0]; + result.m_hitNormal[1] = normal[1]; + result.m_hitNormal[2] = normal[2]; + filterCallback.reportHit(&result); } - hitX = hit[0]; - hitY = hit[1]; - hitZ = hit[2]; - - normalX = normal[0]; - normalY = normal[1]; - normalZ = normal[2]; - - return ret; + return result.m_controller; } //gamelogic callbacks void SumoPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl) @@ -203,6 +202,8 @@ void SumoPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCa case PHY_STATIC_RESPONSE: sumoRespClass = PHY_STATIC_RESPONSE; break; + case PHY_BROADPH_RESPONSE: + return; default: assert(0); return; @@ -226,6 +227,12 @@ void SumoPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ct m_sumoScene->requestCollisionCallback(*smObject); } } + +void SumoPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl) +{ + // intentionally empty +} + PHY_IPhysicsController* SumoPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position) { DT_ShapeHandle shape = DT_NewSphere(0.0); diff --git a/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h b/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h index ce5cd70e8cc..65b07a7a0be 100644 --- a/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h +++ b/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h @@ -63,7 +63,7 @@ public: float pivotX,float pivotY,float pivotZ, float axisX,float axisY,float axisZ, float axis1X=0,float axis1Y=0,float axis1Z=0, - float axis2X=0,float axis2Y=0,float axis2Z=0 + float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0 ); @@ -75,8 +75,7 @@ public: return 0; } - virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ); + virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback,float fromX,float fromY,float fromZ, float toX,float toY,float toZ); //gamelogic callbacks @@ -84,6 +83,7 @@ public: virtual void removeSensor(PHY_IPhysicsController* ctrl); virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user); virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl); + virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl); virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position); virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight); diff --git a/source/gameengine/Physics/common/PHY_DynamicTypes.h b/source/gameengine/Physics/common/PHY_DynamicTypes.h index 68b60192fbc..3b3e42c38d2 100644 --- a/source/gameengine/Physics/common/PHY_DynamicTypes.h +++ b/source/gameengine/Physics/common/PHY_DynamicTypes.h @@ -19,7 +19,6 @@ subject to the following restrictions: -class PHY_ResponseTable; class PHY_Shape; @@ -40,10 +39,11 @@ struct PHY__Vector3 typedef enum { PHY_FH_RESPONSE, - PHY_SENSOR_RESPONSE, /* Touch Sensors */ + PHY_SENSOR_RESPONSE, /* Touch Sensors */ PHY_CAMERA_RESPONSE, /* Visibility Culling */ PHY_OBJECT_RESPONSE, /* Object Dynamic Geometry Response */ PHY_STATIC_RESPONSE, /* Static Geometry Response */ + PHY_BROADPH_RESPONSE, /* broadphase Response */ PHY_NUM_RESPONSE }; @@ -87,6 +87,18 @@ typedef enum PHY_ConstraintType { } PHY_ConstraintType; +typedef enum PHY_ShapeType { + PHY_SHAPE_NONE, + PHY_SHAPE_BOX, + PHY_SHAPE_SPHERE, + PHY_SHAPE_CYLINDER, + PHY_SHAPE_CONE, + PHY_SHAPE_MESH, + PHY_SHAPE_POLYTOPE, + PHY_SHAPE_COMPOUND +} PHY_ShapeType; + + typedef float PHY_Vector3[3]; #endif //__PHY_DYNAMIC_TYPES diff --git a/source/gameengine/Physics/common/PHY_IPhysicsController.h b/source/gameengine/Physics/common/PHY_IPhysicsController.h index 2f1d0b34eb2..884e14cfb5a 100644 --- a/source/gameengine/Physics/common/PHY_IPhysicsController.h +++ b/source/gameengine/Physics/common/PHY_IPhysicsController.h @@ -82,7 +82,6 @@ class PHY_IPhysicsController // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted virtual void setRigidBody(bool rigid)=0; - // clientinfo for raycasts for example virtual void* getNewClientInfo()=0; virtual void setNewClientInfo(void* clientinfo)=0; @@ -91,7 +90,8 @@ class PHY_IPhysicsController virtual void calcXform() =0; virtual void SetMargin(float margin) =0; virtual float GetMargin() const=0; - virtual float GetRadius() const { return 0.f;} + virtual float GetRadius() const=0; + virtual void SetRadius(float margin) = 0; PHY__Vector3 GetWorldPosition(PHY__Vector3& localpos); }; diff --git a/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h b/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h index 7f01d16a796..0e9c571924c 100644 --- a/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h +++ b/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h @@ -33,6 +33,50 @@ #include <vector> #include "PHY_DynamicTypes.h" class PHY_IVehicle; +class RAS_MeshObject; +class PHY_IPhysicsController; + +/** + * pass back information from rayTest + */ +struct PHY_RayCastResult +{ + PHY_IPhysicsController* m_controller; + PHY__Vector3 m_hitPoint; + PHY__Vector3 m_hitNormal; + const RAS_MeshObject* m_meshObject; // !=NULL for mesh object (only for Bullet controllers) + int m_polygon; // index of the polygon hit by the ray, + // only if m_meshObject != NULL +}; + +/** + * This class replaces the ignoreController parameter of rayTest function. + * It allows more sophisticated filtering on the physics controller before computing the ray intersection to save CPU. + * It is only used to its full extend by the Ccd physics environement (Bullet). + */ +class PHY_IRayCastFilterCallback +{ +public: + PHY_IPhysicsController* m_ignoreController; + bool m_faceNormal; + + virtual ~PHY_IRayCastFilterCallback() + { + } + + virtual bool needBroadphaseRayCast(PHY_IPhysicsController* controller) + { + return true; + } + + virtual void reportHit(PHY_RayCastResult* result) = 0; + + PHY_IRayCastFilterCallback(PHY_IPhysicsController* ignoreController, bool faceNormal=false) + :m_ignoreController(ignoreController), + m_faceNormal(faceNormal) + { + } +}; /** * Physics Environment takes care of stepping the simulation and is a container for physics entities (rigidbodies,constraints, materials etc.) @@ -85,7 +129,7 @@ class PHY_IPhysicsEnvironment float pivotX,float pivotY,float pivotZ, float axis0X,float axis0Y,float axis0Z, float axis1X=0,float axis1Y=0,float axis1Z=0, - float axis2X=0,float axis2Y=0,float axis2Z=0 + float axis2X=0,float axis2Y=0,float axis2Z=0,int flag=0 )=0; virtual void removeConstraint(int constraintid)=0; virtual float getAppliedImpulse(int constraintid){ return 0.f;} @@ -94,8 +138,7 @@ class PHY_IPhysicsEnvironment //complex constraint for vehicles virtual PHY_IVehicle* getVehicleConstraint(int constraintId) =0; - virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, - float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ)=0; + virtual PHY_IPhysicsController* rayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ)=0; //Methods for gamelogic collision/physics callbacks @@ -104,6 +147,8 @@ class PHY_IPhysicsEnvironment virtual void removeSensor(PHY_IPhysicsController* ctrl)=0; virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)=0; virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl)=0; + virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl)=0; + //These two methods are *solely* used to create controllers for sensor! Don't use for anything else virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) =0; virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight)=0; |