-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.js
277 lines (251 loc) · 7.74 KB
/
physics.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var CollisionTest =
{
// vector, vector, vector, scalar, scalar, hash
ray_sphere: function( p, d, sp, sr, info )
{
var m = p.minus( sp );
var b = m.dot(d);
var c = m.dot() - Math.pow(sr,2);
// test if start point is outside of sphere and pointing away
if( c > 0.0 && b > 0.0 ){ return false }
var discr = b*b - c;
// if negative than ray missed sphere
if( discr < 0.0 ){ return false }
// user doesn't want to know when/where collision happened
if(!info){ return true }
var t = -b - Math.sqrt(discr);
// if t is negative ray started inside sphere so clamp t
if( t < 0.0 ){ t = 0.0; }
var fa = p.plus( d.multiply( t ) );
var fb = sp.plus( d.multiply( t ) );
// return values back to user
info.t = t;
info.fa = fa;
info.fb = fb;
return true;
},
segment_sphere: function( p, d, sp, sr, l, info )
{
// test if the ray passes through the sphere
if(!this.ray_sphere( p, d, sp, sr, info )){return false}
// test that collision point is on the line segment
return (info.t <= l);
},
sphere_sphere: function( a, b, info )
{
// reduce test to only a single moving sphere
// b becomes a stationary sphere
// v represents movement of both spheres
var v = a.velocity.minus( b.velocity );
var vlen = v.length2();
// both spheres apparently have same velocity
// they must be moving parrallel so cannot collide
// TODO - do we need to now detect if they are already touching?
if(vlen == 0.0){return false};
// reduce test to line segment vs sphere
// b's radius will increase by a's
// and 'a' becomes a point
var r = b.radius + a.radius;
// test if line segment passes through sphere
// line segment representing movement in 'v' from start to finish
return this.segment_sphere( a.pos, v.divide_scalar(vlen), b.pos, r, vlen, info );
}
}
var CollisionResponse =
{
sphere_sphere: function( a, b, info )
{
// since drag is only applied per frame we don't need to update velocity
//a.velocity = info.fa - a.pos
//b.velocity = info.fb - b.pos
// update sphere positions to the location where they collide
a.pos = info.fa;
b.pos = info.fb;
// http://en.wikipedia.org/wiki/Inelastic_collision
// http://en.wikipedia.org/wiki/Elastic_collision
var v = a.pos.minus( b.pos ); // vector between spheres
var vn = v.normalize();
var u1 = vn.multiply_scalar( vn.dot(a.velocity) ); // collision component of velocity
var u2 = vn.multiply_scalar( vn.dot(b.velocity) );
a.velocity = a.velocity.minus( u1 ); // remove collision component
b.velocity = b.velocity.minus( u2 );
var vi = u1.multiply_scalar( a.mass ).plus( u2.multiply_scalar( b.mass ) ); // vi states if collision is elastic or inelastic
var vea = u1.multiply_scalar( a.mass - b.mass ).plus( u2.multiply_scalar( 2 * b.mass ) ); // velocity for elastic collision for object a
var veb = u2.multiply_scalar( b.mass - a.mass ).plus( u1.multiply_scalar( 2 * a.mass ) ); // velocity for elastic collision for object b
var bounce = a.bounce + b.bounce // bounce must be between 0 and 1
if(bounce > 1){bounce = 1.0}
if(bounce < 0){bounce = 0.0}
var fva = vea.multiply_scalar( bounce ).plus( vi.multiply_scalar( 1 - bounce ) ); // if bounce = 1, use elastic; if bounce = 0, use inelastic
var fvb = veb.multiply_scalar( bounce ).plus( vi.multiply_scalar( 1 - bounce ) ); // for values between 0 and 1, pick a point in the middle
fva = fva.divide_scalar( a.mass + b.mass ); // final velocity of a
fvb = fvb.divide_scalar( a.mass + b.mass ); // final velocity of b
// isn't it better to set them to fv ?
a.velocity = fva; //a.velocity.plus( fva );
b.velocity = fvb; //b.velocity.plus( fvb );
// detect if objects are stuck inside one another after movement
var afp = a.pos.plus( a.velocity );
var bfp = b.pos.plus( b.velocity );
// vector between objects
var v = afp.minus( bfp );
var vn = v.normalize();
// check if overlapping
var distance = v.length2()
var collision_distance = Math.pow(a.radius + b.radius,2);
if( distance > collision_distance ){return false}
log("collision response did not successfully separate objects");
/*
// if distance is 0 we are in same position so fabricate a vector
if(distance <= 0)
{ var vn = new Vec(0,0,1).normalize(); }
// separate them
a.pos = a.pos.plus( vn.multiply_scalar( a.radius ) );
b.pos = b.pos.minus( vn.multiply_scalar( b.radius ) );
*/
}
}
var BroadPhase =
{
sphere: function(bodies)
{
var collisions = [];
for(var i=0; i<bodies.length; i++)
{
var a = bodies[i];
var a_has_velocity = a.velocity.has_velocity();
// only check each pair once
for(var j=(i+1); j<bodies.length; j++)
{
var b = bodies[j];
// check collision masks
//if (!(b.mask.include?(a.type) && (!a.mask.include?(b.type)){continue}
// only check if either sphere moving
if (!(a_has_velocity || b.velocity.has_velocity())){continue}
// collect spheres which collide and the time/place it happens
var info = {};
if(CollisionTest.sphere_sphere( a, b, info ))
{
collisions.push([a,b,info])
}
}
}
return collisions;
}
}
var Body = function(s)
{
this.type = s['type'];
this.mask = s['mask'];
this.on_collision = s['on_collision'] || function(){return true};
this.pos = s['pos'] || new Vec(0,0,0);
this.orientation = s['orientation'] || new Quat(0, 0, 0, 1).normalize()
this.velocity = s['velocity'] || new Vec(0,0,0);
this.rotation_velocity = s['rotation_velocity'] || new Vec(0,0,0);
this.mass = s['mass'] || 1;
this.bounce = s['bounce'];
this.drag = s['drag'];
this.rotation_drag = s['rotation_drag'];
// numbers can be 0 which in js is boolean false
if(!this.mass){throw("mass cannot be zero")};
if(s.rotation_drag == undefined){ this.rotation_drag = 5 }
if(s.drag == undefined){ this.drag = 0.1 }
if(s.bounce == undefined){ this.bounce = 0.5 }
}
Body.prototype =
{
move: function( vector ) // eye space
{
this.pos = this.pos.plus(
this.orientation.vector(
vector
)
);
},
rotate: function( vector )
{
this.orientation = this.orientation.rotate(
vector.x, vector.y, vector.z
);
}
}
var SphereBody = function(s)
{
var body = new Body(s);
body.radius = s.radius || 50;
return body;
}
var World = function()
{
this.bodies = []
this.last_run = get_ticks();
this.interval = 1000/30
}
World.prototype =
{
update: function()
{
if(!this.check_interval()){return false}
this.drag();
this.collisions();
if(this.callback){this.callback()}
this.velocities();
return true;
},
check_interval: function()
{
if(!this.interval){return true}
if((get_ticks() - this.last_run) < this.interval){return false}
this.last_run = get_ticks();
return true;
},
drag: function()
{
for(var i=0; i<this.bodies.length; i++)
{
var body = this.bodies[i];
if( body.velocity.has_velocity() )
{
body.velocity = body.velocity.minus(
body.velocity.multiply_scalar(
body.drag
)
);
}
if( body.rotation_velocity.has_velocity() )
{
body.rotation_velocity = body.rotation_velocity.minus(
body.rotation_velocity.multiply_scalar(
body.rotation_drag
)
);
}
}
},
collisions: function()
{
var pairs = BroadPhase.sphere( this.bodies );
for( var i=0; i<pairs.length; i++)
{
var a = pairs[i][0];
var b = pairs[i][1];
var info = pairs[i][2];
if( ! a.on_collision( a, b ) ) { continue }
if( ! b.on_collision( b, a ) ) { continue }
CollisionResponse.sphere_sphere( a, b, info );
}
},
velocities: function()
{
for( var i=0; i<this.bodies.length; i++)
{
var body = this.bodies[i];
if( body.velocity.has_velocity() )
{
body.pos = body.pos.plus( body.velocity );
}
if( body.rotation_velocity.has_velocity() )
{
body.rotate( body.rotation_velocity );
}
}
}
}