Skip to content

Commit 6468614

Browse files
committed
Added Camera.worldView property and adjusted it to the correct values each frame.
1 parent a447d85 commit 6468614

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

src/cameras/2d/Camera.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,24 @@ var Camera = new Class({
209209
this._bounds = new Rectangle();
210210

211211
/**
212-
*
212+
* The World View is a Rectangle that defines the area of the 'world' the Camera is currently looking at.
213+
* This factors in the Camera viewport size, zoom and scroll position and is updated in the Camera preRender step.
214+
* If you have enabled Camera bounds the worldview will be clamped to those bounds accordingly.
215+
* You can use it for culling or intersection checks.
213216
*
214-
* @name Phaser.Cameras.Scene2D.Camera#worldBounds
217+
* @name Phaser.Cameras.Scene2D.Camera#worldView
215218
* @type {Phaser.Geom.Rectangle}
216219
* @readOnly
217220
* @since 3.11.0
218221
*/
219-
this.worldBounds = new Rectangle();
222+
this.worldView = new Rectangle();
220223

221224
/**
222225
* Is this Camera dirty?
223226
*
224-
* A dirty Camera has had either its viewport, its scroll, its rotation or its zoom level changed since the last frame.
227+
* A dirty Camera has had either its viewport size, bounds, scroll, rotation or zoom levels changed since the last frame.
228+
*
229+
* This flag is cleared during the `postRenderCamera` method of the renderer.
225230
*
226231
* @name Phaser.Cameras.Scene2D.Camera#dirty
227232
* @type {boolean}
@@ -1120,12 +1125,19 @@ var Camera = new Class({
11201125
{
11211126
var width = this.width;
11221127
var height = this.height;
1128+
1129+
var halfWidth = width * 0.5;
1130+
var halfHeight = height * 0.5;
1131+
11231132
var zoom = this.zoom * baseScale;
11241133
var matrix = this.matrix;
1134+
11251135
var originX = width * this.originX;
11261136
var originY = height * this.originY;
1137+
11271138
var follow = this._follow;
11281139
var deadzone = this.deadzone;
1140+
11291141
var sx = this.scrollX;
11301142
var sy = this.scrollY;
11311143

@@ -1178,16 +1190,25 @@ var Camera = new Class({
11781190
originY = Math.round(originY);
11791191
}
11801192

1193+
// Values are in pixels and not impacted by zooming the Camera
11811194
this.scrollX = sx;
11821195
this.scrollY = sy;
11831196

1184-
this.midPoint.set(sx + (width * 0.5), sy + (height * 0.5));
1197+
var midX = sx + halfWidth;
1198+
var midY = sy + halfHeight;
1199+
1200+
// The center of the camera, in world space, so taking zoom into account
1201+
// Basically the pixel value of what it's looking at in the middle of the cam
1202+
this.midPoint.set(midX, midY);
1203+
1204+
var displayWidth = width / zoom;
1205+
var displayHeight = height / zoom;
11851206

1186-
this.worldBounds.setTo(
1187-
sx,
1188-
sy,
1189-
sx + width,
1190-
sy + height
1207+
this.worldView.setTo(
1208+
midX - (displayWidth / 2),
1209+
midY - (displayHeight / 2),
1210+
displayWidth,
1211+
displayHeight
11911212
);
11921213

11931214
matrix.loadIdentity();

0 commit comments

Comments
 (0)