-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
239 lines (199 loc) · 5.97 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
module.exports = physical
var aabb = require('aabb-3d')
, THREE = require('three')
function physical(avatar, collidables, dimensions, terminal) {
return new Physical(avatar, collidables, dimensions, terminal)
}
function Physical(avatar, collidables, dimensions, terminal) {
this.avatar = avatar
this.terminal = terminal || new THREE.Vector3(0.9, 0.1, 0.9)
this.dimensions = dimensions = dimensions || [1, 1, 1]
this._aabb = aabb([0, 0, 0], dimensions)
this.resting = {x: false, y: false, z: false}
this.old_resting_y = 0
this.last_rest_y = NaN
this.collidables = collidables
this.friction = new THREE.Vector3(1, 1, 1)
this.rotation = this.avatar.rotation
this.default_friction = 1
// default yaw/pitch/roll controls to the avatar
this.yaw =
this.pitch =
this.roll = avatar
this.forces = new THREE.Vector3(0, 0, 0)
this.attractors = []
this.acceleration = new THREE.Vector3(0, 0, 0)
this.velocity = new THREE.Vector3(0, 0, 0)
}
var cons = Physical
, proto = cons.prototype
, axes = ['x', 'y', 'z']
, abs = Math.abs
// make these *once*, so we're not generating
// garbage for every object in the game.
var WORLD_DESIRED = new THREE.Vector3(0, 0, 0)
, DESIRED = new THREE.Vector3(0, 0, 0)
, START = new THREE.Vector3(0, 0, 0)
, END = new THREE.Vector3(0, 0, 0)
, DIRECTION = new THREE.Vector3()
, LOCAL_ATTRACTOR = new THREE.Vector3()
, TOTAL_FORCES = new THREE.Vector3()
proto.applyWorldAcceleration = applyTo('acceleration')
proto.applyWorldVelocity = applyTo('velocity')
function applyTo(which) {
return function(world) {
var local = this.avatar.worldToLocal(world)
this[which].x += local.x
this[which].y += local.y
this[which].z += local.z
}
}
proto.tick = function(dt) {
var forces = this.forces
, acceleration = this.acceleration
, velocity = this.velocity
, terminal = this.terminal
, friction = this.friction
, desired = DESIRED
, world_desired = WORLD_DESIRED
, bbox
, pcs
TOTAL_FORCES.multiplyScalar(0)
desired.x =
desired.y =
desired.z =
world_desired.x =
world_desired.y =
world_desired.z = 0
for(var i = 0; i < this.attractors.length; i++) {
var distance_factor = this.avatar.position.distanceToSquared(this.attractors[i])
LOCAL_ATTRACTOR.copy(this.attractors[i])
LOCAL_ATTRACTOR = this.avatar.worldToLocal(LOCAL_ATTRACTOR)
DIRECTION.sub(LOCAL_ATTRACTOR, this.avatar.position)
DIRECTION.divideScalar(DIRECTION.length() * distance_factor)
DIRECTION.multiplyScalar(this.attractors[i].mass)
TOTAL_FORCES.addSelf(DIRECTION)
}
if(!this.resting.x) {
acceleration.x /= 8 * dt
acceleration.x += TOTAL_FORCES.x * dt
acceleration.x += forces.x * dt
velocity.x += acceleration.x * dt
velocity.x *= friction.x
if(abs(velocity.x) < terminal.x) {
desired.x = (velocity.x * dt)
} else if(velocity.x !== 0) {
desired.x = (velocity.x / abs(velocity.x)) * terminal.x
}
} else {
acceleration.x = velocity.x = 0
}
if(!this.resting.y) {
acceleration.y /= 8 * dt
acceleration.y += TOTAL_FORCES.y * dt
acceleration.y += forces.y * dt
velocity.y += acceleration.y * dt
velocity.y *= friction.y
if(abs(velocity.y) < terminal.y) {
desired.y = (velocity.y * dt)
} else if(velocity.y !== 0) {
desired.y = (velocity.y / abs(velocity.y)) * terminal.y
}
} else {
acceleration.y = velocity.y = 0
}
if(!this.resting.z) {
acceleration.z /= 8 * dt
acceleration.z += TOTAL_FORCES.z * dt
acceleration.z += forces.z * dt
velocity.z += acceleration.z * dt
velocity.z *= friction.z
if(abs(velocity.z) < terminal.z) {
desired.z = (velocity.z * dt)
} else if(velocity.z !== 0) {
desired.z = (velocity.z / abs(velocity.z)) * terminal.z
}
} else {
acceleration.z = velocity.z = 0
}
START.copy(this.avatar.position)
this.avatar.translateX(desired.x)
this.avatar.translateY(desired.y)
this.avatar.translateZ(desired.z)
END.copy(this.avatar.position)
this.avatar.position.copy(START)
world_desired.x = END.x - START.x
world_desired.y = END.y - START.y
world_desired.z = END.z - START.z
this.friction.x =
this.friction.y =
this.friction.z = this.default_friction
// save old copies, since when normally on the
// ground, this.resting.y alternates (false,-1)
this.old_resting_y = (this.old_resting_y << 1) >>> 0
this.old_resting_y |= !!this.resting.y | 0
// run collisions
this.resting.x =
this.resting.y =
this.resting.z = false
bbox = this.aabb()
pcs = this.collidables
for(var i = 0, len = pcs.length; i < len; ++i) {
if(pcs[i] !== this) {
pcs[i].collide(this, bbox, world_desired, this.resting)
}
}
// fall distance
if(!!(this.old_resting_y & 0x4) !== !!this.resting.y) {
if(!this.resting.y) {
this.last_rest_y = this.avatar.position.y
} else if(!isNaN(this.last_rest_y)) {
this.fell(this.last_rest_y - this.avatar.position.y)
this.last_rest_y = NaN
}
}
// apply translation
this.avatar.position.x += world_desired.x
this.avatar.position.y += world_desired.y
this.avatar.position.z += world_desired.z
}
proto.subjectTo = function(force) {
this.forces.x += force[0]
this.forces.y += force[1]
this.forces.z += force[2]
return this
}
proto.removeForce = function(force) {
this.forces.x -= force[0]
this.forces.y -= force[1]
this.forces.z -= force[2]
return this
}
proto.attractTo = function(vector, mass) {
vector.mass = mass
this.attractors.push(vector)
}
proto.aabb = function() {
var pos = this.avatar.position
var d = this.dimensions
return aabb(
[pos.x - (d[0]/2), pos.y, pos.z - (d[2]/2)],
this.dimensions
)
}
// no object -> object collisions for now, thanks
proto.collide = function(other, bbox, world_vec, resting) {
return
}
proto.atRestX = function() {
return this.resting.x
}
proto.atRestY = function() {
return this.resting.y
}
proto.atRestZ = function() {
return this.resting.z
}
proto.fell = function(distance) {
return
}