Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@humbletim
Last active July 11, 2016 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humbletim/f03cc418bcf0c85ae01db6b14b1b745f to your computer and use it in GitHub Desktop.
Save humbletim/f03cc418bcf0c85ae01db6b14b1b745f to your computer and use it in GitHub Desktop.
potential parented Entity / angularVelocity workaround
// demonstrates a two-phase initialization approach where parenting is set via Entity script
// and Entity updates are "forced" 20 times a second to try and keep everything in sync
uuids = [
rezAtJoint(
"RightShoulder",
"http://hifi-content.s3.amazonaws.com/caitlyn/production/Scansite/buddhaReduced.fbx?1"
),
rezAtJoint(
"LeftShoulder",
"http://hifi-content.s3.amazonaws.com/caitlyn/production/Scansite/buddhaReduced.fbx?1"
)
];
Script.scriptEnding.connect(function() {
uuids.forEach(Entities.deleteEntity);
});
function rezAtJoint(joint, modelURL) {
return Entities.addEntity({
script: Script.resolvePath('entity-heartbeat.js'),
lifetime: 600,
type: 'Model',
modelURL: modelURL,
dimensions: {x: .1, y: .1, z: .1},
collisionless: false,
// these properties get applied verbatim within entity-heartbeat.js's preload
userData: JSON.stringify({
parentID: MyAvatar.sessionUUID,
parentJointIndex: MyAvatar.getJointIndex(joint),
localPosition: /right|left/i.test(joint) ?
{x: 0, y: .1, z: -.125} : {x: 0, y: 0, z: .125},
angularVelocity: {x: 0, y: 0, z: /right/i.test(joint) ? 1 : -1},
angularDamping: 0
})
});
}
(function() {
return {
t: -1,
heartbeat: function(dt) {
this.t += dt;
if (this.t >= 1/20) {
// force updates at least 20 fps (by updating an arbitrary property)
Entities.editEntity(this.uuid, { visible: true });
this.t = 0;
}
},
live: function() {
if (!this.heart) {
// for an update right away to try and prevent the initial "stall"
this.heartbeat(Infinity);
print('... connecting pulse', this.uuid);
Script.update.connect(this, 'heartbeat');
this.heart = true;
}
},
die: function() {
if (this.heart) {
print('...disconnecting pulse', this.uuid);
Script.update.disconnect(this, 'heartbeat');
this.heart = false;
}
},
_clickDownOnEntity: function(uuid, evt) {
print('clickDownOnEntity', uuid, !!this.heart);
if (this.heart)
this.die();
else
this.live();
},
unload: function(uuid) { this.die(); },
preload: function(uuid) {
this.uuid = uuid;
var ent = Entities.getEntityProperties(uuid);
var parentedProps = JSON.parse(ent.userData);
if (parentedProps.parentID !== MyAvatar.sessionUUID)
return print('... parentedProps.parentID !== MyAvatar.sessionUUID -- bailing.');
print('... applying userData properties:', JSON.stringify(parentedProps));
Entities.editEntity(uuid, parentedProps);
this.live();
this.clickDownOnEntity = this._clickDownOnEntity; // if we made it this far, enable onclick toggling
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment