mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 02:16:51 +02:00
update grab.js to use pull-to-point action, various other fixes
This commit is contained in:
parent
9a67f35b59
commit
b2db5f7fee
6 changed files with 47 additions and 19 deletions
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
var isGrabbing = false;
|
var isGrabbing = false;
|
||||||
var grabbedEntity = null;
|
var grabbedEntity = null;
|
||||||
|
var actionID = null;
|
||||||
var prevMouse = {};
|
var prevMouse = {};
|
||||||
var deltaMouse = {
|
var deltaMouse = {
|
||||||
z: 0
|
z: 0
|
||||||
|
@ -88,6 +89,7 @@ function mousePressEvent(event) {
|
||||||
gravity: {x: 0, y: 0, z: 0}
|
gravity: {x: 0, y: 0, z: 0}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +112,10 @@ function updateDropLine(position) {
|
||||||
|
|
||||||
function mouseReleaseEvent() {
|
function mouseReleaseEvent() {
|
||||||
if (isGrabbing) {
|
if (isGrabbing) {
|
||||||
|
Controller.mouseMoveEvent.disconnect(mouseMoveEvent);
|
||||||
isGrabbing = false;
|
isGrabbing = false;
|
||||||
|
Entities.deleteAction(grabbedEntity, actionID);
|
||||||
|
actionID = null;
|
||||||
|
|
||||||
// only restore the original gravity if it's not zero. This is to avoid...
|
// only restore the original gravity if it's not zero. This is to avoid...
|
||||||
// 1. interface A grabs an entity and locally saves off its gravity
|
// 1. interface A grabs an entity and locally saves off its gravity
|
||||||
|
@ -228,21 +233,25 @@ function update(deltaTime) {
|
||||||
}
|
}
|
||||||
if (shouldRotate) {
|
if (shouldRotate) {
|
||||||
angularVelocity = Vec3.subtract(angularVelocity, Vec3.multiply(angularVelocity, ANGULAR_DAMPING_RATE));
|
angularVelocity = Vec3.subtract(angularVelocity, Vec3.multiply(angularVelocity, ANGULAR_DAMPING_RATE));
|
||||||
|
Entities.editEntity(grabbedEntity, {
|
||||||
|
rotation: currentRotation,
|
||||||
|
angularVelocity: angularVelocity
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
angularVelocity = entityProps.angularVelocity;
|
angularVelocity = entityProps.angularVelocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
Entities.editEntity(grabbedEntity, {
|
var newSpeed = Vec3.length(newVelocity);
|
||||||
position: currentPosition,
|
if (!actionID) {
|
||||||
rotation: currentRotation,
|
actionID = Entities.addAction("pull-to-point", grabbedEntity, {target: targetPosition, speed: newSpeed});
|
||||||
velocity: newVelocity,
|
} else {
|
||||||
angularVelocity: angularVelocity
|
Entities.updateAction(grabbedEntity, actionID, {target: targetPosition, speed: newSpeed});
|
||||||
});
|
}
|
||||||
|
|
||||||
updateDropLine(targetPosition);
|
updateDropLine(targetPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
|
||||||
Controller.mousePressEvent.connect(mousePressEvent);
|
Controller.mousePressEvent.connect(mousePressEvent);
|
||||||
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
|
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
|
||||||
Controller.keyPressEvent.connect(keyPressEvent);
|
Controller.keyPressEvent.connect(keyPressEvent);
|
||||||
|
|
|
@ -152,6 +152,7 @@ public:
|
||||||
bool hasEntities() const { return _entityItems ? _entityItems->size() > 0 : false; }
|
bool hasEntities() const { return _entityItems ? _entityItems->size() > 0 : false; }
|
||||||
|
|
||||||
void setTree(EntityTree* tree) { _myTree = tree; }
|
void setTree(EntityTree* tree) { _myTree = tree; }
|
||||||
|
EntityTree* getTree() const { return _myTree; }
|
||||||
|
|
||||||
bool updateEntity(const EntityItem& entity);
|
bool updateEntity(const EntityItem& entity);
|
||||||
void addEntityItem(EntityItemPointer entity);
|
void addEntityItem(EntityItemPointer entity);
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
ObjectAction::ObjectAction(QUuid id, EntityItemPointer ownerEntity) :
|
ObjectAction::ObjectAction(QUuid id, EntityItemPointer ownerEntity) :
|
||||||
btActionInterface(),
|
btActionInterface(),
|
||||||
_id(id),
|
_id(id),
|
||||||
|
_active(false),
|
||||||
_ownerEntity(ownerEntity) {
|
_ownerEntity(ownerEntity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,14 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QUuid _id;
|
QUuid _id;
|
||||||
|
QReadWriteLock _lock;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool tryLockForRead() { return _lock.tryLockForRead(); }
|
||||||
|
void lockForWrite() { _lock.lockForWrite(); }
|
||||||
|
void unlock() { _lock.unlock(); }
|
||||||
|
|
||||||
|
bool _active;
|
||||||
EntityItemPointer _ownerEntity;
|
EntityItemPointer _ownerEntity;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -16,33 +16,40 @@
|
||||||
|
|
||||||
ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity) :
|
ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity) :
|
||||||
ObjectAction(id, ownerEntity) {
|
ObjectAction(id, ownerEntity) {
|
||||||
|
#if WANT_DEBUG
|
||||||
qDebug() << "ObjectActionPullToPoint::ObjectActionPullToPoint";
|
qDebug() << "ObjectActionPullToPoint::ObjectActionPullToPoint";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectActionPullToPoint::~ObjectActionPullToPoint() {
|
ObjectActionPullToPoint::~ObjectActionPullToPoint() {
|
||||||
|
#if WANT_DEBUG
|
||||||
qDebug() << "ObjectActionPullToPoint::~ObjectActionPullToPoint";
|
qDebug() << "ObjectActionPullToPoint::~ObjectActionPullToPoint";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
||||||
glm::vec3 offset = _target - _ownerEntity->getPosition();
|
if (!tryLockForRead()) {
|
||||||
|
// don't risk hanging the thread running the physics simulation
|
||||||
if (glm::length(offset) < IGNORE_POSITION_DELTA) {
|
return;
|
||||||
offset = glm::vec3(0.0f, 0.0f, 0.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 newVelocity = glm::normalize(offset) * _speed;
|
|
||||||
|
|
||||||
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
void* physicsInfo = _ownerEntity->getPhysicsInfo();
|
||||||
if (physicsInfo) {
|
|
||||||
|
if (_active && physicsInfo) {
|
||||||
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
|
||||||
btRigidBody* rigidBody = motionState->getRigidBody();
|
btRigidBody* rigidBody = motionState->getRigidBody();
|
||||||
if (rigidBody) {
|
if (rigidBody) {
|
||||||
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
unlock();
|
||||||
_ownerEntity->updateVelocity(newVelocity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,8 +58,11 @@ bool ObjectActionPullToPoint::updateArguments(QVariantMap arguments) {
|
||||||
glm::vec3 target = EntityActionInterface::extractVec3Argument("pull-to-point action", arguments, "target", ok);
|
glm::vec3 target = EntityActionInterface::extractVec3Argument("pull-to-point action", arguments, "target", ok);
|
||||||
float speed = EntityActionInterface::extractFloatArgument("pull-to-point action", arguments, "speed", ok);
|
float speed = EntityActionInterface::extractFloatArgument("pull-to-point action", arguments, "speed", ok);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
|
lockForWrite();
|
||||||
_target = target;
|
_target = target;
|
||||||
_speed = speed;
|
_speed = speed;
|
||||||
|
_active = true;
|
||||||
|
unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -26,6 +26,7 @@ public:
|
||||||
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
glm::vec3 _target;
|
glm::vec3 _target;
|
||||||
float _speed;
|
float _speed;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue