Path: blob/master/modules/godot_physics_3d/joints/godot_slider_joint_3d.h
10278 views
/**************************************************************************/1/* godot_slider_joint_3d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132/*33Adapted to Godot from the Bullet library.34*/3536#include "../godot_joint_3d.h"37#include "godot_jacobian_entry_3d.h"3839/*40Bullet Continuous Collision Detection and Physics Library41Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/4243This software is provided 'as-is', without any express or implied warranty.44In no event will the authors be held liable for any damages arising from the use of this software.45Permission is granted to anyone to use this software for any purpose,46including commercial applications, and to alter it and redistribute it freely,47subject to the following restrictions:48491. 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.502. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.513. This notice may not be removed or altered from any source distribution.52*/5354/*55Added by Roman Ponomarev ([email protected])56April 04, 20085758*/5960#define SLIDER_CONSTRAINT_DEF_SOFTNESS (real_t(1.0))61#define SLIDER_CONSTRAINT_DEF_DAMPING (real_t(1.0))62#define SLIDER_CONSTRAINT_DEF_RESTITUTION (real_t(0.7))6364//-----------------------------------------------------------------------------6566class GodotSliderJoint3D : public GodotJoint3D {67protected:68union {69struct {70GodotBody3D *A;71GodotBody3D *B;72};7374GodotBody3D *_arr[2] = { nullptr, nullptr };75};7677Transform3D m_frameInA;78Transform3D m_frameInB;7980// linear limits81real_t m_lowerLinLimit = 1.0;82real_t m_upperLinLimit = -1.0;83// angular limits84real_t m_lowerAngLimit = 0.0;85real_t m_upperAngLimit = 0.0;86// softness, restitution and damping for different cases87// DirLin - moving inside linear limits88// LimLin - hitting linear limit89// DirAng - moving inside angular limits90// LimAng - hitting angular limit91// OrthoLin, OrthoAng - against constraint axis92real_t m_softnessDirLin = SLIDER_CONSTRAINT_DEF_SOFTNESS;93real_t m_restitutionDirLin = SLIDER_CONSTRAINT_DEF_RESTITUTION;94real_t m_dampingDirLin = 0.0;95real_t m_softnessDirAng = SLIDER_CONSTRAINT_DEF_SOFTNESS;96real_t m_restitutionDirAng = SLIDER_CONSTRAINT_DEF_RESTITUTION;97real_t m_dampingDirAng = 0.0;98real_t m_softnessLimLin = SLIDER_CONSTRAINT_DEF_SOFTNESS;99real_t m_restitutionLimLin = SLIDER_CONSTRAINT_DEF_RESTITUTION;100real_t m_dampingLimLin = SLIDER_CONSTRAINT_DEF_DAMPING;101real_t m_softnessLimAng = SLIDER_CONSTRAINT_DEF_SOFTNESS;102real_t m_restitutionLimAng = SLIDER_CONSTRAINT_DEF_RESTITUTION;103real_t m_dampingLimAng = SLIDER_CONSTRAINT_DEF_DAMPING;104real_t m_softnessOrthoLin = SLIDER_CONSTRAINT_DEF_SOFTNESS;105real_t m_restitutionOrthoLin = SLIDER_CONSTRAINT_DEF_RESTITUTION;106real_t m_dampingOrthoLin = SLIDER_CONSTRAINT_DEF_DAMPING;107real_t m_softnessOrthoAng = SLIDER_CONSTRAINT_DEF_SOFTNESS;108real_t m_restitutionOrthoAng = SLIDER_CONSTRAINT_DEF_RESTITUTION;109real_t m_dampingOrthoAng = SLIDER_CONSTRAINT_DEF_DAMPING;110111// for interlal use112bool m_solveLinLim = false;113bool m_solveAngLim = false;114115GodotJacobianEntry3D m_jacLin[3] = {};116real_t m_jacLinDiagABInv[3] = {};117118GodotJacobianEntry3D m_jacAng[3] = {};119120real_t m_timeStep = 0.0;121Transform3D m_calculatedTransformA;122Transform3D m_calculatedTransformB;123124Vector3 m_sliderAxis;125Vector3 m_realPivotAInW;126Vector3 m_realPivotBInW;127Vector3 m_projPivotInW;128Vector3 m_delta;129Vector3 m_depth;130Vector3 m_relPosA;131Vector3 m_relPosB;132133real_t m_linPos = 0.0;134135real_t m_angDepth = 0.0;136real_t m_kAngle = 0.0;137138bool m_poweredLinMotor = false;139real_t m_targetLinMotorVelocity = 0.0;140real_t m_maxLinMotorForce = 0.0;141real_t m_accumulatedLinMotorImpulse = 0.0;142143bool m_poweredAngMotor = false;144real_t m_targetAngMotorVelocity = 0.0;145real_t m_maxAngMotorForce = 0.0;146real_t m_accumulatedAngMotorImpulse = 0.0;147148public:149// constructors150GodotSliderJoint3D(GodotBody3D *rbA, GodotBody3D *rbB, const Transform3D &frameInA, const Transform3D &frameInB);151//SliderJointSW();152// overrides153154// access155const GodotBody3D *getRigidBodyA() const { return A; }156const GodotBody3D *getRigidBodyB() const { return B; }157const Transform3D &getCalculatedTransformA() const { return m_calculatedTransformA; }158const Transform3D &getCalculatedTransformB() const { return m_calculatedTransformB; }159const Transform3D &getFrameOffsetA() const { return m_frameInA; }160const Transform3D &getFrameOffsetB() const { return m_frameInB; }161Transform3D &getFrameOffsetA() { return m_frameInA; }162Transform3D &getFrameOffsetB() { return m_frameInB; }163real_t getLowerLinLimit() { return m_lowerLinLimit; }164void setLowerLinLimit(real_t lowerLimit) { m_lowerLinLimit = lowerLimit; }165real_t getUpperLinLimit() { return m_upperLinLimit; }166void setUpperLinLimit(real_t upperLimit) { m_upperLinLimit = upperLimit; }167real_t getLowerAngLimit() { return m_lowerAngLimit; }168void setLowerAngLimit(real_t lowerLimit) { m_lowerAngLimit = lowerLimit; }169real_t getUpperAngLimit() { return m_upperAngLimit; }170void setUpperAngLimit(real_t upperLimit) { m_upperAngLimit = upperLimit; }171172real_t getSoftnessDirLin() { return m_softnessDirLin; }173real_t getRestitutionDirLin() { return m_restitutionDirLin; }174real_t getDampingDirLin() { return m_dampingDirLin; }175real_t getSoftnessDirAng() { return m_softnessDirAng; }176real_t getRestitutionDirAng() { return m_restitutionDirAng; }177real_t getDampingDirAng() { return m_dampingDirAng; }178real_t getSoftnessLimLin() { return m_softnessLimLin; }179real_t getRestitutionLimLin() { return m_restitutionLimLin; }180real_t getDampingLimLin() { return m_dampingLimLin; }181real_t getSoftnessLimAng() { return m_softnessLimAng; }182real_t getRestitutionLimAng() { return m_restitutionLimAng; }183real_t getDampingLimAng() { return m_dampingLimAng; }184real_t getSoftnessOrthoLin() { return m_softnessOrthoLin; }185real_t getRestitutionOrthoLin() { return m_restitutionOrthoLin; }186real_t getDampingOrthoLin() { return m_dampingOrthoLin; }187real_t getSoftnessOrthoAng() { return m_softnessOrthoAng; }188real_t getRestitutionOrthoAng() { return m_restitutionOrthoAng; }189real_t getDampingOrthoAng() { return m_dampingOrthoAng; }190void setSoftnessDirLin(real_t softnessDirLin) { m_softnessDirLin = softnessDirLin; }191void setRestitutionDirLin(real_t restitutionDirLin) { m_restitutionDirLin = restitutionDirLin; }192void setDampingDirLin(real_t dampingDirLin) { m_dampingDirLin = dampingDirLin; }193void setSoftnessDirAng(real_t softnessDirAng) { m_softnessDirAng = softnessDirAng; }194void setRestitutionDirAng(real_t restitutionDirAng) { m_restitutionDirAng = restitutionDirAng; }195void setDampingDirAng(real_t dampingDirAng) { m_dampingDirAng = dampingDirAng; }196void setSoftnessLimLin(real_t softnessLimLin) { m_softnessLimLin = softnessLimLin; }197void setRestitutionLimLin(real_t restitutionLimLin) { m_restitutionLimLin = restitutionLimLin; }198void setDampingLimLin(real_t dampingLimLin) { m_dampingLimLin = dampingLimLin; }199void setSoftnessLimAng(real_t softnessLimAng) { m_softnessLimAng = softnessLimAng; }200void setRestitutionLimAng(real_t restitutionLimAng) { m_restitutionLimAng = restitutionLimAng; }201void setDampingLimAng(real_t dampingLimAng) { m_dampingLimAng = dampingLimAng; }202void setSoftnessOrthoLin(real_t softnessOrthoLin) { m_softnessOrthoLin = softnessOrthoLin; }203void setRestitutionOrthoLin(real_t restitutionOrthoLin) { m_restitutionOrthoLin = restitutionOrthoLin; }204void setDampingOrthoLin(real_t dampingOrthoLin) { m_dampingOrthoLin = dampingOrthoLin; }205void setSoftnessOrthoAng(real_t softnessOrthoAng) { m_softnessOrthoAng = softnessOrthoAng; }206void setRestitutionOrthoAng(real_t restitutionOrthoAng) { m_restitutionOrthoAng = restitutionOrthoAng; }207void setDampingOrthoAng(real_t dampingOrthoAng) { m_dampingOrthoAng = dampingOrthoAng; }208void setPoweredLinMotor(bool onOff) { m_poweredLinMotor = onOff; }209bool getPoweredLinMotor() { return m_poweredLinMotor; }210void setTargetLinMotorVelocity(real_t targetLinMotorVelocity) { m_targetLinMotorVelocity = targetLinMotorVelocity; }211real_t getTargetLinMotorVelocity() { return m_targetLinMotorVelocity; }212void setMaxLinMotorForce(real_t maxLinMotorForce) { m_maxLinMotorForce = maxLinMotorForce; }213real_t getMaxLinMotorForce() { return m_maxLinMotorForce; }214void setPoweredAngMotor(bool onOff) { m_poweredAngMotor = onOff; }215bool getPoweredAngMotor() { return m_poweredAngMotor; }216void setTargetAngMotorVelocity(real_t targetAngMotorVelocity) { m_targetAngMotorVelocity = targetAngMotorVelocity; }217real_t getTargetAngMotorVelocity() { return m_targetAngMotorVelocity; }218void setMaxAngMotorForce(real_t maxAngMotorForce) { m_maxAngMotorForce = maxAngMotorForce; }219real_t getMaxAngMotorForce() { return m_maxAngMotorForce; }220real_t getLinearPos() { return m_linPos; }221222// access for ODE solver223bool getSolveLinLimit() { return m_solveLinLim; }224real_t getLinDepth() { return m_depth[0]; }225bool getSolveAngLimit() { return m_solveAngLim; }226real_t getAngDepth() { return m_angDepth; }227// shared code used by ODE solver228void calculateTransforms();229void testLinLimits();230void testAngLimits();231// access for PE Solver232Vector3 getAncorInA();233Vector3 getAncorInB();234235void set_param(PhysicsServer3D::SliderJointParam p_param, real_t p_value);236real_t get_param(PhysicsServer3D::SliderJointParam p_param) const;237238virtual bool setup(real_t p_step) override;239virtual void solve(real_t p_step) override;240241virtual PhysicsServer3D::JointType get_type() const override { return PhysicsServer3D::JOINT_TYPE_SLIDER; }242};243244245