Skip to content

Commit 92982d8

Browse files
committed
Added ability to get InCenter. translate and rotate Face
1 parent 52d5b00 commit 92982d8

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

src/gameobjects/mesh/Face.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
*/
66

77
var Class = require('../../utils/Class');
8+
var Vector2 = require('../../math/Vector2');
9+
10+
function GetLength (x1, y1, x2, y2)
11+
{
12+
var x = x1 - x2;
13+
var y = y1 - y2;
14+
var magnitude = (x * x) + (y * y);
15+
16+
return Math.sqrt(magnitude);
17+
}
818

919
/**
1020
* @classdesc
@@ -53,6 +63,127 @@ var Face = new Class({
5363
* @since 3.50.0
5464
*/
5565
this.vertex3 = vertex3;
66+
67+
/**
68+
* The face inCenter. Do not access directly, instead use the `getInCenter` method.
69+
*
70+
* @name Phaser.GameObjects.Face#_inCenter
71+
* @type {Phaser.Math.Vector2}
72+
* @private
73+
* @since 3.50.0
74+
*/
75+
this._inCenter = new Vector2();
76+
},
77+
78+
getInCenter: function ()
79+
{
80+
var v1 = this.vertex1;
81+
var v2 = this.vertex2;
82+
var v3 = this.vertex3;
83+
84+
var d1 = GetLength(v3.x, v3.y, v2.x, v2.y);
85+
var d2 = GetLength(v1.x, v1.y, v3.x, v3.y);
86+
var d3 = GetLength(v2.x, v2.y, v1.x, v1.y);
87+
88+
var p = d1 + d2 + d3;
89+
90+
return this._inCenter.set(
91+
(v1.x * d1 + v2.x * d2 + v3.x * d3) / p,
92+
(v1.y * d1 + v2.y * d2 + v3.y * d3) / p
93+
);
94+
},
95+
96+
translate: function (x, y)
97+
{
98+
if (y === undefined) { y = 0; }
99+
100+
var v1 = this.vertex1;
101+
var v2 = this.vertex2;
102+
var v3 = this.vertex3;
103+
104+
v1.translate(x, y);
105+
v2.translate(x, y);
106+
v3.translate(x, y);
107+
108+
return this;
109+
},
110+
111+
rotate: function (angle, cx, cy)
112+
{
113+
var x;
114+
var y;
115+
116+
// No point of rotation? Use the inCenter instead, then.
117+
if (cx === undefined && cy === undefined)
118+
{
119+
var inCenter = this.getInCenter();
120+
121+
x = inCenter.x;
122+
y = inCenter.y;
123+
}
124+
125+
var c = Math.cos(angle);
126+
var s = Math.sin(angle);
127+
128+
var v1 = this.vertex1;
129+
var v2 = this.vertex2;
130+
var v3 = this.vertex3;
131+
132+
var tx = v1.x - x;
133+
var ty = v1.y - y;
134+
135+
v1.setPosition(tx * c - ty * s + x, tx * s + ty * c + y);
136+
137+
tx = v2.x - x;
138+
ty = v2.y - y;
139+
140+
v2.setPosition(tx * c - ty * s + x, tx * s + ty * c + y);
141+
142+
tx = v3.x - x;
143+
ty = v3.y - y;
144+
145+
v3.setPosition(tx * c - ty * s + x, tx * s + ty * c + y);
146+
147+
return this;
148+
},
149+
150+
x: {
151+
152+
get: function ()
153+
{
154+
return this.getInCenter().x;
155+
},
156+
157+
set: function (value)
158+
{
159+
var current = this.getInCenter();
160+
161+
this.translate(value - current.x, 0);
162+
}
163+
164+
},
165+
166+
y: {
167+
168+
get: function ()
169+
{
170+
return this.getInCenter().y;
171+
},
172+
173+
set: function (value)
174+
{
175+
var current = this.getInCenter();
176+
177+
this.translate(0, value - current.y);
178+
}
179+
180+
},
181+
182+
destroy: function ()
183+
{
184+
this.vertex1 = null;
185+
this.vertex2 = null;
186+
this.vertex3 = null;
56187
}
57188

58189
});

0 commit comments

Comments
 (0)