mirror of
https://github.com/overte-org/overte.git
synced 2025-08-04 07:23:39 +02:00
reference count grabs on an entity
This commit is contained in:
parent
635c83dad1
commit
7f8411d947
2 changed files with 70 additions and 45 deletions
|
@ -79,10 +79,8 @@ var STATE_NEAR_GRABBING_NON_COLLIDING = 6;
|
||||||
var STATE_CONTINUE_NEAR_GRABBING_NON_COLLIDING = 7;
|
var STATE_CONTINUE_NEAR_GRABBING_NON_COLLIDING = 7;
|
||||||
var STATE_RELEASE = 8;
|
var STATE_RELEASE = 8;
|
||||||
|
|
||||||
var GRAB_USER_DATA_KEY = "grabKey";
|
var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with grab.js
|
||||||
var GRABBABLE_DATA_KEY = "grabbableKey";
|
var GRAB_USER_DATA_KEY = "grabKey"; // shared with grab.js
|
||||||
var GRAVITY_USER_DATA_KEY = "preGrabGravity";
|
|
||||||
var IGNORE_FOR_COLLISIONS_USER_DATA_KEY = "preGrabIgnoreForCollisions";
|
|
||||||
|
|
||||||
function getTag() {
|
function getTag() {
|
||||||
return "grab-" + MyAvatar.sessionUUID;
|
return "grab-" + MyAvatar.sessionUUID;
|
||||||
|
@ -314,7 +312,8 @@ function MyController(hand, triggerAction) {
|
||||||
|
|
||||||
var handControllerPosition = Controller.getSpatialControlPosition(this.palm);
|
var handControllerPosition = Controller.getSpatialControlPosition(this.palm);
|
||||||
var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm));
|
var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm));
|
||||||
var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation"]);
|
var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation",
|
||||||
|
"gravity", "ignoreForCollisions"]);
|
||||||
|
|
||||||
// add the action and initialize some variables
|
// add the action and initialize some variables
|
||||||
this.currentObjectPosition = grabbedProperties.position;
|
this.currentObjectPosition = grabbedProperties.position;
|
||||||
|
@ -338,7 +337,7 @@ function MyController(hand, triggerAction) {
|
||||||
|
|
||||||
if (this.actionID !== null) {
|
if (this.actionID !== null) {
|
||||||
this.setState(STATE_CONTINUE_DISTANCE_HOLDING);
|
this.setState(STATE_CONTINUE_DISTANCE_HOLDING);
|
||||||
this.activateEntity(this.grabbedEntity);
|
this.activateEntity(this.grabbedEntity, grabbedProperties);
|
||||||
if (this.hand === RIGHT_HAND) {
|
if (this.hand === RIGHT_HAND) {
|
||||||
Entities.callEntityMethod(this.grabbedEntity, "setRightHand");
|
Entities.callEntityMethod(this.grabbedEntity, "setRightHand");
|
||||||
} else {
|
} else {
|
||||||
|
@ -441,10 +440,9 @@ function MyController(hand, triggerAction) {
|
||||||
|
|
||||||
this.lineOff();
|
this.lineOff();
|
||||||
|
|
||||||
this.activateEntity(this.grabbedEntity);
|
|
||||||
|
|
||||||
var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity,
|
var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity,
|
||||||
["position", "rotation", "gravity", "ignoreForCollisions"]);
|
["position", "rotation", "gravity", "ignoreForCollisions"]);
|
||||||
|
this.activateEntity(this.grabbedEntity, grabbedProperties);
|
||||||
|
|
||||||
var handRotation = this.getHandRotation();
|
var handRotation = this.getHandRotation();
|
||||||
var handPosition = this.getHandPosition();
|
var handPosition = this.getHandPosition();
|
||||||
|
@ -456,14 +454,6 @@ function MyController(hand, triggerAction) {
|
||||||
var offset = Vec3.subtract(currentObjectPosition, handPosition);
|
var offset = Vec3.subtract(currentObjectPosition, handPosition);
|
||||||
var offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, offsetRotation)), offset);
|
var offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, offsetRotation)), offset);
|
||||||
|
|
||||||
// zero gravity and set ignoreForCollisions to true, but in a way that lets us put them back, after all grabs are done
|
|
||||||
this.saveGravity = getEntityCustomData(GRAVITY_USER_DATA_KEY, this.grabbedEntity, grabbedProperties.gravity);
|
|
||||||
setEntityCustomData(GRAVITY_USER_DATA_KEY, this.grabbedEntity, this.saveGravity);
|
|
||||||
this.saveIgnoreForCollisions = getEntityCustomData(IGNORE_FOR_COLLISIONS_USER_DATA_KEY, this.grabbedEntity,
|
|
||||||
grabbedProperties.ignoreForCollisions);
|
|
||||||
setEntityCustomData(IGNORE_FOR_COLLISIONS_USER_DATA_KEY, this.grabbedEntity, this.saveIgnoreForCollisions);
|
|
||||||
Entities.editEntity(this.grabbedEntity, {gravity: {x:0, y:0, z:0}, ignoreForCollisions: true});
|
|
||||||
|
|
||||||
this.actionID = NULL_ACTION_ID;
|
this.actionID = NULL_ACTION_ID;
|
||||||
this.actionID = Entities.addAction("kinematic-hold", this.grabbedEntity, {
|
this.actionID = Entities.addAction("kinematic-hold", this.grabbedEntity, {
|
||||||
hand: this.hand === RIGHT_HAND ? "right" : "left",
|
hand: this.hand === RIGHT_HAND ? "right" : "left",
|
||||||
|
@ -492,12 +482,7 @@ function MyController(hand, triggerAction) {
|
||||||
|
|
||||||
this.continueNearGrabbing = function() {
|
this.continueNearGrabbing = function() {
|
||||||
if (this.triggerSmoothedReleased()) {
|
if (this.triggerSmoothedReleased()) {
|
||||||
|
|
||||||
this.setState(STATE_RELEASE);
|
this.setState(STATE_RELEASE);
|
||||||
Entities.editEntity(this.grabbedEntity,
|
|
||||||
{gravity: this.saveGravity, ignoreForCollisions: this.saveIgnoreForCollisions});
|
|
||||||
setEntityCustomData(IGNORE_FOR_COLLISIONS_USER_DATA_KEY, this.grabbedEntity, null);
|
|
||||||
setEntityCustomData(GRAVITY_USER_DATA_KEY, this.grabbedEntity, null);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -656,20 +641,35 @@ function MyController(hand, triggerAction) {
|
||||||
this.release();
|
this.release();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.activateEntity = function() {
|
this.activateEntity = function(entityID, grabbedProperties) {
|
||||||
var data = {
|
var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {});
|
||||||
activated: true,
|
data["activated"] = true;
|
||||||
avatarId: MyAvatar.sessionUUID
|
data["avatarId"] = MyAvatar.sessionUUID;
|
||||||
};
|
data["refCount"] = data["refCount"] ? data["refCount"] + 1 : 1;
|
||||||
setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data);
|
// zero gravity and set ignoreForCollisions to true, but in a way that lets us put them back, after all grabs are done
|
||||||
|
if (data["refCount"] == 1) {
|
||||||
|
data["gravity"] = grabbedProperties.gravity;
|
||||||
|
data["ignoreForCollisions"] = grabbedProperties.ignoreForCollisions;
|
||||||
|
Entities.editEntity(entityID, {gravity: {x:0, y:0, z:0}, ignoreForCollisions: true});
|
||||||
|
}
|
||||||
|
setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.deactivateEntity = function() {
|
this.deactivateEntity = function(entityID) {
|
||||||
var data = {
|
var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {});
|
||||||
activated: false,
|
if (data && data["refCount"]) {
|
||||||
avatarId: null
|
data["refCount"] = data["refCount"] - 1;
|
||||||
};
|
if (data["refCount"] < 1) {
|
||||||
setEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, data);
|
Entities.editEntity(entityID, {
|
||||||
|
gravity: data["gravity"],
|
||||||
|
ignoreForCollisions: data["ignoreForCollisions"]
|
||||||
|
});
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,8 @@ var IDENTITY_QUAT = {
|
||||||
z: 0,
|
z: 0,
|
||||||
w: 0
|
w: 0
|
||||||
};
|
};
|
||||||
var GRABBABLE_DATA_KEY = "grabbableKey";
|
var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with handControllerGrab.js
|
||||||
|
var GRAB_USER_DATA_KEY = "grabKey"; // shared with handControllerGrab.js
|
||||||
|
|
||||||
var defaultGrabbableData = {
|
var defaultGrabbableData = {
|
||||||
grabbable: true
|
grabbable: true
|
||||||
|
@ -247,7 +248,6 @@ Grabber = function() {
|
||||||
this.currentPosition = ZERO_VEC3;
|
this.currentPosition = ZERO_VEC3;
|
||||||
this.planeNormal = ZERO_VEC3;
|
this.planeNormal = ZERO_VEC3;
|
||||||
|
|
||||||
this.originalGravity = ZERO_VEC3;
|
|
||||||
// maxDistance is a function of the size of the object.
|
// maxDistance is a function of the size of the object.
|
||||||
this.maxDistance;
|
this.maxDistance;
|
||||||
|
|
||||||
|
@ -346,14 +346,11 @@ Grabber.prototype.pressEvent = function(event) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Entities.editEntity(clickedEntity, {
|
this.activateEntity(clickedEntity, entityProperties);
|
||||||
gravity: ZERO_VEC3
|
|
||||||
});
|
|
||||||
this.isGrabbing = true;
|
this.isGrabbing = true;
|
||||||
|
|
||||||
this.entityID = clickedEntity;
|
this.entityID = clickedEntity;
|
||||||
this.currentPosition = entityProperties.position;
|
this.currentPosition = entityProperties.position;
|
||||||
this.originalGravity = entityProperties.gravity;
|
|
||||||
this.targetPosition = {
|
this.targetPosition = {
|
||||||
x: this.startPosition.x,
|
x: this.startPosition.x,
|
||||||
y: this.startPosition.y,
|
y: this.startPosition.y,
|
||||||
|
@ -379,12 +376,7 @@ Grabber.prototype.pressEvent = function(event) {
|
||||||
|
|
||||||
Grabber.prototype.releaseEvent = function() {
|
Grabber.prototype.releaseEvent = function() {
|
||||||
if (this.isGrabbing) {
|
if (this.isGrabbing) {
|
||||||
if (Vec3.length(this.originalGravity) != 0) {
|
this.deactivateEntity(this.entityID);
|
||||||
Entities.editEntity(this.entityID, {
|
|
||||||
gravity: this.originalGravity
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isGrabbing = false
|
this.isGrabbing = false
|
||||||
Entities.deleteAction(this.entityID, this.actionID);
|
Entities.deleteAction(this.entityID, this.actionID);
|
||||||
this.actionID = null;
|
this.actionID = null;
|
||||||
|
@ -503,6 +495,39 @@ Grabber.prototype.keyPressEvent = function(event) {
|
||||||
this.computeNewGrabPlane();
|
this.computeNewGrabPlane();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Grabber.prototype.activateEntity = function(entityID, grabbedProperties) {
|
||||||
|
var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {});
|
||||||
|
data["activated"] = true;
|
||||||
|
data["avatarId"] = MyAvatar.sessionUUID;
|
||||||
|
data["refCount"] = data["refCount"] ? data["refCount"] + 1 : 1;
|
||||||
|
// zero gravity and set ignoreForCollisions to true, but in a way that lets us put them back, after all grabs are done
|
||||||
|
if (data["refCount"] == 1) {
|
||||||
|
data["gravity"] = grabbedProperties.gravity;
|
||||||
|
data["ignoreForCollisions"] = grabbedProperties.ignoreForCollisions;
|
||||||
|
Entities.editEntity(entityID, {gravity: {x:0, y:0, z:0}, ignoreForCollisions: true});
|
||||||
|
}
|
||||||
|
setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Grabber.prototype.deactivateEntity = function(entityID) {
|
||||||
|
var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {});
|
||||||
|
if (data && data["refCount"]) {
|
||||||
|
data["refCount"] = data["refCount"] - 1;
|
||||||
|
if (data["refCount"] < 1) {
|
||||||
|
Entities.editEntity(entityID, {
|
||||||
|
gravity: data["gravity"],
|
||||||
|
ignoreForCollisions: data["ignoreForCollisions"]
|
||||||
|
});
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data = null;
|
||||||
|
}
|
||||||
|
setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var grabber = new Grabber();
|
var grabber = new Grabber();
|
||||||
|
|
||||||
function pressEvent(event) {
|
function pressEvent(event) {
|
||||||
|
|
Loading…
Reference in a new issue