Skip to content

Commit c88749a

Browse files
committed
Added in GetBounds component and fixed angle error in Transform.
1 parent 1732419 commit c88749a

5 files changed

Lines changed: 64 additions & 71 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '6b67fed0-f97b-11e6-8dee-c37f09da9559'
2+
build: 'ee28bed0-f9ea-11e6-8c46-dd1a5325b6e9'
33
};
44
module.exports = CHECKSUM;

v3/src/components/GetBounds.js

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,69 @@
1-
var GetBounds = function (transform)
2-
{
3-
transform.updateAncestors();
1+
var GetBounds = {
42

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 ()
524
{
53-
index = 1;
5+
var a = this.angle;
6+
var r = this.rotation;
547

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);
5812

59-
for (; index < length; ++index)
60-
{
61-
childBounds = children[index].getBounds();
13+
var x = this.x;
14+
var y = this.y;
6215

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;
6820

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+
}
7459

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+
}
7667
};
7768

7869
module.exports = GetBounds;

v3/src/components/Transform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var MATH_CONST = require('../math/const');
2-
var WrapAngle = require('../math/angle/Wrap');
2+
var WrapAngle = require('../math/angle/WrapDegrees');
33

44
var _scaleX = 1;
55
var _scaleY = 1;
@@ -122,7 +122,7 @@ var Transform = {
122122
set: function (value)
123123
{
124124
// value is in degrees
125-
this.rotation = WrapAngle(value * MATH_CONST.DEG_TO_RAD);
125+
this.rotation = WrapAngle(value) * MATH_CONST.DEG_TO_RAD;
126126
}
127127
}
128128

v3/src/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
Children: require('./Children'),
66
Color: require('./Color'),
77
Data: require('./Data'),
8+
GetBounds: require('./GetBounds'),
89
ScaleMode: require('./ScaleMode'),
910
Size: require('./Size'),
1011
Texture: require('./Texture'),

v3/src/gameobjects/image/Image.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Image = new Class({
1414
Components.BlendMode,
1515
Components.ScaleMode,
1616
Components.Visible,
17+
Components.GetBounds,
1718
ImageRender
1819
],
1920

0 commit comments

Comments
 (0)