Coordinate Helpers - coldrockgames/gml-raptor GitHub Wiki
raptoroffers a group of small classes that help with coordinates in the room. In C# (or other modern languages) terminology, those might be seen as a kind of Tuples or Triples. However, GML has no design for Tuples and does not allow a function to return more than one value.
That's why the Coord classes have been written for raptor.
There are 3 Coord helper classes available:
Coord2for 2D-Coordinates (holding anxandymember)Coord3for 3D-Coordinates (derived fromCoord2and adding azmember to the mix)Coord4for 4D-Coordinates (derived fromCoord3and adding awmember to the mix)
The usage of x,y,z,w is quite common for coordinate members and you will find a naming like this in many systems and languages, even in shader programming, where the equivalents to my Coord classes are named Vec (Vectors - there's a Vec2, Vec3 and Vec4 class when you do shaders).
[!TIP] While on a first glance, a
Coord4doesn't make much sense, I mean... how often do you deal with 4-dimensional space, right? ... Keep in mind, that nobody forces you, to put only geometric coordinates in there. You could as well use aCoord4to store theRGBAvalues of any color! In fact, in shader programming, theVec4is normally used for exactly this purpose: Store a color. An as it is a simple struct class, it can easily be stored in your Savegame!
But these classes do not only hold members to store coordinates, they also let you perform some calculations on them, as you would expect from any vector object.
Coord functions
clone2 (clone3, clone4)
Each inheritance level of a Coord adds a cloneX function, which not only lets you create a cloned copy of the current instance, you can also use it to do a kind of typecasting. As all Coord classes are derived in a hierarchy (Coord2 -> Coord3 -> Coord4), every child class also owns the clone of its parent. So you can create a 2D instance from any 3D instance by invoking clone2 on a Coord3 instance. You will receive a true Coord2 as result.
/// @function clone2()
/// @description Clones this as Coord2
static clone2 = function() {
return new Coord2(x, y);
}
set
Depending on the inheritance level, the set method lets you apply 2, 3 or 4 values on the instance
/// @function set(xp, yp)
/// @description set all values in one step
/// @param {real} xp the new value for x
/// @param {real} yp the new value for y
/// @returns {Coord2} self for command chaining (fluent syntax)
static set = function(xp, yp) {
mul
Multiply all members with a supplied factor for each dimension
/// @function mul(factor_x, factor_y)
/// @description multiply both values in one step
/// @param {real} factor_x factor to multiply x with
/// @param {real} factor_y factor to multiply y with
/// @returns {Coord2} self for command chaining (fluent syntax)
static mul = function(factor_x, factor_y) {
mul_xy
Multiply all members with the same factor (scaling)
/// @function mulxy(factor)
/// @description multiply both values in one step
/// @param {real} factor the factor to multiply x and y with
/// @returns {Coord2} self for command chaining (fluent syntax)
static mul_xy = function(factor) {
plus
Add the values of another Coord to this one
/// @function plus(other_coord2)
/// @description Add the values of other_coord2 to this one
/// @param {Coord2} other_coord2 The Coord2 to add to this one
/// @returns {Coord2} self for command chaining
static plus = function(other_coord2) {
minus
Subtract the values of another Coord from this one
/// @function minus(other_coord2)
/// @description Subtract the values in other_coord2 from this one
/// @param {Coord2} other_coord2 The Coord2 to subtract from this one
/// @returns {Coord2} self for command chaining
static minus = function(other_coord2) {
add
Adds specified values to each dimension
/// @function add(add_x, add_y)
/// @description add a value to the current values
/// @param {real} add_x value to add to x
/// @param {real} add_y value to add to y
/// @returns {Coord2} self for command chaining (fluent syntax)
static add = function(add_x, add_y) {
add_xy
/// @function addxy(value)
/// @description add the same value to the current values
/// @param {real} value value to add to all dimensions
/// @returns {Coord2} self for command chaining (fluent syntax)
static add_xy = function(value) {
Calculation methods
The Coord classes also offer some methods to do calculations, like the hyptenuse (the length of the vector, Pythagoras), angles and distances.
length
/// @function length_xy()
/// @description 2D-hypotenuse
/// @returns {real} length
static length_xy = function() {
angle
/// @function static angle_xy()
/// @description gets alpha (angle from horizontal to hypo). 0 degrees is right ccw
static angle_xy = function() {
distance_to_coord
/// @function distance_to_coord2(other_Coord2)
/// @description distance between two vectors
/// @param {Coord2} other_Coord2 the coord2 to find the distance to
/// @returns {Coord2} new Coord2
static distance_to_coord2 = function(other_coord2) {
distance_to_xy
/// @function distance_to_xy(xp, yp)
/// @description distance between vector and point
/// @param {real} xp the x to find the distance to
/// @param {real} yp the y to find the distance to
/// @returns {Coord2} new Coord2
static distance_to_xy = function(xp, yp) {
equals
/// @function equals_xy(other_coord2)
/// @description true, if both, x and y match
/// @returns {bool}
static equals_xy = function(other_coord2) {