mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 09:08:37 +02:00
fix some glitchy behavior during rotation
This commit is contained in:
parent
ccb2f17b33
commit
23dab530f9
2 changed files with 27 additions and 39 deletions
|
@ -21,7 +21,6 @@ var ANGULAR_DAMPING_RATE = 0.40;
|
||||||
var gIsGrabbing = false;
|
var gIsGrabbing = false;
|
||||||
var gGrabbedEntity = null;
|
var gGrabbedEntity = null;
|
||||||
var gActionID = null;
|
var gActionID = null;
|
||||||
var gPrevMouse = {x: 0, y: 0};
|
|
||||||
var gEntityProperties;
|
var gEntityProperties;
|
||||||
var gStartPosition;
|
var gStartPosition;
|
||||||
var gStartRotation;
|
var gStartRotation;
|
||||||
|
@ -259,13 +258,12 @@ function mouseMoveEvent(event) {
|
||||||
var deltaMouse = { x: 0, y: 0 };
|
var deltaMouse = { x: 0, y: 0 };
|
||||||
var dx = event.x - gPreviousMouse.x;
|
var dx = event.x - gPreviousMouse.x;
|
||||||
var dy = event.y - gPreviousMouse.y;
|
var dy = event.y - gPreviousMouse.y;
|
||||||
|
|
||||||
var orientation = Camera.getOrientation();
|
var orientation = Camera.getOrientation();
|
||||||
var dragOffset = Vec3.multiply(dx, Quat.getRight(orientation));
|
var dragOffset = Vec3.multiply(dx, Quat.getRight(orientation));
|
||||||
dragOffset = Vec3.sum(dragOffset, Vec3.multiply(-dy, Quat.getUp(orientation)));
|
dragOffset = Vec3.sum(dragOffset, Vec3.multiply(-dy, Quat.getUp(orientation)));
|
||||||
var axis = Vec3.cross(dragOffset, Quat.getFront(orientation));
|
var axis = Vec3.cross(dragOffset, Quat.getFront(orientation));
|
||||||
axis = Vec3.normalize(axis);
|
axis = Vec3.normalize(axis);
|
||||||
var ROTATE_STRENGTH = 8.0; // magic number tuned by hand
|
var ROTATE_STRENGTH = 16.0; // magic number tuned by hand
|
||||||
var angle = ROTATE_STRENGTH * Math.sqrt((dx * dx) + (dy * dy));
|
var angle = ROTATE_STRENGTH * Math.sqrt((dx * dx) + (dy * dy));
|
||||||
var deltaQ = Quat.angleAxis(angle, axis);
|
var deltaQ = Quat.angleAxis(angle, axis);
|
||||||
var qZero = entityProperties.rotation;
|
var qZero = entityProperties.rotation;
|
||||||
|
@ -327,37 +325,8 @@ function keyPressEvent(event) {
|
||||||
computeNewGrabPlane();
|
computeNewGrabPlane();
|
||||||
}
|
}
|
||||||
|
|
||||||
// function update(deltaTime) {
|
|
||||||
// if (!gIsGrabbing) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var entityProperties = Entities.getEntityProperties(gGrabbedEntity);
|
|
||||||
// gCurrentPosition = entityProperties.position;
|
|
||||||
// if (gGrabMode === "rotate") {
|
|
||||||
// gAngularVelocity = Vec3.subtract(gAngularVelocity, Vec3.multiply(gAngularVelocity, ANGULAR_DAMPING_RATE));
|
|
||||||
// Entities.editEntity(gGrabbedEntity, { angularVelocity: gAngularVelocity, });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // always push toward linear grab position, even when rotating
|
|
||||||
// var newVelocity = ZERO_VEC3;
|
|
||||||
// var dPosition = Vec3.subtract(gTargetPosition, gCurrentPosition);
|
|
||||||
// var delta = Vec3.length(dPosition);
|
|
||||||
// if (delta > CLOSE_ENOUGH) {
|
|
||||||
// var MAX_POSITION_DELTA = 4.0;
|
|
||||||
// if (delta > MAX_POSITION_DELTA) {
|
|
||||||
// dPosition = Vec3.multiply(dPosition, MAX_POSITION_DELTA / delta);
|
|
||||||
// }
|
|
||||||
// // desired speed is proportional to displacement by the inverse of timescale
|
|
||||||
// // (for critically damped motion)
|
|
||||||
// newVelocity = Vec3.multiply(dPosition, INV_MOVE_TIMESCALE);
|
|
||||||
// }
|
|
||||||
// Entities.editEntity(gGrabbedEntity, { velocity: newVelocity, });
|
|
||||||
// }
|
|
||||||
|
|
||||||
Controller.mouseMoveEvent.connect(mouseMoveEvent);
|
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);
|
||||||
Controller.keyReleaseEvent.connect(keyReleaseEvent);
|
Controller.keyReleaseEvent.connect(keyReleaseEvent);
|
||||||
// Script.update.connect(update);
|
|
||||||
|
|
|
@ -60,13 +60,32 @@ void ObjectActionSpring::updateAction(btCollisionWorld* collisionWorld, btScalar
|
||||||
|
|
||||||
// handle rotation
|
// handle rotation
|
||||||
if (_rotationalTargetSet) {
|
if (_rotationalTargetSet) {
|
||||||
glm::quat qZeroInverse = glm::inverse(bulletToGLM(rigidBody->getOrientation()));
|
glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation());
|
||||||
glm::quat deltaQ = _rotationalTarget * qZeroInverse;
|
// if qZero and qOne are too close to each other, we can get NaN for angle.
|
||||||
glm::vec3 axis = glm::axis(deltaQ);
|
auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget);
|
||||||
float angle = glm::angle(deltaQ);
|
const float almostOne = 0.99999;
|
||||||
glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis);
|
if (glm::abs(alignmentDot) < almostOne) {
|
||||||
rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity));
|
glm::quat target = _rotationalTarget;
|
||||||
rigidBody->activate();
|
if (alignmentDot < 0) {
|
||||||
|
target = -target;
|
||||||
|
}
|
||||||
|
glm::quat qZeroInverse = glm::inverse(bodyRotation);
|
||||||
|
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));
|
||||||
|
rigidBody->activate();
|
||||||
|
} else {
|
||||||
|
rigidBody->setAngularVelocity(glmToBullet(glm::vec3(0.0f)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue