-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.js
140 lines (118 loc) · 4.11 KB
/
collision.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
136
137
138
139
140
(function() {
AFRAME.registerComponent( 'collision', {
init: function() {
},
tick: function () {
var self = this;
window.debugObj = this;
availableDoors.forEach(door => {
const tokenInRange = window.tokens.find(token => {
const pos = token.obj && token.obj.getAttribute('position');
return token.speed === .7 && pos && distanceBetween(door.object3D.position, pos) < 20;
});
if(distanceBetween(door.object3D.position, this.el.object3D.position) < doorThreshold || tokenInRange) {
openDoor(door, tokenInRange);
} else {
closeDoor(door);
}
});
if ( collides() ) {
// check if we can resolve the collision
const targetx = this.el.object3D.position.x;
const targetz = this.el.object3D.position.z;
this.el.object3D.position.x = this.lastKnownGoodPosition.x;
if(collides()) {
this.el.object3D.position.x = targetx;
this.el.object3D.position.z = this.lastKnownGoodPosition.z;
if(collides()) {
// this.el.object3D.position.x = this.lastKnownGoodPosition.x;
this.el.components['wasd-controls'].data.acceleration = 0;
this.el.setAttribute( 'position', this.lastKnownGoodPosition );
} else {
this.lastKnownGoodPosition = {
x: this.el.object3D.position.x,
y: this.el.object3D.position.y,
z: this.el.object3D.position.z
};
this.el.setAttribute( 'position', this.lastKnownGoodPosition );
this.el.components['wasd-controls'].data.acceleration = 250;
}
} else {
// found acceptable match
this.lastKnownGoodPosition = {
x: this.el.object3D.position.x,
y: this.el.object3D.position.y,
z: this.el.object3D.position.z
};
this.el.setAttribute( 'position', this.lastKnownGoodPosition );
this.el.components['wasd-controls'].data.acceleration = 250;
}
} else {
this.lastKnownGoodPosition = {
x: this.el.object3D.position.x,
y: this.el.object3D.position.y,
z: this.el.object3D.position.z
};
this.el.components['wasd-controls'].data.acceleration = 250;
}
const x = -this.el.object3D.position.z / globalScaleFactor;
const y = this.el.object3D.position.x / globalScaleFactor;
const rotation = 135 - (this.el.object3D.rotation.y / Math.PI * 180);
updatePreviewPosition(x, y, rotation);
/**
* Tests each collideWiths for intersection.
*/
function collides() {
return !window.openSpaces.some(openSpace => {
return isInside(self.el.object3D.position, openSpace);
});
}
}
});
})();
function isInside(pt, space) {
const b1 = sign(pt, space.p1, space.p2) < 0;
const b2 = sign(pt, space.p2, space.p3) < 0;
const b3 = sign(pt, space.p3, space.p1) < 0;
return b1 === b2 && b2 === b3;
}
function sign(p1, p2, p3){
// only x and z coordinates are relevant
return (p1.x - p3.x) * (p2.z - p3.z) - (p2.x - p3.x) * (p1.z - p3.z);
}
function distanceBetween(p1, p2) {
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.z - p2.z, 2);
}
function openDoor(door, fast) {
// door.object3D.position.y = sequenceFlowHeight;
if(door.closingAnimObj) {
door.removeChild(door.closingAnimObj);
door.closingAnimObj = null;
}
if(!door.openingAnimObj) {
const anim = document.createElement('a-animation');
anim.setAttribute('attribute', 'position');
anim.setAttribute('dur', fast ? '100' : '500');
anim.setAttribute('to', door.object3D.position.x + ' ' + (sequenceFlowHeight * 1.5) + ' ' + door.object3D.position.z);
anim.fast = fast;
door.appendChild(anim);
door.openingAnimObj = anim;
}
}
function closeDoor(door) {
// door.object3D.position.y = sequenceFlowHeight / 2;
let wasFast = false;
if(door.openingAnimObj) {
door.removeChild(door.openingAnimObj);
wasFast = door.openingAnimObj.fast;
door.openingAnimObj = null;
}
if(!door.closingAnimObj) {
const anim = document.createElement('a-animation');
anim.setAttribute('attribute', 'position');
anim.setAttribute('dur', wasFast ? '100' : '500');
anim.setAttribute('to', door.object3D.position.x + ' ' + (sequenceFlowHeight * 0.5) + ' ' + door.object3D.position.z);
door.appendChild(anim);
door.closingAnimObj = anim;
}
}