/// Test if point p and d lie on opposite sides of plane through abc int b3VoronoiSimplexSolver::pointOutsideOfPlane(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, const b3Vector3& d) { b3Vector3 normal = (b-a).cross(c-a); b3Scalar signp = (p - a).dot(normal); // [AP AB AC] b3Scalar signd = (d - a).dot( normal); // [AD AB AC] #ifdef B3_CATCH_DEGENERATE_TETRAHEDRON #ifdef BT_USE_DOUBLE_PRECISION if (signd * signd < (b3Scalar(1e-8) * b3Scalar(1e-8))) { return -1; } #else if (signd * signd < (b3Scalar(1e-4) * b3Scalar(1e-4))) { // printf("affine dependent/degenerate\n");// return -1; } #endif #endif // Points on opposite sides if expression signs are opposite return signp * signd < b3Scalar(0.); }
int b3TranslationalLimitMotor::testLimitValue(int limitIndex, b3Scalar test_value) { b3Scalar loLimit = m_lowerLimit[limitIndex]; b3Scalar hiLimit = m_upperLimit[limitIndex]; if(loLimit > hiLimit) { m_currentLimit[limitIndex] = 0;//Free from violation m_currentLimitError[limitIndex] = b3Scalar(0.f); return 0; } if (test_value < loLimit) { m_currentLimit[limitIndex] = 2;//low limit violation m_currentLimitError[limitIndex] = test_value - loLimit; return 2; } else if (test_value> hiLimit) { m_currentLimit[limitIndex] = 1;//High limit violation m_currentLimitError[limitIndex] = test_value - hiLimit; return 1; }; m_currentLimit[limitIndex] = 0;//Free from violation m_currentLimitError[limitIndex] = b3Scalar(0.f); return 0; }
bool matrixToEulerXYZ(const b3Matrix3x3& mat,b3Vector3& xyz) { // // rot = cy*cz -cy*sz sy // // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx // // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy // b3Scalar fi = btGetMatrixElem(mat,2); if (fi < b3Scalar(1.0f)) { if (fi > b3Scalar(-1.0f)) { xyz[0] = b3Atan2(-btGetMatrixElem(mat,5),btGetMatrixElem(mat,8)); xyz[1] = b3Asin(btGetMatrixElem(mat,2)); xyz[2] = b3Atan2(-btGetMatrixElem(mat,1),btGetMatrixElem(mat,0)); return true; } else { // WARNING. Not unique. XA - ZA = -atan2(r10,r11) xyz[0] = -b3Atan2(btGetMatrixElem(mat,3),btGetMatrixElem(mat,4)); xyz[1] = -B3_HALF_PI; xyz[2] = b3Scalar(0.0); return false; } } else { // WARNING. Not unique. XAngle + ZAngle = atan2(r10,r11) xyz[0] = b3Atan2(btGetMatrixElem(mat,3),btGetMatrixElem(mat,4)); xyz[1] = B3_HALF_PI; xyz[2] = 0.0; } return false; }
void resolveSingleConstraintRowGeneric2(b3GpuSolverBody* body1, b3GpuSolverBody* body2, b3GpuSolverConstraint* c) { float deltaImpulse = c->m_rhs - b3Scalar(c->m_appliedImpulse) * c->m_cfm; float deltaVel1Dotn = b3Dot(c->m_contactNormal, body1->m_deltaLinearVelocity) + b3Dot(c->m_relpos1CrossNormal, body1->m_deltaAngularVelocity); float deltaVel2Dotn = -b3Dot(c->m_contactNormal, body2->m_deltaLinearVelocity) + b3Dot(c->m_relpos2CrossNormal, body2->m_deltaAngularVelocity); deltaImpulse -= deltaVel1Dotn * c->m_jacDiagABInv; deltaImpulse -= deltaVel2Dotn * c->m_jacDiagABInv; float sum = b3Scalar(c->m_appliedImpulse) + deltaImpulse; if (sum < c->m_lowerLimit) { deltaImpulse = c->m_lowerLimit - b3Scalar(c->m_appliedImpulse); c->m_appliedImpulse = c->m_lowerLimit; } else if (sum > c->m_upperLimit) { deltaImpulse = c->m_upperLimit - b3Scalar(c->m_appliedImpulse); c->m_appliedImpulse = c->m_upperLimit; } else { c->m_appliedImpulse = sum; } internalApplyImpulse(body1, c->m_contactNormal * body1->m_invMass, c->m_angularComponentA, deltaImpulse); internalApplyImpulse(body2, -c->m_contactNormal * body2->m_invMass, c->m_angularComponentB, deltaImpulse); }
//clear the simplex, remove all the vertices void b3VoronoiSimplexSolver::reset() { m_cachedValidClosest = false; m_numVertices = 0; m_needsUpdate = true; m_lastW = b3MakeVector3(b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT)); m_cachedBC.reset(); }
void SimpleCamera::update() { int forwardAxis(-1); switch (m_data->m_cameraUpAxis) { case 1: forwardAxis = 2; m_data->m_cameraUp = b3MakeVector3(0,1,0); //gLightPos = b3MakeVector3(-50.f,100,30); break; case 2: forwardAxis = 1; m_data->m_cameraUp = b3MakeVector3(0,0,1); //gLightPos = b3MakeVector3(-50.f,30,100); break; default: { b3Assert(0); return; } }; b3Vector3 eyePos = b3MakeVector3(0,0,0); eyePos[forwardAxis] = -m_data->m_cameraDistance; m_data->m_cameraForward = b3MakeVector3(eyePos[0],eyePos[1],eyePos[2]); if (m_data->m_cameraForward.length2() < B3_EPSILON) { m_data->m_cameraForward.setValue(1.f,0.f,0.f); } else { m_data->m_cameraForward.normalize(); } // m_azi=m_azi+0.01; b3Scalar rele = m_data->m_yaw * b3Scalar(0.01745329251994329547);// rads per deg b3Scalar razi = m_data->m_pitch * b3Scalar(0.01745329251994329547);// rads per deg b3Quaternion rot(m_data->m_cameraUp,razi); b3Vector3 right = m_data->m_cameraUp.cross(m_data->m_cameraForward); b3Quaternion roll(right,-rele); eyePos = b3Matrix3x3(rot) * b3Matrix3x3(roll) * eyePos; m_data->m_cameraPosition = eyePos; m_data->m_cameraPosition+= m_data->m_cameraTargetPosition; }
bool b3GeometryUtil::areVerticesBehindPlane(const b3Vector3& planeNormal, const b3AlignedObjectArray<b3Vector3>& vertices, b3Scalar margin) { int numvertices = vertices.size(); for (int i=0;i<numvertices;i++) { const b3Vector3& N1 = vertices[i]; b3Scalar dist = b3Scalar(planeNormal.dot(N1))+b3Scalar(planeNormal[3])-margin; if (dist>b3Scalar(0.)) { return false; } } return true; }
void GLInstancingRenderer::getMouseDirection(float* dir, int x, int y) { float top = 1.f; float bottom = -1.f; float nearPlane = 1.f; float tanFov = (top-bottom)*0.5f / nearPlane; float fov = b3Scalar(2.0) * b3Atan(tanFov); b3Vector3 rayFrom = m_data->m_cameraPosition; b3Vector3 rayForward = (m_data->m_cameraTargetPosition-m_data->m_cameraPosition); rayForward.normalize(); float farPlane = 10000.f; rayForward*= farPlane; b3Vector3 rightOffset; b3Vector3 vertical = m_data->m_cameraUp; b3Vector3 hor; hor = rayForward.cross(vertical); hor.normalize(); vertical = hor.cross(rayForward); vertical.normalize(); float tanfov = tanf(0.5f*fov); hor *= 2.f * farPlane * tanfov; vertical *= 2.f * farPlane * tanfov; b3Scalar aspect; aspect = m_screenWidth / (b3Scalar)m_screenHeight; hor*=aspect; b3Vector3 rayToCenter = rayFrom + rayForward; b3Vector3 dHor = hor * 1.f/float(m_screenWidth); b3Vector3 dVert = vertical * 1.f/float(m_screenHeight); b3Vector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; rayTo += b3Scalar(x) * dHor; rayTo -= b3Scalar(y) * dVert; dir[0] = rayTo[0]; dir[1] = rayTo[1]; dir[2] = rayTo[2]; }
bool b3GeometryUtil::isPointInsidePlanes(const b3AlignedObjectArray<b3Vector3>& planeEquations, const b3Vector3& point, b3Scalar margin) { int numbrushes = planeEquations.size(); for (int i=0;i<numbrushes;i++) { const b3Vector3& N1 = planeEquations[i]; b3Scalar dist = b3Scalar(N1.dot(point))+b3Scalar(N1[3])-margin; if (dist>b3Scalar(0.)) { return false; } } return true; }
void SampleThreadFunc(void* userPtr,void* lsMemory) { printf("thread started\n"); SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory; SampleArgs* args = (SampleArgs*) userPtr; int workLeft = true; while (workLeft) { args->m_cs->lock(); int count = args->m_cs->getSharedParam(0); args->m_cs->setSharedParam(0,count-1); args->m_cs->unlock(); if (count>0) { printf("thread %d processed number %d\n",localStorage->threadId, count); } //do some fake work for (int i=0;i<1000000;i++) args->m_fakeWork = b3Scalar(1.21)*args->m_fakeWork; workLeft = count>0; } printf("finished\n"); //do nothing }
void b3GeometryUtil::getPlaneEquationsFromVertices(b3AlignedObjectArray<b3Vector3>& vertices, b3AlignedObjectArray<b3Vector3>& planeEquationsOut ) { const int numvertices = vertices.size(); // brute force: for (int i=0;i<numvertices;i++) { const b3Vector3& N1 = vertices[i]; for (int j=i+1;j<numvertices;j++) { const b3Vector3& N2 = vertices[j]; for (int k=j+1;k<numvertices;k++) { const b3Vector3& N3 = vertices[k]; b3Vector3 planeEquation,edge0,edge1; edge0 = N2-N1; edge1 = N3-N1; b3Scalar normalSign = b3Scalar(1.); for (int ww=0;ww<2;ww++) { planeEquation = normalSign * edge0.cross(edge1); if (planeEquation.length2() > b3Scalar(0.0001)) { planeEquation.normalize(); if (notExist(planeEquation,planeEquationsOut)) { planeEquation[3] = -planeEquation.dot(N1); //check if inside, and replace supportingVertexOut if needed if (areVerticesBehindPlane(planeEquation,vertices,b3Scalar(0.01))) { planeEquationsOut.push_back(planeEquation); } } } normalSign = b3Scalar(-1.); } } } } }
btVector3 getRayTo(int x,int y) { if (!m_glApp->m_instancingRenderer) { btAssert(0); return btVector3(0,0,0); } float top = 1.f; float bottom = -1.f; float nearPlane = 1.f; float tanFov = (top-bottom)*0.5f / nearPlane; float fov = b3Scalar(2.0) * b3Atan(tanFov); btVector3 camPos,camTarget; m_glApp->m_instancingRenderer->getCameraPosition(camPos); m_glApp->m_instancingRenderer->getCameraTargetPosition(camTarget); btVector3 rayFrom = camPos; btVector3 rayForward = (camTarget-camPos); rayForward.normalize(); float farPlane = 10000.f; rayForward*= farPlane; btVector3 rightOffset; btVector3 m_cameraUp=btVector3(0,1,0); btVector3 vertical = m_cameraUp; btVector3 hor; hor = rayForward.cross(vertical); hor.normalize(); vertical = hor.cross(rayForward); vertical.normalize(); float tanfov = tanf(0.5f*fov); hor *= 2.f * farPlane * tanfov; vertical *= 2.f * farPlane * tanfov; b3Scalar aspect; float width = m_glApp->m_instancingRenderer->getScreenWidth(); float height = m_glApp->m_instancingRenderer->getScreenHeight(); aspect = width / height; hor*=aspect; btVector3 rayToCenter = rayFrom + rayForward; btVector3 dHor = hor * 1.f/width; btVector3 dVert = vertical * 1.f/height; btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical; rayTo += btScalar(x) * dHor; rayTo -= btScalar(y) * dVert; return rayTo; }
virtual void executeJob(int threadIndex) { printf("start SampleJob1 %d using threadIndex %d\n",m_jobId,threadIndex); //do some fake work for (int i=0;i<1000000;i++) m_fakeWork = b3Scalar(1.21)*m_fakeWork; printf("finished SampleJob1 %d using threadIndex %d\n",m_jobId,threadIndex); }
void b3Point2PointConstraint::buildJacobian() { ///we need it for both methods { m_appliedImpulse = b3Scalar(0.); } }
int b3Generic6DofConstraint::setLinearLimits(b3ConstraintInfo2* info, int row, const b3Transform& transA,const b3Transform& transB,const b3Vector3& linVelA,const b3Vector3& linVelB,const b3Vector3& angVelA,const b3Vector3& angVelB) { // int row = 0; //solve linear limits b3RotationalLimitMotor limot; for (int i=0;i<3 ;i++ ) { if(m_linearLimits.needApplyForce(i)) { // re-use rotational motor code limot.m_bounce = b3Scalar(0.f); limot.m_currentLimit = m_linearLimits.m_currentLimit[i]; limot.m_currentPosition = m_linearLimits.m_currentLinearDiff[i]; limot.m_currentLimitError = m_linearLimits.m_currentLimitError[i]; limot.m_damping = m_linearLimits.m_damping; limot.m_enableMotor = m_linearLimits.m_enableMotor[i]; limot.m_hiLimit = m_linearLimits.m_upperLimit[i]; limot.m_limitSoftness = m_linearLimits.m_limitSoftness; limot.m_loLimit = m_linearLimits.m_lowerLimit[i]; limot.m_maxLimitForce = b3Scalar(0.f); limot.m_maxMotorForce = m_linearLimits.m_maxMotorForce[i]; limot.m_targetVelocity = m_linearLimits.m_targetVelocity[i]; b3Vector3 axis = m_calculatedTransformA.getBasis().getColumn(i); int flags = m_flags >> (i * B3_6DOF_FLAGS_AXIS_SHIFT); limot.m_normalCFM = (flags & B3_6DOF_FLAGS_CFM_NORM) ? m_linearLimits.m_normalCFM[i] : info->cfm[0]; limot.m_stopCFM = (flags & B3_6DOF_FLAGS_CFM_STOP) ? m_linearLimits.m_stopCFM[i] : info->cfm[0]; limot.m_stopERP = (flags & B3_6DOF_FLAGS_ERP_STOP) ? m_linearLimits.m_stopERP[i] : info->erp; if(m_useOffsetForConstraintFrame) { int indx1 = (i + 1) % 3; int indx2 = (i + 2) % 3; int rotAllowed = 1; // rotations around orthos to current axis if(m_angularLimits[indx1].m_currentLimit && m_angularLimits[indx2].m_currentLimit) { rotAllowed = 0; } row += get_limit_motor_info2(&limot, transA,transB,linVelA,linVelB,angVelA,angVelB, info, row, axis, 0, rotAllowed); } else { row += get_limit_motor_info2(&limot, transA,transB,linVelA,linVelB,angVelA,angVelB, info, row, axis, 0); } } }
b3Scalar b3VoronoiSimplexSolver::maxVertex() { int i, numverts = numVertices(); b3Scalar maxV = b3Scalar(0.); for (i=0;i<numverts;i++) { b3Scalar curLen2 = m_simplexVectorW[i].length2(); if (maxV < curLen2) maxV = curLen2; } return maxV; }
bool notExist(const b3Vector3& planeEquation,const b3AlignedObjectArray<b3Vector3>& planeEquations) { int numbrushes = planeEquations.size(); for (int i=0;i<numbrushes;i++) { const b3Vector3& N1 = planeEquations[i]; if (planeEquation.dot(N1) > b3Scalar(0.999)) { return false; } } return true; }
void b3Generic6DofConstraint::calculateTransforms(const b3Transform& transA,const b3Transform& transB,const b3RigidBodyData* bodies) { m_calculatedTransformA = transA * m_frameInA; m_calculatedTransformB = transB * m_frameInB; calculateLinearInfo(); calculateAngleInfo(); if(m_useOffsetForConstraintFrame) { // get weight factors depending on masses b3Scalar miA = bodies[m_rbA].m_invMass; b3Scalar miB = bodies[m_rbB].m_invMass; m_hasStaticBody = (miA < B3_EPSILON) || (miB < B3_EPSILON); b3Scalar miS = miA + miB; if(miS > b3Scalar(0.f)) { m_factA = miB / miS; } else { m_factA = b3Scalar(0.5f); } m_factB = b3Scalar(1.0f) - m_factA; } }
void integrateVelocity(double deltaTime) { LWPose newPose; newPose.m_position = m_worldPose.m_position + m_linearVelocity*deltaTime; if (m_flags & LWFLAG_USE_QUATERNION_DERIVATIVE) { newPose.m_orientation = m_worldPose.m_orientation; newPose.m_orientation += (m_angularVelocity * newPose.m_orientation) * (deltaTime * btScalar(0.5)); newPose.m_orientation.normalize(); m_worldPose = newPose; } else { //Exponential map //google for "Practical Parameterization of Rotations Using the Exponential Map", F. Sebastian Grassia //btQuaternion q_w = [ sin(|w|*dt/2) * w/|w| , cos(|w|*dt/2)] //btQuaternion q_new = q_w * q_old; b3Vector3 axis; b3Scalar fAngle = m_angularVelocity.length(); //limit the angular motion const btScalar angularMotionThreshold = btScalar(0.5)*SIMD_HALF_PI; if (fAngle*deltaTime > angularMotionThreshold) { fAngle = angularMotionThreshold / deltaTime; } if ( fAngle < btScalar(0.001) ) { // use Taylor's expansions of sync function axis = m_angularVelocity*( btScalar(0.5)*deltaTime-(deltaTime*deltaTime*deltaTime)*(btScalar(0.020833333333))*fAngle*fAngle ); } else { // sync(fAngle) = sin(c*fAngle)/t axis = m_angularVelocity*( btSin(btScalar(0.5)*fAngle*deltaTime)/fAngle ); } b3Quaternion dorn (axis.x,axis.y,axis.z,btCos( fAngle*deltaTime*b3Scalar(0.5) )); b3Quaternion orn0 = m_worldPose.m_orientation; b3Quaternion predictedOrn = dorn * orn0; predictedOrn.normalize(); m_worldPose.m_orientation = predictedOrn; } }
void b3GeometryUtil::getVerticesFromPlaneEquations(const b3AlignedObjectArray<b3Vector3>& planeEquations , b3AlignedObjectArray<b3Vector3>& verticesOut ) { const int numbrushes = planeEquations.size(); // brute force: for (int i=0;i<numbrushes;i++) { const b3Vector3& N1 = planeEquations[i]; for (int j=i+1;j<numbrushes;j++) { const b3Vector3& N2 = planeEquations[j]; for (int k=j+1;k<numbrushes;k++) { const b3Vector3& N3 = planeEquations[k]; b3Vector3 n2n3; n2n3 = N2.cross(N3); b3Vector3 n3n1; n3n1 = N3.cross(N1); b3Vector3 n1n2; n1n2 = N1.cross(N2); if ( ( n2n3.length2() > b3Scalar(0.0001) ) && ( n3n1.length2() > b3Scalar(0.0001) ) && ( n1n2.length2() > b3Scalar(0.0001) ) ) { //point P out of 3 plane equations: // d1 ( N2 * N3 ) + d2 ( N3 * N1 ) + d3 ( N1 * N2 ) //P = ------------------------------------------------------------------------- // N1 . ( N2 * N3 ) b3Scalar quotient = (N1.dot(n2n3)); if (b3Fabs(quotient) > b3Scalar(0.000001)) { quotient = b3Scalar(-1.) / quotient; n2n3 *= N1[3]; n3n1 *= N2[3]; n1n2 *= N3[3]; b3Vector3 potentialVertex = n2n3; potentialVertex += n3n1; potentialVertex += n1n2; potentialVertex *= quotient; //check if inside, and replace supportingVertexOut if needed if (isPointInsidePlanes(planeEquations,potentialVertex,b3Scalar(0.01))) { verticesOut.push_back(potentialVertex); } } } } } } }
void b3StridingMeshInterface::calculateAabbBruteForce(b3Vector3& aabbMin,b3Vector3& aabbMax) { struct AabbCalculationCallback : public b3InternalTriangleIndexCallback { b3Vector3 m_aabbMin; b3Vector3 m_aabbMax; AabbCalculationCallback() { m_aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT)); m_aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT)); } virtual void internalProcessTriangleIndex(b3Vector3* triangle,int partId,int triangleIndex) { (void)partId; (void)triangleIndex; m_aabbMin.setMin(triangle[0]); m_aabbMax.setMax(triangle[0]); m_aabbMin.setMin(triangle[1]); m_aabbMax.setMax(triangle[1]); m_aabbMin.setMin(triangle[2]); m_aabbMax.setMax(triangle[2]); } }; //first calculate the total aabb for all triangles AabbCalculationCallback aabbCallback; aabbMin.setValue(b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT)); aabbMax.setValue(b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT)); InternalProcessAllTriangles(&aabbCallback,aabbMin,aabbMax); aabbMin = aabbCallback.m_aabbMin; aabbMax = aabbCallback.m_aabbMax; }
b3Scalar b3GpuPgsConstraintSolver::solveGroupCacheFriendlySetup(b3OpenCLArray<b3RigidBodyData>* gpuBodies, b3OpenCLArray<b3InertiaData>* gpuInertias, int numBodies, b3OpenCLArray<b3GpuGenericConstraint>* gpuConstraints, int numConstraints, const b3ContactSolverInfo& infoGlobal) { B3_PROFILE("GPU solveGroupCacheFriendlySetup"); batchConstraints.resize(numConstraints); m_gpuData->m_gpuBatchConstraints->resize(numConstraints); m_staticIdx = -1; m_maxOverrideNumSolverIterations = 0; /* m_gpuData->m_gpuBodies->resize(numBodies); m_gpuData->m_gpuBodies->copyFromHostPointer(bodies,numBodies); b3OpenCLArray<b3InertiaData> gpuInertias(m_gpuData->m_context,m_gpuData->m_queue); gpuInertias.resize(numBodies); gpuInertias.copyFromHostPointer(inertias,numBodies); */ m_gpuData->m_gpuSolverBodies->resize(numBodies); m_tmpSolverBodyPool.resize(numBodies); { if (useGpuInitSolverBodies) { B3_PROFILE("m_initSolverBodiesKernel"); b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_initSolverBodiesKernel, "m_initSolverBodiesKernel"); launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL()); launcher.setBuffer(gpuBodies->getBufferCL()); launcher.setConst(numBodies); launcher.launch1D(numBodies); clFinish(m_gpuData->m_queue); // m_gpuData->m_gpuSolverBodies->copyToHost(m_tmpSolverBodyPool); } else { gpuBodies->copyToHost(m_gpuData->m_cpuBodies); for (int i = 0; i < numBodies; i++) { b3RigidBodyData& body = m_gpuData->m_cpuBodies[i]; b3GpuSolverBody& solverBody = m_tmpSolverBodyPool[i]; initSolverBody(i, &solverBody, &body); solverBody.m_originalBodyIndex = i; } m_gpuData->m_gpuSolverBodies->copyFromHost(m_tmpSolverBodyPool); } } // int totalBodies = 0; int totalNumRows = 0; //b3RigidBody* rb0=0,*rb1=0; //if (1) { { // int i; m_tmpConstraintSizesPool.resizeNoInitialize(numConstraints); // b3OpenCLArray<b3GpuGenericConstraint> gpuConstraints(m_gpuData->m_context,m_gpuData->m_queue); if (useGpuInfo1) { B3_PROFILE("info1 and init batchConstraint"); m_gpuData->m_gpuConstraintInfo1->resize(numConstraints); if (1) { B3_PROFILE("getInfo1Kernel"); b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_getInfo1Kernel, "m_getInfo1Kernel"); launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL()); launcher.setBuffer(gpuConstraints->getBufferCL()); launcher.setConst(numConstraints); launcher.launch1D(numConstraints); clFinish(m_gpuData->m_queue); } if (m_gpuData->m_batchSizes.size() == 0) { B3_PROFILE("initBatchConstraintsKernel"); m_gpuData->m_gpuConstraintRowOffsets->resize(numConstraints); unsigned int total = 0; m_gpuData->m_prefixScan->execute(*m_gpuData->m_gpuConstraintInfo1, *m_gpuData->m_gpuConstraintRowOffsets, numConstraints, &total); unsigned int lastElem = m_gpuData->m_gpuConstraintInfo1->at(numConstraints - 1); totalNumRows = total + lastElem; { B3_PROFILE("init batch constraints"); b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_initBatchConstraintsKernel, "m_initBatchConstraintsKernel"); launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuBatchConstraints->getBufferCL()); launcher.setBuffer(gpuConstraints->getBufferCL()); launcher.setBuffer(gpuBodies->getBufferCL()); launcher.setConst(numConstraints); launcher.launch1D(numConstraints); clFinish(m_gpuData->m_queue); } //assume the batching happens on CPU, so copy the data m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints); } } else { totalNumRows = 0; gpuConstraints->copyToHost(m_gpuData->m_cpuConstraints); //calculate the total number of contraint rows for (int i = 0; i < numConstraints; i++) { unsigned int& info1 = m_tmpConstraintSizesPool[i]; // unsigned int info1; if (m_gpuData->m_cpuConstraints[i].isEnabled()) { m_gpuData->m_cpuConstraints[i].getInfo1(&info1, &m_gpuData->m_cpuBodies[0]); } else { info1 = 0; } totalNumRows += info1; } m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints); m_gpuData->m_gpuConstraintInfo1->copyFromHost(m_tmpConstraintSizesPool); } m_tmpSolverNonContactConstraintPool.resizeNoInitialize(totalNumRows); m_gpuData->m_gpuConstraintRows->resize(totalNumRows); // b3GpuConstraintArray verify; if (useGpuInfo2) { { B3_PROFILE("getInfo2Kernel"); b3LauncherCL launcher(m_gpuData->m_queue, m_gpuData->m_getInfo2Kernel, "m_getInfo2Kernel"); launcher.setBuffer(m_gpuData->m_gpuConstraintRows->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuConstraintInfo1->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuConstraintRowOffsets->getBufferCL()); launcher.setBuffer(gpuConstraints->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuBatchConstraints->getBufferCL()); launcher.setBuffer(gpuBodies->getBufferCL()); launcher.setBuffer(gpuInertias->getBufferCL()); launcher.setBuffer(m_gpuData->m_gpuSolverBodies->getBufferCL()); launcher.setConst(infoGlobal.m_timeStep); launcher.setConst(infoGlobal.m_erp); launcher.setConst(infoGlobal.m_globalCfm); launcher.setConst(infoGlobal.m_damping); launcher.setConst(infoGlobal.m_numIterations); launcher.setConst(numConstraints); launcher.launch1D(numConstraints); clFinish(m_gpuData->m_queue); if (m_gpuData->m_batchSizes.size() == 0) m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints); //m_gpuData->m_gpuConstraintRows->copyToHost(verify); //m_gpuData->m_gpuConstraintRows->copyToHost(m_tmpSolverNonContactConstraintPool); } } else { gpuInertias->copyToHost(m_gpuData->m_cpuInertias); ///setup the b3SolverConstraints for (int i = 0; i < numConstraints; i++) { const int& info1 = m_tmpConstraintSizesPool[i]; if (info1) { int constraintIndex = batchConstraints[i].m_originalConstraintIndex; int constraintRowOffset = m_gpuData->m_cpuConstraintRowOffsets[constraintIndex]; b3GpuSolverConstraint* currentConstraintRow = &m_tmpSolverNonContactConstraintPool[constraintRowOffset]; b3GpuGenericConstraint& constraint = m_gpuData->m_cpuConstraints[i]; b3RigidBodyData& rbA = m_gpuData->m_cpuBodies[constraint.getRigidBodyA()]; //b3RigidBody& rbA = constraint.getRigidBodyA(); // b3RigidBody& rbB = constraint.getRigidBodyB(); b3RigidBodyData& rbB = m_gpuData->m_cpuBodies[constraint.getRigidBodyB()]; int solverBodyIdA = constraint.getRigidBodyA(); //getOrInitSolverBody(constraint.getRigidBodyA(),bodies,inertias); int solverBodyIdB = constraint.getRigidBodyB(); //getOrInitSolverBody(constraint.getRigidBodyB(),bodies,inertias); b3GpuSolverBody* bodyAPtr = &m_tmpSolverBodyPool[solverBodyIdA]; b3GpuSolverBody* bodyBPtr = &m_tmpSolverBodyPool[solverBodyIdB]; if (rbA.m_invMass) { batchConstraints[i].m_bodyAPtrAndSignBit = solverBodyIdA; } else { if (!solverBodyIdA) m_staticIdx = 0; batchConstraints[i].m_bodyAPtrAndSignBit = -solverBodyIdA; } if (rbB.m_invMass) { batchConstraints[i].m_bodyBPtrAndSignBit = solverBodyIdB; } else { if (!solverBodyIdB) m_staticIdx = 0; batchConstraints[i].m_bodyBPtrAndSignBit = -solverBodyIdB; } int overrideNumSolverIterations = 0; //constraint->getOverrideNumSolverIterations() > 0 ? constraint->getOverrideNumSolverIterations() : infoGlobal.m_numIterations; if (overrideNumSolverIterations > m_maxOverrideNumSolverIterations) m_maxOverrideNumSolverIterations = overrideNumSolverIterations; int j; for (j = 0; j < info1; j++) { memset(¤tConstraintRow[j], 0, sizeof(b3GpuSolverConstraint)); currentConstraintRow[j].m_angularComponentA.setValue(0, 0, 0); currentConstraintRow[j].m_angularComponentB.setValue(0, 0, 0); currentConstraintRow[j].m_appliedImpulse = 0.f; currentConstraintRow[j].m_appliedPushImpulse = 0.f; currentConstraintRow[j].m_cfm = 0.f; currentConstraintRow[j].m_contactNormal.setValue(0, 0, 0); currentConstraintRow[j].m_friction = 0.f; currentConstraintRow[j].m_frictionIndex = 0; currentConstraintRow[j].m_jacDiagABInv = 0.f; currentConstraintRow[j].m_lowerLimit = 0.f; currentConstraintRow[j].m_upperLimit = 0.f; currentConstraintRow[j].m_originalContactPoint = 0; currentConstraintRow[j].m_overrideNumSolverIterations = 0; currentConstraintRow[j].m_relpos1CrossNormal.setValue(0, 0, 0); currentConstraintRow[j].m_relpos2CrossNormal.setValue(0, 0, 0); currentConstraintRow[j].m_rhs = 0.f; currentConstraintRow[j].m_rhsPenetration = 0.f; currentConstraintRow[j].m_solverBodyIdA = 0; currentConstraintRow[j].m_solverBodyIdB = 0; currentConstraintRow[j].m_lowerLimit = -B3_INFINITY; currentConstraintRow[j].m_upperLimit = B3_INFINITY; currentConstraintRow[j].m_appliedImpulse = 0.f; currentConstraintRow[j].m_appliedPushImpulse = 0.f; currentConstraintRow[j].m_solverBodyIdA = solverBodyIdA; currentConstraintRow[j].m_solverBodyIdB = solverBodyIdB; currentConstraintRow[j].m_overrideNumSolverIterations = overrideNumSolverIterations; } bodyAPtr->internalGetDeltaLinearVelocity().setValue(0.f, 0.f, 0.f); bodyAPtr->internalGetDeltaAngularVelocity().setValue(0.f, 0.f, 0.f); bodyAPtr->internalGetPushVelocity().setValue(0.f, 0.f, 0.f); bodyAPtr->internalGetTurnVelocity().setValue(0.f, 0.f, 0.f); bodyBPtr->internalGetDeltaLinearVelocity().setValue(0.f, 0.f, 0.f); bodyBPtr->internalGetDeltaAngularVelocity().setValue(0.f, 0.f, 0.f); bodyBPtr->internalGetPushVelocity().setValue(0.f, 0.f, 0.f); bodyBPtr->internalGetTurnVelocity().setValue(0.f, 0.f, 0.f); b3GpuConstraintInfo2 info2; info2.fps = 1.f / infoGlobal.m_timeStep; info2.erp = infoGlobal.m_erp; info2.m_J1linearAxis = currentConstraintRow->m_contactNormal; info2.m_J1angularAxis = currentConstraintRow->m_relpos1CrossNormal; info2.m_J2linearAxis = 0; info2.m_J2angularAxis = currentConstraintRow->m_relpos2CrossNormal; info2.rowskip = sizeof(b3GpuSolverConstraint) / sizeof(b3Scalar); //check this ///the size of b3GpuSolverConstraint needs be a multiple of b3Scalar b3Assert(info2.rowskip * sizeof(b3Scalar) == sizeof(b3GpuSolverConstraint)); info2.m_constraintError = ¤tConstraintRow->m_rhs; currentConstraintRow->m_cfm = infoGlobal.m_globalCfm; info2.m_damping = infoGlobal.m_damping; info2.cfm = ¤tConstraintRow->m_cfm; info2.m_lowerLimit = ¤tConstraintRow->m_lowerLimit; info2.m_upperLimit = ¤tConstraintRow->m_upperLimit; info2.m_numIterations = infoGlobal.m_numIterations; m_gpuData->m_cpuConstraints[i].getInfo2(&info2, &m_gpuData->m_cpuBodies[0]); ///finalize the constraint setup for (j = 0; j < info1; j++) { b3GpuSolverConstraint& solverConstraint = currentConstraintRow[j]; if (solverConstraint.m_upperLimit >= m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold()) { solverConstraint.m_upperLimit = m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold(); } if (solverConstraint.m_lowerLimit <= -m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold()) { solverConstraint.m_lowerLimit = -m_gpuData->m_cpuConstraints[i].getBreakingImpulseThreshold(); } // solverConstraint.m_originalContactPoint = constraint; b3Matrix3x3& invInertiaWorldA = m_gpuData->m_cpuInertias[constraint.getRigidBodyA()].m_invInertiaWorld; { //b3Vector3 angularFactorA(1,1,1); const b3Vector3& ftorqueAxis1 = solverConstraint.m_relpos1CrossNormal; solverConstraint.m_angularComponentA = invInertiaWorldA * ftorqueAxis1; //*angularFactorA; } b3Matrix3x3& invInertiaWorldB = m_gpuData->m_cpuInertias[constraint.getRigidBodyB()].m_invInertiaWorld; { const b3Vector3& ftorqueAxis2 = solverConstraint.m_relpos2CrossNormal; solverConstraint.m_angularComponentB = invInertiaWorldB * ftorqueAxis2; //*constraint.getRigidBodyB().getAngularFactor(); } { //it is ok to use solverConstraint.m_contactNormal instead of -solverConstraint.m_contactNormal //because it gets multiplied iMJlB b3Vector3 iMJlA = solverConstraint.m_contactNormal * rbA.m_invMass; b3Vector3 iMJaA = invInertiaWorldA * solverConstraint.m_relpos1CrossNormal; b3Vector3 iMJlB = solverConstraint.m_contactNormal * rbB.m_invMass; //sign of normal? b3Vector3 iMJaB = invInertiaWorldB * solverConstraint.m_relpos2CrossNormal; b3Scalar sum = iMJlA.dot(solverConstraint.m_contactNormal); sum += iMJaA.dot(solverConstraint.m_relpos1CrossNormal); sum += iMJlB.dot(solverConstraint.m_contactNormal); sum += iMJaB.dot(solverConstraint.m_relpos2CrossNormal); b3Scalar fsum = b3Fabs(sum); b3Assert(fsum > B3_EPSILON); solverConstraint.m_jacDiagABInv = fsum > B3_EPSILON ? b3Scalar(1.) / sum : 0.f; } ///fix rhs ///todo: add force/torque accelerators { b3Scalar rel_vel; b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(rbA.m_linVel) + solverConstraint.m_relpos1CrossNormal.dot(rbA.m_angVel); b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(rbB.m_linVel) + solverConstraint.m_relpos2CrossNormal.dot(rbB.m_angVel); rel_vel = vel1Dotn + vel2Dotn; b3Scalar restitution = 0.f; b3Scalar positionalError = solverConstraint.m_rhs; //already filled in by getConstraintInfo2 b3Scalar velocityError = restitution - rel_vel * info2.m_damping; b3Scalar penetrationImpulse = positionalError * solverConstraint.m_jacDiagABInv; b3Scalar velocityImpulse = velocityError * solverConstraint.m_jacDiagABInv; solverConstraint.m_rhs = penetrationImpulse + velocityImpulse; solverConstraint.m_appliedImpulse = 0.f; } } } } m_gpuData->m_gpuConstraintRows->copyFromHost(m_tmpSolverNonContactConstraintPool); m_gpuData->m_gpuConstraintInfo1->copyFromHost(m_tmpConstraintSizesPool); if (m_gpuData->m_batchSizes.size() == 0) m_gpuData->m_gpuBatchConstraints->copyFromHost(batchConstraints); else m_gpuData->m_gpuBatchConstraints->copyToHost(batchConstraints); m_gpuData->m_gpuSolverBodies->copyFromHost(m_tmpSolverBodyPool); } //end useGpuInfo2 } #ifdef B3_SUPPORT_CONTACT_CONSTRAINTS { int i; for (i = 0; i < numManifolds; i++) { b3Contact4& manifold = manifoldPtr[i]; convertContact(bodies, inertias, &manifold, infoGlobal); } } #endif //B3_SUPPORT_CONTACT_CONSTRAINTS } // b3ContactSolverInfo info = infoGlobal; // int numNonContactPool = m_tmpSolverNonContactConstraintPool.size(); // int numConstraintPool = m_tmpSolverContactConstraintPool.size(); // int numFrictionPool = m_tmpSolverContactFrictionConstraintPool.size(); return 0.f; }
/* Create from TetGen .ele, .face, .node data */ void GpuTetraScene::createFromTetGenData(const char* ele, const char* node, const ConstructionInfo& ci) { b3Scalar scaling(10); b3AlignedObjectArray<b3Vector3> pos; int nnode=0; int ndims=0; int nattrb=0; int hasbounds=0; int result = sscanf(node,"%d %d %d %d",&nnode,&ndims,&nattrb,&hasbounds); result = sscanf(node,"%d %d %d %d",&nnode,&ndims,&nattrb,&hasbounds); node += nextLine(node); //b3AlignedObjectArray<b3Vector3> rigidBodyPositions; //b3AlignedObjectArray<int> rigidBodyIds; pos.resize(nnode); for(int i=0;i<pos.size();++i) { int index=0; //int bound=0; float x,y,z; sscanf(node,"%d %f %f %f",&index,&x,&y,&z); // sn>>index; // sn>>x;sn>>y;sn>>z; node += nextLine(node); //for(int j=0;j<nattrb;++j) // sn>>a; //if(hasbounds) // sn>>bound; pos[index].setX(b3Scalar(x)*scaling); pos[index].setY(b3Scalar(y)*scaling); pos[index].setZ(b3Scalar(z)*scaling); } if(ele&&ele[0]) { int ntetra=0; int ncorner=0; int neattrb=0; sscanf(ele,"%d %d %d",&ntetra,&ncorner,&neattrb); ele += nextLine(ele); //se>>ntetra;se>>ncorner;se>>neattrb; for(int i=0;i<ntetra;++i) { int index=0; int ni[4]; //se>>index; //se>>ni[0];se>>ni[1];se>>ni[2];se>>ni[3]; sscanf(ele,"%d %d %d %d %d",&index,&ni[0],&ni[1],&ni[2],&ni[3]); ele+=nextLine(ele); b3Vector3 average=b3MakeVector3(0,0,0); for (int v=0;v<4;v++) { average+=pos[ni[v]]; } average/=4; for (int v=0;v<4;v++) { b3Vector3 shiftedPos = pos[ni[v]]-average; mytetra_vertices[0+v*9] = shiftedPos.getX(); mytetra_vertices[1+v*9] = shiftedPos.getY(); mytetra_vertices[2+v*9] = shiftedPos.getZ(); } //todo: subtract average int strideInBytes = 9*sizeof(float); int numVertices = sizeof(mytetra_vertices)/strideInBytes; int numIndices = sizeof(mytetra_indices)/sizeof(int); int shapeId = ci.m_instancingRenderer->registerShape(&mytetra_vertices[0],numVertices,mytetra_indices,numIndices); int group=1; int mask=1; { b3Vector4 scaling=b3MakeVector4(1,1,1,1); int colIndex = m_data->m_np->registerConvexHullShape(&mytetra_vertices[0],strideInBytes,numVertices, scaling); b3Vector3 position=b3MakeVector3(0,150,0); // position+=average;//*1.2;//*2; position+=average*1.2;//*2; //rigidBodyPositions.push_back(position); b3Quaternion orn(0,0,0,1); static int curColor=0; b3Vector4 color = colors[curColor++]; curColor&=3; int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling); int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(1.f,position,orn,colIndex,0,false); //rigidBodyIds.push_back(pid); } //for(int j=0;j<neattrb;++j) // se>>a; //psb->appendTetra(ni[0],ni[1],ni[2],ni[3]); } // printf("Nodes: %u\r\n",psb->m_nodes.size()); // printf("Links: %u\r\n",psb->m_links.size()); // printf("Faces: %u\r\n",psb->m_faces.size()); // printf("Tetras: %u\r\n",psb->m_tetras.size()); } m_data->m_rigidBodyPipeline->writeAllInstancesToGpu(); m_data->m_np->writeAllBodiesToGpu(); m_data->m_bp->writeAabbsToGpu(); m_data->m_rigidBodyPipeline->setupGpuAabbsFull(); m_data->m_bp->calculateOverlappingPairs(m_data->m_config.m_maxBroadphasePairs); int numPairs = m_data->m_bp->getNumOverlap(); cl_mem pairs = m_data->m_bp->getOverlappingPairBuffer(); b3OpenCLArray<b3Int2> clPairs(m_clData->m_clContext,m_clData->m_clQueue); clPairs.setFromOpenCLBuffer(pairs,numPairs); b3AlignedObjectArray<b3Int2> allPairs; clPairs.copyToHost(allPairs); for (int p=0;p<allPairs.size();p++) { b3Vector3 posA,posB; b3Quaternion ornA,ornB; int bodyIndexA = allPairs[p].x; int bodyIndexB = allPairs[p].y; m_data->m_np->getObjectTransformFromCpu(posA,ornA,bodyIndexA); m_data->m_np->getObjectTransformFromCpu(posB,ornB,bodyIndexB); b3Vector3 pivotWorld = (posA+posB)*0.5f; b3Transform transA,transB; transA.setIdentity(); transA.setOrigin(posA); transA.setRotation(ornA); transB.setIdentity(); transB.setOrigin(posB); transB.setRotation(ornB); b3Vector3 pivotInA = transA.inverse()*pivotWorld; b3Vector3 pivotInB = transB.inverse()*pivotWorld; b3Transform frameInA,frameInB; frameInA.setIdentity(); frameInB.setIdentity(); frameInA.setOrigin(pivotInA); frameInB.setOrigin(pivotInB); b3Quaternion relTargetAB = frameInA.getRotation()*frameInB.getRotation().inverse(); //c = new b3FixedConstraint(pid,prevBody,frameInA,frameInB); float breakingThreshold = 45;//37.f; //c->setBreakingImpulseThreshold(37.1); bool useGPU = true; if (useGPU) { int cid = m_data->m_rigidBodyPipeline->createFixedConstraint(bodyIndexA,bodyIndexB,pivotInA,pivotInB,relTargetAB,breakingThreshold); } else { b3FixedConstraint* c = new b3FixedConstraint(bodyIndexA,bodyIndexB,frameInA,frameInB); c->setBreakingImpulseThreshold(breakingThreshold); m_data->m_rigidBodyPipeline->addConstraint(c); } } printf("numPairs = %d\n",numPairs); }
AabbCalculationCallback() { m_aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT),b3Scalar(B3_LARGE_FLOAT)); m_aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT),b3Scalar(-B3_LARGE_FLOAT)); }
//using localPointA for all points int b3ContactCache::sortCachedPoints(const b3Vector3& pt) { //calculate 4 possible cases areas, and take biggest area //also need to keep 'deepest' int maxPenetrationIndex = -1; #define KEEP_DEEPEST_POINT 1 #ifdef KEEP_DEEPEST_POINT b3Scalar maxPenetration = pt.getDistance(); for (int i=0;i<4;i++) { if (m_pointCache[i].getDistance() < maxPenetration) { maxPenetrationIndex = i; maxPenetration = m_pointCache[i].getDistance(); } } #endif //KEEP_DEEPEST_POINT b3Scalar res0(b3Scalar(0.)),res1(b3Scalar(0.)),res2(b3Scalar(0.)),res3(b3Scalar(0.)); if (gContactCalcArea3Points) { if (maxPenetrationIndex != 0) { b3Vector3 a0 = pt.m_localPointA-m_pointCache[1].m_localPointA; b3Vector3 b0 = m_pointCache[3].m_localPointA-m_pointCache[2].m_localPointA; b3Vector3 cross = a0.cross(b0); res0 = cross.length2(); } if (maxPenetrationIndex != 1) { b3Vector3 a1 = pt.m_localPointA-m_pointCache[0].m_localPointA; b3Vector3 b1 = m_pointCache[3].m_localPointA-m_pointCache[2].m_localPointA; b3Vector3 cross = a1.cross(b1); res1 = cross.length2(); } if (maxPenetrationIndex != 2) { b3Vector3 a2 = pt.m_localPointA-m_pointCache[0].m_localPointA; b3Vector3 b2 = m_pointCache[3].m_localPointA-m_pointCache[1].m_localPointA; b3Vector3 cross = a2.cross(b2); res2 = cross.length2(); } if (maxPenetrationIndex != 3) { b3Vector3 a3 = pt.m_localPointA-m_pointCache[0].m_localPointA; b3Vector3 b3 = m_pointCache[2].m_localPointA-m_pointCache[1].m_localPointA; b3Vector3 cross = a3.cross(b3); res3 = cross.length2(); } } else { if(maxPenetrationIndex != 0) { res0 = calcArea4Points(pt.m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[2].m_localPointA,m_pointCache[3].m_localPointA); } if(maxPenetrationIndex != 1) { res1 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[2].m_localPointA,m_pointCache[3].m_localPointA); } if(maxPenetrationIndex != 2) { res2 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[3].m_localPointA); } if(maxPenetrationIndex != 3) { res3 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[2].m_localPointA); } } b3Vector4 maxvec(res0,res1,res2,res3); int biggestarea = maxvec.closestAxis4(); return biggestarea; }
bool b3VoronoiSimplexSolver::updateClosestVectorAndPoints() { if (m_needsUpdate) { m_cachedBC.reset(); m_needsUpdate = false; switch (numVertices()) { case 0: m_cachedValidClosest = false; break; case 1: { m_cachedP1 = m_simplexPointsP[0]; m_cachedP2 = m_simplexPointsQ[0]; m_cachedV = m_cachedP1-m_cachedP2; //== m_simplexVectorW[0] m_cachedBC.reset(); m_cachedBC.setBarycentricCoordinates(b3Scalar(1.),b3Scalar(0.),b3Scalar(0.),b3Scalar(0.)); m_cachedValidClosest = m_cachedBC.isValid(); break; }; case 2: { //closest point origin from line segment const b3Vector3& from = m_simplexVectorW[0]; const b3Vector3& to = m_simplexVectorW[1]; b3Vector3 nearest; b3Vector3 p =b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(0.)); b3Vector3 diff = p - from; b3Vector3 v = to - from; b3Scalar t = v.dot(diff); if (t > 0) { b3Scalar dotVV = v.dot(v); if (t < dotVV) { t /= dotVV; diff -= t*v; m_cachedBC.m_usedVertices.usedVertexA = true; m_cachedBC.m_usedVertices.usedVertexB = true; } else { t = 1; diff -= v; //reduce to 1 point m_cachedBC.m_usedVertices.usedVertexB = true; } } else { t = 0; //reduce to 1 point m_cachedBC.m_usedVertices.usedVertexA = true; } m_cachedBC.setBarycentricCoordinates(1-t,t); nearest = from + t*v; m_cachedP1 = m_simplexPointsP[0] + t * (m_simplexPointsP[1] - m_simplexPointsP[0]); m_cachedP2 = m_simplexPointsQ[0] + t * (m_simplexPointsQ[1] - m_simplexPointsQ[0]); m_cachedV = m_cachedP1 - m_cachedP2; reduceVertices(m_cachedBC.m_usedVertices); m_cachedValidClosest = m_cachedBC.isValid(); break; } case 3: { //closest point origin from triangle b3Vector3 p =b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(0.)); const b3Vector3& a = m_simplexVectorW[0]; const b3Vector3& b = m_simplexVectorW[1]; const b3Vector3& c = m_simplexVectorW[2]; closestPtPointTriangle(p,a,b,c,m_cachedBC); m_cachedP1 = m_simplexPointsP[0] * m_cachedBC.m_barycentricCoords[0] + m_simplexPointsP[1] * m_cachedBC.m_barycentricCoords[1] + m_simplexPointsP[2] * m_cachedBC.m_barycentricCoords[2]; m_cachedP2 = m_simplexPointsQ[0] * m_cachedBC.m_barycentricCoords[0] + m_simplexPointsQ[1] * m_cachedBC.m_barycentricCoords[1] + m_simplexPointsQ[2] * m_cachedBC.m_barycentricCoords[2]; m_cachedV = m_cachedP1-m_cachedP2; reduceVertices (m_cachedBC.m_usedVertices); m_cachedValidClosest = m_cachedBC.isValid(); break; } case 4: { b3Vector3 p =b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(0.)); const b3Vector3& a = m_simplexVectorW[0]; const b3Vector3& b = m_simplexVectorW[1]; const b3Vector3& c = m_simplexVectorW[2]; const b3Vector3& d = m_simplexVectorW[3]; bool hasSeperation = closestPtPointTetrahedron(p,a,b,c,d,m_cachedBC); if (hasSeperation) { m_cachedP1 = m_simplexPointsP[0] * m_cachedBC.m_barycentricCoords[0] + m_simplexPointsP[1] * m_cachedBC.m_barycentricCoords[1] + m_simplexPointsP[2] * m_cachedBC.m_barycentricCoords[2] + m_simplexPointsP[3] * m_cachedBC.m_barycentricCoords[3]; m_cachedP2 = m_simplexPointsQ[0] * m_cachedBC.m_barycentricCoords[0] + m_simplexPointsQ[1] * m_cachedBC.m_barycentricCoords[1] + m_simplexPointsQ[2] * m_cachedBC.m_barycentricCoords[2] + m_simplexPointsQ[3] * m_cachedBC.m_barycentricCoords[3]; m_cachedV = m_cachedP1-m_cachedP2; reduceVertices (m_cachedBC.m_usedVertices); } else { // printf("sub distance got penetration\n"); if (m_cachedBC.m_degenerate) { m_cachedValidClosest = false; } else { m_cachedValidClosest = true; //degenerate case == false, penetration = true + zero m_cachedV.setValue(b3Scalar(0.),b3Scalar(0.),b3Scalar(0.)); } break; } m_cachedValidClosest = m_cachedBC.isValid(); //closest point origin from tetrahedron break; } default: { m_cachedValidClosest = false; } }; } return m_cachedValidClosest; }
In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #include "b3ContactCache.h" #include "Bullet3Common/b3Transform.h" #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h" b3Scalar gContactBreakingThreshold = b3Scalar(0.02); ///gContactCalcArea3Points will approximate the convex hull area using 3 points ///when setting it to false, it will use 4 points to compute the area: it is more accurate but slower bool gContactCalcArea3Points = true; static inline b3Scalar calcArea4Points(const b3Vector3 &p0,const b3Vector3 &p1,const b3Vector3 &p2,const b3Vector3 &p3) { // It calculates possible 3 area constructed from random 4 points and returns the biggest one. b3Vector3 a[3],b[3]; a[0] = p0 - p1; a[1] = p0 - p2;
bool b3VoronoiSimplexSolver::closestPtPointTriangle(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c,b3SubSimplexClosestResult& result) { result.m_usedVertices.reset(); // Check if P in vertex region outside A b3Vector3 ab = b - a; b3Vector3 ac = c - a; b3Vector3 ap = p - a; b3Scalar d1 = ab.dot(ap); b3Scalar d2 = ac.dot(ap); if (d1 <= b3Scalar(0.0) && d2 <= b3Scalar(0.0)) { result.m_closestPointOnSimplex = a; result.m_usedVertices.usedVertexA = true; result.setBarycentricCoordinates(1,0,0); return true;// a; // barycentric coordinates (1,0,0) } // Check if P in vertex region outside B b3Vector3 bp = p - b; b3Scalar d3 = ab.dot(bp); b3Scalar d4 = ac.dot(bp); if (d3 >= b3Scalar(0.0) && d4 <= d3) { result.m_closestPointOnSimplex = b; result.m_usedVertices.usedVertexB = true; result.setBarycentricCoordinates(0,1,0); return true; // b; // barycentric coordinates (0,1,0) } // Check if P in edge region of AB, if so return projection of P onto AB b3Scalar vc = d1*d4 - d3*d2; if (vc <= b3Scalar(0.0) && d1 >= b3Scalar(0.0) && d3 <= b3Scalar(0.0)) { b3Scalar v = d1 / (d1 - d3); result.m_closestPointOnSimplex = a + v * ab; result.m_usedVertices.usedVertexA = true; result.m_usedVertices.usedVertexB = true; result.setBarycentricCoordinates(1-v,v,0); return true; //return a + v * ab; // barycentric coordinates (1-v,v,0) } // Check if P in vertex region outside C b3Vector3 cp = p - c; b3Scalar d5 = ab.dot(cp); b3Scalar d6 = ac.dot(cp); if (d6 >= b3Scalar(0.0) && d5 <= d6) { result.m_closestPointOnSimplex = c; result.m_usedVertices.usedVertexC = true; result.setBarycentricCoordinates(0,0,1); return true;//c; // barycentric coordinates (0,0,1) } // Check if P in edge region of AC, if so return projection of P onto AC b3Scalar vb = d5*d2 - d1*d6; if (vb <= b3Scalar(0.0) && d2 >= b3Scalar(0.0) && d6 <= b3Scalar(0.0)) { b3Scalar w = d2 / (d2 - d6); result.m_closestPointOnSimplex = a + w * ac; result.m_usedVertices.usedVertexA = true; result.m_usedVertices.usedVertexC = true; result.setBarycentricCoordinates(1-w,0,w); return true; //return a + w * ac; // barycentric coordinates (1-w,0,w) } // Check if P in edge region of BC, if so return projection of P onto BC b3Scalar va = d3*d6 - d5*d4; if (va <= b3Scalar(0.0) && (d4 - d3) >= b3Scalar(0.0) && (d5 - d6) >= b3Scalar(0.0)) { b3Scalar w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); result.m_closestPointOnSimplex = b + w * (c - b); result.m_usedVertices.usedVertexB = true; result.m_usedVertices.usedVertexC = true; result.setBarycentricCoordinates(0,1-w,w); return true; // return b + w * (c - b); // barycentric coordinates (0,1-w,w) } // P inside face region. Compute Q through its barycentric coordinates (u,v,w) b3Scalar denom = b3Scalar(1.0) / (va + vb + vc); b3Scalar v = vb * denom; b3Scalar w = vc * denom; result.m_closestPointOnSimplex = a + ab * v + ac * w; result.m_usedVertices.usedVertexA = true; result.m_usedVertices.usedVertexB = true; result.m_usedVertices.usedVertexC = true; result.setBarycentricCoordinates(1-v-w,v,w); return true; // return a + ab * v + ac * w; // = u*a + v*b + w*c, u = va * denom = b3Scalar(1.0) - v - w }
void computeLocalInertia(b3Scalar mass, b3Vector3& localInertia) { btScalar elem = b3Scalar(0.4) * mass * m_radius*m_radius; localInertia.setValue(elem,elem,elem); }
void GLInstancingRenderer::updateCamera() { GLint err = glGetError(); b3Assert(err==GL_NO_ERROR); int m_forwardAxis(2); float m_frustumZNear=1; float m_frustumZFar=10000.f; // m_azi=m_azi+0.01; b3Scalar rele = m_data->m_ele * b3Scalar(0.01745329251994329547);// rads per deg b3Scalar razi = m_data->m_azi * b3Scalar(0.01745329251994329547);// rads per deg b3Quaternion rot(m_data->m_cameraUp,razi); b3Vector3 eyePos = b3MakeVector3(0,0,0); eyePos[m_forwardAxis] = -m_data->m_cameraDistance; b3Vector3 forward = b3MakeVector3(eyePos[0],eyePos[1],eyePos[2]); if (forward.length2() < B3_EPSILON) { forward.setValue(1.f,0.f,0.f); } b3Vector3 right = m_data->m_cameraUp.cross(forward); b3Quaternion roll(right,-rele); eyePos = b3Matrix3x3(rot) * b3Matrix3x3(roll) * eyePos; m_data->m_cameraPosition[0] = eyePos.x; m_data->m_cameraPosition[1] = eyePos.y; m_data->m_cameraPosition[2] = eyePos.z; m_data->m_cameraPosition += m_data->m_cameraTargetPosition; if (m_screenWidth == 0 && m_screenHeight == 0) return; b3Scalar aspect; b3Vector3 extents; aspect = m_screenWidth / (b3Scalar)m_screenHeight; extents.setValue(aspect * 1.0f, 1.0f,0); if (m_screenWidth > m_screenHeight) { b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); } else { b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); } b3CreateLookAt(m_data->m_cameraPosition,m_data->m_cameraTargetPosition,m_data->m_cameraUp,modelviewMatrix); }