PfxInt32 pfxInitializeBallJoint(PfxJoint &joint, const PfxRigidState &stateA,const PfxRigidState &stateB, const PfxBallJointInitParam ¶m) { joint.m_active = 1; joint.m_numConstraints = 3; joint.m_userData = NULL; joint.m_type = kPfxJointBall; joint.m_rigidBodyIdA = stateA.getRigidBodyId(); joint.m_rigidBodyIdB = stateB.getRigidBodyId(); for(int i=0;i<3;i++) { joint.m_constraints[i].reset(); } joint.m_constraints[0].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[1].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[2].m_lock = SCE_PFX_JOINT_LOCK_FIX; PfxMatrix3 rotA = transpose(PfxMatrix3(stateA.getOrientation())); PfxMatrix3 rotB = transpose(PfxMatrix3(stateB.getOrientation())); joint.m_anchorA = rotA * (param.anchorPoint - stateA.getPosition()); joint.m_anchorB = rotB * (param.anchorPoint - stateB.getPosition()); joint.m_frameA = PfxMatrix3::identity(); joint.m_frameB = rotB * PfxMatrix3(stateA.getOrientation()) * joint.m_frameA; return SCE_PFX_OK; }
PfxInt32 pfxInitializeSwingTwistJoint(PfxJoint &joint, const PfxRigidState &stateA,const PfxRigidState &stateB, const PfxSwingTwistJointInitParam ¶m) { joint.m_active = 1; joint.m_numConstraints = 6; joint.m_userData = NULL; joint.m_type = kPfxJointSwingtwist; joint.m_rigidBodyIdA = stateA.getRigidBodyId(); joint.m_rigidBodyIdB = stateB.getRigidBodyId(); for(int i=0;i<6;i++) { joint.m_constraints[i].reset(); } joint.m_constraints[0].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[1].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[2].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[3].m_lock = SCE_PFX_JOINT_LOCK_LIMIT; joint.m_constraints[4].m_lock = SCE_PFX_JOINT_LOCK_LIMIT; joint.m_constraints[5].m_lock = SCE_PFX_JOINT_LOCK_FREE; // Set twist angle limit if(param.twistLowerAngle > param.twistUpperAngle || !SCE_PFX_RANGE_CHECK(param.twistLowerAngle,-SCE_PFX_PI-0.01f,SCE_PFX_PI+0.01f) || !SCE_PFX_RANGE_CHECK(param.twistUpperAngle,-SCE_PFX_PI-0.01f,SCE_PFX_PI+0.01f)) { return SCE_PFX_ERR_OUT_OF_RANGE; } joint.m_constraints[3].m_movableLowerLimit = param.twistLowerAngle; joint.m_constraints[3].m_movableUpperLimit = param.twistUpperAngle; // Set swing angle limit if(param.swingLowerAngle > param.swingUpperAngle || !SCE_PFX_RANGE_CHECK(param.swingLowerAngle,0.0f,SCE_PFX_PI+0.01f) || !SCE_PFX_RANGE_CHECK(param.swingUpperAngle,0.0f,SCE_PFX_PI+0.01f)) { return SCE_PFX_ERR_OUT_OF_RANGE; } joint.m_constraints[4].m_movableLowerLimit = param.swingLowerAngle; joint.m_constraints[4].m_movableUpperLimit = param.swingUpperAngle; // Calc joint frame PfxMatrix3 rotA = transpose(PfxMatrix3(stateA.getOrientation())); PfxMatrix3 rotB = transpose(PfxMatrix3(stateB.getOrientation())); PfxVector3 axisInA = rotA * normalize(param.twistAxis); joint.m_anchorA = rotA * (param.anchorPoint - stateA.getPosition()); joint.m_anchorB = rotB * (param.anchorPoint - stateB.getPosition()); PfxVector3 axis1, axis2; pfxGetPlaneSpace(axisInA, axis1, axis2 ); joint.m_frameA = PfxMatrix3(axisInA, axis1, axis2); joint.m_frameB = rotB * PfxMatrix3(stateA.getOrientation()) * joint.m_frameA; return SCE_PFX_OK; }
PfxInt32 pfxInitializeSliderJoint(PfxJoint &joint, const PfxRigidState &stateA,const PfxRigidState &stateB, const PfxSliderJointInitParam ¶m) { joint.m_active = 1; joint.m_numConstraints = 6; joint.m_userData = NULL; joint.m_type = kPfxJointSlider; joint.m_rigidBodyIdA = stateA.getRigidBodyId(); joint.m_rigidBodyIdB = stateB.getRigidBodyId(); for(int i=0;i<6;i++) { joint.m_constraints[i].reset(); } if(param.lowerDistance == 0.0f && param.upperDistance == 0.0f) { joint.m_constraints[0].m_lock = SCE_PFX_JOINT_LOCK_FREE; } else { joint.m_constraints[0].m_lock = SCE_PFX_JOINT_LOCK_LIMIT; } joint.m_constraints[1].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[2].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[3].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[4].m_lock = SCE_PFX_JOINT_LOCK_FIX; joint.m_constraints[5].m_lock = SCE_PFX_JOINT_LOCK_FIX; if(param.lowerDistance > param.upperDistance ) { return SCE_PFX_ERR_OUT_OF_RANGE; } joint.m_constraints[0].m_movableLowerLimit = param.lowerDistance; joint.m_constraints[0].m_movableUpperLimit = param.upperDistance; // Calc joint frame PfxMatrix3 rotA = transpose(PfxMatrix3(stateA.getOrientation())); PfxMatrix3 rotB = transpose(PfxMatrix3(stateB.getOrientation())); PfxVector3 axisInA = rotA * normalize(param.direction); joint.m_anchorA = rotA * (param.anchorPoint - stateA.getPosition()); joint.m_anchorB = rotB * (param.anchorPoint - stateB.getPosition()); PfxVector3 axis1, axis2; pfxGetPlaneSpace(axisInA, axis1, axis2 ); joint.m_frameA = PfxMatrix3(axisInA, axis1, axis2); joint.m_frameB = rotB * PfxMatrix3(stateA.getOrientation()) * joint.m_frameA; return SCE_PFX_OK; }
void pfxSetupBallJoint( PfxJoint &joint, const PfxRigidState &stateA, const PfxRigidState &stateB, PfxSolverBody &solverBodyA, PfxSolverBody &solverBodyB, PfxFloat timeStep ) { PfxVector3 rA = rotate(solverBodyA.m_orientation,joint.m_anchorA); PfxVector3 rB = rotate(solverBodyB.m_orientation,joint.m_anchorB); PfxVector3 vA = stateA.getLinearVelocity() + cross(stateA.getAngularVelocity(),rA); PfxVector3 vB = stateB.getLinearVelocity() + cross(stateB.getAngularVelocity(),rB); PfxVector3 vAB = vA-vB; PfxVector3 distance = (stateA.getPosition() + rA) - (stateB.getPosition() + rB); PfxMatrix3 worldFrameA,worldFrameB; worldFrameA = PfxMatrix3(solverBodyA.m_orientation) * joint.m_frameA; worldFrameB = PfxMatrix3(solverBodyB.m_orientation) * joint.m_frameB; // Linear Constraint PfxMatrix3 K = PfxMatrix3::scale(PfxVector3(solverBodyA.m_massInv + solverBodyB.m_massInv)) - crossMatrix(rA) * solverBodyA.m_inertiaInv * crossMatrix(rA) - crossMatrix(rB) * solverBodyB.m_inertiaInv * crossMatrix(rB); for(int c=0;c<3;c++) { PfxJointConstraint &jointConstraint = joint.m_constraints[c]; PfxConstraintRow &constraint = jointConstraint.m_constraintRow; PfxVector3 normal = worldFrameA[c]; PfxFloat posErr = dot(distance,-normal); PfxFloat lowerLimit = -jointConstraint.m_maxImpulse; PfxFloat upperLimit = jointConstraint.m_maxImpulse; PfxFloat velocityAmp = 1.0f; pfxCalcLinearLimit(jointConstraint,posErr,velocityAmp,lowerLimit,upperLimit); PfxFloat denom = dot(K*normal,normal); constraint.m_rhs = -velocityAmp*dot(vAB,normal); constraint.m_rhs -= (jointConstraint.m_bias * (-posErr)) / timeStep; constraint.m_rhs *= jointConstraint.m_weight/denom; constraint.m_jacDiagInv = jointConstraint.m_weight*velocityAmp/denom; constraint.m_lowerLimit = lowerLimit; constraint.m_upperLimit = upperLimit; pfxStoreVector3(normal,constraint.m_normal); } }