|
1 | | -var GetBounds = function (transform) |
2 | | -{ |
3 | | - transform.updateAncestors(); |
| 1 | +var GetBounds = { |
4 | 2 |
|
5 | | - var min = Math.min; |
6 | | - var max = Math.max; |
7 | | - var parent = transform.parent; |
8 | | - var matrix = (parent) ? parent.world : transform.world; |
9 | | - |
10 | | - var bounds = {}; |
11 | | - |
12 | | - var frame = transform.gameObject.frame; |
13 | | - var width = frame ? frame.cutWidth : 0; |
14 | | - var height = frame ? frame.cutHeight : 0; |
15 | | - var children = transform.children; |
16 | | - |
17 | | - var x0 = transform._posX + transform._pivotX; |
18 | | - var y0 = transform._posY + transform._pivotY; |
19 | | - var x1 = x0 + width; |
20 | | - var y1 = y0 + height; |
21 | | - |
22 | | - // Apply transformation to every corner of our AABB |
23 | | - var topLeftX = x0 * matrix.a + y0 * matrix.c + matrix.tx; |
24 | | - var topLeftY = x0 * matrix.b + y0 * matrix.d + matrix.ty; |
25 | | - var topRightX = x1 * matrix.a + y0 * matrix.c + matrix.tx; |
26 | | - var topRightY = x1 * matrix.b + y0 * matrix.d + matrix.ty; |
27 | | - var bottomLeftX = x0 * matrix.a + y1 * matrix.c + matrix.tx; |
28 | | - var bottomLeftY = x0 * matrix.b + y1 * matrix.d + matrix.ty; |
29 | | - var bottomRightX = x1 * matrix.a + y1 * matrix.c + matrix.tx; |
30 | | - var bottomRightY = x1 * matrix.b + y1 * matrix.d + matrix.ty; |
31 | | - |
32 | | - // Get the minimum bounding rectangle |
33 | | - var xMin = min(topLeftX, topRightX, bottomLeftX, bottomRightX); |
34 | | - var xMax = max(topLeftX, topRightX, bottomLeftX, bottomRightX); |
35 | | - var yMin = min(topLeftY, topRightY, bottomLeftY, bottomRightY); |
36 | | - var yMax = max(topLeftY, topRightY, bottomLeftY, bottomRightY); |
37 | | - |
38 | | - var index; |
39 | | - var childBounds; |
40 | | - var tx; |
41 | | - var ty; |
42 | | - var tw; |
43 | | - var th; |
44 | | - var length = children.length; |
45 | | - |
46 | | - bounds.x = xMin; |
47 | | - bounds.y = yMin; |
48 | | - bounds.width = xMax - xMin; |
49 | | - bounds.height = yMax - yMin; |
50 | | - |
51 | | - if ((width === 0 || height === 0) && length > 0) |
| 3 | + getBounds: function () |
52 | 4 | { |
53 | | - index = 1; |
| 5 | + var a = this.angle; |
| 6 | + var r = this.rotation; |
54 | 7 |
|
55 | | - // The current game object doesn't have a size so we skip it. |
56 | | - bounds = children[0].getBounds(); |
57 | | - } |
| 8 | + var hct = this.height * Math.cos(r); |
| 9 | + var wct = this.width * Math.cos(r); |
| 10 | + var hst = this.height * Math.sin(r); |
| 11 | + var wst = this.width * Math.sin(r); |
58 | 12 |
|
59 | | - for (; index < length; ++index) |
60 | | - { |
61 | | - childBounds = children[index].getBounds(); |
| 13 | + var x = this.x; |
| 14 | + var y = this.y; |
62 | 15 |
|
63 | | - // Wrap around the child bounds |
64 | | - tx = min(childBounds.x, bounds.x); |
65 | | - ty = min(childBounds.y, bounds.y); |
66 | | - tw = max(childBounds.x + childBounds.width, bounds.x + bounds.width) - tx; |
67 | | - th = max(childBounds.y + childBounds.height, bounds.y + bounds.height) - ty; |
| 16 | + var x_min; |
| 17 | + var x_max; |
| 18 | + var y_min; |
| 19 | + var y_max; |
68 | 20 |
|
69 | | - bounds.x = tx; |
70 | | - bounds.y = ty; |
71 | | - bounds.width = tw; |
72 | | - bounds.height = th; |
73 | | - } |
| 21 | + if (a > 0) |
| 22 | + { |
| 23 | + if (a < 90) |
| 24 | + { |
| 25 | + // 0 < theta < 90 |
| 26 | + y_min = y; |
| 27 | + y_max = y + hct + wst; |
| 28 | + x_min = x - hst; |
| 29 | + x_max = x + wct; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + // 90 <= theta <= 180 |
| 34 | + y_min = y + hct; |
| 35 | + y_max = y + wst; |
| 36 | + x_min = x - hst + wct; |
| 37 | + x_max = x; |
| 38 | + } |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + if (a > -90) |
| 43 | + { |
| 44 | + // -90 < theta <= 0 |
| 45 | + y_min = y + wst; |
| 46 | + y_max = y + hct; |
| 47 | + x_min = x; |
| 48 | + x_max = x + wct - hst; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + // -180 <= theta <= -90 |
| 53 | + y_min = y + wst + hct; |
| 54 | + y_max = y; |
| 55 | + x_min = x + wct; |
| 56 | + x_max = x - hst; |
| 57 | + } |
| 58 | + } |
74 | 59 |
|
75 | | - return bounds; |
| 60 | + return { |
| 61 | + x: x_min, |
| 62 | + y: y_min, |
| 63 | + width: x_max - x_min, |
| 64 | + height: y_max - y_min |
| 65 | + }; |
| 66 | + } |
76 | 67 | }; |
77 | 68 |
|
78 | 69 | module.exports = GetBounds; |
0 commit comments