-
Notifications
You must be signed in to change notification settings - Fork 0
/
createConstraint-component.js
135 lines (116 loc) · 4.86 KB
/
createConstraint-component.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Or inline before the <a-scene>.
AFRAME.registerComponent('createConstraint', {
// ...
// Target (other) body for the constraint.
target: { type: "selector" },
// Offset of the hinge or point-to-point constraint, defined locally in the body. Used for hinge, coneTwist pointToPoint constraints.
pivot: { type: "vec3" },
targetPivot: { type: "vec3" },
// An axis that each body can rotate around, defined locally to that body. Used for hinge constraints.
axis: { type: "vec3", default: { x: 0, y: 1, z: 0 } },
targetAxis: { type: "vec3", default: { x: 0, y: 1, z: 0 } }
},
init: function() {
this.system = this.el.sceneEl.systems.physics;
this.constraint = null;
},
remove: function() {
if (!this.constraint) return;
this.system.removeConstraint(this.constraint);
this.constraint = null;
},
update: function() {
const el = this.el,
data = this.data;
this.remove();
if (!el.body || !data.target.body) {
(el.body ? data.target : el).addEventListener("body-loaded", this.update.bind(this, {}), { once: true });
return;
}
this.constraint = this.createConstraint();
this.system.addConstraint(this.constraint);
},
/**
* @return {Ammo.btTypedConstraint}
*/
createConstraint, function() {
let constraint;
const data = this.data,
body = this.el.body,
targetBody = data.target.body;
const bodyTransform = body
.getCenterOfMassTransform()
.inverse()
.op_mul(targetBody.getWorldTransform());
const targetTransform = new Ammo.btTransform();
targetTransform.setIdentity();
switch (data.type) {
case CONSTRAINT.LOCK: {
constraint = new Ammo.btGeneric6DofConstraint(body, targetBody, bodyTransform, targetTransform, true);
const zero = new Ammo.btVector3(0, 0, 0);
//TODO: allow these to be configurable
constraint.setLinearLowerLimit(zero);
constraint.setLinearUpperLimit(zero);
constraint.setAngularLowerLimit(zero);
constraint.setAngularUpperLimit(zero);
Ammo.destroy(zero);
break;
}
//TODO: test and verify all other constraint types
case CONSTRAINT.FIXED: {
//btFixedConstraint does not seem to debug render
bodyTransform.setRotation(body.getWorldTransform().getRotation());
targetTransform.setRotation(targetBody.getWorldTransform().getRotation());
constraint = new Ammo.btFixedConstraint(body, targetBody, bodyTransform, targetTransform);
break;
}
case CONSTRAINT.SPRING: {
constraint = new Ammo.btGeneric6DofSpringConstraint(body, targetBody, bodyTransform, targetTransform, true);
//TODO: enableSpring, setStiffness and setDamping
break;
}
case CONSTRAINT.SLIDER: {
//TODO: support setting linear and angular limits
constraint = new Ammo.btSliderConstraint(body, targetBody, bodyTransform, targetTransform, true);
constraint.setLowerLinLimit(-1);
constraint.setUpperLinLimit(1);
// constraint.setLowerAngLimit();
// constraint.setUpperAngLimit();
break;
}
case CONSTRAINT.HINGE: {
const pivot = new Ammo.btVector3(data.pivot.x, data.pivot.y, data.pivot.z);
const targetPivot = new Ammo.btVector3(data.targetPivot.x, data.targetPivot.y, data.targetPivot.z);
const axis = new Ammo.btVector3(data.axis.x, data.axis.y, data.axis.z);
const targetAxis = new Ammo.btVector3(data.targetAxis.x, data.targetAxis.y, data.targetAxis.z);
constraint = new Ammo.btHingeConstraint(body, targetBody, pivot, targetPivot, axis, targetAxis, true);
Ammo.destroy(pivot);
Ammo.destroy(targetPivot);
Ammo.destroy(axis);
Ammo.destroy(targetAxis);
break;
}
case CONSTRAINT.CONE_TWIST: {
const pivotTransform = new Ammo.btTransform();
pivotTransform.setIdentity();
pivotTransform.getOrigin().setValue(data.targetPivot.x, data.targetPivot.y, data.targetPivot.z);
constraint = new Ammo.btConeTwistConstraint(body, pivotTransform);
Ammo.destroy(pivotTransform);
break;
}
case CONSTRAINT.POINT_TO_POINT: {
const pivot = new Ammo.btVector3(data.pivot.x, data.pivot.y, data.pivot.z);
const targetPivot = new Ammo.btVector3(data.targetPivot.x, data.targetPivot.y, data.targetPivot.z);
constraint = new Ammo.btPoint2PointConstraint(body, targetBody, pivot, targetPivot);
Ammo.destroy(pivot);
Ammo.destroy(targetPivot);
break;
}
default:
throw new Error("[constraint] Unexpected type: " + data.type);
}
Ammo.destroy(bodyTransform);
Ammo.destroy(targetTransform);
return constraint;
}
});