add a way to set hinge-constraint motor speed from javascript

This commit is contained in:
Seth Alves 2017-04-15 21:10:02 -07:00
parent b4ff3a8691
commit fbd4677a9a
2 changed files with 58 additions and 14 deletions

View file

@ -48,6 +48,40 @@ QList<btRigidBody*> ObjectConstraintHinge::getRigidBodies() {
return result; return result;
} }
void ObjectConstraintHinge::updateHinge() {
btHingeConstraint* constraint { nullptr };
float low;
float high;
float softness;
float biasFactor;
float relaxationFactor;
float motorVelocity;
withReadLock([&]{
constraint = static_cast<btHingeConstraint*>(_constraint);
low = _low;
high = _high;
softness = _softness;
biasFactor = _biasFactor;
relaxationFactor = _relaxationFactor;
motorVelocity = _motorVelocity;
});
if (!constraint) {
return;
}
constraint->setLimit(low, high, softness, biasFactor, relaxationFactor);
if (motorVelocity != 0.0f) {
qDebug() << "setting motor velocity on" << _tag << "to" << motorVelocity;
constraint->setMotorTargetVelocity(motorVelocity);
constraint->enableMotor(true);
} else {
constraint->enableMotor(false);
}
}
btTypedConstraint* ObjectConstraintHinge::getConstraint() { btTypedConstraint* ObjectConstraintHinge::getConstraint() {
btHingeConstraint* constraint { nullptr }; btHingeConstraint* constraint { nullptr };
QUuid otherEntityID; QUuid otherEntityID;
@ -55,11 +89,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
glm::vec3 axisInA; glm::vec3 axisInA;
glm::vec3 pivotInB; glm::vec3 pivotInB;
glm::vec3 axisInB; glm::vec3 axisInB;
float low;
float high;
float softness;
float biasFactor;
float relaxationFactor;
withReadLock([&]{ withReadLock([&]{
constraint = static_cast<btHingeConstraint*>(_constraint); constraint = static_cast<btHingeConstraint*>(_constraint);
@ -68,11 +97,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
otherEntityID = _otherEntityID; otherEntityID = _otherEntityID;
pivotInB = _pivotInB; pivotInB = _pivotInB;
axisInB = _axisInB; axisInB = _axisInB;
low = _low;
high = _high;
softness = _softness;
biasFactor = _biasFactor;
relaxationFactor = _relaxationFactor;
}); });
if (constraint) { if (constraint) {
return constraint; return constraint;
@ -102,8 +126,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
true); // useReferenceFrameA true); // useReferenceFrameA
} }
constraint->setLimit(low, high, softness, biasFactor, relaxationFactor);
withWriteLock([&]{ withWriteLock([&]{
_constraint = constraint; _constraint = constraint;
}); });
@ -112,6 +134,8 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
forceBodyNonStatic(); forceBodyNonStatic();
activateBody(); activateBody();
updateHinge();
return constraint; return constraint;
} }
@ -127,6 +151,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
float softness; float softness;
float biasFactor; float biasFactor;
float relaxationFactor; float relaxationFactor;
float motorVelocity;
bool needUpdate = false; bool needUpdate = false;
bool somethingChanged = ObjectDynamic::updateArguments(arguments); bool somethingChanged = ObjectDynamic::updateArguments(arguments);
@ -147,7 +172,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("hinge constraint", otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("hinge constraint",
arguments, "otherEntityID", ok, false)); arguments, "otherEntityID", ok, false));
if (!ok) { if (!ok) {
otherEntityID = QUuid(); otherEntityID = _otherEntityID;
} }
ok = true; ok = true;
@ -193,6 +218,13 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
relaxationFactor = _relaxationFactor; relaxationFactor = _relaxationFactor;
} }
ok = true;
motorVelocity = EntityDynamicInterface::extractFloatArgument("hinge constraint", arguments,
"motorVelocity", ok, false);
if (!ok) {
motorVelocity = _motorVelocity;
}
if (somethingChanged || if (somethingChanged ||
pivotInA != _pivotInA || pivotInA != _pivotInA ||
axisInA != _axisInA || axisInA != _axisInA ||
@ -203,7 +235,8 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
high != _high || high != _high ||
softness != _softness || softness != _softness ||
biasFactor != _biasFactor || biasFactor != _biasFactor ||
relaxationFactor != _relaxationFactor) { relaxationFactor != _relaxationFactor ||
motorVelocity != _motorVelocity) {
// something changed // something changed
needUpdate = true; needUpdate = true;
} }
@ -221,6 +254,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
_softness = softness; _softness = softness;
_biasFactor = biasFactor; _biasFactor = biasFactor;
_relaxationFactor = relaxationFactor; _relaxationFactor = relaxationFactor;
_motorVelocity = motorVelocity;
_active = true; _active = true;
@ -230,6 +264,8 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
ownerEntity->setDynamicDataNeedsTransmit(true); ownerEntity->setDynamicDataNeedsTransmit(true);
} }
}); });
updateHinge();
} }
return true; return true;
@ -249,6 +285,7 @@ QVariantMap ObjectConstraintHinge::getArguments() {
arguments["softness"] = _softness; arguments["softness"] = _softness;
arguments["biasFactor"] = _biasFactor; arguments["biasFactor"] = _biasFactor;
arguments["relaxationFactor"] = _relaxationFactor; arguments["relaxationFactor"] = _relaxationFactor;
arguments["motorVelocity"] = _motorVelocity;
arguments["angle"] = static_cast<btHingeConstraint*>(_constraint)->getHingeAngle(); // [-PI,PI] arguments["angle"] = static_cast<btHingeConstraint*>(_constraint)->getHingeAngle(); // [-PI,PI]
} }
}); });
@ -277,6 +314,8 @@ QByteArray ObjectConstraintHinge::serialize() const {
dataStream << localTimeToServerTime(_expires); dataStream << localTimeToServerTime(_expires);
dataStream << _tag; dataStream << _tag;
dataStream << _motorVelocity;
}); });
return serializedConstraintArguments; return serializedConstraintArguments;
@ -318,6 +357,8 @@ void ObjectConstraintHinge::deserialize(QByteArray serializedArguments) {
dataStream >> _tag; dataStream >> _tag;
dataStream >> _motorVelocity;
_active = true; _active = true;
}); });
} }

View file

@ -33,6 +33,8 @@ public:
protected: protected:
static const uint16_t constraintVersion; static const uint16_t constraintVersion;
void updateHinge();
glm::vec3 _pivotInA; glm::vec3 _pivotInA;
glm::vec3 _axisInA; glm::vec3 _axisInA;
@ -45,6 +47,7 @@ protected:
float _softness { 0.9f }; float _softness { 0.9f };
float _biasFactor { 0.3f }; float _biasFactor { 0.3f };
float _relaxationFactor { 1.0f }; float _relaxationFactor { 1.0f };
float _motorVelocity { 0.0f };
}; };
#endif // hifi_ObjectConstraintHinge_h #endif // hifi_ObjectConstraintHinge_h