Showing the changes to libraries/physics/src/ObjectActionSpring.cpp (1 of 5 files). Show all
Unified · Split
| @@ -0,0 +1,107 @@ | ||
| 1 | +// | |
| 2 | +// ObjectActionSpring.cpp | |
| 3 | +// libraries/physics/src | |
| 4 | +// | |
| 5 | +// Created by Seth Alves 2015-6-5 | |
| 6 | +// Copyright 2015 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "ObjectMotionState.h" | |
| 13 | +#include "BulletUtil.h" | |
| 14 | + | |
| 15 | +#include "ObjectActionSpring.h" | |
| 16 | + | |
| 17 | +ObjectActionSpring::ObjectActionSpring(QUuid id, EntityItemPointer ownerEntity) : | |
| 18 | + ObjectAction(id, ownerEntity) { | |
| 19 | + #if WANT_DEBUG | |
| 20 | + qDebug() << "ObjectActionSpring::ObjectActionSpring"; | |
| 21 | + #endif | |
| 22 | +} | |
| 23 | + | |
| 24 | +ObjectActionSpring::~ObjectActionSpring() { | |
| 25 | + #if WANT_DEBUG | |
| 26 | + qDebug() << "ObjectActionSpring::~ObjectActionSpring"; | |
| 27 | + #endif | |
| 28 | +} | |
| 29 | + | |
| 30 | +void ObjectActionSpring::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) { | |
| 31 | + if (!tryLockForRead()) { | |
| 32 | + // don't risk hanging the thread running the physics simulation | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + void* physicsInfo = _ownerEntity->getPhysicsInfo(); | |
| 36 | + | |
| 37 | + if (_active && physicsInfo) { | |
| 38 | + ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo); | |
| 39 | + btRigidBody* rigidBody = motionState->getRigidBody(); | |
| 40 | + if (rigidBody) { | |
| 41 | + glm::vec3 offset = _positionalTarget - bulletToGLM(rigidBody->getCenterOfMassPosition()); | |
| 42 | + | |
| 43 | + | |
| 44 | + // btQuaternion getOrientation() const; | |
| 45 | + // const btTransform& getCenterOfMassTransform() const; | |
| 46 | + | |
| 47 | + float offsetLength = glm::length(offset); | |
| 48 | + float speed = offsetLength; // XXX use _positionalSpringConstant | |
| 49 | + | |
| 50 | + float interpolation_value = 0.5; // XXX | |
| 51 | + const glm::quat slerped_quat = glm::slerp(bulletToGLM(rigidBody->getOrientation()), | |
| 52 | + _rotationalTarget, | |
| 53 | + interpolation_value); | |
| 54 | + | |
| 55 | + if (offsetLength > IGNORE_POSITION_DELTA) { | |
| 56 | + glm::vec3 newVelocity = glm::normalize(offset) * speed; | |
| 57 | + rigidBody->setLinearVelocity(glmToBullet(newVelocity)); | |
| 58 | + // void setAngularVelocity (const btVector3 &ang_vel); | |
| 59 | + rigidBody->activate(); | |
| 60 | + } else { | |
| 61 | + rigidBody->setLinearVelocity(glmToBullet(glm::vec3())); | |
| 62 | + } | |
| 63 | + } | |
| 64 | + } | |
| 65 | + unlock(); | |
| 66 | +} | |
| 67 | + | |
| 68 | + | |
| 69 | +bool ObjectActionSpring::updateArguments(QVariantMap arguments) { | |
| 70 | + // targets are required, spring-constants are optional | |
| 71 | + bool ok = true; | |
| 72 | + glm::vec3 positionalTarget = | |
| 73 | + EntityActionInterface::extractVec3Argument("spring action", arguments, "positionalTarget", ok); | |
| 74 | + bool pscOK = true; | |
| 75 | + float positionalSpringConstant = | |
| 76 | + EntityActionInterface::extractFloatArgument("spring action", arguments, "positionalSpringConstant", pscOK); | |
| 77 | + | |
| 78 | + glm::quat rotationalTarget = | |
| 79 | + EntityActionInterface::extractQuatArgument("spring action", arguments, "rotationalTarget", ok); | |
| 80 | + bool rscOK = true; | |
| 81 | + float rotationalSpringConstant = | |
| 82 | + EntityActionInterface::extractFloatArgument("spring action", arguments, "rotationalSpringConstant", rscOK); | |
| 83 | + | |
| 84 | + if (!ok) { | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + | |
| 88 | + lockForWrite(); | |
| 89 | + | |
| 90 | + _positionalTarget = positionalTarget; | |
| 91 | + if (pscOK) { | |
| 92 | + _positionalSpringConstant = positionalSpringConstant; | |
| 93 | + } else { | |
| 94 | + _positionalSpringConstant = 0.5; // XXX pick a good default; | |
| 95 | + } | |
| 96 | + | |
| 97 | + _rotationalTarget = rotationalTarget; | |
| 98 | + if (rscOK) { | |
| 99 | + _rotationalSpringConstant = rotationalSpringConstant; | |
| 100 | + } else { | |
| 101 | + _rotationalSpringConstant = 0.5; // XXX pick a good default; | |
| 102 | + } | |
| 103 | + | |
| 104 | + _active = true; | |
| 105 | + unlock(); | |
| 106 | + return true; | |
| 107 | +} | |