Normalizing Points in Phaser
In this post on Phaser ce the html5 powered game framework, I will be writing about the Point.normalize method that can be used to normalize 2d points. Normalizing points is a way to bring one or more Points into a common scale, once that is the case the process of scaling it back up, or down, as well a translating it is much easier.
1 - What to know before continuing
2 - A very Basic example of Phaser.Point.normalize
So for a very basic example of Point.normalize I will be using the Point.normalize prototype method. There is also a static form of the method as well, but that expects only an instance of Point as the first argument, so you might as well just use the prototype method in most cases.
3 - A Point of Phaser.Point.normalize, easy scaling, and translating.
So the nice thing about normalizing is that it can bring an array of points into a standard once unit long size that can then be easily positioned and scaled. Say you have some kind of constructor that gives you an array of Points, and you want to set them to a certain scale, and translate them somewhere.
To make the process of doing this easy it would be nice to have all the points positioned relative to 0,0, and set to a certain polar distance from 0,0 that is between 0, and 1. Then the process of scaling is just a matter or running through each point and changing the distance from 0,0 to the desired unit length, and then translating to where I want them to be. This is where normalization comes into play.
4 - Conclusion
So Point.normalize can be used to set the unit length of one or more points to 1, which can be thought of as a kind of standard unit length of the point. From there it can be scaled up to a higher distance, assuming that the angle, or direction of the point remains the same. For more on this you might want to check out my post on Point.setMagnitude as well.