Make physics work with multiple entities per HiFi's current capabilities

This commit is contained in:
David Rowe 2017-09-06 18:36:06 +12:00
parent 52bf8ac9a1
commit 7b19b39bce
2 changed files with 54 additions and 11 deletions

View file

@ -81,13 +81,27 @@ Groups = function () {
function group() {
// Groups all selections into one.
var rootID,
var DYNAMIC_AND_COLLISIONLESS = { dynamic: true, collisionless: true },
rootID,
i,
count;
lengthI,
j,
lengthJ;
// If the first group has physics (i.e., root entity is dynamic) make all entities in child groups dynamic and
// collisionless. (Don't need to worry about other groups physics properties because only those of the the first entity
// in the linkset are used by High Fidelity.) See Selection.applyPhysics().
if (selections[0][0].dynamic) {
for (i = 1, lengthI = selections.length; i < lengthI; i += 1) {
for (j = 0, lengthJ = selections[i].length; j < lengthJ; j += 1) {
Entities.editEntity(selections[i][j].id, DYNAMIC_AND_COLLISIONLESS);
}
}
}
// Make the first entity in the first group the root and link the first entities of all other groups to it.
rootID = rootEntityIDs[0];
for (i = 1, count = rootEntityIDs.length; i < count; i += 1) {
for (i = 1, lengthI = rootEntityIDs.length; i < lengthI; i += 1) {
Entities.editEntity(rootEntityIDs[i], {
parentID: rootID
});
@ -95,7 +109,7 @@ Groups = function () {
// Update selection.
rootEntityIDs.splice(1, rootEntityIDs.length - 1);
for (i = 1, count = selections.length; i < count; i += 1) {
for (i = 1, lengthI = selections.length; i < lengthI; i += 1) {
selections[i][0].parentID = rootID;
selections[0] = selections[0].concat(selections[i]);
}
@ -114,8 +128,11 @@ Groups = function () {
hasSoloChildren = false,
hasGroupChildren = false,
isUngroupAll,
NONDYNAMIC_AND_NONCOLLISIONLESS = { dynamic: false, collisionless: false },
i,
count;
lengthI,
j,
lengthJ;
function updateGroupInformation() {
var childrenIndexesLength = childrenIndexes.length;
@ -141,7 +158,7 @@ Groups = function () {
// Compile information on immediate children.
rootID = rootEntityIDs[0];
for (i = 1, count = selections[0].length; i < count; i += 1) {
for (i = 1, lengthI = selections[0].length; i < lengthI; i += 1) {
if (selections[0][i].parentID === rootID) {
childrenIDs.push(selections[0][i].id);
childrenIndexes.push(i);
@ -163,6 +180,16 @@ Groups = function () {
selections.push(selections[0].splice(childrenIndexes[i], childrenIndexes[i + 1] - childrenIndexes[i]));
}
}
// If root group has physics, reset child groups to defaults for dynamic and collisionless. See
// Selection.applyPhysics().
if (selections[0][0].dynamic) {
for (i = 1, lengthI = selections.length; i < lengthI; i += 1) {
for (j = 0, lengthJ = selections[i].length; j < lengthJ; j += 1) {
Entities.editEntity(selections[i][j].id, NONDYNAMIC_AND_NONCOLLISIONLESS);
}
}
}
}
function clear() {

View file

@ -224,11 +224,11 @@ Selection = function (side) {
}
function finishEditing() {
var count,
i;
var i;
// Restore entity set's physics.
for (i = 0, count = selection.length; i < count; i += 1) {
// Note: Need to apply children-first in order to avoid children's relative positions sometimes drifting.
for (i = selection.length - 1; i >= 0; i -= 1) {
Entities.editEntity(selection[i].id, {
dynamic: selection[i].dynamic,
collisionless: selection[i].collisionless
@ -424,9 +424,25 @@ Selection = function (side) {
}
function applyPhysics(physicsProperties) {
// Apply physics to just the root entity.
var properties;
// Regarding trees of entities, when physics is to be enabled the physics engine currently:
// - Only works with physics applied to the root entity; i.e., child entities are ignored for collisions.
// - Requires child entities to be dynamic if the root entity is dynamic, otherwise child entities can drift.
// - Requires child entities to be collisionless, otherwise the entity tree can become self-propelled.
// See also: Groups.group() and ungroup().
var properties,
i,
length;
// Make children cater to physicsProperties.
properties = {
dynamic: physicsProperties.dynamic,
collisionless: physicsProperties.dynamic || physicsProperties.collisionless
};
for (i = 1, length = selection.length; i < length; i += 1) {
Entities.editEntity(selection[i].id, properties);
}
// Set root per physicsProperties.
properties = Object.clone(physicsProperties);
properties.userData = updatePhysicsUserData(selection[intersectedEntityIndex].userData, physicsProperties.userData);
Entities.editEntity(rootEntityID, properties);