2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
btConeTwistConstraint is Copyright (c) 2007 Starbreeze Studios
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
|
|
|
Written by: Marcus Hennix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "btConeTwistConstraint.h"
|
|
|
|
#include "BulletDynamics/Dynamics/btRigidBody.h"
|
|
|
|
#include "LinearMath/btTransformUtil.h"
|
|
|
|
#include "LinearMath/btMinMax.h"
|
2019-06-11 11:18:05 +00:00
|
|
|
#include <cmath>
|
2017-08-01 12:30:58 +00:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
//#define CONETWIST_USE_OBSOLETE_SOLVER true
|
|
|
|
#define CONETWIST_USE_OBSOLETE_SOLVER false
|
|
|
|
#define CONETWIST_DEF_FIX_THRESH btScalar(.05f)
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE btScalar computeAngularImpulseDenominator(const btVector3& axis, const btMatrix3x3& invInertiaWorld)
|
|
|
|
{
|
|
|
|
btVector3 vec = axis * invInertiaWorld;
|
|
|
|
return axis.dot(vec);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btConeTwistConstraint::btConeTwistConstraint(btRigidBody& rbA, btRigidBody& rbB,
|
|
|
|
const btTransform& rbAFrame, const btTransform& rbBFrame)
|
|
|
|
: btTypedConstraint(CONETWIST_CONSTRAINT_TYPE, rbA, rbB), m_rbAFrame(rbAFrame), m_rbBFrame(rbBFrame), m_angularOnly(false), m_useSolveConstraintObsolete(CONETWIST_USE_OBSOLETE_SOLVER)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btConeTwistConstraint::btConeTwistConstraint(btRigidBody& rbA, const btTransform& rbAFrame)
|
|
|
|
: btTypedConstraint(CONETWIST_CONSTRAINT_TYPE, rbA), m_rbAFrame(rbAFrame), m_angularOnly(false), m_useSolveConstraintObsolete(CONETWIST_USE_OBSOLETE_SOLVER)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_rbBFrame = m_rbAFrame;
|
|
|
|
m_rbBFrame.setOrigin(btVector3(0., 0., 0.));
|
2019-01-03 13:26:51 +00:00
|
|
|
init();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void btConeTwistConstraint::init()
|
|
|
|
{
|
|
|
|
m_angularOnly = false;
|
|
|
|
m_solveTwistLimit = false;
|
|
|
|
m_solveSwingLimit = false;
|
|
|
|
m_bMotorEnabled = false;
|
|
|
|
m_maxMotorImpulse = btScalar(-1);
|
|
|
|
|
|
|
|
setLimit(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
|
|
|
|
m_damping = btScalar(0.01);
|
|
|
|
m_fixThresh = CONETWIST_DEF_FIX_THRESH;
|
|
|
|
m_flags = 0;
|
|
|
|
m_linCFM = btScalar(0.f);
|
|
|
|
m_linERP = btScalar(0.7f);
|
|
|
|
m_angCFM = btScalar(0.f);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::getInfo1(btConstraintInfo1* info)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (m_useSolveConstraintObsolete)
|
|
|
|
{
|
|
|
|
info->m_numConstraintRows = 0;
|
|
|
|
info->nub = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
info->m_numConstraintRows = 3;
|
|
|
|
info->nub = 3;
|
2019-01-03 13:26:51 +00:00
|
|
|
calcAngleInfo2(m_rbA.getCenterOfMassTransform(), m_rbB.getCenterOfMassTransform(), m_rbA.getInvInertiaTensorWorld(), m_rbB.getInvInertiaTensorWorld());
|
|
|
|
if (m_solveSwingLimit)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->m_numConstraintRows++;
|
|
|
|
info->nub--;
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->m_numConstraintRows++;
|
|
|
|
info->nub--;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_solveTwistLimit)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->m_numConstraintRows++;
|
|
|
|
info->nub--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::getInfo1NonVirtual(btConstraintInfo1* info)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//always reserve 6 rows: object transform is not available on SPU
|
|
|
|
info->m_numConstraintRows = 6;
|
|
|
|
info->nub = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::getInfo2(btConstraintInfo2* info)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
getInfo2NonVirtual(info, m_rbA.getCenterOfMassTransform(), m_rbB.getCenterOfMassTransform(), m_rbA.getInvInertiaTensorWorld(), m_rbB.getInvInertiaTensorWorld());
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::getInfo2NonVirtual(btConstraintInfo2* info, const btTransform& transA, const btTransform& transB, const btMatrix3x3& invInertiaWorldA, const btMatrix3x3& invInertiaWorldB)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
calcAngleInfo2(transA, transB, invInertiaWorldA, invInertiaWorldB);
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btAssert(!m_useSolveConstraintObsolete);
|
2019-01-03 13:26:51 +00:00
|
|
|
// set jacobian
|
|
|
|
info->m_J1linearAxis[0] = 1;
|
|
|
|
info->m_J1linearAxis[info->rowskip + 1] = 1;
|
|
|
|
info->m_J1linearAxis[2 * info->rowskip + 2] = 1;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 a1 = transA.getBasis() * m_rbAFrame.getOrigin();
|
|
|
|
{
|
|
|
|
btVector3* angular0 = (btVector3*)(info->m_J1angularAxis);
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3* angular1 = (btVector3*)(info->m_J1angularAxis + info->rowskip);
|
|
|
|
btVector3* angular2 = (btVector3*)(info->m_J1angularAxis + 2 * info->rowskip);
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 a1neg = -a1;
|
2019-01-03 13:26:51 +00:00
|
|
|
a1neg.getSkewSymmetricMatrix(angular0, angular1, angular2);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
info->m_J2linearAxis[0] = -1;
|
|
|
|
info->m_J2linearAxis[info->rowskip + 1] = -1;
|
|
|
|
info->m_J2linearAxis[2 * info->rowskip + 2] = -1;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 a2 = transB.getBasis() * m_rbBFrame.getOrigin();
|
|
|
|
{
|
|
|
|
btVector3* angular0 = (btVector3*)(info->m_J2angularAxis);
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3* angular1 = (btVector3*)(info->m_J2angularAxis + info->rowskip);
|
|
|
|
btVector3* angular2 = (btVector3*)(info->m_J2angularAxis + 2 * info->rowskip);
|
|
|
|
a2.getSkewSymmetricMatrix(angular0, angular1, angular2);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
// set right hand side
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar linERP = (m_flags & BT_CONETWIST_FLAGS_LIN_ERP) ? m_linERP : info->erp;
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar k = info->fps * linERP;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
info->m_constraintError[j * info->rowskip] = k * (a2[j] + transB.getOrigin()[j] - a1[j] - transA.getOrigin()[j]);
|
|
|
|
info->m_lowerLimit[j * info->rowskip] = -SIMD_INFINITY;
|
|
|
|
info->m_upperLimit[j * info->rowskip] = SIMD_INFINITY;
|
|
|
|
if (m_flags & BT_CONETWIST_FLAGS_LIN_CFM)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
info->cfm[j * info->rowskip] = m_linCFM;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
int row = 3;
|
2019-01-03 13:26:51 +00:00
|
|
|
int srow = row * info->rowskip;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 ax1;
|
|
|
|
// angular limits
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_solveSwingLimit)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar* J1 = info->m_J1angularAxis;
|
|
|
|
btScalar* J2 = info->m_J2angularAxis;
|
|
|
|
if ((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btTransform trA = transA * m_rbAFrame;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 p = trA.getBasis().getColumn(1);
|
|
|
|
btVector3 q = trA.getBasis().getColumn(2);
|
|
|
|
int srow1 = srow + info->rowskip;
|
2019-01-03 13:26:51 +00:00
|
|
|
J1[srow + 0] = p[0];
|
|
|
|
J1[srow + 1] = p[1];
|
|
|
|
J1[srow + 2] = p[2];
|
|
|
|
J1[srow1 + 0] = q[0];
|
|
|
|
J1[srow1 + 1] = q[1];
|
|
|
|
J1[srow1 + 2] = q[2];
|
|
|
|
J2[srow + 0] = -p[0];
|
|
|
|
J2[srow + 1] = -p[1];
|
|
|
|
J2[srow + 2] = -p[2];
|
|
|
|
J2[srow1 + 0] = -q[0];
|
|
|
|
J2[srow1 + 1] = -q[1];
|
|
|
|
J2[srow1 + 2] = -q[2];
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar fact = info->fps * m_relaxationFactor;
|
2019-01-03 13:26:51 +00:00
|
|
|
info->m_constraintError[srow] = fact * m_swingAxis.dot(p);
|
|
|
|
info->m_constraintError[srow1] = fact * m_swingAxis.dot(q);
|
2017-08-01 12:30:58 +00:00
|
|
|
info->m_lowerLimit[srow] = -SIMD_INFINITY;
|
|
|
|
info->m_upperLimit[srow] = SIMD_INFINITY;
|
|
|
|
info->m_lowerLimit[srow1] = -SIMD_INFINITY;
|
|
|
|
info->m_upperLimit[srow1] = SIMD_INFINITY;
|
|
|
|
srow = srow1 + info->rowskip;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ax1 = m_swingAxis * m_relaxationFactor * m_relaxationFactor;
|
2019-01-03 13:26:51 +00:00
|
|
|
J1[srow + 0] = ax1[0];
|
|
|
|
J1[srow + 1] = ax1[1];
|
|
|
|
J1[srow + 2] = ax1[2];
|
|
|
|
J2[srow + 0] = -ax1[0];
|
|
|
|
J2[srow + 1] = -ax1[1];
|
|
|
|
J2[srow + 2] = -ax1[2];
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar k = info->fps * m_biasFactor;
|
|
|
|
|
|
|
|
info->m_constraintError[srow] = k * m_swingCorrection;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_flags & BT_CONETWIST_FLAGS_ANG_CFM)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->cfm[srow] = m_angCFM;
|
|
|
|
}
|
|
|
|
// m_swingCorrection is always positive or 0
|
|
|
|
info->m_lowerLimit[srow] = 0;
|
|
|
|
info->m_upperLimit[srow] = (m_bMotorEnabled && m_maxMotorImpulse >= 0.0f) ? m_maxMotorImpulse : SIMD_INFINITY;
|
|
|
|
srow += info->rowskip;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_solveTwistLimit)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
ax1 = m_twistAxis * m_relaxationFactor * m_relaxationFactor;
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar* J1 = info->m_J1angularAxis;
|
|
|
|
btScalar* J2 = info->m_J2angularAxis;
|
|
|
|
J1[srow + 0] = ax1[0];
|
|
|
|
J1[srow + 1] = ax1[1];
|
|
|
|
J1[srow + 2] = ax1[2];
|
|
|
|
J2[srow + 0] = -ax1[0];
|
|
|
|
J2[srow + 1] = -ax1[1];
|
|
|
|
J2[srow + 2] = -ax1[2];
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar k = info->fps * m_biasFactor;
|
|
|
|
info->m_constraintError[srow] = k * m_twistCorrection;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_flags & BT_CONETWIST_FLAGS_ANG_CFM)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->cfm[srow] = m_angCFM;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_twistSpan > 0.0f)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_twistCorrection > 0.0f)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
info->m_lowerLimit[srow] = 0;
|
|
|
|
info->m_upperLimit[srow] = SIMD_INFINITY;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
info->m_lowerLimit[srow] = -SIMD_INFINITY;
|
|
|
|
info->m_upperLimit[srow] = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->m_lowerLimit[srow] = -SIMD_INFINITY;
|
|
|
|
info->m_upperLimit[srow] = SIMD_INFINITY;
|
|
|
|
}
|
|
|
|
srow += info->rowskip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::buildJacobian()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (m_useSolveConstraintObsolete)
|
|
|
|
{
|
|
|
|
m_appliedImpulse = btScalar(0.);
|
|
|
|
m_accTwistLimitImpulse = btScalar(0.);
|
|
|
|
m_accSwingLimitImpulse = btScalar(0.);
|
2019-01-03 13:26:51 +00:00
|
|
|
m_accMotorImpulse = btVector3(0., 0., 0.);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (!m_angularOnly)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 pivotAInW = m_rbA.getCenterOfMassTransform() * m_rbAFrame.getOrigin();
|
|
|
|
btVector3 pivotBInW = m_rbB.getCenterOfMassTransform() * m_rbBFrame.getOrigin();
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 relPos = pivotBInW - pivotAInW;
|
|
|
|
|
|
|
|
btVector3 normal[3];
|
|
|
|
if (relPos.length2() > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
normal[0] = relPos.normalized();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
normal[0].setValue(btScalar(1.0), 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
btPlaneSpace1(normal[0], normal[1], normal[2]);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
new (&m_jac[i]) btJacobianEntry(
|
2019-01-03 13:26:51 +00:00
|
|
|
m_rbA.getCenterOfMassTransform().getBasis().transpose(),
|
|
|
|
m_rbB.getCenterOfMassTransform().getBasis().transpose(),
|
|
|
|
pivotAInW - m_rbA.getCenterOfMassPosition(),
|
|
|
|
pivotBInW - m_rbB.getCenterOfMassPosition(),
|
|
|
|
normal[i],
|
|
|
|
m_rbA.getInvInertiaDiagLocal(),
|
|
|
|
m_rbA.getInvMass(),
|
|
|
|
m_rbB.getInvInertiaDiagLocal(),
|
|
|
|
m_rbB.getInvMass());
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
calcAngleInfo2(m_rbA.getCenterOfMassTransform(), m_rbB.getCenterOfMassTransform(), m_rbA.getInvInertiaTensorWorld(), m_rbB.getInvInertiaTensorWorld());
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::solveConstraintObsolete(btSolverBody& bodyA, btSolverBody& bodyB, btScalar timeStep)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
#ifndef __SPU__
|
2017-08-01 12:30:58 +00:00
|
|
|
if (m_useSolveConstraintObsolete)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 pivotAInW = m_rbA.getCenterOfMassTransform() * m_rbAFrame.getOrigin();
|
|
|
|
btVector3 pivotBInW = m_rbB.getCenterOfMassTransform() * m_rbBFrame.getOrigin();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btScalar tau = btScalar(0.3);
|
|
|
|
|
|
|
|
//linear part
|
|
|
|
if (!m_angularOnly)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 rel_pos1 = pivotAInW - m_rbA.getCenterOfMassPosition();
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 rel_pos2 = pivotBInW - m_rbB.getCenterOfMassPosition();
|
|
|
|
|
|
|
|
btVector3 vel1;
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalGetVelocityInLocalPointObsolete(rel_pos1, vel1);
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 vel2;
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyB.internalGetVelocityInLocalPointObsolete(rel_pos2, vel2);
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 vel = vel1 - vel2;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
const btVector3& normal = m_jac[i].m_linearJointAxis;
|
|
|
|
btScalar jacDiagABInv = btScalar(1.) / m_jac[i].getDiagonal();
|
|
|
|
|
|
|
|
btScalar rel_vel;
|
|
|
|
rel_vel = normal.dot(vel);
|
|
|
|
//positional error (zeroth order error)
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal
|
|
|
|
btScalar impulse = depth * tau / timeStep * jacDiagABInv - rel_vel * jacDiagABInv;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_appliedImpulse += impulse;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 ftorqueAxis1 = rel_pos1.cross(normal);
|
|
|
|
btVector3 ftorqueAxis2 = rel_pos2.cross(normal);
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalApplyImpulse(normal * m_rbA.getInvMass(), m_rbA.getInvInertiaTensorWorld() * ftorqueAxis1, impulse);
|
|
|
|
bodyB.internalApplyImpulse(normal * m_rbB.getInvMass(), m_rbB.getInvInertiaTensorWorld() * ftorqueAxis2, -impulse);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply motor
|
|
|
|
if (m_bMotorEnabled)
|
|
|
|
{
|
|
|
|
// compute current and predicted transforms
|
|
|
|
btTransform trACur = m_rbA.getCenterOfMassTransform();
|
|
|
|
btTransform trBCur = m_rbB.getCenterOfMassTransform();
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 omegaA;
|
|
|
|
bodyA.internalGetAngularVelocity(omegaA);
|
|
|
|
btVector3 omegaB;
|
|
|
|
bodyB.internalGetAngularVelocity(omegaB);
|
|
|
|
btTransform trAPred;
|
|
|
|
trAPred.setIdentity();
|
|
|
|
btVector3 zerovec(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
btTransformUtil::integrateTransform(
|
|
|
|
trACur, zerovec, omegaA, timeStep, trAPred);
|
2019-01-03 13:26:51 +00:00
|
|
|
btTransform trBPred;
|
|
|
|
trBPred.setIdentity();
|
2017-08-01 12:30:58 +00:00
|
|
|
btTransformUtil::integrateTransform(
|
|
|
|
trBCur, zerovec, omegaB, timeStep, trBPred);
|
|
|
|
|
|
|
|
// compute desired transforms in world
|
|
|
|
btTransform trPose(m_qTarget);
|
|
|
|
btTransform trABDes = m_rbBFrame * trPose * m_rbAFrame.inverse();
|
|
|
|
btTransform trADes = trBPred * trABDes;
|
|
|
|
btTransform trBDes = trAPred * trABDes.inverse();
|
|
|
|
|
|
|
|
// compute desired omegas in world
|
|
|
|
btVector3 omegaADes, omegaBDes;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
btTransformUtil::calculateVelocity(trACur, trADes, timeStep, zerovec, omegaADes);
|
|
|
|
btTransformUtil::calculateVelocity(trBCur, trBDes, timeStep, zerovec, omegaBDes);
|
|
|
|
|
|
|
|
// compute delta omegas
|
|
|
|
btVector3 dOmegaA = omegaADes - omegaA;
|
|
|
|
btVector3 dOmegaB = omegaBDes - omegaB;
|
|
|
|
|
|
|
|
// compute weighted avg axis of dOmega (weighting based on inertias)
|
|
|
|
btVector3 axisA, axisB;
|
|
|
|
btScalar kAxisAInv = 0, kAxisBInv = 0;
|
|
|
|
|
|
|
|
if (dOmegaA.length2() > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
axisA = dOmegaA.normalized();
|
|
|
|
kAxisAInv = getRigidBodyA().computeAngularImpulseDenominator(axisA);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dOmegaB.length2() > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
axisB = dOmegaB.normalized();
|
|
|
|
kAxisBInv = getRigidBodyB().computeAngularImpulseDenominator(axisB);
|
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 avgAxis = kAxisAInv * axisA + kAxisBInv * axisB;
|
|
|
|
|
|
|
|
static bool bDoTorque = true;
|
|
|
|
if (bDoTorque && avgAxis.length2() > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
avgAxis.normalize();
|
|
|
|
kAxisAInv = getRigidBodyA().computeAngularImpulseDenominator(avgAxis);
|
|
|
|
kAxisBInv = getRigidBodyB().computeAngularImpulseDenominator(avgAxis);
|
|
|
|
btScalar kInvCombined = kAxisAInv + kAxisBInv;
|
|
|
|
|
|
|
|
btVector3 impulse = (kAxisAInv * dOmegaA - kAxisBInv * dOmegaB) /
|
|
|
|
(kInvCombined * kInvCombined);
|
|
|
|
|
|
|
|
if (m_maxMotorImpulse >= 0)
|
|
|
|
{
|
|
|
|
btScalar fMaxImpulse = m_maxMotorImpulse;
|
|
|
|
if (m_bNormalizedMotorStrength)
|
2019-01-03 13:26:51 +00:00
|
|
|
fMaxImpulse = fMaxImpulse / kAxisAInv;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
btVector3 newUnclampedAccImpulse = m_accMotorImpulse + impulse;
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar newUnclampedMag = newUnclampedAccImpulse.length();
|
2017-08-01 12:30:58 +00:00
|
|
|
if (newUnclampedMag > fMaxImpulse)
|
|
|
|
{
|
|
|
|
newUnclampedAccImpulse.normalize();
|
|
|
|
newUnclampedAccImpulse *= fMaxImpulse;
|
|
|
|
impulse = newUnclampedAccImpulse - m_accMotorImpulse;
|
|
|
|
}
|
|
|
|
m_accMotorImpulse += impulse;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar impulseMag = impulse.length();
|
|
|
|
btVector3 impulseAxis = impulse / impulseMag;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalApplyImpulse(btVector3(0, 0, 0), m_rbA.getInvInertiaTensorWorld() * impulseAxis, impulseMag);
|
|
|
|
bodyB.internalApplyImpulse(btVector3(0, 0, 0), m_rbB.getInvInertiaTensorWorld() * impulseAxis, -impulseMag);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (m_damping > SIMD_EPSILON) // no motor: do a little damping
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 angVelA;
|
|
|
|
bodyA.internalGetAngularVelocity(angVelA);
|
|
|
|
btVector3 angVelB;
|
|
|
|
bodyB.internalGetAngularVelocity(angVelB);
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 relVel = angVelB - angVelA;
|
|
|
|
if (relVel.length2() > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
btVector3 relVelAxis = relVel.normalized();
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar m_kDamping = btScalar(1.) /
|
|
|
|
(getRigidBodyA().computeAngularImpulseDenominator(relVelAxis) +
|
|
|
|
getRigidBodyB().computeAngularImpulseDenominator(relVelAxis));
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 impulse = m_damping * m_kDamping * relVel;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar impulseMag = impulse.length();
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 impulseAxis = impulse / impulseMag;
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalApplyImpulse(btVector3(0, 0, 0), m_rbA.getInvInertiaTensorWorld() * impulseAxis, impulseMag);
|
|
|
|
bodyB.internalApplyImpulse(btVector3(0, 0, 0), m_rbB.getInvInertiaTensorWorld() * impulseAxis, -impulseMag);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// joint limits
|
|
|
|
{
|
|
|
|
///solve angular part
|
|
|
|
btVector3 angVelA;
|
|
|
|
bodyA.internalGetAngularVelocity(angVelA);
|
|
|
|
btVector3 angVelB;
|
|
|
|
bodyB.internalGetAngularVelocity(angVelB);
|
|
|
|
|
|
|
|
// solve swing limit
|
|
|
|
if (m_solveSwingLimit)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar amplitude = m_swingLimitRatio * m_swingCorrection * m_biasFactor / timeStep;
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar relSwingVel = (angVelB - angVelA).dot(m_swingAxis);
|
|
|
|
if (relSwingVel > 0)
|
|
|
|
amplitude += m_swingLimitRatio * relSwingVel * m_relaxationFactor;
|
|
|
|
btScalar impulseMag = amplitude * m_kSwing;
|
|
|
|
|
|
|
|
// Clamp the accumulated impulse
|
|
|
|
btScalar temp = m_accSwingLimitImpulse;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_accSwingLimitImpulse = btMax(m_accSwingLimitImpulse + impulseMag, btScalar(0.0));
|
2017-08-01 12:30:58 +00:00
|
|
|
impulseMag = m_accSwingLimitImpulse - temp;
|
|
|
|
|
|
|
|
btVector3 impulse = m_swingAxis * impulseMag;
|
|
|
|
|
|
|
|
// don't let cone response affect twist
|
|
|
|
// (this can happen since body A's twist doesn't match body B's AND we use an elliptical cone limit)
|
|
|
|
{
|
|
|
|
btVector3 impulseTwistCouple = impulse.dot(m_twistAxisA) * m_twistAxisA;
|
|
|
|
btVector3 impulseNoTwistCouple = impulse - impulseTwistCouple;
|
|
|
|
impulse = impulseNoTwistCouple;
|
|
|
|
}
|
|
|
|
|
|
|
|
impulseMag = impulse.length();
|
|
|
|
btVector3 noTwistSwingAxis = impulse / impulseMag;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalApplyImpulse(btVector3(0, 0, 0), m_rbA.getInvInertiaTensorWorld() * noTwistSwingAxis, impulseMag);
|
|
|
|
bodyB.internalApplyImpulse(btVector3(0, 0, 0), m_rbB.getInvInertiaTensorWorld() * noTwistSwingAxis, -impulseMag);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// solve twist limit
|
|
|
|
if (m_solveTwistLimit)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar amplitude = m_twistLimitRatio * m_twistCorrection * m_biasFactor / timeStep;
|
|
|
|
btScalar relTwistVel = (angVelB - angVelA).dot(m_twistAxis);
|
|
|
|
if (relTwistVel > 0) // only damp when moving towards limit (m_twistAxis flipping is important)
|
2017-08-01 12:30:58 +00:00
|
|
|
amplitude += m_twistLimitRatio * relTwistVel * m_relaxationFactor;
|
|
|
|
btScalar impulseMag = amplitude * m_kTwist;
|
|
|
|
|
|
|
|
// Clamp the accumulated impulse
|
|
|
|
btScalar temp = m_accTwistLimitImpulse;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_accTwistLimitImpulse = btMax(m_accTwistLimitImpulse + impulseMag, btScalar(0.0));
|
2017-08-01 12:30:58 +00:00
|
|
|
impulseMag = m_accTwistLimitImpulse - temp;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// btVector3 impulse = m_twistAxis * impulseMag;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bodyA.internalApplyImpulse(btVector3(0, 0, 0), m_rbA.getInvInertiaTensorWorld() * m_twistAxis, impulseMag);
|
|
|
|
bodyB.internalApplyImpulse(btVector3(0, 0, 0), m_rbB.getInvInertiaTensorWorld() * m_twistAxis, -impulseMag);
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(0);
|
|
|
|
#endif //__SPU__
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::updateRHS(btScalar timeStep)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
(void)timeStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __SPU__
|
|
|
|
void btConeTwistConstraint::calcAngleInfo()
|
|
|
|
{
|
|
|
|
m_swingCorrection = btScalar(0.);
|
|
|
|
m_twistLimitSign = btScalar(0.);
|
|
|
|
m_solveTwistLimit = false;
|
|
|
|
m_solveSwingLimit = false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 b1Axis1(0, 0, 0), b1Axis2(0, 0, 0), b1Axis3(0, 0, 0);
|
|
|
|
btVector3 b2Axis1(0, 0, 0), b2Axis2(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
b1Axis1 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(0);
|
|
|
|
b2Axis1 = getRigidBodyB().getCenterOfMassTransform().getBasis() * this->m_rbBFrame.getBasis().getColumn(0);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar swing1 = btScalar(0.), swing2 = btScalar(0.);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar swx = btScalar(0.), swy = btScalar(0.);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar thresh = btScalar(10.);
|
|
|
|
btScalar fact;
|
|
|
|
|
|
|
|
// Get Frame into world space
|
|
|
|
if (m_swingSpan1 >= btScalar(0.05f))
|
|
|
|
{
|
|
|
|
b1Axis2 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(1);
|
|
|
|
swx = b2Axis1.dot(b1Axis1);
|
|
|
|
swy = b2Axis1.dot(b1Axis2);
|
2019-01-03 13:26:51 +00:00
|
|
|
swing1 = btAtan2Fast(swy, swx);
|
|
|
|
fact = (swy * swy + swx * swx) * thresh * thresh;
|
2017-08-01 12:30:58 +00:00
|
|
|
fact = fact / (fact + btScalar(1.0));
|
2019-01-03 13:26:51 +00:00
|
|
|
swing1 *= fact;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_swingSpan2 >= btScalar(0.05f))
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
b1Axis3 = getRigidBodyA().getCenterOfMassTransform().getBasis() * this->m_rbAFrame.getBasis().getColumn(2);
|
2017-08-01 12:30:58 +00:00
|
|
|
swx = b2Axis1.dot(b1Axis1);
|
|
|
|
swy = b2Axis1.dot(b1Axis3);
|
2019-01-03 13:26:51 +00:00
|
|
|
swing2 = btAtan2Fast(swy, swx);
|
|
|
|
fact = (swy * swy + swx * swx) * thresh * thresh;
|
2017-08-01 12:30:58 +00:00
|
|
|
fact = fact / (fact + btScalar(1.0));
|
2019-01-03 13:26:51 +00:00
|
|
|
swing2 *= fact;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar RMaxAngle1Sq = 1.0f / (m_swingSpan1 * m_swingSpan1);
|
|
|
|
btScalar RMaxAngle2Sq = 1.0f / (m_swingSpan2 * m_swingSpan2);
|
|
|
|
btScalar EllipseAngle = btFabs(swing1 * swing1) * RMaxAngle1Sq + btFabs(swing2 * swing2) * RMaxAngle2Sq;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (EllipseAngle > 1.0f)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_swingCorrection = EllipseAngle - 1.0f;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_solveSwingLimit = true;
|
|
|
|
// Calculate necessary axis & factors
|
2019-01-03 13:26:51 +00:00
|
|
|
m_swingAxis = b2Axis1.cross(b1Axis2 * b2Axis1.dot(b1Axis2) + b1Axis3 * b2Axis1.dot(b1Axis3));
|
2017-08-01 12:30:58 +00:00
|
|
|
m_swingAxis.normalize();
|
|
|
|
btScalar swingAxisSign = (b2Axis1.dot(b1Axis1) >= 0.0f) ? 1.0f : -1.0f;
|
|
|
|
m_swingAxis *= swingAxisSign;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Twist limits
|
|
|
|
if (m_twistSpan >= btScalar(0.))
|
|
|
|
{
|
|
|
|
btVector3 b2Axis2 = getRigidBodyB().getCenterOfMassTransform().getBasis() * this->m_rbBFrame.getBasis().getColumn(1);
|
2019-01-03 13:26:51 +00:00
|
|
|
btQuaternion rotationArc = shortestArcQuat(b2Axis1, b1Axis1);
|
|
|
|
btVector3 TwistRef = quatRotate(rotationArc, b2Axis2);
|
|
|
|
btScalar twist = btAtan2Fast(TwistRef.dot(b1Axis3), TwistRef.dot(b1Axis2));
|
2017-08-01 12:30:58 +00:00
|
|
|
m_twistAngle = twist;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// btScalar lockedFreeFactor = (m_twistSpan > btScalar(0.05f)) ? m_limitSoftness : btScalar(0.);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar lockedFreeFactor = (m_twistSpan > btScalar(0.05f)) ? btScalar(1.0f) : btScalar(0.);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (twist <= -m_twistSpan * lockedFreeFactor)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_twistCorrection = -(twist + m_twistSpan);
|
|
|
|
m_solveTwistLimit = true;
|
|
|
|
m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
|
|
|
|
m_twistAxis.normalize();
|
|
|
|
m_twistAxis *= -1.0f;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (twist > m_twistSpan * lockedFreeFactor)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_twistCorrection = (twist - m_twistSpan);
|
|
|
|
m_solveTwistLimit = true;
|
|
|
|
m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
|
|
|
|
m_twistAxis.normalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //__SPU__
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
static btVector3 vTwist(1, 0, 0); // twist axis in constraint's space
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::calcAngleInfo2(const btTransform& transA, const btTransform& transB, const btMatrix3x3& invInertiaWorldA, const btMatrix3x3& invInertiaWorldB)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_swingCorrection = btScalar(0.);
|
|
|
|
m_twistLimitSign = btScalar(0.);
|
|
|
|
m_solveTwistLimit = false;
|
|
|
|
m_solveSwingLimit = false;
|
|
|
|
// compute rotation of A wrt B (in constraint space)
|
|
|
|
if (m_bMotorEnabled && (!m_useSolveConstraintObsolete))
|
2019-01-03 13:26:51 +00:00
|
|
|
{ // it is assumed that setMotorTarget() was alredy called
|
2017-08-01 12:30:58 +00:00
|
|
|
// and motor target m_qTarget is within constraint limits
|
|
|
|
// TODO : split rotation to pure swing and pure twist
|
|
|
|
// compute desired transforms in world
|
|
|
|
btTransform trPose(m_qTarget);
|
|
|
|
btTransform trA = transA * m_rbAFrame;
|
|
|
|
btTransform trB = transB * m_rbBFrame;
|
|
|
|
btTransform trDeltaAB = trB * trPose * trA.inverse();
|
|
|
|
btQuaternion qDeltaAB = trDeltaAB.getRotation();
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 swingAxis = btVector3(qDeltaAB.x(), qDeltaAB.y(), qDeltaAB.z());
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar swingAxisLen2 = swingAxis.length2();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (btFuzzyZero(swingAxisLen2))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
m_swingAxis = swingAxis;
|
|
|
|
m_swingAxis.normalize();
|
|
|
|
m_swingCorrection = qDeltaAB.getAngle();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!btFuzzyZero(m_swingCorrection))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_solveSwingLimit = true;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// compute rotation of A wrt B (in constraint space)
|
|
|
|
btQuaternion qA = transA.getRotation() * m_rbAFrame.getRotation();
|
|
|
|
btQuaternion qB = transB.getRotation() * m_rbBFrame.getRotation();
|
|
|
|
btQuaternion qAB = qB.inverse() * qA;
|
|
|
|
// split rotation into cone and twist
|
|
|
|
// (all this is done from B's perspective. Maybe I should be averaging axes...)
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 vConeNoTwist = quatRotate(qAB, vTwist);
|
|
|
|
vConeNoTwist.normalize();
|
|
|
|
btQuaternion qABCone = shortestArcQuat(vTwist, vConeNoTwist);
|
|
|
|
qABCone.normalize();
|
|
|
|
btQuaternion qABTwist = qABCone.inverse() * qAB;
|
|
|
|
qABTwist.normalize();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (m_swingSpan1 >= m_fixThresh && m_swingSpan2 >= m_fixThresh)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar swingAngle, swingLimit = 0;
|
|
|
|
btVector3 swingAxis;
|
2017-08-01 12:30:58 +00:00
|
|
|
computeConeLimitInfo(qABCone, swingAngle, swingAxis, swingLimit);
|
|
|
|
|
|
|
|
if (swingAngle > swingLimit * m_limitSoftness)
|
|
|
|
{
|
|
|
|
m_solveSwingLimit = true;
|
|
|
|
|
|
|
|
// compute limit ratio: 0->1, where
|
|
|
|
// 0 == beginning of soft limit
|
|
|
|
// 1 == hard/real limit
|
|
|
|
m_swingLimitRatio = 1.f;
|
|
|
|
if (swingAngle < swingLimit && m_limitSoftness < 1.f - SIMD_EPSILON)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_swingLimitRatio = (swingAngle - swingLimit * m_limitSoftness) /
|
2017-08-01 12:30:58 +00:00
|
|
|
(swingLimit - swingLimit * m_limitSoftness);
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// swing correction tries to get back to soft limit
|
|
|
|
m_swingCorrection = swingAngle - (swingLimit * m_limitSoftness);
|
|
|
|
|
|
|
|
// adjustment of swing axis (based on ellipse normal)
|
|
|
|
adjustSwingAxisToUseEllipseNormal(swingAxis);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// Calculate necessary axis & factors
|
2017-08-01 12:30:58 +00:00
|
|
|
m_swingAxis = quatRotate(qB, -swingAxis);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_twistAxisA.setValue(0, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
m_kSwing = btScalar(1.) /
|
|
|
|
(computeAngularImpulseDenominator(m_swingAxis, invInertiaWorldA) +
|
|
|
|
computeAngularImpulseDenominator(m_swingAxis, invInertiaWorldB));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// you haven't set any limits;
|
|
|
|
// or you're trying to set at least one of the swing limits too small. (if so, do you really want a conetwist constraint?)
|
|
|
|
// anyway, we have either hinge or fixed joint
|
|
|
|
btVector3 ivA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(0);
|
|
|
|
btVector3 jvA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(1);
|
|
|
|
btVector3 kvA = transA.getBasis() * m_rbAFrame.getBasis().getColumn(2);
|
|
|
|
btVector3 ivB = transB.getBasis() * m_rbBFrame.getBasis().getColumn(0);
|
|
|
|
btVector3 target;
|
|
|
|
btScalar x = ivB.dot(ivA);
|
|
|
|
btScalar y = ivB.dot(jvA);
|
|
|
|
btScalar z = ivB.dot(kvA);
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((m_swingSpan1 < m_fixThresh) && (m_swingSpan2 < m_fixThresh))
|
|
|
|
{ // fixed. We'll need to add one more row to constraint
|
|
|
|
if ((!btFuzzyZero(y)) || (!(btFuzzyZero(z))))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_solveSwingLimit = true;
|
|
|
|
m_swingAxis = -ivB.cross(ivA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_swingSpan1 < m_fixThresh)
|
|
|
|
{ // hinge around Y axis
|
|
|
|
// if(!(btFuzzyZero(y)))
|
|
|
|
if ((!(btFuzzyZero(x))) || (!(btFuzzyZero(z))))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_solveSwingLimit = true;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_swingSpan2 >= m_fixThresh)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
y = btScalar(0.f);
|
|
|
|
btScalar span2 = btAtan2(z, x);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (span2 > m_swingSpan2)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
x = btCos(m_swingSpan2);
|
|
|
|
z = btSin(m_swingSpan2);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (span2 < -m_swingSpan2)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
x = btCos(m_swingSpan2);
|
2017-08-01 12:30:58 +00:00
|
|
|
z = -btSin(m_swingSpan2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-01-03 13:26:51 +00:00
|
|
|
{ // hinge around Z axis
|
|
|
|
// if(!btFuzzyZero(z))
|
|
|
|
if ((!(btFuzzyZero(x))) || (!(btFuzzyZero(y))))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_solveSwingLimit = true;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_swingSpan1 >= m_fixThresh)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
z = btScalar(0.f);
|
|
|
|
btScalar span1 = btAtan2(y, x);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (span1 > m_swingSpan1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
x = btCos(m_swingSpan1);
|
|
|
|
y = btSin(m_swingSpan1);
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (span1 < -m_swingSpan1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
x = btCos(m_swingSpan1);
|
2017-08-01 12:30:58 +00:00
|
|
|
y = -btSin(m_swingSpan1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target[0] = x * ivA[0] + y * jvA[0] + z * kvA[0];
|
|
|
|
target[1] = x * ivA[1] + y * jvA[1] + z * kvA[1];
|
|
|
|
target[2] = x * ivA[2] + y * jvA[2] + z * kvA[2];
|
|
|
|
target.normalize();
|
|
|
|
m_swingAxis = -ivB.cross(target);
|
2019-01-03 13:26:51 +00:00
|
|
|
m_swingCorrection = m_swingAxis.length();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!btFuzzyZero(m_swingCorrection))
|
|
|
|
m_swingAxis.normalize();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_twistSpan >= btScalar(0.f))
|
|
|
|
{
|
|
|
|
btVector3 twistAxis;
|
|
|
|
computeTwistLimitInfo(qABTwist, m_twistAngle, twistAxis);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_twistAngle > m_twistSpan * m_limitSoftness)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_solveTwistLimit = true;
|
|
|
|
|
|
|
|
m_twistLimitRatio = 1.f;
|
|
|
|
if (m_twistAngle < m_twistSpan && m_limitSoftness < 1.f - SIMD_EPSILON)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_twistLimitRatio = (m_twistAngle - m_twistSpan * m_limitSoftness) /
|
|
|
|
(m_twistSpan - m_twistSpan * m_limitSoftness);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// twist correction tries to get back to soft limit
|
|
|
|
m_twistCorrection = m_twistAngle - (m_twistSpan * m_limitSoftness);
|
|
|
|
|
|
|
|
m_twistAxis = quatRotate(qB, -twistAxis);
|
|
|
|
|
|
|
|
m_kTwist = btScalar(1.) /
|
2019-01-03 13:26:51 +00:00
|
|
|
(computeAngularImpulseDenominator(m_twistAxis, invInertiaWorldA) +
|
|
|
|
computeAngularImpulseDenominator(m_twistAxis, invInertiaWorldB));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_solveSwingLimit)
|
|
|
|
m_twistAxisA = quatRotate(qA, -twistAxis);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_twistAngle = btScalar(0.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a cone rotation in constraint space, (pre: twist must already be removed)
|
|
|
|
// this method computes its corresponding swing angle and axis.
|
|
|
|
// more interestingly, it computes the cone/swing limit (angle) for this cone "pose".
|
|
|
|
void btConeTwistConstraint::computeConeLimitInfo(const btQuaternion& qCone,
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar& swingAngle, // out
|
|
|
|
btVector3& vSwingAxis, // out
|
|
|
|
btScalar& swingLimit) // out
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
swingAngle = qCone.getAngle();
|
|
|
|
if (swingAngle > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
vSwingAxis = btVector3(qCone.x(), qCone.y(), qCone.z());
|
|
|
|
vSwingAxis.normalize();
|
|
|
|
#if 0
|
|
|
|
// non-zero twist?! this should never happen.
|
|
|
|
btAssert(fabs(vSwingAxis.x()) <= SIMD_EPSILON));
|
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
// Compute limit for given swing. tricky:
|
|
|
|
// Given a swing axis, we're looking for the intersection with the bounding cone ellipse.
|
|
|
|
// (Since we're dealing with angles, this ellipse is embedded on the surface of a sphere.)
|
|
|
|
|
|
|
|
// For starters, compute the direction from center to surface of ellipse.
|
|
|
|
// This is just the perpendicular (ie. rotate 2D vector by PI/2) of the swing axis.
|
|
|
|
// (vSwingAxis is the cone rotation (in z,y); change vars and rotate to (x,y) coords.)
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar xEllipse = vSwingAxis.y();
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar yEllipse = -vSwingAxis.z();
|
|
|
|
|
|
|
|
// Now, we use the slope of the vector (using x/yEllipse) and find the length
|
|
|
|
// of the line that intersects the ellipse:
|
|
|
|
// x^2 y^2
|
|
|
|
// --- + --- = 1, where a and b are semi-major axes 2 and 1 respectively (ie. the limits)
|
|
|
|
// a^2 b^2
|
|
|
|
// Do the math and it should be clear.
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
swingLimit = m_swingSpan1; // if xEllipse == 0, we have a pure vSwingAxis.z rotation: just use swingspan1
|
2017-08-01 12:30:58 +00:00
|
|
|
if (fabs(xEllipse) > SIMD_EPSILON)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar surfaceSlope2 = (yEllipse * yEllipse) / (xEllipse * xEllipse);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar norm = 1 / (m_swingSpan2 * m_swingSpan2);
|
|
|
|
norm += surfaceSlope2 / (m_swingSpan1 * m_swingSpan1);
|
|
|
|
btScalar swingLimit2 = (1 + surfaceSlope2) / norm;
|
2019-06-11 11:18:05 +00:00
|
|
|
swingLimit = std::sqrt(swingLimit2);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test!
|
|
|
|
/*swingLimit = m_swingSpan2;
|
|
|
|
if (fabs(vSwingAxis.z()) > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
btScalar mag_2 = m_swingSpan1*m_swingSpan1 + m_swingSpan2*m_swingSpan2;
|
|
|
|
btScalar sinphi = m_swingSpan2 / sqrt(mag_2);
|
|
|
|
btScalar phi = asin(sinphi);
|
|
|
|
btScalar theta = atan2(fabs(vSwingAxis.y()),fabs(vSwingAxis.z()));
|
|
|
|
btScalar alpha = 3.14159f - theta - phi;
|
|
|
|
btScalar sinalpha = sin(alpha);
|
|
|
|
swingLimit = m_swingSpan1 * sinphi/sinalpha;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
else if (swingAngle < 0)
|
|
|
|
{
|
|
|
|
// this should never happen!
|
|
|
|
#if 0
|
|
|
|
btAssert(0);
|
|
|
|
#endif
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 btConeTwistConstraint::GetPointForAngle(btScalar fAngleInRadians, btScalar fLength) const
|
|
|
|
{
|
|
|
|
// compute x/y in ellipse using cone angle (0 -> 2*PI along surface of cone)
|
|
|
|
btScalar xEllipse = btCos(fAngleInRadians);
|
|
|
|
btScalar yEllipse = btSin(fAngleInRadians);
|
|
|
|
|
|
|
|
// Use the slope of the vector (using x/yEllipse) and find the length
|
|
|
|
// of the line that intersects the ellipse:
|
|
|
|
// x^2 y^2
|
|
|
|
// --- + --- = 1, where a and b are semi-major axes 2 and 1 respectively (ie. the limits)
|
|
|
|
// a^2 b^2
|
|
|
|
// Do the math and it should be clear.
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar swingLimit = m_swingSpan1; // if xEllipse == 0, just use axis b (1)
|
2017-08-01 12:30:58 +00:00
|
|
|
if (fabs(xEllipse) > SIMD_EPSILON)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar surfaceSlope2 = (yEllipse * yEllipse) / (xEllipse * xEllipse);
|
2017-08-01 12:30:58 +00:00
|
|
|
btScalar norm = 1 / (m_swingSpan2 * m_swingSpan2);
|
|
|
|
norm += surfaceSlope2 / (m_swingSpan1 * m_swingSpan1);
|
|
|
|
btScalar swingLimit2 = (1 + surfaceSlope2) / norm;
|
2019-06-11 11:18:05 +00:00
|
|
|
swingLimit = std::sqrt(swingLimit2);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert into point in constraint space:
|
|
|
|
// note: twist is x-axis, swing 1 and 2 are along the z and y axes respectively
|
|
|
|
btVector3 vSwingAxis(0, xEllipse, -yEllipse);
|
|
|
|
btQuaternion qSwing(vSwingAxis, swingLimit);
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 vPointInConstraintSpace(fLength, 0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
return quatRotate(qSwing, vPointInConstraintSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a twist rotation in constraint space, (pre: cone must already be removed)
|
|
|
|
// this method computes its corresponding angle and axis.
|
|
|
|
void btConeTwistConstraint::computeTwistLimitInfo(const btQuaternion& qTwist,
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar& twistAngle, // out
|
|
|
|
btVector3& vTwistAxis) // out
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btQuaternion qMinTwist = qTwist;
|
|
|
|
twistAngle = qTwist.getAngle();
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (twistAngle > SIMD_PI) // long way around. flip quat and recalculate.
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
qMinTwist = -(qTwist);
|
|
|
|
twistAngle = qMinTwist.getAngle();
|
|
|
|
}
|
|
|
|
if (twistAngle < 0)
|
|
|
|
{
|
|
|
|
// this should never happen
|
|
|
|
#if 0
|
|
|
|
btAssert(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
vTwistAxis = btVector3(qMinTwist.x(), qMinTwist.y(), qMinTwist.z());
|
|
|
|
if (twistAngle > SIMD_EPSILON)
|
|
|
|
vTwistAxis.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void btConeTwistConstraint::adjustSwingAxisToUseEllipseNormal(btVector3& vSwingAxis) const
|
|
|
|
{
|
|
|
|
// the swing axis is computed as the "twist-free" cone rotation,
|
|
|
|
// but the cone limit is not circular, but elliptical (if swingspan1 != swingspan2).
|
2019-01-03 13:26:51 +00:00
|
|
|
// so, if we're outside the limits, the closest way back inside the cone isn't
|
2017-08-01 12:30:58 +00:00
|
|
|
// along the vector back to the center. better (and more stable) to use the ellipse normal.
|
|
|
|
|
|
|
|
// convert swing axis to direction from center to surface of ellipse
|
|
|
|
// (ie. rotate 2D vector by PI/2)
|
|
|
|
btScalar y = -vSwingAxis.z();
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar z = vSwingAxis.y();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// do the math...
|
2019-01-03 13:26:51 +00:00
|
|
|
if (fabs(z) > SIMD_EPSILON) // avoid division by 0. and we don't need an update if z == 0.
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// compute gradient/normal of ellipse surface at current "point"
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar grad = y / z;
|
2017-08-01 12:30:58 +00:00
|
|
|
grad *= m_swingSpan2 / m_swingSpan1;
|
|
|
|
|
|
|
|
// adjust y/z to represent normal at point (instead of vector to point)
|
|
|
|
if (y > 0)
|
2019-01-03 13:26:51 +00:00
|
|
|
y = fabs(grad * z);
|
2017-08-01 12:30:58 +00:00
|
|
|
else
|
|
|
|
y = -fabs(grad * z);
|
|
|
|
|
|
|
|
// convert ellipse direction back to swing axis
|
|
|
|
vSwingAxis.setZ(-y);
|
2019-01-03 13:26:51 +00:00
|
|
|
vSwingAxis.setY(z);
|
2017-08-01 12:30:58 +00:00
|
|
|
vSwingAxis.normalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::setMotorTarget(const btQuaternion& q)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//btTransform trACur = m_rbA.getCenterOfMassTransform();
|
|
|
|
//btTransform trBCur = m_rbB.getCenterOfMassTransform();
|
2019-01-03 13:26:51 +00:00
|
|
|
// btTransform trABCur = trBCur.inverse() * trACur;
|
|
|
|
// btQuaternion qABCur = trABCur.getRotation();
|
|
|
|
// btTransform trConstraintCur = (trBCur * m_rbBFrame).inverse() * (trACur * m_rbAFrame);
|
2017-08-01 12:30:58 +00:00
|
|
|
//btQuaternion qConstraintCur = trConstraintCur.getRotation();
|
|
|
|
|
|
|
|
btQuaternion qConstraint = m_rbBFrame.getRotation().inverse() * q * m_rbAFrame.getRotation();
|
|
|
|
setMotorTargetInConstraintSpace(qConstraint);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::setMotorTargetInConstraintSpace(const btQuaternion& q)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_qTarget = q;
|
|
|
|
|
|
|
|
// clamp motor target to within limits
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar softness = 1.f; //m_limitSoftness;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// split into twist and cone
|
|
|
|
btVector3 vTwisted = quatRotate(m_qTarget, vTwist);
|
2019-01-03 13:26:51 +00:00
|
|
|
btQuaternion qTargetCone = shortestArcQuat(vTwist, vTwisted);
|
|
|
|
qTargetCone.normalize();
|
|
|
|
btQuaternion qTargetTwist = qTargetCone.inverse() * m_qTarget;
|
|
|
|
qTargetTwist.normalize();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// clamp cone
|
|
|
|
if (m_swingSpan1 >= btScalar(0.05f) && m_swingSpan2 >= btScalar(0.05f))
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar swingAngle, swingLimit;
|
|
|
|
btVector3 swingAxis;
|
2017-08-01 12:30:58 +00:00
|
|
|
computeConeLimitInfo(qTargetCone, swingAngle, swingAxis, swingLimit);
|
|
|
|
|
|
|
|
if (fabs(swingAngle) > SIMD_EPSILON)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (swingAngle > swingLimit * softness)
|
|
|
|
swingAngle = swingLimit * softness;
|
|
|
|
else if (swingAngle < -swingLimit * softness)
|
|
|
|
swingAngle = -swingLimit * softness;
|
2017-08-01 12:30:58 +00:00
|
|
|
qTargetCone = btQuaternion(swingAxis, swingAngle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clamp twist
|
|
|
|
if (m_twistSpan >= btScalar(0.05f))
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar twistAngle;
|
|
|
|
btVector3 twistAxis;
|
2017-08-01 12:30:58 +00:00
|
|
|
computeTwistLimitInfo(qTargetTwist, twistAngle, twistAxis);
|
|
|
|
|
|
|
|
if (fabs(twistAngle) > SIMD_EPSILON)
|
|
|
|
{
|
|
|
|
// eddy todo: limitSoftness used here???
|
2019-01-03 13:26:51 +00:00
|
|
|
if (twistAngle > m_twistSpan * softness)
|
|
|
|
twistAngle = m_twistSpan * softness;
|
|
|
|
else if (twistAngle < -m_twistSpan * softness)
|
|
|
|
twistAngle = -m_twistSpan * softness;
|
2017-08-01 12:30:58 +00:00
|
|
|
qTargetTwist = btQuaternion(twistAxis, twistAngle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_qTarget = qTargetCone * qTargetTwist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
|
2017-08-01 12:30:58 +00:00
|
|
|
///If no axis is provided, it uses the default axis for this constraint.
|
|
|
|
void btConeTwistConstraint::setParam(int num, btScalar value, int axis)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
switch (num)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
case BT_CONSTRAINT_ERP:
|
|
|
|
case BT_CONSTRAINT_STOP_ERP:
|
|
|
|
if ((axis >= 0) && (axis < 3))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_linERP = value;
|
|
|
|
m_flags |= BT_CONETWIST_FLAGS_LIN_ERP;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_biasFactor = value;
|
|
|
|
}
|
|
|
|
break;
|
2019-01-03 13:26:51 +00:00
|
|
|
case BT_CONSTRAINT_CFM:
|
|
|
|
case BT_CONSTRAINT_STOP_CFM:
|
|
|
|
if ((axis >= 0) && (axis < 3))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_linCFM = value;
|
|
|
|
m_flags |= BT_CONETWIST_FLAGS_LIN_CFM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_angCFM = value;
|
|
|
|
m_flags |= BT_CONETWIST_FLAGS_ANG_CFM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
btAssertConstrParams(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///return the local value of parameter
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar btConeTwistConstraint::getParam(int num, int axis) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btScalar retVal = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
switch (num)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
case BT_CONSTRAINT_ERP:
|
|
|
|
case BT_CONSTRAINT_STOP_ERP:
|
|
|
|
if ((axis >= 0) && (axis < 3))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_LIN_ERP);
|
|
|
|
retVal = m_linERP;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if ((axis >= 3) && (axis < 6))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
retVal = m_biasFactor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
btAssertConstrParams(0);
|
|
|
|
}
|
|
|
|
break;
|
2019-01-03 13:26:51 +00:00
|
|
|
case BT_CONSTRAINT_CFM:
|
|
|
|
case BT_CONSTRAINT_STOP_CFM:
|
|
|
|
if ((axis >= 0) && (axis < 3))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_LIN_CFM);
|
|
|
|
retVal = m_linCFM;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if ((axis >= 3) && (axis < 6))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btAssertConstrParams(m_flags & BT_CONETWIST_FLAGS_ANG_CFM);
|
|
|
|
retVal = m_angCFM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
btAssertConstrParams(0);
|
|
|
|
}
|
|
|
|
break;
|
2019-01-03 13:26:51 +00:00
|
|
|
default:
|
2017-08-01 12:30:58 +00:00
|
|
|
btAssertConstrParams(0);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConeTwistConstraint::setFrames(const btTransform& frameA, const btTransform& frameB)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_rbAFrame = frameA;
|
|
|
|
m_rbBFrame = frameB;
|
|
|
|
buildJacobian();
|
|
|
|
//calculateTransforms();
|
|
|
|
}
|