| 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 |
#include "../godot_joint_3d.h" |
| 38 |
#include "godot_jacobian_entry_3d.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
class GodotG6DOFRotationalLimitMotor3D { |
| 64 |
public: |
| 65 |
|
| 66 |
|
| 67 |
real_t m_loLimit = -1e30; |
| 68 |
real_t m_hiLimit = 1e30; |
| 69 |
real_t m_targetVelocity = 0.0; |
| 70 |
real_t m_maxMotorForce = 0.1; |
| 71 |
real_t m_maxLimitForce = 300.0; |
| 72 |
real_t m_damping = 1.0; |
| 73 |
real_t m_limitSoftness = 0.5; |
| 74 |
real_t m_ERP = 0.5; |
| 75 |
real_t m_bounce = 0.0; |
| 76 |
bool m_enableMotor = false; |
| 77 |
bool m_enableLimit = false; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
real_t m_currentLimitError = 0.0; |
| 84 |
int m_currentLimit = 0; |
| 85 |
real_t m_accumulatedImpulse = 0.0; |
| 86 |
|
| 87 |
|
| 88 |
GodotG6DOFRotationalLimitMotor3D() {} |
| 89 |
|
| 90 |
bool isLimited() { |
| 91 |
return (m_loLimit < m_hiLimit); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
bool needApplyTorques() { |
| 96 |
return (m_enableMotor || m_currentLimit != 0); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
int testLimitValue(real_t test_value); |
| 101 |
|
| 102 |
|
| 103 |
real_t solveAngularLimits(real_t timeStep, Vector3 &axis, real_t jacDiagABInv, GodotBody3D *body0, GodotBody3D *body1, bool p_body0_dynamic, bool p_body1_dynamic); |
| 104 |
}; |
| 105 |
|
| 106 |
class GodotG6DOFTranslationalLimitMotor3D { |
| 107 |
public: |
| 108 |
Vector3 m_lowerLimit = Vector3(0.0, 0.0, 0.0); |
| 109 |
Vector3 m_upperLimit = Vector3(0.0, 0.0, 0.0); |
| 110 |
Vector3 m_accumulatedImpulse = Vector3(0.0, 0.0, 0.0); |
| 111 |
|
| 112 |
|
| 113 |
Vector3 m_limitSoftness = Vector3(0.7, 0.7, 0.7); |
| 114 |
Vector3 m_damping = Vector3(1.0, 1.0, 1.0); |
| 115 |
Vector3 m_restitution = Vector3(0.5, 0.5, 0.5); |
| 116 |
|
| 117 |
bool enable_limit[3] = { true, true, true }; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
inline bool isLimited(int limitIndex) { |
| 127 |
return (m_upperLimit[limitIndex] >= m_lowerLimit[limitIndex]); |
| 128 |
} |
| 129 |
|
| 130 |
real_t solveLinearAxis( |
| 131 |
real_t timeStep, |
| 132 |
real_t jacDiagABInv, |
| 133 |
GodotBody3D *body1, const Vector3 &pointInA, |
| 134 |
GodotBody3D *body2, const Vector3 &pointInB, |
| 135 |
bool p_body1_dynamic, bool p_body2_dynamic, |
| 136 |
int limit_index, |
| 137 |
const Vector3 &axis_normal_on_a, |
| 138 |
const Vector3 &anchorPos); |
| 139 |
}; |
| 140 |
|
| 141 |
class GodotGeneric6DOFJoint3D : public GodotJoint3D { |
| 142 |
protected: |
| 143 |
union { |
| 144 |
struct { |
| 145 |
GodotBody3D *A; |
| 146 |
GodotBody3D *B; |
| 147 |
}; |
| 148 |
|
| 149 |
GodotBody3D *_arr[2] = { nullptr, nullptr }; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
Transform3D m_frameInA; |
| 155 |
Transform3D m_frameInB; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
GodotJacobianEntry3D m_jacLinear[3]; |
| 161 |
GodotJacobianEntry3D m_jacAng[3]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
GodotG6DOFTranslationalLimitMotor3D m_linearLimits; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
GodotG6DOFRotationalLimitMotor3D m_angularLimits[3]; |
| 172 |
|
| 173 |
|
| 174 |
protected: |
| 175 |
|
| 176 |
|
| 177 |
real_t m_timeStep = 0.0; |
| 178 |
Transform3D m_calculatedTransformA; |
| 179 |
Transform3D m_calculatedTransformB; |
| 180 |
Vector3 m_calculatedAxisAngleDiff; |
| 181 |
Vector3 m_calculatedAxis[3]; |
| 182 |
|
| 183 |
Vector3 m_AnchorPos; |
| 184 |
|
| 185 |
bool m_useLinearReferenceFrameA = false; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
GodotGeneric6DOFJoint3D(GodotGeneric6DOFJoint3D const &) = delete; |
| 190 |
void operator=(GodotGeneric6DOFJoint3D const &) = delete; |
| 191 |
|
| 192 |
void buildLinearJacobian( |
| 193 |
GodotJacobianEntry3D &jacLinear, const Vector3 &normalWorld, |
| 194 |
const Vector3 &pivotAInW, const Vector3 &pivotBInW); |
| 195 |
|
| 196 |
void buildAngularJacobian(GodotJacobianEntry3D &jacAngular, const Vector3 &jointAxisW); |
| 197 |
|
| 198 |
|
| 199 |
void calculateAngleInfo(); |
| 200 |
|
| 201 |
public: |
| 202 |
GodotGeneric6DOFJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &frameInA, const Transform3D &frameInB, bool useLinearReferenceFrameA); |
| 203 |
|
| 204 |
virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_6DOF; } |
| 205 |
|
| 206 |
virtual bool setup(real_t p_step) override; |
| 207 |
virtual void solve(real_t p_step) override; |
| 208 |
|
| 209 |
|
| 210 |
void calculateTransforms(); |
| 211 |
|
| 212 |
|
| 213 |
const Transform3D &getCalculatedTransformA() const { |
| 214 |
return m_calculatedTransformA; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
const Transform3D &getCalculatedTransformB() const { |
| 219 |
return m_calculatedTransformB; |
| 220 |
} |
| 221 |
|
| 222 |
const Transform3D &getFrameOffsetA() const { |
| 223 |
return m_frameInA; |
| 224 |
} |
| 225 |
|
| 226 |
const Transform3D &getFrameOffsetB() const { |
| 227 |
return m_frameInB; |
| 228 |
} |
| 229 |
|
| 230 |
Transform3D &getFrameOffsetA() { |
| 231 |
return m_frameInA; |
| 232 |
} |
| 233 |
|
| 234 |
Transform3D &getFrameOffsetB() { |
| 235 |
return m_frameInB; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
void updateRHS(real_t timeStep); |
| 240 |
|
| 241 |
|
| 242 |
Vector3 getAxis(int axis_index) const; |
| 243 |
|
| 244 |
|
| 245 |
real_t getAngle(int axis_index) const; |
| 246 |
|
| 247 |
|
| 248 |
bool testAngularLimitMotor(int axis_index); |
| 249 |
|
| 250 |
void setLinearLowerLimit(const Vector3 &linearLower) { |
| 251 |
m_linearLimits.m_lowerLimit = linearLower; |
| 252 |
} |
| 253 |
|
| 254 |
void setLinearUpperLimit(const Vector3 &linearUpper) { |
| 255 |
m_linearLimits.m_upperLimit = linearUpper; |
| 256 |
} |
| 257 |
|
| 258 |
void setAngularLowerLimit(const Vector3 &angularLower) { |
| 259 |
m_angularLimits[0].m_loLimit = angularLower.x; |
| 260 |
m_angularLimits[1].m_loLimit = angularLower.y; |
| 261 |
m_angularLimits[2].m_loLimit = angularLower.z; |
| 262 |
} |
| 263 |
|
| 264 |
void setAngularUpperLimit(const Vector3 &angularUpper) { |
| 265 |
m_angularLimits[0].m_hiLimit = angularUpper.x; |
| 266 |
m_angularLimits[1].m_hiLimit = angularUpper.y; |
| 267 |
m_angularLimits[2].m_hiLimit = angularUpper.z; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
GodotG6DOFRotationalLimitMotor3D *getRotationalLimitMotor(int index) { |
| 272 |
return &m_angularLimits[index]; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
GodotG6DOFTranslationalLimitMotor3D *getTranslationalLimitMotor() { |
| 277 |
return &m_linearLimits; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
void setLimit(int axis, real_t lo, real_t hi) { |
| 282 |
if (axis < 3) { |
| 283 |
m_linearLimits.m_lowerLimit[axis] = lo; |
| 284 |
m_linearLimits.m_upperLimit[axis] = hi; |
| 285 |
} else { |
| 286 |
m_angularLimits[axis - 3].m_loLimit = lo; |
| 287 |
m_angularLimits[axis - 3].m_hiLimit = hi; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
bool isLimited(int limitIndex) { |
| 299 |
if (limitIndex < 3) { |
| 300 |
return m_linearLimits.isLimited(limitIndex); |
| 301 |
} |
| 302 |
return m_angularLimits[limitIndex - 3].isLimited(); |
| 303 |
} |
| 304 |
|
| 305 |
const GodotBody3D *getRigidBodyA() const { |
| 306 |
return A; |
| 307 |
} |
| 308 |
const GodotBody3D *getRigidBodyB() const { |
| 309 |
return B; |
| 310 |
} |
| 311 |
|
| 312 |
virtual void calcAnchorPos(); |
| 313 |
|
| 314 |
void set_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param, real_t p_value); |
| 315 |
real_t get_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param) const; |
| 316 |
|
| 317 |
void set_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag, bool p_value); |
| 318 |
bool get_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag) const; |
| 319 |
}; |