int b3GpuNarrowPhase::registerFace(const b3Vector3& faceNormal, float faceConstant) { int faceOffset = m_data->m_convexFaces.size(); b3GpuFace& face = m_data->m_convexFaces.expand(); face.m_plane[0] = faceNormal.getX(); face.m_plane[1] = faceNormal.getY(); face.m_plane[2] = faceNormal.getZ(); face.m_plane[3] = faceConstant; return faceOffset; }
KukaGraspExample(GUIHelperInterface* helper, int /* options */) :m_app(helper->getAppInterface()), m_guiHelper(helper), // m_options(options), m_kukaIndex(-1), m_time(0) { m_targetPos.setValue(0.5,0,1); m_worldPos.setValue(0, 0, 0); m_app->setUpAxis(2); }
static void b3CreateLookAt(const b3Vector3& eye, const b3Vector3& center,const b3Vector3& up, GLfloat result[16]) { b3Vector3 f = (center - eye).normalized(); b3Vector3 u = up.normalized(); b3Vector3 s = (f.cross(u)).normalized(); u = s.cross(f); result[0*4+0] = s.x; result[1*4+0] = s.y; result[2*4+0] = s.z; result[0*4+1] = u.x; result[1*4+1] = u.y; result[2*4+1] = u.z; result[0*4+2] =-f.x; result[1*4+2] =-f.y; result[2*4+2] =-f.z; result[0*4+3] = 0.f; result[1*4+3] = 0.f; result[2*4+3] = 0.f; result[3*4+0] = -s.dot(eye); result[3*4+1] = -u.dot(eye); result[3*4+2] = f.dot(eye); result[3*4+3] = 1.f; }
void wheelCallback( float deltax, float deltay) { if (!m_mouseButton) { if (b3Fabs(deltax)>b3Fabs(deltay)) { m_azi -= deltax*0.1; } else { //m_cameraDistance -= deltay*0.1; b3Vector3 fwd = m_cameraTargetPosition-m_cameraPosition; fwd.normalize(); m_cameraTargetPosition += fwd*deltay*0.1; } } else { if (b3Fabs(deltax)>b3Fabs(deltay)) { b3Vector3 fwd = m_cameraTargetPosition-m_cameraPosition; b3Vector3 side = m_cameraUp.cross(fwd); side.normalize(); m_cameraTargetPosition += side * deltax*0.1; } else { m_cameraTargetPosition -= m_cameraUp * deltay*0.1; } } }
void wheelCallback( float deltax, float deltay) { if (!m_leftMouseButton) { if (deltay<0 || m_cameraDistance>1) { m_cameraDistance -= deltay*0.1; if (m_cameraDistance<1) m_cameraDistance=1; } else { b3Vector3 fwd = m_cameraTargetPosition-m_cameraPosition; fwd.normalize(); m_cameraTargetPosition += fwd*deltay*WHEEL_MULTIPLIER; } } else { if (b3Fabs(deltax)>b3Fabs(deltay)) { b3Vector3 fwd = m_cameraTargetPosition-m_cameraPosition; b3Vector3 side = m_cameraUp.cross(fwd); side.normalize(); m_cameraTargetPosition += side * deltax*WHEEL_MULTIPLIER; } else { m_cameraTargetPosition -= m_cameraUp * deltay*WHEEL_MULTIPLIER; } } }
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; }
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 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; }
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 mouseMoveCallback(float x, float y) { if (m_altPressed || m_controlPressed) { float xDelta = x-m_mouseXpos; float yDelta = y-m_mouseYpos; if (m_leftMouseButton) { // if (b3Fabs(xDelta)>b3Fabs(yDelta)) // { m_azi -= xDelta*MOUSE_MOVE_MULTIPLIER; // } else // { m_ele += yDelta*MOUSE_MOVE_MULTIPLIER; // } } if (m_middleMouseButton) { m_cameraTargetPosition += m_cameraUp * yDelta*0.01; b3Vector3 fwd = m_cameraTargetPosition-m_cameraPosition; b3Vector3 side = m_cameraUp.cross(fwd); side.normalize(); m_cameraTargetPosition += side * xDelta*0.01; } if (m_rightMouseButton) { m_cameraDistance -= xDelta*0.01; m_cameraDistance -= yDelta*0.01; if (m_cameraDistance<1) m_cameraDistance=1; if (m_cameraDistance>1000) m_cameraDistance=1000; } } //printf("m_azi/pitch = %f\n", m_azi); // printf("m_ele/yaw = %f\n", m_ele); m_mouseXpos = x; m_mouseYpos = y; m_mouseInitialized = true; }
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]); }
void ComputeClosestPointsPlaneSphere(const b3Vector3& planeNormalWorld, b3Scalar planeConstant, const b3Vector3& spherePosWorld,b3Scalar sphereRadius, plContactCache* contactCache) { if (contactCache->numAddedPoints < contactCache->pointCapacity) { lwContactPoint& pointOut = contactCache->pointsOut[contactCache->numAddedPoints]; b3Scalar t = -(spherePosWorld.dot(-planeNormalWorld)+planeConstant); b3Vector3 intersectionPoint = spherePosWorld+t*-planeNormalWorld; b3Scalar distance = t-sphereRadius; if (distance<=0) { pointOut.m_distance = distance; plVecCopy(pointOut.m_ptOnBWorld,intersectionPoint); plVecCopy(pointOut.m_ptOnAWorld,spherePosWorld+sphereRadius*-planeNormalWorld); plVecCopy(pointOut.m_normalOnB,planeNormalWorld); contactCache->numAddedPoints++; } } }
void computeLocalInertia(b3Scalar mass, b3Vector3& localInertia) { btScalar elem = b3Scalar(0.4) * mass * m_radius*m_radius; localInertia.setValue(elem,elem,elem); }
void applyImpulse(const b3Vector3& impulse, const b3Vector3& rel_pos) { m_linearVelocity += impulse * m_invMass; b3Vector3 torqueImpulse = rel_pos.cross(impulse); m_angularVelocity += m_invInertiaTensorWorld * torqueImpulse; }
b3Vector3 getVelocity(const b3Vector3& relPos) const { return m_linearVelocity + m_angularVelocity.cross(relPos); }
virtual void stepSimulation(float deltaTime) { float dt = deltaTime; btClamp(dt,0.0001f,0.01f); m_time+=dt; m_targetPos.setValue(0.4-0.4*b3Cos( m_time), 0, 0.8+0.4*b3Cos( m_time)); m_targetOri.setValue(0, 1.0, 0, 0); m_targetPos.setValue(0.2*b3Cos( m_time), 0.2*b3Sin( m_time), 1.1); int numJoints = m_robotSim.getNumJoints(m_kukaIndex); if (numJoints==7) { double q_current[7]={0,0,0,0,0,0,0}; b3JointStates2 jointStates; if (m_robotSim.getJointStates(m_kukaIndex,jointStates)) { //skip the base positions (7 values) b3Assert(7+numJoints == jointStates.m_numDegreeOfFreedomQ); for (int i=0;i<numJoints;i++) { q_current[i] = jointStates.m_actualStateQ[i+7]; } } // compute body position and orientation b3LinkState linkState; m_robotSim.getLinkState(0, 6, &linkState); m_worldPos.setValue(linkState.m_worldLinkFramePosition[0], linkState.m_worldLinkFramePosition[1], linkState.m_worldLinkFramePosition[2]); m_worldOri.setValue(linkState.m_worldLinkFrameOrientation[0], linkState.m_worldLinkFrameOrientation[1], linkState.m_worldLinkFrameOrientation[2], linkState.m_worldLinkFrameOrientation[3]); b3Vector3DoubleData targetPosDataOut; m_targetPos.serializeDouble(targetPosDataOut); b3Vector3DoubleData worldPosDataOut; m_worldPos.serializeDouble(worldPosDataOut); b3Vector3DoubleData targetOriDataOut; m_targetOri.serializeDouble(targetOriDataOut); b3Vector3DoubleData worldOriDataOut; m_worldOri.serializeDouble(worldOriDataOut); b3RobotSimulatorInverseKinematicArgs ikargs; b3RobotSimulatorInverseKinematicsResults ikresults; ikargs.m_bodyUniqueId = m_kukaIndex; // ikargs.m_currentJointPositions = q_current; // ikargs.m_numPositions = 7; ikargs.m_endEffectorTargetPosition[0] = targetPosDataOut.m_floats[0]; ikargs.m_endEffectorTargetPosition[1] = targetPosDataOut.m_floats[1]; ikargs.m_endEffectorTargetPosition[2] = targetPosDataOut.m_floats[2]; //ikargs.m_flags |= B3_HAS_IK_TARGET_ORIENTATION; ikargs.m_flags |= B3_HAS_JOINT_DAMPING; ikargs.m_endEffectorTargetOrientation[0] = targetOriDataOut.m_floats[0]; ikargs.m_endEffectorTargetOrientation[1] = targetOriDataOut.m_floats[1]; ikargs.m_endEffectorTargetOrientation[2] = targetOriDataOut.m_floats[2]; ikargs.m_endEffectorTargetOrientation[3] = targetOriDataOut.m_floats[3]; ikargs.m_endEffectorLinkIndex = 6; // Settings based on default KUKA arm setting ikargs.m_lowerLimits.resize(numJoints); ikargs.m_upperLimits.resize(numJoints); ikargs.m_jointRanges.resize(numJoints); ikargs.m_restPoses.resize(numJoints); ikargs.m_jointDamping.resize(numJoints,0.5); ikargs.m_lowerLimits[0] = -2.32; ikargs.m_lowerLimits[1] = -1.6; ikargs.m_lowerLimits[2] = -2.32; ikargs.m_lowerLimits[3] = -1.6; ikargs.m_lowerLimits[4] = -2.32; ikargs.m_lowerLimits[5] = -1.6; ikargs.m_lowerLimits[6] = -2.4; ikargs.m_upperLimits[0] = 2.32; ikargs.m_upperLimits[1] = 1.6; ikargs.m_upperLimits[2] = 2.32; ikargs.m_upperLimits[3] = 1.6; ikargs.m_upperLimits[4] = 2.32; ikargs.m_upperLimits[5] = 1.6; ikargs.m_upperLimits[6] = 2.4; ikargs.m_jointRanges[0] = 5.8; ikargs.m_jointRanges[1] = 4; ikargs.m_jointRanges[2] = 5.8; ikargs.m_jointRanges[3] = 4; ikargs.m_jointRanges[4] = 5.8; ikargs.m_jointRanges[5] = 4; ikargs.m_jointRanges[6] = 6; ikargs.m_restPoses[0] = 0; ikargs.m_restPoses[1] = 0; ikargs.m_restPoses[2] = 0; ikargs.m_restPoses[3] = SIMD_HALF_PI; ikargs.m_restPoses[4] = 0; ikargs.m_restPoses[5] = -SIMD_HALF_PI*0.66; ikargs.m_restPoses[6] = 0; ikargs.m_jointDamping[0] = 10.0; ikargs.m_numDegreeOfFreedom = numJoints; if (m_robotSim.calculateInverseKinematics(ikargs,ikresults)) { //copy the IK result to the desired state of the motor/actuator for (int i=0;i<numJoints;i++) { b3RobotSimulatorJointMotorArgs t(CONTROL_MODE_POSITION_VELOCITY_PD); t.m_targetPosition = ikresults.m_calculatedJointPositions[i]; t.m_maxTorqueValue = 100.0; t.m_kp= 1.0; t.m_targetVelocity = 0; t.m_kd = 1.0; m_robotSim.setJointMotorControl(m_kukaIndex,i,t); } } } m_robotSim.stepSimulation(); }
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; }