| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#include "core/math/transform_3d.h" |
| 53 |
|
| 54 |
class GodotJacobianEntry3D { |
| 55 |
public: |
| 56 |
GodotJacobianEntry3D() {} |
| 57 |
|
| 58 |
GodotJacobianEntry3D( |
| 59 |
const Basis &world2A, |
| 60 |
const Basis &world2B, |
| 61 |
const Vector3 &rel_pos1, const Vector3 &rel_pos2, |
| 62 |
const Vector3 &jointAxis, |
| 63 |
const Vector3 &inertiaInvA, |
| 64 |
const real_t massInvA, |
| 65 |
const Vector3 &inertiaInvB, |
| 66 |
const real_t massInvB) : |
| 67 |
m_linearJointAxis(jointAxis) { |
| 68 |
m_aJ = world2A.xform(rel_pos1.cross(m_linearJointAxis)); |
| 69 |
m_bJ = world2B.xform(rel_pos2.cross(-m_linearJointAxis)); |
| 70 |
m_0MinvJt = inertiaInvA * m_aJ; |
| 71 |
m_1MinvJt = inertiaInvB * m_bJ; |
| 72 |
m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ); |
| 73 |
|
| 74 |
ERR_FAIL_COND(m_Adiag <= real_t(0.0)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
GodotJacobianEntry3D(const Vector3 &jointAxis, |
| 79 |
const Basis &world2A, |
| 80 |
const Basis &world2B, |
| 81 |
const Vector3 &inertiaInvA, |
| 82 |
const Vector3 &inertiaInvB) : |
| 83 |
m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))) { |
| 84 |
m_aJ = world2A.xform(jointAxis); |
| 85 |
m_bJ = world2B.xform(-jointAxis); |
| 86 |
m_0MinvJt = inertiaInvA * m_aJ; |
| 87 |
m_1MinvJt = inertiaInvB * m_bJ; |
| 88 |
m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ); |
| 89 |
|
| 90 |
ERR_FAIL_COND(m_Adiag <= real_t(0.0)); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
GodotJacobianEntry3D(const Vector3 &axisInA, |
| 95 |
const Vector3 &axisInB, |
| 96 |
const Vector3 &inertiaInvA, |
| 97 |
const Vector3 &inertiaInvB) : |
| 98 |
m_linearJointAxis(Vector3(real_t(0.), real_t(0.), real_t(0.))), |
| 99 |
m_aJ(axisInA), |
| 100 |
m_bJ(-axisInB) { |
| 101 |
m_0MinvJt = inertiaInvA * m_aJ; |
| 102 |
m_1MinvJt = inertiaInvB * m_bJ; |
| 103 |
m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ); |
| 104 |
|
| 105 |
ERR_FAIL_COND(m_Adiag <= real_t(0.0)); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
GodotJacobianEntry3D( |
| 110 |
const Basis &world2A, |
| 111 |
const Vector3 &rel_pos1, const Vector3 &rel_pos2, |
| 112 |
const Vector3 &jointAxis, |
| 113 |
const Vector3 &inertiaInvA, |
| 114 |
const real_t massInvA) : |
| 115 |
m_linearJointAxis(jointAxis) { |
| 116 |
m_aJ = world2A.xform(rel_pos1.cross(jointAxis)); |
| 117 |
m_bJ = world2A.xform(rel_pos2.cross(-jointAxis)); |
| 118 |
m_0MinvJt = inertiaInvA * m_aJ; |
| 119 |
m_1MinvJt = Vector3(real_t(0.), real_t(0.), real_t(0.)); |
| 120 |
m_Adiag = massInvA + m_0MinvJt.dot(m_aJ); |
| 121 |
|
| 122 |
ERR_FAIL_COND(m_Adiag <= real_t(0.0)); |
| 123 |
} |
| 124 |
|
| 125 |
real_t getDiagonal() const { return m_Adiag; } |
| 126 |
|
| 127 |
|
| 128 |
real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA) const { |
| 129 |
const GodotJacobianEntry3D &jacA = *this; |
| 130 |
real_t lin = massInvA * jacA.m_linearJointAxis.dot(jacB.m_linearJointAxis); |
| 131 |
real_t ang = jacA.m_0MinvJt.dot(jacB.m_aJ); |
| 132 |
return lin + ang; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
real_t getNonDiagonal(const GodotJacobianEntry3D &jacB, const real_t massInvA, const real_t massInvB) const { |
| 137 |
const GodotJacobianEntry3D &jacA = *this; |
| 138 |
Vector3 lin = jacA.m_linearJointAxis * jacB.m_linearJointAxis; |
| 139 |
Vector3 ang0 = jacA.m_0MinvJt * jacB.m_aJ; |
| 140 |
Vector3 ang1 = jacA.m_1MinvJt * jacB.m_bJ; |
| 141 |
Vector3 lin0 = massInvA * lin; |
| 142 |
Vector3 lin1 = massInvB * lin; |
| 143 |
Vector3 sum = ang0 + ang1 + lin0 + lin1; |
| 144 |
return sum[0] + sum[1] + sum[2]; |
| 145 |
} |
| 146 |
|
| 147 |
real_t getRelativeVelocity(const Vector3 &linvelA, const Vector3 &angvelA, const Vector3 &linvelB, const Vector3 &angvelB) { |
| 148 |
Vector3 linrel = linvelA - linvelB; |
| 149 |
Vector3 angvela = angvelA * m_aJ; |
| 150 |
Vector3 angvelb = angvelB * m_bJ; |
| 151 |
linrel *= m_linearJointAxis; |
| 152 |
angvela += angvelb; |
| 153 |
angvela += linrel; |
| 154 |
real_t rel_vel2 = angvela[0] + angvela[1] + angvela[2]; |
| 155 |
return rel_vel2 + CMP_EPSILON; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
Vector3 m_linearJointAxis; |
| 160 |
Vector3 m_aJ; |
| 161 |
Vector3 m_bJ; |
| 162 |
Vector3 m_0MinvJt; |
| 163 |
Vector3 m_1MinvJt; |
| 164 |
|
| 165 |
real_t m_Adiag = 1.0; |
| 166 |
}; |