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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HingeJointBullet::set_param(PhysicsServer::HingeJointParam p_param, real_t p_value) {
|
|
|
|
switch (p_param) {
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
|
|
|
|
hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), p_value, hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
|
|
|
|
hingeConstraint->setLimit(p_value, hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
|
|
|
|
hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), p_value, hingeConstraint->getLimitRelaxationFactor());
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
|
|
|
|
hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), p_value, hingeConstraint->getLimitBiasFactor(), hingeConstraint->getLimitRelaxationFactor());
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
|
|
|
|
hingeConstraint->setLimit(hingeConstraint->getLowerLimit(), hingeConstraint->getUpperLimit(), hingeConstraint->getLimitSoftness(), hingeConstraint->getLimitBiasFactor(), p_value);
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
|
|
|
|
hingeConstraint->setMotorTargetVelocity(p_value);
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
|
|
|
|
hingeConstraint->setMaxMotorImpulse(p_value);
|
|
|
|
break;
|
|
|
|
default:
|
2019-08-11 10:49:53 +02:00
|
|
|
WARN_DEPRECATED_MSG("The HingeJoint parameter " + itos(p_param) + " is deprecated.");
|
2018-08-27 10:10:40 +02:00
|
|
|
break;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t HingeJointBullet::get_param(PhysicsServer::HingeJointParam p_param) const {
|
|
|
|
switch (p_param) {
|
|
|
|
case PhysicsServer::HINGE_JOINT_BIAS:
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_UPPER:
|
|
|
|
return hingeConstraint->getUpperLimit();
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_LOWER:
|
|
|
|
return hingeConstraint->getLowerLimit();
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_BIAS:
|
|
|
|
return hingeConstraint->getLimitBiasFactor();
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS:
|
|
|
|
return hingeConstraint->getLimitSoftness();
|
|
|
|
case PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION:
|
|
|
|
return hingeConstraint->getLimitRelaxationFactor();
|
|
|
|
case PhysicsServer::HINGE_JOINT_MOTOR_TARGET_VELOCITY:
|
|
|
|
return hingeConstraint->getMotorTargetVelocity();
|
|
|
|
case PhysicsServer::HINGE_JOINT_MOTOR_MAX_IMPULSE:
|
|
|
|
return hingeConstraint->getMaxMotorImpulse();
|
|
|
|
default:
|
2019-08-11 10:49:53 +02:00
|
|
|
WARN_DEPRECATED_MSG("The HingeJoint parameter " + itos(p_param) + " is deprecated.");
|
2017-11-04 20:52:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HingeJointBullet::set_flag(PhysicsServer::HingeJointFlag p_flag, bool p_value) {
|
|
|
|
switch (p_flag) {
|
|
|
|
case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
|
|
|
|
if (!p_value) {
|
|
|
|
hingeConstraint->setLimit(-Math_PI, Math_PI);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
|
|
|
|
hingeConstraint->enableMotor(p_value);
|
|
|
|
break;
|
2018-09-26 13:13:56 +02:00
|
|
|
case PhysicsServer::HINGE_JOINT_FLAG_MAX: break; // Can't happen, but silences warning
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HingeJointBullet::get_flag(PhysicsServer::HingeJointFlag p_flag) const {
|
|
|
|
switch (p_flag) {
|
|
|
|
case PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT:
|
|
|
|
return true;
|
|
|
|
case PhysicsServer::HINGE_JOINT_FLAG_ENABLE_MOTOR:
|
|
|
|
return hingeConstraint->getEnableAngularMotor();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|