forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab.js
74 lines (65 loc) · 2.53 KB
/
grab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* global CANNON */
/**
* Based on aframe/examples/showcase/tracked-controls.
*
* Handles events coming from the hand-controls.
* Determines if the entity is grabbed or released.
* Updates its position to move along the controller.
*/
module.exports = AFRAME.registerComponent('grab', {
init: function () {
this.system = this.el.sceneEl.systems.physics;
this.GRABBED_STATE = 'grabbed';
this.grabbing = false;
this.hitEl = /** @type {AFRAME.Element} */ null;
this.physics = /** @type {AFRAME.System} */ this.el.sceneEl.systems.physics;
this.constraint = /** @type {CANNON.Constraint} */ null;
// Bind event handlers
this.onHit = this.onHit.bind(this);
this.onGripOpen = this.onGripOpen.bind(this);
this.onGripClose = this.onGripClose.bind(this);
},
play: function () {
const el = this.el;
el.addEventListener('hit', this.onHit);
el.addEventListener('gripdown', this.onGripClose);
el.addEventListener('gripup', this.onGripOpen);
el.addEventListener('trackpaddown', this.onGripClose);
el.addEventListener('trackpadup', this.onGripOpen);
el.addEventListener('triggerdown', this.onGripClose);
el.addEventListener('triggerup', this.onGripOpen);
},
pause: function () {
const el = this.el;
el.removeEventListener('hit', this.onHit);
el.removeEventListener('gripdown', this.onGripClose);
el.removeEventListener('gripup', this.onGripOpen);
el.removeEventListener('trackpaddown', this.onGripClose);
el.removeEventListener('trackpadup', this.onGripOpen);
el.removeEventListener('triggerdown', this.onGripClose);
el.removeEventListener('triggerup', this.onGripOpen);
},
onGripClose: function () {
this.grabbing = true;
},
onGripOpen: function () {
const hitEl = this.hitEl;
this.grabbing = false;
if (!hitEl) { return; }
hitEl.removeState(this.GRABBED_STATE);
this.hitEl = undefined;
this.system.removeConstraint(this.constraint);
this.constraint = null;
},
onHit: function (evt) {
const hitEl = evt.detail.el;
// If the element is already grabbed (it could be grabbed by another controller).
// If the hand is not grabbing the element does not stick.
// If we're already grabbing something you can't grab again.
if (!hitEl || hitEl.is(this.GRABBED_STATE) || !this.grabbing || this.hitEl) { return; }
hitEl.addState(this.GRABBED_STATE);
this.hitEl = hitEl;
this.constraint = new CANNON.LockConstraint(this.el.body, hitEl.body);
this.system.addConstraint(this.constraint);
}
});