2013-08-02 11:38:56 +00:00
|
|
|
/// <reference path="../../Game.ts" />
|
|
|
|
/// <reference path="../../gameobjects/Sprite.ts" />
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Motion
|
|
|
|
*
|
|
|
|
* The Motion class contains lots of useful functions for moving game objects around in world space.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class Motion {
|
|
|
|
|
|
|
|
constructor(game: Game) {
|
2013-05-31 17:21:32 +00:00
|
|
|
this.game = game;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
public game: Game;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the angle and speed calculate the velocity and return it as a Point
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
|
|
|
* @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 The speed it will move, in pixels per second sq
|
|
|
|
*
|
|
|
|
* @return {Point} A Point where Point.x contains the velocity x value and Point.y contains the velocity y value
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
public velocityFromAngle(angle: number, speed: number): Point {
|
|
|
|
|
2013-04-23 20:27:45 +00:00
|
|
|
if (isNaN(speed))
|
|
|
|
{
|
|
|
|
speed = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.game.math.degreesToRadians(angle);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
return new Point((Math.cos(a) * speed), (Math.sin(a) * speed));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Sets the source Sprite x/y velocity so it will move directly towards the destination Sprite at the speed given (in pixels per second)<br>
|
|
|
|
* 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.<br>
|
|
|
|
* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
|
|
|
|
* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
|
|
|
|
* If you need the object to accelerate, see accelerateTowardsObject() instead
|
|
|
|
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you set drag or acceleration too high this object may not move at all)
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite on which the velocity will be set
|
|
|
|
* @param {Sprite} dest The Sprite where the source object will move to
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {number} speed The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {number} maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public moveTowardsObject(source: Sprite, dest: Sprite, speed: number = 60, maxTime: number = 0) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetween(source, dest);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
var d: number = this.distanceBetween(source, dest);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
// We know how many pixels we need to move, but how fast?
|
|
|
|
speed = d / (maxTime / 1000);
|
|
|
|
}
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = Math.cos(a) * speed;
|
|
|
|
source.body.velocity.y = Math.sin(a) * speed;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Sets the x/y acceleration on the source Sprite so it will move towards the destination Sprite at the speed given (in pixels per second)<br>
|
|
|
|
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
|
|
|
|
* If you don't need acceleration look at moveTowardsObject() instead.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite on which the acceleration will be set
|
|
|
|
* @param {Sprite} dest The Sprite where the source object will move towards
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {number} speed The speed it will accelerate in pixels per second
|
|
|
|
* @param {number} xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
|
|
|
|
* @param {number} ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public accelerateTowardsObject(source: Sprite, dest: Sprite, speed: number, xSpeedMax: number, ySpeedMax: number) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
|
|
|
/*
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetween(source, dest);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = 0;
|
|
|
|
source.body.velocity.y = 0;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.acceleration.x = Math.cos(a) * speed;
|
|
|
|
source.body.acceleration.y = Math.sin(a) * speed;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.maxVelocity.x = xSpeedMax;
|
|
|
|
source.body.maxVelocity.y = ySpeedMax;
|
2013-06-26 04:44:56 +00:00
|
|
|
*/
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Move the given Sprite towards the mouse pointer 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.<br>
|
|
|
|
* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
|
|
|
|
* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite to move
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {number} speed The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {number} maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public moveTowardsMouse(source: Sprite, speed: number = 60, maxTime: number = 0) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetweenMouse(source);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
var d: number = this.distanceToMouse(source);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
// We know how many pixels we need to move, but how fast?
|
|
|
|
speed = d / (maxTime / 1000);
|
|
|
|
}
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = Math.cos(a) * speed;
|
|
|
|
source.body.velocity.y = Math.sin(a) * speed;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Sets the x/y acceleration on the source Sprite so it will move towards the mouse coordinates at the speed given (in pixels per second)<br>
|
|
|
|
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
|
|
|
|
* If you don't need acceleration look at moveTowardsMouse() instead.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite on which the acceleration will be set
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {number} speed The speed it will accelerate in pixels per second
|
|
|
|
* @param {number} xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
|
|
|
|
* @param {number} ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public accelerateTowardsMouse(source: Sprite, speed: number, xSpeedMax: number, ySpeedMax: number) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
|
|
|
/*
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetweenMouse(source);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = 0;
|
|
|
|
source.body.velocity.y = 0;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.acceleration.x = Math.cos(a) * speed;
|
|
|
|
source.body.acceleration.y = Math.sin(a) * speed;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.maxVelocity.x = xSpeedMax;
|
|
|
|
source.body.maxVelocity.y = ySpeedMax;
|
2013-06-26 04:44:56 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Sets the x/y velocity on the source Sprite so it will move towards the target coordinates at the speed given (in pixels per second)<br>
|
|
|
|
* 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.<br>
|
|
|
|
* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
|
|
|
|
* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite to move
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {Point} target The Point coordinates to move the source Sprite towards
|
|
|
|
* @param {number} speed The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
|
|
* @param {number} maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public moveTowardsPoint(source: Sprite, target: Point, speed: number = 60, maxTime: number = 0) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetweenPoint(source, target);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
if (maxTime > 0)
|
|
|
|
{
|
|
|
|
var d: number = this.distanceToPoint(source, target);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
// We know how many pixels we need to move, but how fast?
|
|
|
|
speed = d / (maxTime / 1000);
|
|
|
|
}
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = Math.cos(a) * speed;
|
|
|
|
source.body.velocity.y = Math.sin(a) * speed;
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Sets the x/y acceleration on the source Sprite so it will move towards the target coordinates at the speed given (in pixels per second)<br>
|
|
|
|
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
|
|
|
|
* If you don't need acceleration look at moveTowardsPoint() instead.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} source The Sprite on which the acceleration will be set
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {Point} target The Point coordinates to move the source Sprite towards
|
|
|
|
* @param {number} speed The speed it will accelerate in pixels per second
|
|
|
|
* @param {number} xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
|
|
|
|
* @param {number} ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public accelerateTowardsPoint(source: Sprite, target: Point, speed: number, xSpeedMax: number, ySpeedMax: number) {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
|
|
|
/*
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number = this.angleBetweenPoint(source, target);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.velocity.x = 0;
|
|
|
|
source.body.velocity.y = 0;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.acceleration.x = Math.cos(a) * speed;
|
|
|
|
source.body.acceleration.y = Math.sin(a) * speed;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
source.body.maxVelocity.x = xSpeedMax;
|
|
|
|
source.body.maxVelocity.y = ySpeedMax;
|
2013-06-26 04:44:56 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Find the distance between two Sprites, taking their origin into account
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a The first Sprite
|
|
|
|
* @param {Sprite} b The second Sprite
|
2013-05-11 16:26:30 +00:00
|
|
|
* @return {number} int Distance (in pixels)
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public distanceBetween(a: Sprite, b: Sprite): number {
|
|
|
|
return Vec2Utils.distance(a.body.position, b.body.position);
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Find the distance from an Sprite to the given Point, taking the source origin into account
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a The Sprite
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {Point} target The Point
|
|
|
|
* @return {number} Distance (in pixels)
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public distanceToPoint(a: Sprite, target: Point): number {
|
2013-06-06 01:47:08 +00:00
|
|
|
var dx: number = (a.x + a.transform.origin.x) - (target.x);
|
|
|
|
var dy: number = (a.y + a.transform.origin.y) - (target.y);
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
return this.game.math.vectorLength(dx, dy);
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Find the distance (in pixels, rounded) from the object x/y and the mouse x/y
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a Sprite to test against
|
2013-05-11 16:26:30 +00:00
|
|
|
* @return {number} The distance between the given sprite and the mouse coordinates
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public distanceToMouse(a: Sprite): number {
|
2013-06-06 01:47:08 +00:00
|
|
|
var dx: number = (a.x + a.transform.origin.x) - this.game.input.x;
|
|
|
|
var dy: number = (a.y + a.transform.origin.y) - this.game.input.y;
|
2013-05-11 16:26:30 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
return this.game.math.vectorLength(dx, dy);
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Find the angle (in radians) between an Sprite and an Point. The source sprite takes its x/y and origin into account.
|
|
|
|
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a The Sprite to test from
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {Point} target The Point to angle the Sprite towards
|
|
|
|
* @param {boolean} asDegrees If you need the value in degrees instead of radians, set to true
|
|
|
|
*
|
|
|
|
* @return {number} The angle (in radians unless asDegrees is true)
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public angleBetweenPoint(a: Sprite, target: Point, asDegrees: bool = false): number {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
var dx: number = (target.x) - (a.x + a.transform.origin.x);
|
|
|
|
var dy: number = (target.y) - (a.y + a.transform.origin.y);
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
if (asDegrees)
|
|
|
|
{
|
|
|
|
return this.game.math.radiansToDegrees(Math.atan2(dy, dx));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Math.atan2(dy, dx);
|
|
|
|
}
|
2013-06-26 04:44:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Find the angle (in radians) between the two Sprite, taking their x/y and origin into account.
|
|
|
|
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a The Sprite to test from
|
|
|
|
* @param {Sprite} b The Sprite to test to
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {boolean} asDegrees If you need the value in degrees instead of radians, set to true
|
|
|
|
*
|
|
|
|
* @return {number} The angle (in radians unless asDegrees is true)
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public angleBetween(a: Sprite, b: Sprite, asDegrees: bool = false): number {
|
|
|
|
|
2013-06-06 01:47:08 +00:00
|
|
|
var dx: number = (b.x + b.transform.origin.x) - (a.x + a.transform.origin.x);
|
|
|
|
var dy: number = (b.y + b.transform.origin.y) - (a.y + a.transform.origin.y);
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
if (asDegrees)
|
|
|
|
{
|
|
|
|
return this.game.math.radiansToDegrees(Math.atan2(dy, dx));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Math.atan2(dy, dx);
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Given the Sprite and speed calculate the velocity and return it as an Point based on the direction the sprite is facing
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} parent The Sprite to get the facing value from
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {number} speed The speed it will move, in pixels per second sq
|
|
|
|
*
|
|
|
|
* @return {Point} An Point where Point.x contains the velocity x value and Point.y contains the velocity y value
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public velocityFromFacing(parent: Sprite, speed: number): Point {
|
2013-06-26 04:44:56 +00:00
|
|
|
|
|
|
|
/*
|
2013-05-31 17:21:32 +00:00
|
|
|
var a: number;
|
|
|
|
|
|
|
|
if (parent.body.facing == Types.LEFT)
|
|
|
|
{
|
|
|
|
a = this.game.math.degreesToRadians(180);
|
|
|
|
}
|
|
|
|
else if (parent.body.facing == Types.RIGHT)
|
|
|
|
{
|
|
|
|
a = this.game.math.degreesToRadians(0);
|
|
|
|
}
|
|
|
|
else if (parent.body.facing == Types.UP)
|
|
|
|
{
|
|
|
|
a = this.game.math.degreesToRadians(-90);
|
|
|
|
}
|
|
|
|
else if (parent.body.facing == Types.DOWN)
|
|
|
|
{
|
|
|
|
a = this.game.math.degreesToRadians(90);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Point(Math.cos(a) * speed, Math.sin(a) * speed);
|
2013-06-26 04:44:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
return new Point;
|
2013-05-31 17:21:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-18 13:16:18 +00:00
|
|
|
* Find the angle (in radians) between an Sprite and the mouse, taking their x/y and origin into account.
|
|
|
|
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-31 17:21:32 +00:00
|
|
|
* @param {Sprite} a The Object to test from
|
2013-05-11 16:26:30 +00:00
|
|
|
* @param {boolean} asDegrees If you need the value in degrees instead of radians, set to true
|
|
|
|
*
|
|
|
|
* @return {number} The angle (in radians unless asDegrees is true)
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-31 17:21:32 +00:00
|
|
|
public angleBetweenMouse(a: Sprite, asDegrees: bool = false): number {
|
|
|
|
|
|
|
|
// In order to get the angle between the object and mouse, we need the objects screen coordinates (rather than world coordinates)
|
|
|
|
var p: Point = SpriteUtils.getScreenXY(a);
|
|
|
|
|
|
|
|
var dx: number = a.game.input.x - p.x;
|
|
|
|
var dy: number = a.game.input.y - p.y;
|
|
|
|
|
|
|
|
if (asDegrees)
|
|
|
|
{
|
|
|
|
return this.game.math.radiansToDegrees(Math.atan2(dy, dx));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Math.atan2(dy, dx);
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|