2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* godot_generic_6dof_joint_3d.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2016-06-18 12:46:12 +00:00
|
|
|
|
2022-09-26 11:03:44 +00:00
|
|
|
#ifndef GODOT_GENERIC_6DOF_JOINT_3D_H
|
|
|
|
#define GODOT_GENERIC_6DOF_JOINT_3D_H
|
|
|
|
|
2016-06-18 12:46:12 +00:00
|
|
|
/*
|
|
|
|
Adapted to Godot from the Bullet library.
|
|
|
|
*/
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#include "servers/physics_3d/godot_joint_3d.h"
|
|
|
|
#include "servers/physics_3d/joints/godot_jacobian_entry_3d.h"
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
2021-09-13 20:18:28 +00:00
|
|
|
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
2007-09-09
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotGeneric6DOFJoint3D Refactored by Francisco Le?n
|
2014-09-15 14:33:30 +00:00
|
|
|
email: projectileman@yahoo.com
|
|
|
|
http://gimpact.sf.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! Rotation Limit structure for generic joints
|
2021-10-18 19:24:30 +00:00
|
|
|
class GodotG6DOFRotationalLimitMotor3D {
|
2014-09-15 14:33:30 +00:00
|
|
|
public:
|
|
|
|
//! limit_parameters
|
|
|
|
//!@{
|
2021-09-14 09:01:49 +00:00
|
|
|
real_t m_loLimit = -1e30; //!< joint limit
|
|
|
|
real_t m_hiLimit = 1e30; //!< joint limit
|
|
|
|
real_t m_targetVelocity = 0.0; //!< target motor velocity
|
|
|
|
real_t m_maxMotorForce = 0.1; //!< max force on motor
|
|
|
|
real_t m_maxLimitForce = 300.0; //!< max force on limit
|
|
|
|
real_t m_damping = 1.0; //!< Damping.
|
|
|
|
real_t m_limitSoftness = 0.5; //! Relaxation factor
|
|
|
|
real_t m_ERP = 0.5; //!< Error tolerance factor when joint is at limit
|
|
|
|
real_t m_bounce = 0.0; //!< restitution factor
|
|
|
|
bool m_enableMotor = false;
|
|
|
|
bool m_enableLimit = false;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
//!@}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
//! temp_variables
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@{
|
2021-09-14 09:01:49 +00:00
|
|
|
real_t m_currentLimitError = 0.0; //!< How much is violated this limit
|
|
|
|
int m_currentLimit = 0; //!< 0=free, 1=at lo limit, 2=at hi limit
|
|
|
|
real_t m_accumulatedImpulse = 0.0;
|
2014-09-15 14:33:30 +00:00
|
|
|
//!@}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotG6DOFRotationalLimitMotor3D() {}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
bool isLimited() {
|
2018-10-06 20:20:41 +00:00
|
|
|
return (m_loLimit < m_hiLimit);
|
2014-09-15 14:33:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Need apply correction.
|
2014-09-15 14:33:30 +00:00
|
|
|
bool needApplyTorques() {
|
2018-10-06 20:20:41 +00:00
|
|
|
return (m_enableMotor || m_currentLimit != 0);
|
2014-09-15 14:33:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Calculates m_currentLimit and m_currentLimitError.
|
2014-09-15 14:33:30 +00:00
|
|
|
int testLimitValue(real_t test_value);
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Apply the correction impulses for two bodies.
|
|
|
|
real_t solveAngularLimits(real_t timeStep, Vector3 &axis, real_t jacDiagABInv, GodotBody3D *body0, GodotBody3D *body1, bool p_body0_dynamic, bool p_body1_dynamic);
|
2017-03-05 15:44:50 +00:00
|
|
|
};
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
class GodotG6DOFTranslationalLimitMotor3D {
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2021-09-14 09:01:49 +00:00
|
|
|
Vector3 m_lowerLimit = Vector3(0.0, 0.0, 0.0); //!< the constraint lower limits
|
|
|
|
Vector3 m_upperLimit = Vector3(0.0, 0.0, 0.0); //!< the constraint upper limits
|
|
|
|
Vector3 m_accumulatedImpulse = Vector3(0.0, 0.0, 0.0);
|
2014-09-15 14:33:30 +00:00
|
|
|
//! Linear_Limit_parameters
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@{
|
2021-09-14 09:01:49 +00:00
|
|
|
Vector3 m_limitSoftness = Vector3(0.7, 0.7, 0.7); //!< Softness for linear limit
|
|
|
|
Vector3 m_damping = Vector3(1.0, 1.0, 1.0); //!< Damping for linear limit
|
|
|
|
Vector3 m_restitution = Vector3(0.5, 0.5, 0.5); //! Bounce parameter for linear limit
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@}
|
2021-09-14 09:01:49 +00:00
|
|
|
bool enable_limit[3] = { true, true, true };
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
//! Test limit
|
|
|
|
/*!
|
2021-10-28 13:43:36 +00:00
|
|
|
* - free means upper < lower,
|
|
|
|
* - locked means upper == lower
|
|
|
|
* - limited means upper > lower
|
|
|
|
* - limitIndex: first 3 are linear, next 3 are angular
|
|
|
|
*/
|
2014-09-15 14:33:30 +00:00
|
|
|
inline bool isLimited(int limitIndex) {
|
|
|
|
return (m_upperLimit[limitIndex] >= m_lowerLimit[limitIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t solveLinearAxis(
|
|
|
|
real_t timeStep,
|
|
|
|
real_t jacDiagABInv,
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody3D *body1, const Vector3 &pointInA,
|
|
|
|
GodotBody3D *body2, const Vector3 &pointInB,
|
2021-04-20 01:38:11 +00:00
|
|
|
bool p_body1_dynamic, bool p_body2_dynamic,
|
2014-09-15 14:33:30 +00:00
|
|
|
int limit_index,
|
|
|
|
const Vector3 &axis_normal_on_a,
|
|
|
|
const Vector3 &anchorPos);
|
|
|
|
};
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
class GodotGeneric6DOFJoint3D : public GodotJoint3D {
|
2014-09-15 14:33:30 +00:00
|
|
|
protected:
|
|
|
|
union {
|
|
|
|
struct {
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody3D *A;
|
|
|
|
GodotBody3D *B;
|
2014-09-15 14:33:30 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotBody3D *_arr[2] = { nullptr, nullptr };
|
2014-09-15 14:33:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//! relative_frames
|
|
|
|
//!@{
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D m_frameInA; //!< the constraint space w.r.t body A
|
|
|
|
Transform3D m_frameInB; //!< the constraint space w.r.t body B
|
2014-09-15 14:33:30 +00:00
|
|
|
//!@}
|
|
|
|
|
|
|
|
//! Jacobians
|
|
|
|
//!@{
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotJacobianEntry3D m_jacLinear[3]; //!< 3 orthogonal linear constraints
|
|
|
|
GodotJacobianEntry3D m_jacAng[3]; //!< 3 orthogonal angular constraints
|
2014-09-15 14:33:30 +00:00
|
|
|
//!@}
|
|
|
|
|
|
|
|
//! Linear_Limit_parameters
|
|
|
|
//!@{
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotG6DOFTranslationalLimitMotor3D m_linearLimits;
|
2014-09-15 14:33:30 +00:00
|
|
|
//!@}
|
|
|
|
|
|
|
|
//! hinge_parameters
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@{
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotG6DOFRotationalLimitMotor3D m_angularLimits[3];
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
//! temporal variables
|
|
|
|
//!@{
|
2021-09-14 09:01:49 +00:00
|
|
|
real_t m_timeStep = 0.0;
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D m_calculatedTransformA;
|
|
|
|
Transform3D m_calculatedTransformB;
|
2014-09-15 14:33:30 +00:00
|
|
|
Vector3 m_calculatedAxisAngleDiff;
|
|
|
|
Vector3 m_calculatedAxis[3];
|
|
|
|
|
2017-03-24 20:45:31 +00:00
|
|
|
Vector3 m_AnchorPos; // point between pivots of bodies A and B to solve linear axes
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-09-14 09:01:49 +00:00
|
|
|
bool m_useLinearReferenceFrameA = false;
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
//!@}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotGeneric6DOFJoint3D(GodotGeneric6DOFJoint3D const &) = delete;
|
|
|
|
void operator=(GodotGeneric6DOFJoint3D const &) = delete;
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
void buildLinearJacobian(
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotJacobianEntry3D &jacLinear, const Vector3 &normalWorld,
|
2014-09-15 14:33:30 +00:00
|
|
|
const Vector3 &pivotAInW, const Vector3 &pivotBInW);
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
void buildAngularJacobian(GodotJacobianEntry3D &jacAngular, const Vector3 &jointAxisW);
|
2014-09-15 14:33:30 +00:00
|
|
|
|
|
|
|
//! calcs the euler angles between the two bodies.
|
|
|
|
void calculateAngleInfo();
|
|
|
|
|
|
|
|
public:
|
2021-10-18 19:24:30 +00:00
|
|
|
GodotGeneric6DOFJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &frameInA, const Transform3D &frameInB, bool useLinearReferenceFrameA);
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-04-20 01:38:11 +00:00
|
|
|
virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_6DOF; }
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-04-20 01:38:11 +00:00
|
|
|
virtual bool setup(real_t p_step) override;
|
|
|
|
virtual void solve(real_t p_step) override;
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Calcs the global transform for the joint offset for body A an B, and also calcs the angle differences between the bodies.
|
2014-09-15 14:33:30 +00:00
|
|
|
void calculateTransforms();
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Gets the global transform of the offset for body A.
|
2020-10-17 05:08:21 +00:00
|
|
|
const Transform3D &getCalculatedTransformA() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_calculatedTransformA;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Gets the global transform of the offset for body B.
|
2020-10-17 05:08:21 +00:00
|
|
|
const Transform3D &getCalculatedTransformB() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_calculatedTransformB;
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
const Transform3D &getFrameOffsetA() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_frameInA;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
const Transform3D &getFrameOffsetB() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_frameInB;
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D &getFrameOffsetA() {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_frameInA;
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D &getFrameOffsetB() {
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_frameInB;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Performs Jacobian calculation, and also calculates angle differences and axis.
|
2014-09-15 14:33:30 +00:00
|
|
|
void updateRHS(real_t timeStep);
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Get the rotation axis in global coordinates.
|
2014-09-15 14:33:30 +00:00
|
|
|
Vector3 getAxis(int axis_index) const;
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Get the relative Euler angle.
|
2014-09-15 14:33:30 +00:00
|
|
|
real_t getAngle(int axis_index) const;
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Calculates angular correction and returns true if limit needs to be corrected.
|
2014-09-15 14:33:30 +00:00
|
|
|
bool testAngularLimitMotor(int axis_index);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
void setLinearLowerLimit(const Vector3 &linearLower) {
|
|
|
|
m_linearLimits.m_lowerLimit = linearLower;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
void setLinearUpperLimit(const Vector3 &linearUpper) {
|
|
|
|
m_linearLimits.m_upperLimit = linearUpper;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
void setAngularLowerLimit(const Vector3 &angularLower) {
|
|
|
|
m_angularLimits[0].m_loLimit = angularLower.x;
|
|
|
|
m_angularLimits[1].m_loLimit = angularLower.y;
|
|
|
|
m_angularLimits[2].m_loLimit = angularLower.z;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-09-15 14:33:30 +00:00
|
|
|
void setAngularUpperLimit(const Vector3 &angularUpper) {
|
|
|
|
m_angularLimits[0].m_hiLimit = angularUpper.x;
|
|
|
|
m_angularLimits[1].m_hiLimit = angularUpper.y;
|
|
|
|
m_angularLimits[2].m_hiLimit = angularUpper.z;
|
|
|
|
}
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Retrieves the angular limit information.
|
|
|
|
GodotG6DOFRotationalLimitMotor3D *getRotationalLimitMotor(int index) {
|
2014-09-15 14:33:30 +00:00
|
|
|
return &m_angularLimits[index];
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// Retrieves the limit information.
|
|
|
|
GodotG6DOFTranslationalLimitMotor3D *getTranslationalLimitMotor() {
|
2014-09-15 14:33:30 +00:00
|
|
|
return &m_linearLimits;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
// First 3 are linear, next 3 are angular.
|
2014-09-15 14:33:30 +00:00
|
|
|
void setLimit(int axis, real_t lo, real_t hi) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (axis < 3) {
|
2014-09-15 14:33:30 +00:00
|
|
|
m_linearLimits.m_lowerLimit[axis] = lo;
|
|
|
|
m_linearLimits.m_upperLimit[axis] = hi;
|
|
|
|
} else {
|
|
|
|
m_angularLimits[axis - 3].m_loLimit = lo;
|
|
|
|
m_angularLimits[axis - 3].m_hiLimit = hi;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-09-15 14:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Test limit
|
|
|
|
/*!
|
2021-10-28 13:43:36 +00:00
|
|
|
* - free means upper < lower,
|
|
|
|
* - locked means upper == lower
|
|
|
|
* - limited means upper > lower
|
|
|
|
* - limitIndex: first 3 are linear, next 3 are angular
|
|
|
|
*/
|
2014-09-15 14:33:30 +00:00
|
|
|
bool isLimited(int limitIndex) {
|
|
|
|
if (limitIndex < 3) {
|
|
|
|
return m_linearLimits.isLimited(limitIndex);
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-09-15 14:33:30 +00:00
|
|
|
return m_angularLimits[limitIndex - 3].isLimited();
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
const GodotBody3D *getRigidBodyA() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return A;
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2021-10-18 19:24:30 +00:00
|
|
|
const GodotBody3D *getRigidBodyB() const {
|
2014-09-15 14:33:30 +00:00
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2020-05-14 09:11:27 +00:00
|
|
|
virtual void calcAnchorPos(); // overridable
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param, real_t p_value);
|
|
|
|
real_t get_param(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisParam p_param) const;
|
2014-09-15 14:33:30 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
void set_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag, bool p_value);
|
|
|
|
bool get_flag(Vector3::Axis p_axis, PhysicsServer3D::G6DOFJointAxisFlag p_flag) const;
|
2014-09-15 14:33:30 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 19:24:30 +00:00
|
|
|
#endif // GODOT_GENERIC_6DOF_JOINT_3D_H
|