From 944f0965c059a1d9af6a30d8c18523d59c836cff Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 30 Oct 2015 16:35:50 -0700 Subject: [PATCH] some refactoring and a bug fix -- grab script can throw things again --- examples/controllers/handControllerGrab.js | 8 +------- libraries/physics/src/PhysicsEngine.cpp | 9 ++++++--- libraries/physics/src/ThreadSafeDynamicsWorld.cpp | 6 ++---- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 10775a483f..815f903bfa 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -15,13 +15,11 @@ Script.include("../libraries/utils.js"); -//////////////////////////////////////////////////////////// // // add lines where the hand ray picking is happening // var WANT_DEBUG = false; -///////////////////////////////////////////////////////////////// // // these tune time-averaging and "on" value for analog trigger // @@ -30,7 +28,6 @@ var TRIGGER_SMOOTH_RATIO = 0.1; // 0.0 disables smoothing of trigger value var TRIGGER_ON_VALUE = 0.4; var TRIGGER_OFF_VALUE = 0.15; -///////////////////////////////////////////////////////////////// // // distant manipulation // @@ -44,7 +41,6 @@ var LINE_ENTITY_DIMENSIONS = { x: 1000, y: 1000,z: 1000}; var LINE_LENGTH = 500; var PICK_MAX_DISTANCE = 500; // max length of pick-ray -///////////////////////////////////////////////////////////////// // // near grabbing // @@ -57,7 +53,6 @@ var RELEASE_VELOCITY_MULTIPLIER = 1.5; // affects throwing things var PICK_BACKOFF_DISTANCE = 0.2; // helps when hand is intersecting the grabble object var NEAR_GRABBING_KINEMATIC = true; // force objects to be kinematic when near-grabbed -///////////////////////////////////////////////////////////////// // // other constants // @@ -228,7 +223,7 @@ function MyController(hand, triggerAction) { this.setState = function(newState) { if (WANT_DEBUG) { - print("STATE: " + stateToName(this.state) + " --> " + newState + ", hand: " + this.hand); + print("STATE: " + stateToName(this.state) + " --> " + stateToName(newState) + ", hand: " + this.hand); } this.state = newState; } @@ -849,7 +844,6 @@ function MyController(hand, triggerAction) { // the action will tend to quickly bring an object's velocity to zero. now that // the action is gone, set the objects velocity to something the holder might expect. - print("release velocity is " + vec3toStr(this.grabbedVelocity)); Entities.editEntity(this.grabbedEntity, { velocity: this.grabbedVelocity }); diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 1e652b75c4..f3ef855e50 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -82,12 +82,12 @@ void PhysicsEngine::addObject(ObjectMotionState* motionState) { btCollisionShape* shape = motionState->getShape(); assert(shape); body = new btRigidBody(mass, motionState, shape, inertia); + motionState->setRigidBody(body); } else { body->setMassProps(mass, inertia); } body->setCollisionFlags(btCollisionObject::CF_KINEMATIC_OBJECT); body->updateInertiaTensor(); - motionState->setRigidBody(body); motionState->updateBodyVelocities(); const float KINEMATIC_LINEAR_VELOCITY_THRESHOLD = 0.01f; // 1 cm/sec const float KINEMATIC_ANGULAR_VELOCITY_THRESHOLD = 0.01f; // ~1 deg/sec @@ -101,12 +101,15 @@ void PhysicsEngine::addObject(ObjectMotionState* motionState) { shape->calculateLocalInertia(mass, inertia); if (!body) { body = new btRigidBody(mass, motionState, shape, inertia); + motionState->setRigidBody(body); } else { body->setMassProps(mass, inertia); } + body->setCollisionFlags(body->getCollisionFlags() & ~(btCollisionObject::CF_KINEMATIC_OBJECT | + btCollisionObject::CF_STATIC_OBJECT)); body->updateInertiaTensor(); - motionState->setRigidBody(body); motionState->updateBodyVelocities(); + // NOTE: Bullet will deactivate any object whose velocity is below these thresholds for longer than 2 seconds. // (the 2 seconds is determined by: static btRigidBody::gDeactivationTime const float DYNAMIC_LINEAR_VELOCITY_THRESHOLD = 0.05f; // 5 cm/sec @@ -123,12 +126,12 @@ void PhysicsEngine::addObject(ObjectMotionState* motionState) { if (!body) { assert(motionState->getShape()); body = new btRigidBody(mass, motionState, motionState->getShape(), inertia); + motionState->setRigidBody(body); } else { body->setMassProps(mass, inertia); } body->setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT); body->updateInertiaTensor(); - motionState->setRigidBody(body); break; } } diff --git a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp index f725315330..d06a9b8e07 100644 --- a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp +++ b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp @@ -98,13 +98,11 @@ void ThreadSafeDynamicsWorld::synchronizeMotionState(btRigidBody* body) { { if (body->isKinematicObject()) { ObjectMotionState* objectMotionState = static_cast(body->getMotionState()); - if (!objectMotionState->hasInternalKinematicChanges()) { - return; - } else { + if (objectMotionState->hasInternalKinematicChanges()) { objectMotionState->clearInternalKinematicChanges(); body->getMotionState()->setWorldTransform(body->getWorldTransform()); - return; } + return; } btTransform interpolatedTransform; btTransformUtil::integrateTransform(body->getInterpolationWorldTransform(),