-
Notifications
You must be signed in to change notification settings - Fork 3
/
box2d-helper.js
79 lines (60 loc) · 1.68 KB
/
box2d-helper.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
var B2Helper = B2Helper || (function () {
// -----------------------------------------------------------------------------
// Scale Methods
// -----------------------------------------------------------------------------
var scaleFactor = 10;
var helper = {
getScaleFactor: function() {
return scaleFactor;
},
scaleToWorld: function(a,b) {
var newv;
if (a instanceof box2d.b2Vec2) {
newv = new box2d.b2Vec2();
newv.x = (a.x)/scaleFactor;
newv.y = (a.y)/scaleFactor;
return newv;
} else if ("undefined"!=typeof b) {
newv = new box2d.b2Vec2();
newv.x = (a)/scaleFactor;
newv.y = (b)/scaleFactor;
return newv;
} else {
return a/scaleFactor;
}
},
scaleToPixels: function(a,b) {
var newv;
if (a instanceof box2d.b2Vec2) {
newv = new box2d.b2Vec2();
newv.x = a.x*scaleFactor;
newv.y = a.y*scaleFactor;
return newv;
} else if ("undefined"!=typeof b) {
newv = new box2d.b2Vec2();
newv.x = a*scaleFactor;
newv.y = b*scaleFactor;
return newv;
} else {
return a*scaleFactor;
}
},
// -----------------------------------------------------------------------------
// Create Methods
// -----------------------------------------------------------------------------
createWorld: function(scaleFactor_, noGravityFlag_) {
var theGravity = 20;
if ( noGravityFlag_ ) {
theGravity = 0;
}
var worldAABB = new box2d.b2AABB();
worldAABB.lowerBound.SetXY(-this.bounds, -this.bounds);
worldAABB.upperBound.SetXY(this.bounds, this.bounds);
var gravity = new box2d.b2Vec2(0,theGravity);
var doSleep = true;
scaleFactor = scaleFactor_ || scaleFactor;
return new box2d.b2World(gravity, doSleep);
}
};
return helper;
})();