mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 05:33:35 +00:00
Added in the Arcade Physics utils functions.
This commit is contained in:
parent
7c657a916a
commit
3c91bbf236
8 changed files with 207 additions and 0 deletions
32
v3/src/physics/arcade/utils/AccelerateTo.js
Normal file
32
v3/src/physics/arcade/utils/AccelerateTo.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* Move the given display object towards the x/y coordinates at a steady velocity.
|
||||
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
||||
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
||||
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
||||
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
||||
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#moveTo
|
||||
* @param {any} displayObject - The display object to move.
|
||||
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
||||
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
||||
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
||||
*/
|
||||
var AccelerateTo = function (gameObject, x, y, speed, xSpeedMax, ySpeedMax)
|
||||
{
|
||||
if (speed === undefined) { speed = 60; }
|
||||
|
||||
var angle = Math.atan2(y - gameObject.y, x - gameObject.x);
|
||||
|
||||
gameObject.body.acceleration.setToPolar(angle, speed);
|
||||
|
||||
if (xSpeedMax !== undefined && ySpeedMax !== undefined)
|
||||
{
|
||||
gameObject.body.maxVelocity.set(xSpeedMax, ySpeedMax);
|
||||
}
|
||||
|
||||
return angle;
|
||||
};
|
||||
|
||||
module.exports = AccelerateTo;
|
23
v3/src/physics/arcade/utils/AccelerateToObject.js
Normal file
23
v3/src/physics/arcade/utils/AccelerateToObject.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
var AccelerateTo = require('./AccelerateTo');
|
||||
|
||||
/**
|
||||
* Move the given display object towards the destination object at a steady velocity.
|
||||
* If you specify a maxTime then it will adjust the speed (overwriting what you set) so it arrives at the destination in that number of seconds.
|
||||
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
||||
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
||||
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
||||
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#AccelerateToObject
|
||||
* @param {any} displayObject - The display object to move.
|
||||
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
||||
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
||||
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
||||
*/
|
||||
var AccelerateToObject = function (gameObject, destination, speed, xSpeedMax, ySpeedMax)
|
||||
{
|
||||
return AccelerateTo(gameObject, destination.x, destination.y, speed, xSpeedMax, ySpeedMax);
|
||||
};
|
||||
|
||||
module.exports = AccelerateToObject;
|
27
v3/src/physics/arcade/utils/Closest.js
Normal file
27
v3/src/physics/arcade/utils/Closest.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
var DistanceBetween = require('../../../math/distance/DistanceBetween');
|
||||
|
||||
var Closest = function (source)
|
||||
{
|
||||
var bodies = this.tree.all();
|
||||
|
||||
var min = Number.MAX_SAFE_INTEGER;
|
||||
var closest = null;
|
||||
var x = source.x;
|
||||
var y = source.y;
|
||||
|
||||
for (var i = bodies.length - 1; i >= 0; i--)
|
||||
{
|
||||
var target = bodies[i];
|
||||
var distance = DistanceBetween(x, y, target.x, target.y);
|
||||
|
||||
if (distance < min)
|
||||
{
|
||||
closest = target;
|
||||
min = distance;
|
||||
}
|
||||
}
|
||||
|
||||
return closest;
|
||||
};
|
||||
|
||||
module.exports = Closest;
|
27
v3/src/physics/arcade/utils/Furthest.js
Normal file
27
v3/src/physics/arcade/utils/Furthest.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
var DistanceBetween = require('../../../math/distance/DistanceBetween');
|
||||
|
||||
var Furthest = function (source)
|
||||
{
|
||||
var bodies = this.tree.all();
|
||||
|
||||
var max = -1;
|
||||
var farthest = null;
|
||||
var x = source.x;
|
||||
var y = source.y;
|
||||
|
||||
for (var i = bodies.length - 1; i >= 0; i--)
|
||||
{
|
||||
var target = bodies[i];
|
||||
var distance = DistanceBetween(x, y, target.x, target.y);
|
||||
|
||||
if (distance > max)
|
||||
{
|
||||
farthest = target;
|
||||
max = distance;
|
||||
}
|
||||
}
|
||||
|
||||
return farthest;
|
||||
};
|
||||
|
||||
module.exports = Furthest;
|
36
v3/src/physics/arcade/utils/MoveTo.js
Normal file
36
v3/src/physics/arcade/utils/MoveTo.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
var DistanceBetween = require('../../../math/distance/DistanceBetween');
|
||||
|
||||
/**
|
||||
* Move the given display object towards the x/y coordinates at a steady velocity.
|
||||
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
||||
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
||||
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
||||
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
||||
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#moveTo
|
||||
* @param {any} displayObject - The display object to move.
|
||||
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
||||
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
||||
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
||||
*/
|
||||
var MoveTo = function (gameObject, x, y, speed, maxTime)
|
||||
{
|
||||
if (speed === undefined) { speed = 60; }
|
||||
if (maxTime === undefined) { maxTime = 0; }
|
||||
|
||||
var angle = Math.atan2(y - gameObject.y, x - gameObject.x);
|
||||
|
||||
if (maxTime > 0)
|
||||
{
|
||||
// We know how many pixels we need to move, but how fast?
|
||||
speed = DistanceBetween(gameObject.x, gameObject.y, x, y) / (maxTime / 1000);
|
||||
}
|
||||
|
||||
gameObject.body.velocity.setToPolar(angle, speed);
|
||||
|
||||
return angle;
|
||||
};
|
||||
|
||||
module.exports = MoveTo;
|
23
v3/src/physics/arcade/utils/MoveToObject.js
Normal file
23
v3/src/physics/arcade/utils/MoveToObject.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
var MoveTo = require('./MoveTo');
|
||||
|
||||
/**
|
||||
* Move the given display object towards the destination object at a steady velocity.
|
||||
* If you specify a maxTime then it will adjust the speed (overwriting what you set) so it arrives at the destination in that number of seconds.
|
||||
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
||||
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
||||
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
||||
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#moveToObject
|
||||
* @param {any} displayObject - The display object to move.
|
||||
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
||||
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
||||
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
||||
*/
|
||||
var MoveToObject = function (gameObject, destination, speed, maxTime)
|
||||
{
|
||||
return MoveTo(gameObject, destination.x, destination.y, speed, maxTime);
|
||||
};
|
||||
|
||||
module.exports = MoveToObject;
|
20
v3/src/physics/arcade/utils/VelocityFromAngle.js
Normal file
20
v3/src/physics/arcade/utils/VelocityFromAngle.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
var DegToRad = require('../../../math/DegToRad');
|
||||
|
||||
/**
|
||||
* Given the angle (in degrees) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
||||
* One way to use this is: velocityFromAngle(angle, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#velocityFromAngle
|
||||
* @param {number} angle - The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
||||
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
||||
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
||||
*/
|
||||
var VelocityFromAngle = function (angle, speed, vec2)
|
||||
{
|
||||
if (speed === undefined) { speed = 60; }
|
||||
|
||||
return vec2.setToPolar(DegToRad(angle), speed);
|
||||
};
|
||||
|
||||
module.exports = VelocityFromAngle;
|
19
v3/src/physics/arcade/utils/VelocityFromRotation.js
Normal file
19
v3/src/physics/arcade/utils/VelocityFromRotation.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Given the rotation (in radians) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
||||
* One way to use this is: velocityFromRotation(rotation, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#velocityFromRotation
|
||||
* @param {number} rotation - The angle in radians.
|
||||
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
||||
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
||||
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
||||
*/
|
||||
|
||||
var VelocityFromRotation = function (rotation, speed, vec2)
|
||||
{
|
||||
if (speed === undefined) { speed = 60; }
|
||||
|
||||
return vec2.setToPolar(rotation, speed);
|
||||
};
|
||||
|
||||
module.exports = VelocityFromRotation;
|
Loading…
Reference in a new issue