mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 13:49:46 +02:00
respond to code review, add a simple hold-a-stick script
This commit is contained in:
parent
eccf4eb8a8
commit
45c7cd4929
5 changed files with 31 additions and 13 deletions
18
examples/stick.js
Normal file
18
examples/stick.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
var stickID = null;
|
||||||
|
// sometimes if this is run immediately the stick doesn't get created? use a timer.
|
||||||
|
Script.setTimeout(function() {
|
||||||
|
var stickID = Entities.addEntity({
|
||||||
|
type: "Model",
|
||||||
|
modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx",
|
||||||
|
compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj",
|
||||||
|
dimensions: {x: .11, y: .11, z: .59},
|
||||||
|
position: MyAvatar.getRightPalmPosition(),
|
||||||
|
rotation: MyAvatar.orientation,
|
||||||
|
damping: .1,
|
||||||
|
collisionsWillMove: true
|
||||||
|
});
|
||||||
|
Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -0.9}, timeScale: 0.15});
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
|
function cleanup() { Entities.deleteEntity(stickID); }
|
||||||
|
Script.scriptEnding.connect(cleanup);
|
|
@ -36,8 +36,6 @@ public:
|
||||||
virtual const EntityItemPointer& getOwnerEntity() const = 0;
|
virtual const EntityItemPointer& getOwnerEntity() const = 0;
|
||||||
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0;
|
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0;
|
||||||
virtual bool updateArguments(QVariantMap arguments) = 0;
|
virtual bool updateArguments(QVariantMap arguments) = 0;
|
||||||
// virtual QByteArray serialize() = 0;
|
|
||||||
// static EntityActionPointer deserialize(EntityItemPointer ownerEntity, QByteArray data);
|
|
||||||
|
|
||||||
static EntityActionType actionTypeFromString(QString actionTypeString);
|
static EntityActionType actionTypeFromString(QString actionTypeString);
|
||||||
static QString actionTypeToString(EntityActionType actionType);
|
static QString actionTypeToString(EntityActionType actionType);
|
||||||
|
@ -52,6 +50,9 @@ protected:
|
||||||
virtual glm::vec3 getAngularVelocity() = 0;
|
virtual glm::vec3 getAngularVelocity() = 0;
|
||||||
virtual void setAngularVelocity(glm::vec3 angularVelocity) = 0;
|
virtual void setAngularVelocity(glm::vec3 angularVelocity) = 0;
|
||||||
|
|
||||||
|
// these look in the arguments map for a named argument. if it's not found or isn't well formed,
|
||||||
|
// ok will be set to false (note that it's never set to true -- set it to true before calling these).
|
||||||
|
// if required is true, failure to extract an argument will cause a warning to be printed.
|
||||||
static glm::vec3 extractVec3Argument (QString objectName, QVariantMap arguments,
|
static glm::vec3 extractVec3Argument (QString objectName, QVariantMap arguments,
|
||||||
QString argumentName, bool& ok, bool required = true);
|
QString argumentName, bool& ok, bool required = true);
|
||||||
static glm::quat extractQuatArgument (QString objectName, QVariantMap arguments,
|
static glm::quat extractQuatArgument (QString objectName, QVariantMap arguments,
|
||||||
|
|
|
@ -495,6 +495,12 @@ QUuid EntityScriptingInterface::addAction(const QString& actionTypeString,
|
||||||
QUuid actionID = QUuid::createUuid();
|
QUuid actionID = QUuid::createUuid();
|
||||||
auto actionFactory = DependencyManager::get<EntityActionFactoryInterface>();
|
auto actionFactory = DependencyManager::get<EntityActionFactoryInterface>();
|
||||||
bool success = actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) {
|
bool success = actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) {
|
||||||
|
// create this action even if the entity doesn't have physics info. it will often be the
|
||||||
|
// case that a script adds an action immediately after an object is created, and the physicsInfo
|
||||||
|
// is computed asynchronously.
|
||||||
|
// if (!entity->getPhysicsInfo()) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
EntityActionType actionType = EntityActionInterface::actionTypeFromString(actionTypeString);
|
EntityActionType actionType = EntityActionInterface::actionTypeFromString(actionTypeString);
|
||||||
if (actionType == ACTION_TYPE_NONE) {
|
if (actionType == ACTION_TYPE_NONE) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -34,7 +34,6 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
|
||||||
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
||||||
if (!physicsInfo) {
|
if (!physicsInfo) {
|
||||||
unlock();
|
unlock();
|
||||||
qDebug() << "ObjectActionSpring::updateActionWorker no physicsInfo";
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
||||||
|
@ -65,7 +64,7 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
|
||||||
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
|
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
|
||||||
// if qZero and qOne are too close to each other, we can get NaN for angle.
|
// if qZero and qOne are too close to each other, we can get NaN for angle.
|
||||||
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
|
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
|
||||||
const float almostOne = 0.99999;
|
const float almostOne = 0.99999f;
|
||||||
if (glm::abs(alignmentDot) < almostOne) {
|
if (glm::abs(alignmentDot) < almostOne) {
|
||||||
glm::quat target = _rotationalTarget;
|
glm::quat target = _rotationalTarget;
|
||||||
if (alignmentDot < 0) {
|
if (alignmentDot < 0) {
|
||||||
|
@ -75,12 +74,6 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
|
||||||
glm::quat deltaQ = target * qZeroInverse;
|
glm::quat deltaQ = target * qZeroInverse;
|
||||||
glm::vec3 axis = glm::axis(deltaQ);
|
glm::vec3 axis = glm::axis(deltaQ);
|
||||||
float angle = glm::angle(deltaQ);
|
float angle = glm::angle(deltaQ);
|
||||||
if (isNaN(angle)) {
|
|
||||||
qDebug() << "ObjectActionSpring::updateAction angle =" << angle
|
|
||||||
<< "body-rotation =" << bodyRotation.x << bodyRotation.y << bodyRotation.z << bodyRotation.w
|
|
||||||
<< "target-rotation ="
|
|
||||||
<< target.x << target.y << target.z<< target.w;
|
|
||||||
}
|
|
||||||
assert(!isNaN(angle));
|
assert(!isNaN(angle));
|
||||||
glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis);
|
glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis);
|
||||||
rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity));
|
rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity));
|
||||||
|
@ -130,7 +123,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) {
|
||||||
if (pscOk) {
|
if (pscOk) {
|
||||||
_linearTimeScale = linearTimeScale;
|
_linearTimeScale = linearTimeScale;
|
||||||
} else {
|
} else {
|
||||||
_linearTimeScale = 0.1;
|
_linearTimeScale = 0.1f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +134,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) {
|
||||||
if (rscOk) {
|
if (rscOk) {
|
||||||
_angularTimeScale = angularTimeScale;
|
_angularTimeScale = angularTimeScale;
|
||||||
} else {
|
} else {
|
||||||
_angularTimeScale = 0.1;
|
_angularTimeScale = 0.1f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -245,7 +245,7 @@ public:
|
||||||
void update(const UpdateFunctorPointer& updateFunctor) { _payload->update(updateFunctor); }
|
void update(const UpdateFunctorPointer& updateFunctor) { _payload->update(updateFunctor); }
|
||||||
|
|
||||||
// Shape Type Interface
|
// Shape Type Interface
|
||||||
const model::MaterialKey& getMaterialKey() const { return _payload->getMaterialKey(); }
|
const model::MaterialKey getMaterialKey() const { return _payload->getMaterialKey(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PayloadPointer _payload;
|
PayloadPointer _payload;
|
||||||
|
|
Loading…
Reference in a new issue