Skip to content

Commit f10c772

Browse files
committed
Graphics.drawTriangles && Polygon.area
Demo: http://phaser-triangles.herokuapp.com
1 parent 776e384 commit f10c772

2 files changed

Lines changed: 161 additions & 2 deletions

File tree

src/gameobjects/Graphics.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,92 @@ Phaser.Graphics.prototype.drawPolygon = function (poly) {
212212

213213
};
214214

215+
/*
216+
* Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
217+
*
218+
* @param {Array<Phaser.Point>} points - An array of Phaser.Points that make up the three vertices of this triangle
219+
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
220+
* @method Phaser.Graphics.prototype.drawTriangle
221+
*/
222+
223+
Phaser.Graphics.prototype.drawTriangle = function(points, cull) {
224+
var triangle = new Phaser.Polygon(points);
225+
if (cull) {
226+
var cameraToFace = new Phaser.Point(this.game.camera.x - points[0].x, this.game.camera.y - points[0].y);
227+
var ab = new Phaser.Point(points[1].x - points[0].x, points[1].y - points[0].y);
228+
var cb = new Phaser.Point(points[1].x - points[2].x, points[1].y - points[2].y);
229+
var faceNormal = cb.cross(ab);
230+
if (cameraToFace.dot(faceNormal) > 0) {
231+
this.drawPolygon(triangle);
232+
}
233+
} else {
234+
this.drawPolygon(triangle);
235+
}
236+
return;
237+
};
238+
239+
/*
240+
* Draws {Phaser.Polygon} triangles
241+
*
242+
* @param {Array<Phaser.Point>|Array<number>} vertices - An array of Phaser.Points or numbers that make up the vertices of the triangles
243+
* @param {Array<number>} {indices=null} - An array of numbers that describe what order to draw the vertices in
244+
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
245+
* @method Phaser.Graphics.prototype.drawTriangles
246+
*/
247+
248+
Phaser.Graphics.prototype.drawTriangles = function(vertices, indices, cull) {
249+
250+
var point1 = new Phaser.Point(),
251+
point2 = new Phaser.Point(),
252+
point3 = new Phaser.Point(),
253+
points = [],
254+
i;
255+
256+
if (!indices) {
257+
if(vertices[0] instanceof Phaser.Point) {
258+
for(i = 0; i < vertices.length / 3; i++) {
259+
this.drawTriangle([vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]], cull);
260+
}
261+
} else {
262+
for (i = 0; i < vertices.length / 6; i++) {
263+
point1.x = vertices[i * 6 + 0];
264+
point1.y = vertices[i * 6 + 1];
265+
point2.x = vertices[i * 6 + 2];
266+
point2.y = vertices[i * 6 + 3];
267+
point3.x = vertices[i * 6 + 4];
268+
point3.y = vertices[i * 6 + 5];
269+
this.drawTriangle([point1, point2, point3], cull);
270+
}
271+
272+
}
273+
} else {
274+
if(vertices[0] instanceof Phaser.Point) {
275+
for(i = 0; i < indices.length /3; i++) {
276+
points.push(vertices[indices[i * 3 ]]);
277+
points.push(vertices[indices[i * 3 + 1]]);
278+
points.push(vertices[indices[i * 3 + 2]]);
279+
if(points.length === 3) {
280+
this.drawTriangle(points, cull);
281+
points = [];
282+
}
283+
284+
}
285+
} else {
286+
for (i = 0; i < indices.length; i++) {
287+
point1.x = vertices[indices[i] * 2];
288+
point1.y = vertices[indices[i] * 2 + 1];
289+
points.push(point1.copyTo({}));
290+
if (points.length === 3) {
291+
this.drawTriangle(points, cull);
292+
points = [];
293+
}
294+
}
295+
}
296+
}
297+
};
298+
299+
300+
215301
/**
216302
* Indicates the rotation of the Graphics, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
217303
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.

src/geom/Polygon.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ Phaser.Polygon = function (points) {
4343
}
4444

4545
/**
46-
* @property {array<Phaser.Point>|array<number>} points - The array of Points.
46+
* @property {array<Phaser.Point>|array<number>} points - The array of vertex Points.
47+
* @private
4748
*/
48-
this.points = points;
49+
this._points = points;
4950

5051
};
5152

@@ -106,5 +107,77 @@ Phaser.Polygon.prototype = {
106107

107108
Phaser.Polygon.prototype.constructor = Phaser.Polygon;
108109

110+
/*
111+
* Sets and modifys the points of this polygon.
112+
*
113+
* @name Phaser.Graphics#fixedToCamera
114+
* @property {array<Phaser.Point>|array<number>} points - The array of vertex points
115+
*/
116+
117+
Object.defineProperty(Phaser.Polygon.prototype, 'points', {
118+
get: function() {
119+
return this._points;
120+
},
121+
set: function(points) {
122+
//if points isn't an array, use arguments as the array
123+
if (!(points instanceof Array))
124+
{
125+
points = Array.prototype.slice.call(arguments);
126+
}
127+
128+
//if this is a flat array of numbers, convert it to points
129+
if (typeof points[0] === 'number')
130+
{
131+
var p = [];
132+
133+
for (var i = 0, len = points.length; i < len; i += 2)
134+
{
135+
p.push(new Phaser.Point(points[i], points[i + 1]));
136+
}
137+
138+
points = p;
139+
}
140+
this._points = points;
141+
}
142+
});
143+
144+
/**
145+
* Returns the area of the polygon.
146+
*
147+
* @name Phaser.Polygon#area
148+
* @readonly
149+
*/
150+
151+
Object.defineProperty(Phaser.Polygon.prototype, 'area', {
152+
get: function() {
153+
var p1, p2, avgHeight, width, i,
154+
y0 = Number.MAX_VALUE,
155+
area = 0;
156+
157+
// Find lowest boundary
158+
for(i = 0; i< this.points.length; i++) {
159+
if(this.points[i].y < y0) {
160+
y0 = this.points[i].y;
161+
}
162+
}
163+
164+
for(i = 0; i< this.points.length; i++) {
165+
p1 = this.points[i];
166+
if(i === this.points.length - 1) {
167+
p2 = this.points[0];
168+
} else {
169+
p2 = this.points[i+1];
170+
}
171+
172+
avgHeight = ((p1.y - y0) + (p2.y - y0)) / 2;
173+
width = p1.x - p2.x;
174+
area += avgHeight * width;
175+
}
176+
177+
return area;
178+
}
179+
});
109180
// Because PIXI uses its own Polygon, we'll replace it with ours to avoid duplicating code or confusion.
110181
PIXI.Polygon = Phaser.Polygon;
182+
183+

0 commit comments

Comments
 (0)