-
Notifications
You must be signed in to change notification settings - Fork 1
/
cp.js
5927 lines (5923 loc) · 225 KB
/
cp.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007-2013 Scott Lembcke and Howling Moon Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Chipmunk JS 6.2.0 - Port of Chipmunk Physics
* @author lytc
*/
(function(exports, global) {
global["cp"] = exports;
var NDEBUG = true;
"use strict";
var CP_VERSION_MAJOR = 6;
var CP_VERSION_MINOR = 2;
var CP_VERSION_RELEASE = 0;
var cp = exports;
if (typeof module != "undefined") {
module.exports = cp;
}
var _nothing = function() {};
var _merge = function(dest, source) {
for (var i in source) {
if (source.hasOwnProperty(i)) {
dest[i] = source[i];
}
}
return dest;
};
var _extend = function(parent, child, overrides) {
var ctor = function() {
this.constructor = child;
};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
if (overrides) {
_merge(child.prototype, overrides);
}
child.__super__ = parent.prototype;
return child;
};
var cpAssertHard = function(condition, message) {
if (!condition) {
console.trace();
throw new Error(message);
}
};
var cpAssertSoft = function(condition, message) {
if (!condition) {
console.trace();
throw new Error(message);
}
};
var cpAssertWarn = function(condition, message) {
if (!condition) {
console.warn(message);
}
};
var CP_HASH_PAIR = cp.CP_HASH_PAIR = function(a, b) {
return a < b ? a + " " + b : b + " " + a;
};
//var CP_HASH_COEF = 3344921057
//var CP_HASH_PAIR = cp.CP_HASH_PAIR = function(A, B) {
// return /*(cpHashValue)*/A*CP_HASH_COEF ^ /*(cpHashValue)*/B*CP_HASH_COEF
//}
cp.versionString = CP_VERSION_MAJOR + "." + CP_VERSION_MINOR + "." + CP_VERSION_RELEASE;
//MARK: Misc Functions
//cpFloat
var cpMomentForCircle = cp.momentForCircle = function(/*cpFloat*/ m, /*cpFloat*/ r1, /*cpFloat*/ r2, /*cpVect*/ offset) {
return m * (.5 * (r1 * r1 + r2 * r2) + cpvlengthsq(offset));
};
//cpFloat
cp.areaForCircle = function(/*cpFloat*/ r1, /*cpFloat*/ r2) {
/*cpFloat*/
return M_PI * cpfabs(r1 * r1 - r2 * r2);
};
//cpFloat
var cpMomentForSegment = cp.momentForSegment = function(/*cpFloat*/ m, /*cpVect*/ a, /*cpVect*/ b) {
/*cpVect*/
var offset = cpvmult(cpvadd(a, b), .5);
return m * (cpvdistsq(b, a) / 12 + cpvlengthsq(offset));
};
//cpFloat
cp.areaForSegment = function(/*cpVect*/ a, /*cpVect*/ b, /*cpFloat*/ r) {
return r * (/*cpFloat*/ M_PI * r + 2 * cpvdist(a, b));
};
//cpFloat
var cpMomentForPoly = cp.momentForPoly = function(/*cpFloat*/ m, /*const cpVect*/ verts, /*cpVect*/ offset) {
var numVerts = verts.length;
if (numVerts == 2) return cpMomentForSegment(m, verts[0], verts[1]);
/*cpFloat*/
var sum1 = 0;
/*cpFloat*/
var sum2 = 0;
for (var i = 0; i < numVerts; i++) {
/*cpVect*/
var v1 = cpvadd(verts[i], offset);
/*cpVect*/
var v2 = cpvadd(verts[(i + 1) % numVerts], offset);
/*cpFloat*/
var a = cpvcross(v2, v1);
/*cpFloat*/
var b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
sum1 += a * b;
sum2 += a;
}
return m * sum1 / (6 * sum2);
};
//cpFloat
cp.areaForPoly = function(/*const cpVect*/ verts) {
/*cpFloat*/
var area = 0;
var numVerts = verts.length;
for (var i = 0; i < numVerts; i++) {
area += cpvcross(verts[i], verts[(i + 1) % numVerts]);
}
return -area / 2;
};
//cpVect
var cpCentroidForPoly = cp.centroidForPoly = function(/*const cpVect*/ verts) {
/*cpFloat*/
var sum = 0;
/*cpVect*/
var vsum = cpvzero;
var numVerts = verts.length;
for (/*int*/ var i = 0; i < numVerts; i++) {
/*cpVect*/
var v1 = verts[i];
/*cpVect*/
var v2 = verts[(i + 1) % numVerts];
/*cpFloat*/
var cross = cpvcross(v1, v2);
sum += cross;
vsum = cpvadd(vsum, cpvmult(cpvadd(v1, v2), cross));
}
return cpvmult(vsum, 1 / (3 * sum));
};
//void
cp.recenterPoly = function(/*cpVect*/ verts) {
/*cpVect*/
var centroid = cpCentroidForPoly(verts);
var numVerts = verts.length;
for (var i = 0; i < numVerts; i++) {
verts[i] = cpvsub(verts[i], centroid);
}
};
//cpFloat
var cpMomentForBox = cp.momentForBox = function(/*cpFloat*/ m, /*cpFloat*/ width, /*cpFloat*/ height) {
return m * (width * width + height * height) / 12;
};
//cpFloat
cp.momentForBox2 = function(/*cpFloat*/ m, /*cpBB*/ box) {
/*cpFloat*/
var width = box.r - box.l;
/*cpFloat*/
var height = box.t - box.b;
/*cpVect*/
var offset = cpvmult(new Vect(box.l + box.r, box.b + box.t), .5);
// TODO NaN when offset is 0 and m is Infinity
return cpMomentForBox(m, width, height) + m * cpvlengthsq(offset);
};
//MARK: Quick Hull
//void
var cpLoopIndexes = cp.loopIndexes = function(/*cpVect*/ verts, /*int*/ count) {
var start = 0, end = 0;
/*cpVect*/
var min = verts[0];
/*cpVect*/
var max = min;
for (var i = 1; i < count; i++) {
/*cpVect*/
var v = verts[i];
if (v.x < min.x || v.x == min.x && v.y < min.y) {
min = v;
start = i;
} else if (v.x > max.x || v.x == max.x && v.y > max.y) {
max = v;
end = i;
}
}
return [ start, end ];
};
var SWAP = function(arr, i1, i2) {
var tmp = arr[i2];
arr[i2] = arr[i1];
arr[i1] = tmp;
};
//static int
var QHullPartition = function(/*cpVect*/ verts, offset, /*int*/ count, /*cpVect*/ a, /*cpVect*/ b, /*cpFloat*/ tol) {
if (count == 0) return 0;
/*cpFloat*/
var max = 0;
/*int*/
var pivot = offset;
/*cpVect*/
var delta = cpvsub(b, a);
/*cpFloat*/
var valueTol = tol * cpvlength(delta);
/*int*/
var head = offset;
for (/*int*/ var tail = offset + count - 1; head <= tail; ) {
/*cpFloat*/
var value = cpvcross(delta, cpvsub(verts[head], a));
if (value > valueTol) {
if (value > max) {
max = value;
pivot = head;
}
head++;
} else {
SWAP(verts, head, tail);
tail--;
}
}
// move the new pivot to the front if it's not already there.
if (pivot != offset) SWAP(verts, offset, pivot);
return head - offset;
};
//static int
var QHullReduce = function(/*cpFloat*/ tol, /*cpVect*/ verts, offset, /*int*/ count, /*cpVect*/ a, /*cpVect*/ pivot, /*cpVect*/ b, resultPos) {
if (count < 0) {
return 0;
} else if (count == 0) {
verts[resultPos] = pivot;
return 1;
} else {
/*int*/
var left_count = QHullPartition(verts, offset, count, a, pivot, tol);
/*int*/
var index = QHullReduce(tol, verts, offset + 1, left_count - 1, a, verts[offset], pivot, resultPos);
verts[resultPos + index++] = pivot;
/*int*/
var right_count = QHullPartition(verts, offset + left_count, count - left_count, pivot, b, tol);
// console.log(verts);
// throw new Error('xxx')
return index + QHullReduce(tol, verts, offset + left_count + 1, right_count - 1, pivot, verts[offset + left_count], b, resultPos + index);
}
};
// QuickHull seemed like a neat algorithm, and efficient-ish for large input sets.
// My implementation performs an in place reduction using the result array as scratch space.
//int
cp.convexHull = function(/*int*/ count, /*cpVect*/ verts, /*cpVect*/ result, /*int*/ first, /*cpFloat*/ tol) {
if (result) {
// Copy the line vertexes into the empty part of the result polyline to use as a scratch buffer.
for (var i = 0; i < verts.length; i++) {
result[i] = verts[i];
}
} else {
// If a result array was not specified, reduce the input instead.
result = verts;
}
// Degenerate case, all poins are the same.
var indexes = cpLoopIndexes(verts, count);
var start = indexes[0], end = indexes[1];
if (start == end) {
return result;
}
SWAP(result, 0, start);
SWAP(result, 1, end == 0 ? start : end);
/*cpVect*/
var a = result[0];
/*cpVect*/
var b = result[1];
/*int*/
var resultCount = QHullReduce(tol, result, 2, count - 2, a, b, a, 1) + 1;
result.length = resultCount;
if (NDEBUG) {
cpAssertSoft(cpPolyValidate(result, resultCount), "Internal error: cpConvexHull() and cpPolyValidate() did not agree." + "Please report this error with as much info as you can.");
}
return result;
};
/// @defgroup basicTypes Basic Types
/// Most of these types can be configured at compile time.
/// @{
/// Chipmunk's floating point type.
/// Can be reconfigured at compile time.
var cpfsqrt = cp.fsqrt = Math.sqrt;
var cpfsin = cp.fsin = Math.sin;
var cpfcos = cp.fcos = Math.cos;
var cpfacos = cp.facos = Math.acos;
var cpfatan2 = cp.fatan2 = Math.atan2;
var cpfmod = cp.fmod = function(a, b) {
return a % b;
};
var cpfexp = cp.fexp = Math.exp;
var cpfpow = cp.fpow = Math.pow;
var cpffloor = cp.ffloor = Math.floor;
var CPFLOAT_MIN = 2.225074e-308;
var M_PI = Math.PI;
/// Return the max of two cpFloats.
//cpFloat
var isFF = global.navigator && global.navigator.userAgent.indexOf("Firefox") != -1;
var cpfmax = cp.fmax = isFF ? Math.max : function(/*cpFloat*/ a, /*cpFloat*/ b) {
return a > b ? a : b;
};
/// Return the min of two cpFloats.
//cpFloat
var cpfmin = cp.fmin = isFF ? Math.min : function(/*cpFloat*/ a, /*cpFloat*/ b) {
return a < b ? a : b;
};
/// Return the absolute value of a cpFloat.
//cpFloat
var cpfabs = cp.fabs = function(/*cpFloat*/ f) {
return f < 0 ? -f : f;
};
/// Clamp @c f to be between @c min and @c max.
//cpFloat
var cpfclamp = cp.fclamp = function(/*cpFloat*/ f, /*cpFloat*/ min, /*cpFloat*/ max) {
return cpfmin(cpfmax(f, min), max);
};
/// Clamp @c f to be between 0 and 1.
//cpFloat
var cpfclamp01 = function(/*cpFloat*/ f) {
return cpfmax(0, cpfmin(f, 1));
};
/// Linearly interpolate (or extrapolate) between @c f1 and @c f2 by @c t percent.
//cpFloat
cp.flerp = function(/*cpFloat*/ f1, /*cpFloat*/ f2, /*cpFloat*/ t) {
return f1 * (1 - t) + f2 * t;
};
/// Linearly interpolate from @c f1 to @c f2 by no more than @c d.
//cpFloat
cp.flerpconst = function(/*cpFloat*/ f1, /*cpFloat*/ f2, /*cpFloat*/ d) {
return f1 + cpfclamp(f2 - f1, -d, d);
};
/// Value for cpShape.group signifying that a shape is in no group.
var CP_NO_GROUP = cp.NO_GROUP = 0;
/// Value for cpShape.layers signifying that a shape is in every layer.
var CP_ALL_LAYERS = cp.ALL_LAYERS = ~0;
//
var Mat2x2 = function(/*cpFloat*/ a, b, c, d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
};
var Vect = cp.Vect = function(x, y) {
this.x = x;
this.y = y;
};
var cpv = cp.v = function(x, y) {
return {
x: x,
y: y
};
};
/// Constant for the zero vector.
/*cpVect*/
var cpvzero = cp.vzero = new Vect(0, 0);
/// Check if two vectors are equal. (Be careful when comparing floating point numbers!)
//cpBool
var cpveql = cp.v.eql = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return v1.x == v2.x && v1.y == v2.y;
};
/// Add two vectors
//cpVect
var cpvadd = cp.v.add = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return new Vect(v1.x + v2.x, v1.y + v2.y);
};
/// Subtract two vectors.
//cpVect
var cpvsub = cp.v.sub = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return new Vect(v1.x - v2.x, v1.y - v2.y);
};
/// Negate a vector.
//cpVect
var cpvneg = cp.v.neg = function(/*const cpVect*/ v) {
return new Vect(-v.x, -v.y);
};
/// Scalar multiplication.
//cpVect
var cpvmult = cp.v.mult = function(/*const cpVect*/ v, /*const cpFloat*/ s) {
return new Vect(v.x * s, v.y * s);
};
/// Vector dot product.
//cpFloat
var cpvdot = cp.v.dot = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return v1.x * v2.x + v1.y * v2.y;
};
/// 2D vector cross product analog.
/// The cross product of 2D vectors results in a 3D vector with only a z component.
/// This function returns the magnitude of the z value.
//cpFloat
var cpvcross = cp.v.cross = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return v1.x * v2.y - v1.y * v2.x;
};
/// Returns a perpendicular vector. (90 degree rotation)
//cpVect
var cpvperp = cp.v.perp = function(/*const cpVect*/ v) {
return new Vect(-v.y, v.x);
};
/// Returns a perpendicular vector. (-90 degree rotation)
//cpVect
cp.v.rperp = function(/*const cpVect*/ v) {
return new Vect(v.y, -v.x);
};
/// Returns the vector projection of v1 onto v2.
//cpVect
var cpvproject = cp.v.project = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
// return cpvmult(v2, cpvdot(v1, v2)/cpvdot(v2, v2));
var f = (v1.x * v2.x + v1.y * v2.y) / (v2.x * v2.x + v2.y * v2.y);
return new Vect(v2.x * f, v2.y * f);
};
/// Returns the unit length vector for the given angle (in radians).
//cpVect
cp.v.forangle = function(/*const cpFloat*/ a) {
return new Vect(cpfcos(a), cpfsin(a));
};
/// Returns the angular direction v is pointing in (in radians).
//cpFloat
cp.v.toangle = function(/*const cpVect*/ v) {
return cpfatan2(v.y, v.x);
};
/// Uses complex number multiplication to rotate v1 by v2. Scaling will occur if v1 is not a unit vector.
//cpVect
var cpvrotate = cp.v.rotate = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return new Vect(v1.x * v2.x - v1.y * v2.y, v1.x * v2.y + v1.y * v2.x);
};
/// Inverse of cpvrotate().
//cpVect
var cpvunrotate = cp.v.unrotate = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
return new Vect(v1.x * v2.x + v1.y * v2.y, v1.y * v2.x - v1.x * v2.y);
};
/// Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths.
//cpFloat
var cpvlengthsq = cp.v.lengthsq = function(/*const cpVect*/ v) {
// return cpvdot(v, v);
return v.x * v.x + v.y * v.y;
};
/// Returns the length of v.
//cpFloat
var cpvlength = cp.v.len = function(/*const cpVect*/ v) {
// return cpfsqrt(cpvdot(v, v));
return cpfsqrt(v.x * v.x + v.y * v.y);
};
/// Linearly interpolate between v1 and v2.
//cpVect
var cpvlerp = cp.v.lerp = function(/*const cpVect*/ v1, /*const cpVect*/ v2, /*const cpFloat*/ t) {
// return cpvadd(cpvmult(v1, 1.0 - t), cpvmult(v2, t));
var t2 = 1 - t;
return new Vect(v1.x * t2 + v2.x * t, v1.y * t2 + v2.y * t);
};
/// Returns a normalized copy of v.
//cpVect
var cpvnormalize = cp.v.normalize = function(/*const cpVect*/ v) {
// Neat trick I saw somewhere to avoid div/0.
// return cpvmult(v, 1.0/(cpvlength(v) + CPFLOAT_MIN));
var f = cpfsqrt(v.x * v.x + v.y * v.y) + CPFLOAT_MIN;
return new Vect(v.x / f, v.y / f);
};
/// @deprecated Just an alias for cpvnormalize() now.
//cpVect
cp.v.normalize_safe = function(/*const cpVect*/ v) {
return cpvnormalize(v);
};
/// Clamp v to length len.
//cpVect
var cpvclamp = cp.v.clamp = function(/*const cpVect*/ v, /*const cpFloat*/ len) {
// return (cpvdot(v,v) > len*len) ? cpvmult(cpvnormalize(v), len) : v;
var vlenSq = v.x * v.x + v.y * v.y;
if (vlenSq > len * len) {
var f = cpfsqrt(vlenSq) + CPFLOAT_MIN;
return new Vect(v.x * len / f, v.y * len / f);
}
return v;
};
/// Linearly interpolate between v1 towards v2 by distance d.
//cpVect
cp.v.lerpconst = function(/*cpVect*/ v1, /*cpVect*/ v2, /*cpFloat*/ d) {
return cpvadd(v1, cpvclamp(cpvsub(v2, v1), d));
};
/// Returns the distance between v1 and v2.
//cpFloat
var cpvdist = cp.v.dist = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
// return cpvlength(cpvsub(v1, v2));
var x = v1.x - v2.x;
var y = v1.y - v2.y;
return cpfsqrt(x * x + y * y);
};
/// Returns the squared distance between v1 and v2. Faster than cpvdist() when you only need to compare distances.
//cpFloat
var cpvdistsq = cp.v.distsq = function(/*const cpVect*/ v1, /*const cpVect*/ v2) {
// return cpvlengthsq(cpvsub(v1, v2));
var x = v1.x - v2.x;
var y = v1.y - v2.y;
return x * x + y * y;
};
/// Returns true if the distance between v1 and v2 is less than dist.
//cpBool
cp.v.near = function(/*const cpVect*/ v1, /*const cpVect*/ v2, /*const cpFloat*/ dist) {
return cpvdistsq(v1, v2) < dist * dist;
};
/// @}
/// @defgroup cpMat2x2 cpMat2x2
/// 2x2 matrix type used for tensors and such.
/// @{
//static inline cpVect
Mat2x2.prototype.transform = function(/*cpVect*/ v) {
var m = this;
return new Vect(v.x * m.a + v.y * m.b, v.x * m.c + v.y * m.d);
};
//cpVect
var cpvslerp = cp.v.slerp = function(/*const cpVect*/ v1, /*const cpVect*/ v2, /*const cpFloat*/ t) {
/*cpFloat*/
var dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2));
/*cpFloat*/
var omega = cpfacos(cpfclamp(dot, -1, 1));
if (omega < .001) {
// If the angle between two vectors is very small, lerp instead to avoid precision issues.
return cpvlerp(v1, v2, t);
} else {
/*cpFloat*/
var denom = 1 / cpfsin(omega);
return cpvadd(cpvmult(v1, cpfsin((1 - t) * omega) * denom), cpvmult(v2, cpfsin(t * omega) * denom));
}
};
//cpVect
cp.v.slerpconst = function(/*const cpVect*/ v1, /*const cpVect*/ v2, /*const cpFloat*/ a) {
/*cpFloat*/
var dot = cpvdot(cpvnormalize(v1), cpvnormalize(v2));
/*cpFloat*/
var omega = cpfacos(cpfclamp(dot, -1, 1));
return cpvslerp(v1, v2, cpfmin(a, omega) / omega);
};
//char*
cp.v.str = function(/*const cpVect*/ v) {
return "(" + v.x + ", " + v.y + ")";
};
//void
var Constraint = cp.Constraint = function(/*cpBody*/ a, /*cpBody*/ b) {
var constraint = this;
constraint.a = a;
constraint.b = b;
constraint.space = null;
constraint.next_a = null;
constraint.next_b = null;
constraint.maxForce = Infinity;
constraint.errorBias = cpfpow(1 - .1, 60);
constraint.maxBias = Infinity;
};
//Constraint.prototype.applyCachedImpulse = function(/*cpFloat*/ dt_coef) {}
Constraint.prototype.preSolve = _nothing;
Constraint.prototype.postSolve = _nothing;
Constraint.prototype.applyCachedImpulse = _nothing;
//static cpFloat
var defaultSpringTorque = function(/*cpDampedRotarySpring*/ spring, /*cpFloat*/ relativeAngle) {
return (relativeAngle - spring.restAngle) * spring.stiffness;
};
//cpDampedRotarySpring *
var DampedRotarySpring = cp.DampedRotarySpring = function(/*cpBody*/ a, /*cpBody*/ b, /*cpFloat*/ restAngle, /*cpFloat*/ stiffness, /*cpFloat*/ damping) {
Constraint.apply(this, arguments);
var spring = this;
spring.restAngle = restAngle;
spring.stiffness = stiffness;
spring.damping = damping;
spring.springTorqueFunc = /*(cpDampedRotarySpringTorqueFunc)*/ defaultSpringTorque;
spring.jAcc = 0;
};
_extend(Constraint, DampedRotarySpring);
//static void
DampedRotarySpring.prototype.preStep = function(/*cpFloat*/ dt) {
var spring = this;
/*cpBody*/
var a = spring.a;
/*cpBody*/
var b = spring.b;
/*cpFloat*/
var moment = a.i_inv + b.i_inv;
if (NDEBUG) {
cpAssertSoft(moment != 0, "Unsolvable spring.");
}
spring.iSum = 1 / moment;
spring.w_coef = 1 - cpfexp(-spring.damping * dt * moment);
spring.target_wrn = 0;
// apply spring torque
/*cpFloat*/
var j_spring = spring.springTorqueFunc(/*cpConstraint*/ spring, a.a - b.a) * dt;
spring.jAcc = j_spring;
a.w -= j_spring * a.i_inv;
b.w += j_spring * b.i_inv;
};
//static void
DampedRotarySpring.prototype.applyImpulse = function(/*cpFloat*/ dt) {
var spring = this;
/*cpBody*/
var a = spring.a;
/*cpBody*/
var b = spring.b;
// compute relative velocity
/*cpFloat*/
var wrn = a.w - b.w;
//normal_relative_velocity(a, b, r1, r2, n) - spring.target_vrn;
// compute velocity loss from drag
// not 100% certain this is derived correctly, though it makes sense
/*cpFloat*/
var w_damp = (spring.target_wrn - wrn) * spring.w_coef;
spring.target_wrn = wrn + w_damp;
//apply_impulses(a, b, spring.r1, spring.r2, cpvmult(spring.n, v_damp*spring.nMass));
/*cpFloat*/
var j_damp = w_damp * spring.iSum;
spring.jAcc += j_damp;
a.w += j_damp * a.i_inv;
b.w -= j_damp * b.i_inv;
};
//static cpFloat
DampedRotarySpring.prototype.getImpulse = function() {
return this.jAcc;
};
//static cpFloat
var defaultSpringForce = function(/*cpDampedSpring **/ spring, /*cpFloat*/ dist) {
return (spring.restLength - dist) * spring.stiffness;
};
//cpDampedSpring *
var DampedSpring = cp.DampedSpring = function(/*cpBody*/ a, /*cpBody*/ b, /*cpVect*/ anchr1, /*cpVect*/ anchr2, /*cpFloat*/ restLength, /*cpFloat*/ stiffness, /*cpFloat*/ damping) {
Constraint.apply(this, arguments);
var spring = this;
spring.anchr1 = anchr1;
spring.anchr2 = anchr2;
spring.restLength = restLength;
spring.stiffness = stiffness;
spring.damping = damping;
spring.springForceFunc = /*(cpDampedSpringForceFunc)*/ defaultSpringForce;
spring.jAcc = 0;
};
_extend(Constraint, DampedSpring);
//static void
DampedSpring.prototype.preStep = function(/*cpFloat*/ dt) {
var spring = this;
/*cpBody*/
var a = spring.a;
/*cpBody*/
var b = spring.b;
spring.r1 = cpvrotate(spring.anchr1, a.rot);
spring.r2 = cpvrotate(spring.anchr2, b.rot);
/*cpVect*/
var delta = cpvsub(cpvadd(b.p, spring.r2), cpvadd(a.p, spring.r1));
/*cpFloat*/
var dist = cpvlength(delta);
spring.n = cpvmult(delta, 1 / (dist ? dist : Infinity));
/*cpFloat*/
var k = k_scalar(a, b, spring.r1, spring.r2, spring.n);
if (NDEBUG) {
cpAssertSoft(k != 0, "Unsolvable spring.");
}
spring.nMass = 1 / k;
spring.target_vrn = 0;
spring.v_coef = 1 - cpfexp(-spring.damping * dt * k);
// apply spring force
/*cpFloat*/
var f_spring = spring.springForceFunc(/*cpConstraint*/ spring, dist);
/*cpFloat*/
var j_spring = spring.jAcc = f_spring * dt;
apply_impulses(a, b, spring.r1, spring.r2, cpvmult(spring.n, j_spring));
};
//static void
DampedSpring.prototype.applyImpulse = function(/*cpFloat*/ dt) {
var spring = this;
/*cpBody*/
var a = spring.a;
/*cpBody*/
var b = spring.b;
/*cpVect*/
var n = spring.n;
/*cpVect*/
var r1 = spring.r1;
/*cpVect*/
var r2 = spring.r2;
// compute relative velocity
/*cpFloat*/
var vrn = normal_relative_velocity(a, b, r1, r2, n);
// compute velocity loss from drag
/*cpFloat*/
var v_damp = (spring.target_vrn - vrn) * spring.v_coef;
spring.target_vrn = vrn + v_damp;
/*cpFloat*/
var j_damp = v_damp * spring.nMass;
spring.jAcc += j_damp;
apply_impulses(a, b, spring.r1, spring.r2, cpvmult(spring.n, j_damp));
};
//static cpFloat
DampedSpring.prototype.getImpulse = function() {
return this.jAcc;
};
//cpGearJoint *
var GearJoint = cp.GearJoint = function(/*cpBody*/ a, /*cpBody*/ b, /*cpFloat*/ phase, /*cpFloat*/ ratio) {
Constraint.apply(this, arguments);
var joint = this;
joint.phase = phase;
joint.ratio = ratio;
joint.ratio_inv = 1 / ratio;
joint.jAcc = 0;
};
_extend(Constraint, GearJoint);
//static void
GearJoint.prototype.preStep = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
// calculate moment of inertia coefficient.
joint.iSum = 1 / (a.i_inv * joint.ratio_inv + joint.ratio * b.i_inv);
// calculate bias velocity
/*cpFloat*/
var maxBias = joint.maxBias;
joint.bias = cpfclamp(-bias_coef(joint.errorBias, dt) * (b.a * joint.ratio - a.a - joint.phase) / dt, -maxBias, maxBias);
};
//static void
GearJoint.prototype.applyCachedImpulse = function(/*cpFloat*/ dt_coef) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
/*cpFloat*/
var j = joint.jAcc * dt_coef;
a.w -= j * a.i_inv * joint.ratio_inv;
b.w += j * b.i_inv;
};
//static void
GearJoint.prototype.applyImpulse = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
// compute relative rotational velocity
/*cpFloat*/
var wr = b.w * joint.ratio - a.w;
/*cpFloat*/
var jMax = joint.maxForce * dt;
// compute normal impulse
/*cpFloat*/
var j = (joint.bias - wr) * joint.iSum;
/*cpFloat*/
var jOld = joint.jAcc;
joint.jAcc = cpfclamp(jOld + j, -jMax, jMax);
j = joint.jAcc - jOld;
// apply impulse
a.w -= j * a.i_inv * joint.ratio_inv;
b.w += j * b.i_inv;
};
//static cpFloat
GearJoint.prototype.getImpulse = function() {
var joint = this;
return cpfabs(joint.jAcc);
};
//void
GearJoint.prototype.setRatio = function(/*cpFloat*/ value) {
var constraint = this;
constraint.ratio = value;
constraint.ratio_inv = 1 / value;
constraint.activateBodies();
};
//cpGrooveJoint *
var GrooveJoint = cp.GrooveJoint = function(/*cpBody*/ a, /*cpBody*/ b, /*cpVect*/ groove_a, /*cpVect*/ groove_b, /*cpVect*/ anchr2) {
Constraint.apply(this, arguments);
var joint = this;
joint.grv_a = groove_a;
joint.grv_b = groove_b;
joint.grv_n = cpvperp(cpvnormalize(cpvsub(groove_b, groove_a)));
joint.anchr2 = anchr2;
joint.jAcc = cpvzero;
};
_extend(Constraint, GrooveJoint);
//static void
GrooveJoint.prototype.preStep = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
// calculate endpoints in worldspace
/*cpVect*/
var ta = a.local2World(joint.grv_a);
/*cpVect*/
var tb = a.local2World(joint.grv_b);
// calculate axis
/*cpVect*/
var n = cpvrotate(joint.grv_n, a.rot);
/*cpFloat*/
var d = cpvdot(ta, n);
joint.grv_tn = n;
joint.r2 = cpvrotate(joint.anchr2, b.rot);
// calculate tangential distance along the axis of r2
/*cpFloat*/
var td = cpvcross(cpvadd(b.p, joint.r2), n);
// calculate clamping factor and r2
if (td <= cpvcross(ta, n)) {
joint.clamp = 1;
joint.r1 = cpvsub(ta, a.p);
} else if (td >= cpvcross(tb, n)) {
joint.clamp = -1;
joint.r1 = cpvsub(tb, a.p);
} else {
joint.clamp = 0;
joint.r1 = cpvsub(cpvadd(cpvmult(cpvperp(n), -td), cpvmult(n, d)), a.p);
}
// Calculate mass tensor
joint.k = k_tensor(a, b, joint.r1, joint.r2);
// calculate bias velocity
/*cpVect*/
var delta = cpvsub(cpvadd(b.p, joint.r2), cpvadd(a.p, joint.r1));
joint.bias = cpvclamp(cpvmult(delta, -bias_coef(joint.errorBias, dt) / dt), joint.maxBias);
};
//static void
GrooveJoint.prototype.applyCachedImpulse = function(/*cpFloat*/ dt_coef) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
apply_impulses(a, b, joint.r1, joint.r2, cpvmult(joint.jAcc, dt_coef));
};
//static inline cpVect
GrooveJoint.prototype.grooveConstrain = function(/*cpVect*/ j, /*cpFloat*/ dt) {
var joint = this;
/*cpVect*/
var n = joint.grv_tn;
/*cpVect*/
var jClamp = joint.clamp * cpvcross(j, n) > 0 ? j : cpvproject(j, n);
return cpvclamp(jClamp, joint.maxForce * dt);
};
//static void
GrooveJoint.prototype.applyImpulse = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
/*cpVect*/
var r1 = joint.r1;
/*cpVect*/
var r2 = joint.r2;
// compute impulse
/*cpVect*/
var vr = relative_velocity(a, b, r1, r2);
/*cpVect*/
var j = joint.k.transform(cpvsub(joint.bias, vr));
/*cpVect*/
var jOld = joint.jAcc;
joint.jAcc = joint.grooveConstrain(cpvadd(jOld, j), dt);
j = cpvsub(joint.jAcc, jOld);
// apply impulse
apply_impulses(a, b, joint.r1, joint.r2, j);
};
//static cpFloat
GrooveJoint.prototype.getImpulse = function() {
return cpvlength(this.jAcc);
};
//void
GrooveJoint.prototype.setGrooveA = function(/*cpVect*/ value) {
/*cpGrooveJoint*/
var g = this;
g.grv_a = value;
g.grv_n = cpvperp(cpvnormalize(cpvsub(g.grv_b, value)));
g.activateBodies();
};
//void
GrooveJoint.prototype.setGrooveB = function(/*cpVect*/ value) {
/*cpGrooveJoint*/
var g = this;
g.grv_b = value;
g.grv_n = cpvperp(cpvnormalize(cpvsub(value, g.grv_a)));
g.activateBodies();
};
//cpPinJoint *
var PinJoint = cp.PinJoint = function(/*cpBody*/ a, /*cpBody*/ b, /*cpVect*/ anchr1, /*cpVect*/ anchr2) {
Constraint.apply(this, arguments);
var joint = this;
joint.anchr1 = anchr1;
joint.anchr2 = anchr2;
// STATIC_BODY_CHECK
/*cpVect*/
var p1 = a ? cpvadd(a.p, cpvrotate(anchr1, a.rot)) : anchr1;
/*cpVect*/
var p2 = b ? cpvadd(b.p, cpvrotate(anchr2, b.rot)) : anchr2;
joint.dist = cpvlength(cpvsub(p2, p1));
if (NDEBUG) {
cpAssertWarn(joint.dist > 0, "You created a 0 length pin joint. A pivot joint will be much more stable.");
}
joint.jnAcc = 0;
};
_extend(Constraint, PinJoint);
//static void
PinJoint.prototype.preStep = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
joint.r1 = cpvrotate(joint.anchr1, a.rot);
joint.r2 = cpvrotate(joint.anchr2, b.rot);
/*cpVect*/
var delta = cpvsub(cpvadd(b.p, joint.r2), cpvadd(a.p, joint.r1));
/*cpFloat*/
var dist = cpvlength(delta);
joint.n = cpvmult(delta, 1 / (dist ? dist : Infinity));
// calculate mass normal
joint.nMass = 1 / k_scalar(a, b, joint.r1, joint.r2, joint.n);
// calculate bias velocity
/*cpFloat*/
var maxBias = joint.maxBias;
joint.bias = cpfclamp(-bias_coef(joint.errorBias, dt) * (dist - joint.dist) / dt, -maxBias, maxBias);
};
//static void
PinJoint.prototype.applyCachedImpulse = function(/*cpFloat*/ dt_coef) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
/*cpVect*/
var j = cpvmult(joint.n, joint.jnAcc * dt_coef);
apply_impulses(a, b, joint.r1, joint.r2, j);
};
//static void
PinJoint.prototype.applyImpulse = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
/*cpVect*/
var n = joint.n;
// compute relative velocity
/*cpFloat*/
var vrn = normal_relative_velocity(a, b, joint.r1, joint.r2, n);
/*cpFloat*/
var jnMax = joint.maxForce * dt;
// compute normal impulse
/*cpFloat*/
var jn = (joint.bias - vrn) * joint.nMass;
/*cpFloat*/
var jnOld = joint.jnAcc;
joint.jnAcc = cpfclamp(jnOld + jn, -jnMax, jnMax);
jn = joint.jnAcc - jnOld;
// apply impulse
apply_impulses(a, b, joint.r1, joint.r2, cpvmult(n, jn));
};
//static cpFloat
PinJoint.prototype.getImpulse = function() {
return cpfabs(this.jnAcc);
};
//cpPivotJoint *
var PivotJoint = cp.PivotJoint = function(/*cpBody*/ a, /*cpBody*/ b, /*cpVect*/ anchr1, /*cpVect*/ anchr2) {
Constraint.apply(this, arguments);
var joint = this;
if (!anchr2) {
var pivot = anchr1;
/*cpVect*/
anchr1 = a ? a.world2Local(pivot) : pivot;
/*cpVect*/
anchr2 = b ? b.world2Local(pivot) : pivot;
}
joint.anchr1 = anchr1;
joint.anchr2 = anchr2;
joint.jAcc = cpvzero;
};
_extend(Constraint, PivotJoint);
//static void
PivotJoint.prototype.preStep = function(/*cpFloat*/ dt) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;
joint.r1 = cpvrotate(joint.anchr1, a.rot);
joint.r2 = cpvrotate(joint.anchr2, b.rot);
// Calculate mass tensor
joint.k = k_tensor(a, b, joint.r1, joint.r2);
// calculate bias velocity
/*cpVect*/
var delta = cpvsub(cpvadd(b.p, joint.r2), cpvadd(a.p, joint.r1));
joint.bias = cpvclamp(cpvmult(delta, -bias_coef(joint.errorBias, dt) / dt), joint.maxBias);
};
//static void
PivotJoint.prototype.applyCachedImpulse = function(/*cpFloat*/ dt_coef) {
var joint = this;
/*cpBody*/
var a = joint.a;
/*cpBody*/
var b = joint.b;