mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 12:57:59 +02:00
convert grab.js to use spring action. rotation doesn't work right, yet
This commit is contained in:
parent
eb85f7f818
commit
14a45e8349
3 changed files with 65 additions and 39 deletions
|
@ -20,6 +20,7 @@ var ANGULAR_DAMPING_RATE = 0.40;
|
|||
// NOTE: to improve readability global variable names start with 'g'
|
||||
var gIsGrabbing = false;
|
||||
var gGrabbedEntity = null;
|
||||
var gActionID = null;
|
||||
var gPrevMouse = {x: 0, y: 0};
|
||||
var gEntityProperties;
|
||||
var gStartPosition;
|
||||
|
@ -193,6 +194,7 @@ function mousePressEvent(event) {
|
|||
var cameraPosition = Camera.getPosition();
|
||||
|
||||
gBeaconHeight = Vec3.length(entityProperties.dimensions);
|
||||
print("gBeaconHeight = " + gBeaconHeight);
|
||||
gMaxGrabDistance = gBeaconHeight / MAX_SOLID_ANGLE;
|
||||
if (Vec3.distance(objectPosition, cameraPosition) > gMaxGrabDistance) {
|
||||
// don't allow grabs of things far away
|
||||
|
@ -231,6 +233,8 @@ function mouseReleaseEvent() {
|
|||
}
|
||||
|
||||
gIsGrabbing = false
|
||||
Entities.deleteAction(grabbedEntity, gActionID);
|
||||
gActionID = null;
|
||||
|
||||
Overlays.editOverlay(gBeacon, { visible: false });
|
||||
|
||||
|
@ -250,6 +254,8 @@ function mouseMoveEvent(event) {
|
|||
gOriginalGravity = entityProperties.gravity;
|
||||
}
|
||||
|
||||
var actionArgs;
|
||||
|
||||
if (gGrabMode === "rotate") {
|
||||
var deltaMouse = { x: 0, y: 0 };
|
||||
var dx = event.x - gPreviousMouse.x;
|
||||
|
@ -259,9 +265,12 @@ function mouseMoveEvent(event) {
|
|||
var dragOffset = Vec3.multiply(dx, Quat.getRight(orientation));
|
||||
dragOffset = Vec3.sum(dragOffset, Vec3.multiply(-dy, Quat.getUp(orientation)));
|
||||
var axis = Vec3.cross(dragOffset, Quat.getFront(orientation));
|
||||
var axis = Vec3.normalize(axis);
|
||||
var ROTATE_STRENGTH = 8.0; // magic number tuned by hand
|
||||
gAngularVelocity = Vec3.multiply(ROTATE_STRENGTH, axis);
|
||||
// var axis = Vec3.normalize(axis);
|
||||
// var ROTATE_STRENGTH = 8.0; // magic number tuned by hand
|
||||
// gAngularVelocity = Vec3.multiply(ROTATE_STRENGTH, axis);
|
||||
|
||||
var targetRotation = Quat.angleAxis(Vec3.length(axis), Vec3.normalize(axis));
|
||||
actionArgs = {targetRotation: targetRotation, angularTimeScale: 1.0};
|
||||
} else {
|
||||
var newTargetPosition;
|
||||
if (gGrabMode === "verticalCylinder") {
|
||||
|
@ -284,9 +293,18 @@ function mouseMoveEvent(event) {
|
|||
}
|
||||
}
|
||||
gTargetPosition = Vec3.sum(newTargetPosition, gGrabOffset);
|
||||
actionArgs = {targetPosition: gTargetPosition, linearTimeScale: 0.1};
|
||||
}
|
||||
gPreviousMouse = { x: event.x, y: event.y };
|
||||
gMouseCursorLocation = { x: Window.getCursorPositionX(), y: Window.getCursorPositionY() };
|
||||
|
||||
if (!gActionID) {
|
||||
gActionID = Entities.addAction("spring", gGrabbedEntity, actionArgs);
|
||||
} else {
|
||||
Entities.updateAction(gGrabbedEntity, gActionID, actionArgs);
|
||||
}
|
||||
|
||||
updateDropLine(gTargetPosition);
|
||||
}
|
||||
|
||||
function keyReleaseEvent(event) {
|
||||
|
@ -309,38 +327,37 @@ function keyPressEvent(event) {
|
|||
computeNewGrabPlane();
|
||||
}
|
||||
|
||||
function update(deltaTime) {
|
||||
if (!gIsGrabbing) {
|
||||
return;
|
||||
}
|
||||
// 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, });
|
||||
}
|
||||
// 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, });
|
||||
updateDropLine(gTargetPosition);
|
||||
}
|
||||
// // 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.mousePressEvent.connect(mousePressEvent);
|
||||
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
|
||||
Controller.keyPressEvent.connect(keyPressEvent);
|
||||
Controller.keyReleaseEvent.connect(keyReleaseEvent);
|
||||
Script.update.connect(update);
|
||||
// Script.update.connect(update);
|
||||
|
|
|
@ -28,6 +28,10 @@ ObjectActionPullToPoint::~ObjectActionPullToPoint() {
|
|||
}
|
||||
|
||||
void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
||||
if (!_ownerEntity) {
|
||||
qDebug() << "ObjectActionPullToPoint::updateAction no owner entity";
|
||||
return;
|
||||
}
|
||||
if (!tryLockForRead()) {
|
||||
// don't risk hanging the thread running the physics simulation
|
||||
return;
|
||||
|
|
|
@ -28,6 +28,10 @@ ObjectActionSpring::~ObjectActionSpring() {
|
|||
}
|
||||
|
||||
void ObjectActionSpring::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
|
||||
if (!_ownerEntity) {
|
||||
qDebug() << "ObjectActionSpring::updateAction no owner entity";
|
||||
return;
|
||||
}
|
||||
if (!tryLockForRead()) {
|
||||
// don't risk hanging the thread running the physics simulation
|
||||
return;
|
||||
|
@ -111,6 +115,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) {
|
|||
|
||||
if (rtOk) {
|
||||
_rotationalTarget = rotationalTarget;
|
||||
_rotationalTargetSet = true;
|
||||
|
||||
if (rscOk) {
|
||||
_angularTimeScale = angularTimeScale;
|
||||
|
|
Loading…
Reference in a new issue