Skip to content

Commit dba84d5

Browse files
committed
Camera has a new property: lerp. This is a Point object, that allows you to control the amount of horizontal and vertical smoothing to be applied to the camera when it tracks a Sprite. It works both with and without deadzones, and is turned off by default. Set it to low values such as 0.1 for really smooth motion tracking (thanks to @WombatTurkey for the idea of adding this)
1 parent b9fab96 commit dba84d5

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
330330
* FrameData.destroy will nullify the local arrays used to contain Frame instances.
331331
* SoundManager.muteOnPause is a new boolean that allows you to control if the Sound system gets muted automatically when a Phaser game pauses, such as when it loses focus. You may need to set this to `false` if you wish to control the audio system from outside of your Phaser game, i.e. from DOM buttons or similar (#2382)
332332
* You can now pass a TilemapLayer as a Texture to a TileSprite. A limitation of this is that if you pass it to a TileSprite it will make a fill pattern from the TilemapLayer at that instant it's passed, and it won't keep track of the layer in future should it update (thanks @jdnichollsc #1989)
333+
* Camera has a new property: `lerp`. This is a Point object, that allows you to control the amount of horizontal and vertical smoothing to be applied to the camera when it tracks a Sprite. It works both with and without deadzones, and is turned off by default. Set it to low values such as 0.1 for really smooth motion tracking (thanks to @WombatTurkey for the idea of adding this)
333334

334335
### New Arcade Physics Features
335336

src/core/Camera.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ Phaser.Camera = function (game, id, x, y, width, height) {
9797
*/
9898
this.totalInView = 0;
9999

100+
/**
101+
* The linear interpolation value to use when following a target.
102+
* The default values of 1 means the camera will instantly snap to the target coordinates.
103+
* A lower value, such as 0.1 means the camera will more slowly track the target, giving
104+
* a smooth transition. You can set the horizontal and vertical values independently, and also
105+
* adjust this value in real-time during your game.
106+
* @property {Phaser.Point} lerp
107+
* @default
108+
*/
109+
this.lerp = Phaser.Point(1, 1);
110+
100111
/**
101112
* @property {Phaser.Point} _targetPosition - Internal point used to calculate target position
102113
* @private
@@ -158,19 +169,27 @@ Phaser.Camera.prototype = {
158169

159170
/**
160171
* Tell the camera which sprite to follow.
172+
*
173+
* You can set the follow type and a linear interpolation value.
174+
* Use low lerp values (such as 0.1) to automatically smooth the camera motion.
161175
*
162176
* If you find you're getting a slight "jitter" effect when following a Sprite it's probably to do with sub-pixel rendering of the Sprite position.
163177
* This can be disabled by setting `game.renderer.renderSession.roundPixels = true` to force full pixel rendering.
164178
*
165179
* @method Phaser.Camera#follow
166180
* @param {Phaser.Sprite|Phaser.Image|Phaser.Text} target - The object you want the camera to track. Set to null to not follow anything.
167181
* @param {number} [style] - Leverage one of the existing "deadzone" presets. If you use a custom deadzone, ignore this parameter and manually specify the deadzone after calling follow().
182+
* @param {float} [lerpX=1] - A value between 0 and 1. This value specifies the amount of linear interpolation to use when horizontally tracking the target. The closer the value to 1, the faster the camera will track.
183+
* @param {float} [lerpY=1] - A value between 0 and 1. This value specifies the amount of linear interpolation to use when vertically tracking the target. The closer the value to 1, the faster the camera will track.
168184
*/
169-
follow: function (target, style) {
185+
follow: function (target, style, lerpX, lerpY) {
170186

171187
if (style === undefined) { style = Phaser.Camera.FOLLOW_LOCKON; }
188+
if (lerpX === undefined) { lerpX = 1; }
189+
if (lerpY === undefined) { lerpY = 1; }
172190

173191
this.target = target;
192+
this.lerp.set(lerpX, lerpY);
174193

175194
var helper;
176195

@@ -278,28 +297,28 @@ Phaser.Camera.prototype = {
278297

279298
if (this._edge < this.deadzone.left)
280299
{
281-
this.view.x = this._targetPosition.x - this.deadzone.left;
300+
this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.deadzone.left, this.lerp.x);
282301
}
283302
else if (this._edge > this.deadzone.right)
284303
{
285-
this.view.x = this._targetPosition.x - this.deadzone.right;
304+
this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.deadzone.right, this.lerp.x);
286305
}
287306

288307
this._edge = this._targetPosition.y - this.view.y;
289308

290309
if (this._edge < this.deadzone.top)
291310
{
292-
this.view.y = this._targetPosition.y - this.deadzone.top;
311+
this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.deadzone.top, this.lerp.y);
293312
}
294313
else if (this._edge > this.deadzone.bottom)
295314
{
296-
this.view.y = this._targetPosition.y - this.deadzone.bottom;
315+
this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.deadzone.bottom, this.lerp.y);
297316
}
298317
}
299318
else
300319
{
301-
this.view.x = this._targetPosition.x - this.view.halfWidth;
302-
this.view.y = this._targetPosition.y - this.view.halfHeight;
320+
this.view.x = this.game.math.linear(this.view.x, this._targetPosition.x - this.view.halfWidth, this.lerp.x);
321+
this.view.y = this.game.math.linear(this.view.y, this._targetPosition.y - this.view.halfHeight, this.lerp.y);
303322
}
304323

305324
},

0 commit comments

Comments
 (0)