Skip to content

Commit b2c68c1

Browse files
committed
Graphics.drawTriangles will draw an array of vertices to the Graphics object (thanks @codevinsky, phaserjs#795)
Polygon.area will calculate the area of the Polygon (thanks @codevinsky, phaserjs#795)
1 parent 6f1ba28 commit b2c68c1

3 files changed

Lines changed: 90 additions & 48 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Version 2.0.5 - "Tanchico" - in development
8787
* Tilemap.setCollisionBetween has a new boolean parameter 'recalculate' which lets you control recalculation of the collision faces (thanks @max-m, #819)
8888
* Tilemap.setCollisionByExclusion has a new boolean parameter 'recalculate' which lets you control recalculation of the collision faces (thanks @max-m, #819)
8989
* Tilemap.setCollisionByIndex has a new boolean parameter 'recalculate' which lets you control recalculation of the collision faces (thanks @max-m, #819)
90+
* Graphics.drawTriangles will draw an array of vertices to the Graphics object (thanks @codevinsky, #795)
91+
* Polygon.area will calculate the area of the Polygon (thanks @codevinsky, #795)
9092

9193

9294
### New Plugins

src/gameobjects/Graphics.js

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -215,51 +215,66 @@ Phaser.Graphics.prototype.drawPolygon = function (poly) {
215215
/*
216216
* Draws a single {Phaser.Polygon} triangle from a {Phaser.Point} array
217217
*
218+
* @method Phaser.Graphics.prototype.drawTriangle
218219
* @param {Array<Phaser.Point>} points - An array of Phaser.Points that make up the three vertices of this triangle
219220
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
220-
* @method Phaser.Graphics.prototype.drawTriangle
221221
*/
222-
223222
Phaser.Graphics.prototype.drawTriangle = function(points, cull) {
223+
224+
if (typeof cull === 'undefined') { cull = false; }
225+
224226
var triangle = new Phaser.Polygon(points);
225-
if (cull) {
227+
228+
if (cull)
229+
{
226230
var cameraToFace = new Phaser.Point(this.game.camera.x - points[0].x, this.game.camera.y - points[0].y);
227231
var ab = new Phaser.Point(points[1].x - points[0].x, points[1].y - points[0].y);
228232
var cb = new Phaser.Point(points[1].x - points[2].x, points[1].y - points[2].y);
229233
var faceNormal = cb.cross(ab);
230-
if (cameraToFace.dot(faceNormal) > 0) {
234+
235+
if (cameraToFace.dot(faceNormal) > 0)
236+
{
231237
this.drawPolygon(triangle);
232238
}
233-
} else {
239+
}
240+
else
241+
{
234242
this.drawPolygon(triangle);
235243
}
236-
return;
244+
237245
};
238246

239247
/*
240248
* Draws {Phaser.Polygon} triangles
241249
*
250+
* @method Phaser.Graphics.prototype.drawTriangles
242251
* @param {Array<Phaser.Point>|Array<number>} vertices - An array of Phaser.Points or numbers that make up the vertices of the triangles
243252
* @param {Array<number>} {indices=null} - An array of numbers that describe what order to draw the vertices in
244253
* @param {boolean} [cull=false] - Should we check if the triangle is back-facing
245-
* @method Phaser.Graphics.prototype.drawTriangles
246254
*/
247-
248255
Phaser.Graphics.prototype.drawTriangles = function(vertices, indices, cull) {
249256

250-
var point1 = new Phaser.Point(),
251-
point2 = new Phaser.Point(),
252-
point3 = new Phaser.Point(),
253-
points = [],
254-
i;
257+
if (typeof cull === 'undefined') { cull = false; }
255258

256-
if (!indices) {
257-
if(vertices[0] instanceof Phaser.Point) {
258-
for(i = 0; i < vertices.length / 3; i++) {
259+
var point1 = new Phaser.Point();
260+
var point2 = new Phaser.Point();
261+
var point3 = new Phaser.Point();
262+
var points = [];
263+
var i;
264+
265+
if (!indices)
266+
{
267+
if (vertices[0] instanceof Phaser.Point)
268+
{
269+
for (i = 0; i < vertices.length / 3; i++)
270+
{
259271
this.drawTriangle([vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]], cull);
260272
}
261-
} else {
262-
for (i = 0; i < vertices.length / 6; i++) {
273+
}
274+
else
275+
{
276+
for (i = 0; i < vertices.length / 6; i++)
277+
{
263278
point1.x = vertices[i * 6 + 0];
264279
point1.y = vertices[i * 6 + 1];
265280
point2.x = vertices[i * 6 + 2];
@@ -268,26 +283,35 @@ Phaser.Graphics.prototype.drawTriangles = function(vertices, indices, cull) {
268283
point3.y = vertices[i * 6 + 5];
269284
this.drawTriangle([point1, point2, point3], cull);
270285
}
271-
272286
}
273-
} else {
274-
if(vertices[0] instanceof Phaser.Point) {
275-
for(i = 0; i < indices.length /3; i++) {
287+
}
288+
else
289+
{
290+
if (vertices[0] instanceof Phaser.Point)
291+
{
292+
for (i = 0; i < indices.length /3; i++)
293+
{
276294
points.push(vertices[indices[i * 3 ]]);
277295
points.push(vertices[indices[i * 3 + 1]]);
278296
points.push(vertices[indices[i * 3 + 2]]);
279-
if(points.length === 3) {
280-
this.drawTriangle(points, cull);
297+
298+
if (points.length === 3)
299+
{
300+
this.drawTriangle(points, cull);
281301
points = [];
282302
}
283-
284303
}
285-
} else {
286-
for (i = 0; i < indices.length; i++) {
304+
}
305+
else
306+
{
307+
for (i = 0; i < indices.length; i++)
308+
{
287309
point1.x = vertices[indices[i] * 2];
288310
point1.y = vertices[indices[i] * 2 + 1];
289311
points.push(point1.copyTo({}));
290-
if (points.length === 3) {
312+
313+
if (points.length === 3)
314+
{
291315
this.drawTriangle(points, cull);
292316
points = [];
293317
}
@@ -296,12 +320,11 @@ Phaser.Graphics.prototype.drawTriangles = function(vertices, indices, cull) {
296320
}
297321
};
298322

299-
300-
301323
/**
302324
* 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.
303325
* 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.
304326
* If you wish to work in radians instead of degrees use the property Sprite.rotation instead.
327+
*
305328
* @name Phaser.Graphics#angle
306329
* @property {number} angle - Gets or sets the angle of rotation in degrees.
307330
*/

src/geom/Polygon.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,19 @@ Phaser.Polygon.prototype = {
108108
Phaser.Polygon.prototype.constructor = Phaser.Polygon;
109109

110110
/*
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-
111+
* Sets and modifies the points of this polygon.
112+
*
113+
* @name Phaser.Polygon#points
114+
* @property {array<Phaser.Point>|array<number>} points - The array of vertex points
115+
*/
117116
Object.defineProperty(Phaser.Polygon.prototype, 'points', {
117+
118118
get: function() {
119119
return this._points;
120120
},
121+
121122
set: function(points) {
123+
122124
//if points isn't an array, use arguments as the array
123125
if (!(points instanceof Array))
124126
{
@@ -137,35 +139,49 @@ Object.defineProperty(Phaser.Polygon.prototype, 'points', {
137139

138140
points = p;
139141
}
142+
140143
this._points = points;
141144
}
145+
142146
});
143147

144148
/**
145149
* Returns the area of the polygon.
146150
*
147-
* @name Phaser.Polygon#area
151+
* @name Phaser.Circle#right
148152
* @readonly
149153
*/
150-
151154
Object.defineProperty(Phaser.Polygon.prototype, 'area', {
155+
152156
get: function() {
153-
var p1, p2, avgHeight, width, i,
154-
y0 = Number.MAX_VALUE,
155-
area = 0;
157+
158+
var p1;
159+
var p2;
160+
var avgHeight;
161+
var width;
162+
var i;
163+
var y0 = Number.MAX_VALUE;
164+
var area = 0;
156165

157166
// Find lowest boundary
158-
for(i = 0; i< this.points.length; i++) {
159-
if(this.points[i].y < y0) {
167+
for (i = 0; i < this.points.length; i++)
168+
{
169+
if (this.points[i].y < y0)
170+
{
160171
y0 = this.points[i].y;
161172
}
162173
}
163174

164-
for(i = 0; i< this.points.length; i++) {
175+
for (i = 0; i< this.points.length; i++)
176+
{
165177
p1 = this.points[i];
166-
if(i === this.points.length - 1) {
178+
179+
if (i === this.points.length - 1)
180+
{
167181
p2 = this.points[0];
168-
} else {
182+
}
183+
else
184+
{
169185
p2 = this.points[i+1];
170186
}
171187

@@ -175,9 +191,10 @@ Object.defineProperty(Phaser.Polygon.prototype, 'area', {
175191
}
176192

177193
return area;
194+
178195
}
196+
179197
});
198+
180199
// Because PIXI uses its own Polygon, we'll replace it with ours to avoid duplicating code or confusion.
181200
PIXI.Polygon = Phaser.Polygon;
182-
183-

0 commit comments

Comments
 (0)