| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  hinge_joint_bullet.cpp                                               */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2020-01-01 11:16:22 +01:00
										 |  |  | /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* 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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "hinge_joint_bullet.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | #include "bullet_types_converter.h"
 | 
					
						
							|  |  |  | #include "bullet_utilities.h"
 | 
					
						
							|  |  |  | #include "rigid_body_bullet.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | #include <BulletDynamics/ConstraintSolver/btHingeConstraint.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  | 	@author AndreaCatania | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Transform &frameA, const Transform &frameB) : | 
					
						
							|  |  |  | 		JointBullet() { | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Transform scaled_AFrame(frameA.scaled(rbA->get_body_scale())); | 
					
						
							|  |  |  | 	scaled_AFrame.basis.rotref_posscale_decomposition(scaled_AFrame.basis); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	btTransform btFrameA; | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 	G_TO_B(scaled_AFrame, btFrameA); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rbB) { | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Transform scaled_BFrame(frameB.scaled(rbB->get_body_scale())); | 
					
						
							|  |  |  | 		scaled_BFrame.basis.rotref_posscale_decomposition(scaled_BFrame.basis); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 		btTransform btFrameB; | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 		G_TO_B(scaled_BFrame, btFrameB); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btFrameA, btFrameB)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), btFrameA)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setup(hingeConstraint); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | HingeJointBullet::HingeJointBullet(RigidBodyBullet *rbA, RigidBodyBullet *rbB, const Vector3 &pivotInA, const Vector3 &pivotInB, const Vector3 &axisInA, const Vector3 &axisInB) : | 
					
						
							|  |  |  | 		JointBullet() { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	btVector3 btPivotA; | 
					
						
							|  |  |  | 	btVector3 btAxisA; | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 	G_TO_B(pivotInA * rbA->get_body_scale(), btPivotA); | 
					
						
							|  |  |  | 	G_TO_B(axisInA * rbA->get_body_scale(), btAxisA); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rbB) { | 
					
						
							|  |  |  | 		btVector3 btPivotB; | 
					
						
							|  |  |  | 		btVector3 btAxisB; | 
					
						
							| 
									
										
										
										
											2017-12-09 01:22:36 +01:00
										 |  |  | 		G_TO_B(pivotInB * rbB->get_body_scale(), btPivotB); | 
					
						
							|  |  |  | 		G_TO_B(axisInB * rbB->get_body_scale(), btAxisB); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), *rbB->get_bt_rigid_body(), btPivotA, btPivotB, btAxisA, btAxisB)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hingeConstraint = bulletnew(btHingeConstraint(*rbA->get_bt_rigid_body(), btPivotA, btAxisA)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setup(hingeConstraint); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | real_t HingeJointBullet::get_hinge_angle() { | 
					
						
							|  |  |  | 	return hingeConstraint->getHingeAngle(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void HingeJointBullet::set_param(PhysicsServer3D::HingeJointParam p_param, real_t p_value) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_param) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_BIAS: | 
					
						
							| 
									
										
										
										
											2020-02-13 11:37:37 +01:00
										 |  |  | 			WARN_DEPRECATED_MSG("The HingeJoint parameter \"bias\" is deprecated."); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), p_value, hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor()); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setLimit(p_value, hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor()); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), p_value, hingeConstraint->getLimitRelaxationFactor()); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), p_value, hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor()); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MOTOR_TARGET_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setMotorTargetVelocity(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MOTOR_MAX_IMPULSE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->setMaxMotorImpulse(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MAX: | 
					
						
							| 
									
										
										
										
											2020-02-13 11:37:37 +01:00
										 |  |  | 			// Internal size value, nothing to do.
 | 
					
						
							| 
									
										
										
										
											2018-08-27 10:10:40 +02:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | real_t HingeJointBullet::get_param(PhysicsServer3D::HingeJointParam p_param) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_param) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_BIAS: | 
					
						
							| 
									
										
										
										
											2020-02-13 11:37:37 +01:00
										 |  |  | 			WARN_DEPRECATED_MSG("The HingeJoint parameter \"bias\" is deprecated."); | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_UPPER: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getUpperLimit(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_LOWER: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getLowerLimit(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_BIAS: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getLimitBiasFactor(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_SOFTNESS: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getLimitSoftness(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_LIMIT_RELAXATION: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getLimitRelaxationFactor(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MOTOR_TARGET_VELOCITY: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getMotorTargetVelocity(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MOTOR_MAX_IMPULSE: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getMaxMotorImpulse(); | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_MAX: | 
					
						
							| 
									
										
										
										
											2020-02-13 11:37:37 +01:00
										 |  |  | 			// Internal size value, nothing to do.
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-02-13 11:37:37 +01:00
										 |  |  | 	// Compiler doesn't seem to notice that all code paths are fulfilled...
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | void HingeJointBullet::set_flag(PhysicsServer3D::HingeJointFlag p_flag, bool p_value) { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_flag) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			if (!p_value) { | 
					
						
							|  |  |  | 				hingeConstraint->setLimit(-Math_PI, Math_PI); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_FLAG_ENABLE_MOTOR: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			hingeConstraint->enableMotor(p_value); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_FLAG_MAX: break; // Can't happen, but silences warning
 | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | bool HingeJointBullet::get_flag(PhysicsServer3D::HingeJointFlag p_flag) const { | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 	switch (p_flag) { | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_FLAG_USE_LIMIT: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2020-03-27 15:21:27 -03:00
										 |  |  | 		case PhysicsServer3D::HINGE_JOINT_FLAG_ENABLE_MOTOR: | 
					
						
							| 
									
										
										
										
											2017-11-04 20:52:59 +01:00
										 |  |  | 			return hingeConstraint->getEnableAngularMotor(); | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |