mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 17:39:26 +02:00
pull some common code out of subclasses and into ObjectAction
This commit is contained in:
parent
6cce845984
commit
b1a209b9db
7 changed files with 90 additions and 98 deletions
|
@ -1012,7 +1012,9 @@ void EntityTreeRenderer::deletingEntity(const EntityItemID& entityID) {
|
||||||
void EntityTreeRenderer::addingEntity(const EntityItemID& entityID) {
|
void EntityTreeRenderer::addingEntity(const EntityItemID& entityID) {
|
||||||
checkAndCallPreload(entityID);
|
checkAndCallPreload(entityID);
|
||||||
auto entity = static_cast<EntityTree*>(_tree)->findEntityByID(entityID);
|
auto entity = static_cast<EntityTree*>(_tree)->findEntityByID(entityID);
|
||||||
addEntityToScene(entity);
|
if (entity) {
|
||||||
|
addEntityToScene(entity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntityTreeRenderer::addEntityToScene(EntityItemPointer entity) {
|
void EntityTreeRenderer::addEntityToScene(EntityItemPointer entity) {
|
||||||
|
|
|
@ -24,7 +24,27 @@ ObjectAction::~ObjectAction() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
||||||
qDebug() << "ObjectAction::updateAction called";
|
if (!_active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_ownerEntity) {
|
||||||
|
qDebug() << "ObjectActionPullToPoint::updateAction no owner entity";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!tryLockForRead()) {
|
||||||
|
// don't risk hanging the thread running the physics simulation
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
||||||
|
|
||||||
|
if (physicsInfo) {
|
||||||
|
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
||||||
|
btRigidBody* rigidBody = motionState->getRigidBody();
|
||||||
|
if (rigidBody) {
|
||||||
|
updateActionWorker(collisionWorld, deltaTimeStep, motionState, rigidBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectAction::debugDraw(btIDebugDraw* debugDrawer) {
|
void ObjectAction::debugDraw(btIDebugDraw* debugDrawer) {
|
||||||
|
|
|
@ -13,11 +13,14 @@
|
||||||
#ifndef hifi_ObjectAction_h
|
#ifndef hifi_ObjectAction_h
|
||||||
#define hifi_ObjectAction_h
|
#define hifi_ObjectAction_h
|
||||||
|
|
||||||
#include <btBulletDynamicsCommon.h>
|
|
||||||
|
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
|
|
||||||
|
#include <btBulletDynamicsCommon.h>
|
||||||
|
|
||||||
#include <EntityItem.h>
|
#include <EntityItem.h>
|
||||||
|
#include "ObjectMotionState.h"
|
||||||
|
#include "BulletUtil.h"
|
||||||
|
|
||||||
|
|
||||||
class ObjectAction : public btActionInterface, public EntityActionInterface {
|
class ObjectAction : public btActionInterface, public EntityActionInterface {
|
||||||
public:
|
public:
|
||||||
|
@ -30,6 +33,10 @@ public:
|
||||||
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; }
|
virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; }
|
||||||
virtual bool updateArguments(QVariantMap arguments) { return false; }
|
virtual bool updateArguments(QVariantMap arguments) { return false; }
|
||||||
|
|
||||||
|
// this is called from updateAction and should be overridden by subclasses
|
||||||
|
virtual void updateActionWorker(btCollisionWorld* collisionWorld, btScalar deltaTimeStep,
|
||||||
|
ObjectMotionState* motionState, btRigidBody* rigidBody) {}
|
||||||
|
|
||||||
// these are from btActionInterface
|
// these are from btActionInterface
|
||||||
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
||||||
virtual void debugDraw(btIDebugDraw* debugDrawer);
|
virtual void debugDraw(btIDebugDraw* debugDrawer);
|
||||||
|
|
|
@ -9,9 +9,6 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "ObjectMotionState.h"
|
|
||||||
#include "BulletUtil.h"
|
|
||||||
|
|
||||||
#include "ObjectActionPullToPoint.h"
|
#include "ObjectActionPullToPoint.h"
|
||||||
|
|
||||||
ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity) :
|
ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity) :
|
||||||
|
@ -27,33 +24,17 @@ ObjectActionPullToPoint::~ObjectActionPullToPoint() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
void ObjectActionPullToPoint::updateActionWorker(btCollisionWorld* collisionWorld, btScalar deltaTimeStep,
|
||||||
if (!_ownerEntity) {
|
ObjectMotionState* motionState, btRigidBody* rigidBody) {
|
||||||
qDebug() << "ObjectActionPullToPoint::updateAction no owner entity";
|
glm::vec3 offset = _target - bulletToGLM(rigidBody->getCenterOfMassPosition());
|
||||||
return;
|
float offsetLength = glm::length(offset);
|
||||||
|
if (offsetLength > IGNORE_POSITION_DELTA) {
|
||||||
|
glm::vec3 newVelocity = glm::normalize(offset) * _speed;
|
||||||
|
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
|
||||||
|
rigidBody->activate();
|
||||||
|
} else {
|
||||||
|
rigidBody->setLinearVelocity(glmToBullet(glm::vec3()));
|
||||||
}
|
}
|
||||||
if (!tryLockForRead()) {
|
|
||||||
// don't risk hanging the thread running the physics simulation
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
|
||||||
|
|
||||||
if (_active && physicsInfo) {
|
|
||||||
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
|
||||||
btRigidBody* rigidBody = motionState->getRigidBody();
|
|
||||||
if (rigidBody) {
|
|
||||||
glm::vec3 offset = _target - bulletToGLM(rigidBody->getCenterOfMassPosition());
|
|
||||||
float offsetLength = glm::length(offset);
|
|
||||||
if (offsetLength > IGNORE_POSITION_DELTA) {
|
|
||||||
glm::vec3 newVelocity = glm::normalize(offset) * _speed;
|
|
||||||
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
|
|
||||||
rigidBody->activate();
|
|
||||||
} else {
|
|
||||||
rigidBody->setLinearVelocity(glmToBullet(glm::vec3()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,8 @@ public:
|
||||||
virtual ~ObjectActionPullToPoint();
|
virtual ~ObjectActionPullToPoint();
|
||||||
|
|
||||||
virtual bool updateArguments(QVariantMap arguments);
|
virtual bool updateArguments(QVariantMap arguments);
|
||||||
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
virtual void updateActionWorker(btCollisionWorld* collisionWorld, btScalar deltaTimeStep,
|
||||||
|
ObjectMotionState* motionState, btRigidBody* rigidBody);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,6 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "ObjectMotionState.h"
|
|
||||||
#include "BulletUtil.h"
|
|
||||||
|
|
||||||
#include "ObjectActionSpring.h"
|
#include "ObjectActionSpring.h"
|
||||||
|
|
||||||
ObjectActionSpring::ObjectActionSpring(QUuid id, EntityItemPointer ownerEntity) :
|
ObjectActionSpring::ObjectActionSpring(QUuid id, EntityItemPointer ownerEntity) :
|
||||||
|
@ -27,69 +24,52 @@ ObjectActionSpring::~ObjectActionSpring() {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectActionSpring::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
void ObjectActionSpring::updateActionWorker(btCollisionWorld* collisionWorld, btScalar deltaTimeStep,
|
||||||
if (!_ownerEntity) {
|
ObjectMotionState* motionState, btRigidBody* rigidBody) {
|
||||||
qDebug() << "ObjectActionSpring::updateAction no owner entity";
|
// handle the linear part
|
||||||
return;
|
if (_positionalTargetSet) {
|
||||||
}
|
glm::vec3 offset = _positionalTarget - bulletToGLM(rigidBody->getCenterOfMassPosition());
|
||||||
if (!tryLockForRead()) {
|
float offsetLength = glm::length(offset);
|
||||||
// don't risk hanging the thread running the physics simulation
|
float speed = offsetLength / _linearTimeScale;
|
||||||
return;
|
|
||||||
}
|
|
||||||
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
|
||||||
|
|
||||||
if (_active && physicsInfo) {
|
if (offsetLength > IGNORE_POSITION_DELTA) {
|
||||||
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
glm::vec3 newVelocity = glm::normalize(offset) * speed;
|
||||||
btRigidBody* rigidBody = motionState->getRigidBody();
|
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
|
||||||
if (rigidBody) {
|
rigidBody->activate();
|
||||||
// handle the linear part
|
} else {
|
||||||
if (_positionalTargetSet) {
|
rigidBody->setLinearVelocity(glmToBullet(glm::vec3(0.0f)));
|
||||||
glm::vec3 offset = _positionalTarget - bulletToGLM(rigidBody->getCenterOfMassPosition());
|
}
|
||||||
float offsetLength = glm::length(offset);
|
}
|
||||||
float speed = offsetLength / _linearTimeScale;
|
|
||||||
|
// handle rotation
|
||||||
if (offsetLength > IGNORE_POSITION_DELTA) {
|
if (_rotationalTargetSet) {
|
||||||
glm::vec3 newVelocity = glm::normalize(offset) * speed;
|
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
|
||||||
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
|
// if qZero and qOne are too close to each other, we can get NaN for angle.
|
||||||
// void setAngularVelocity (const btVector3 &ang_vel);
|
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
|
||||||
rigidBody->activate();
|
const float almostOne = 0.99999;
|
||||||
} else {
|
if (glm::abs(alignmentDot) < almostOne) {
|
||||||
rigidBody->setLinearVelocity(glmToBullet(glm::vec3()));
|
glm::quat target = _rotationalTarget;
|
||||||
}
|
if (alignmentDot < 0) {
|
||||||
}
|
target = -target;
|
||||||
|
}
|
||||||
// handle rotation
|
glm::quat qZeroInverse = glm::inverse(bodyRotation);
|
||||||
if (_rotationalTargetSet) {
|
glm::quat deltaQ = target * qZeroInverse;
|
||||||
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
|
glm::vec3 axis = glm::axis(deltaQ);
|
||||||
// if qZero and qOne are too close to each other, we can get NaN for angle.
|
float angle = glm::angle(deltaQ);
|
||||||
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
|
if (isNaN(angle)) {
|
||||||
const float almostOne = 0.99999;
|
qDebug() << "ObjectActionSpring::updateAction angle =" << angle
|
||||||
if (glm::abs(alignmentDot) < almostOne) {
|
<< "body-rotation =" << bodyRotation.x << bodyRotation.y << bodyRotation.z << bodyRotation.w
|
||||||
glm::quat target = _rotationalTarget;
|
<< "target-rotation ="
|
||||||
if (alignmentDot < 0) {
|
<< target.x << target.y << target.z<< target.w;
|
||||||
target = -target;
|
}
|
||||||
}
|
assert(!isNaN(angle));
|
||||||
glm::quat qZeroInverse = glm::inverse(bodyRotation);
|
glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis);
|
||||||
glm::quat deltaQ = target * qZeroInverse;
|
rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity));
|
||||||
glm::vec3 axis = glm::axis(deltaQ);
|
rigidBody->activate();
|
||||||
float angle = glm::angle(deltaQ);
|
} else {
|
||||||
if (isNaN(angle)) {
|
rigidBody->setAngularVelocity(glmToBullet(glm::vec3(0.0f)));
|
||||||
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));
|
|
||||||
rigidBody->activate();
|
|
||||||
} else {
|
|
||||||
rigidBody->setAngularVelocity(glmToBullet(glm::vec3(0.0f)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,8 @@ public:
|
||||||
virtual ~ObjectActionSpring();
|
virtual ~ObjectActionSpring();
|
||||||
|
|
||||||
virtual bool updateArguments(QVariantMap arguments);
|
virtual bool updateArguments(QVariantMap arguments);
|
||||||
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
virtual void updateActionWorker(btCollisionWorld* collisionWorld, btScalar deltaTimeStep,
|
||||||
|
ObjectMotionState* motionState, btRigidBody* rigidBody);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue