respond to code review, add a simple hold-a-stick script

This commit is contained in:
Seth Alves 2015-06-10 18:48:51 -07:00
parent eccf4eb8a8
commit 45c7cd4929
5 changed files with 31 additions and 13 deletions

18
examples/stick.js Normal file
View 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);

View file

@ -36,8 +36,6 @@ public:
virtual const EntityItemPointer& getOwnerEntity() const = 0;
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0;
virtual bool updateArguments(QVariantMap arguments) = 0;
// virtual QByteArray serialize() = 0;
// static EntityActionPointer deserialize(EntityItemPointer ownerEntity, QByteArray data);
static EntityActionType actionTypeFromString(QString actionTypeString);
static QString actionTypeToString(EntityActionType actionType);
@ -52,6 +50,9 @@ protected:
virtual glm::vec3 getAngularVelocity() = 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,
QString argumentName, bool& ok, bool required = true);
static glm::quat extractQuatArgument (QString objectName, QVariantMap arguments,

View file

@ -495,6 +495,12 @@ QUuid EntityScriptingInterface::addAction(const QString& actionTypeString,
QUuid actionID = QUuid::createUuid();
auto actionFactory = DependencyManager::get<EntityActionFactoryInterface>();
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);
if (actionType == ACTION_TYPE_NONE) {
return false;

View file

@ -34,7 +34,6 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
void* physicsInfo = _ownerEntity->getPhysicsInfo();
if (!physicsInfo) {
unlock();
qDebug() << "ObjectActionSpring::updateActionWorker no physicsInfo";
return;
}
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
@ -65,7 +64,7 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
// if qZero and qOne are too close to each other, we can get NaN for angle.
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
const float almostOne = 0.99999;
const float almostOne = 0.99999f;
if (glm::abs(alignmentDot) < almostOne) {
glm::quat target = _rotationalTarget;
if (alignmentDot < 0) {
@ -75,12 +74,6 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
glm::quat deltaQ = target * qZeroInverse;
glm::vec3 axis = glm::axis(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));
glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis);
rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity));
@ -130,7 +123,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) {
if (pscOk) {
_linearTimeScale = linearTimeScale;
} else {
_linearTimeScale = 0.1;
_linearTimeScale = 0.1f;
}
}
@ -141,7 +134,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) {
if (rscOk) {
_angularTimeScale = angularTimeScale;
} else {
_angularTimeScale = 0.1;
_angularTimeScale = 0.1f;
}
}

View file

@ -245,7 +245,7 @@ public:
void update(const UpdateFunctorPointer& updateFunctor) { _payload->update(updateFunctor); }
// Shape Type Interface
const model::MaterialKey& getMaterialKey() const { return _payload->getMaterialKey(); }
const model::MaterialKey getMaterialKey() const { return _payload->getMaterialKey(); }
protected:
PayloadPointer _payload;