Finding the distance between two points in Phaser
In Phaser there is a Math object like that of the Math object in core javaScript. There are a few methods there that come in handy, one of which is a usual suspects in most projects that is used to find the distance between two points. That is the 2d distance formula that can be found at Phaser.Math.distance.
If new to Phaser
If you are new to phaser you might want to check out my post on getting started with phaser, I also assume that you have at least some knowlage of html, css, and javaScript as I do not have any posts on how to get up to speed with that at this time. This is my standard hexo tag for all phaser posts where I want to inform readers that this is not a post for beginers.
The basic example
If I am using phaser as a dependency there is no need for a quick refresher for how to write the 2d distance formula, it’s right there in Phasers Math Object. To use it I gave it four arguments that are the x, and y positions of both points concerned.
So anywhere in my project I can find distance like this.
|
|
A more advanced Phaser.Math.distance example using some graphics
So I put togather a quick demo for this one method involving two Graphics display objects.
|
|
Conclusion
The distance between two points comes up all the time in my projects. As I learn more about phaser I am happy to know that this is a great project to know a thing or two about. When it comes to making projects vanilla js style I can not tell you how many times I end up fishing for the distance formula, trying to remember how to write it yet again, but now it is always there for me within phaser, along with almost everything else I could want.