Skip to content

Instantly share code, notes, and snippets.

@wassname
Created November 13, 2015 05:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wassname/38cb3a69507a3af66900 to your computer and use it in GitHub Desktop.
moveToXY for the Phaser Isometric Plugin
/**
* 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.IsoArcade#moveToXY
* @param {any} displayObject - The display object to move.
* @param {number} x - The x coordinate to move towards.
* @param {number} y - The y coordinate to move towards.
* @param {number} z - The z coordinate to move towards.
* @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.
*/
function moveToXYZ(displayObject, x, y, z, speed, maxTime, drag) {
if (typeof drag === 'undefined') {
drag = {x:0,y:0,z:0};
}
if (typeof speed === 'undefined') {
speed = 60;
}
if (typeof maxTime === 'undefined') {
maxTime = 0;
}
if (maxTime > 0) {
// We know how many pixels we need to move, but how fast?
var speed = distanceToXYZ(displayObject, x, y ,z) / (maxTime / 1000);
}
var phi = Math.atan2(y - displayObject.isoPosition.y, x - displayObject.isoPosition.x);
var theta = Math.atan2(z - displayObject.isoPosition.z, x - displayObject.isoPosition.x);
var v={
x:Math.cos(theta) * Math.cos(phi) * speed + drag.x*(maxTime / 1000),
y:Math.cos(theta) * Math.sin(phi) * speed + drag.y*(maxTime / 1000),
z:Math.sin(theta) * Math.sin(phi) * speed + drag.z*(maxTime / 1000)
}
displayObject.body.velocity.set(v.x,v.y,v.z)
return phi;
}
@deividkamui
Copy link

I'm going to try to implement this, as soon as I improve the performance on the rendering cause my game runs on 10 fps, don't know why :(

@wassname
Copy link
Author

FYI This snippet has been added to PhaserIsometric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment