Game Development Reference
In-Depth Information
public Vector2( float x, float y) {
this .x = x;
this .y = y;
}
public Vector2(Vector2 other) {
this .x = other.x;
this .y = other.y;
}
Put that class in the package com.badlogic.androidgames.framework.math , where we'll also
house any other math-related classes.
We start off by defining two static constants, TO_RADIANS and TO_DEGREES . To convert an angle
given in radians, we simply multiply it by TO_DEGREES ; to convert an angle given in degrees to
radians, we multiply it by TO_RADIANS . We can double-check this by looking at the two previously
defined equations that govern degree-to-radian conversion. With this little trick, we can shave off
some division and speed things up.
Next, we define the members x and y , which store the components of the vector, and a couple
of constructors—nothing too complex:
public Vector2 cpy() {
return new Vector2(x, y);
}
The cpy() method will create a duplicate instance of the current vector and return it. This might
come in handy if we want to manipulate a copy of a vector, preserving the value of the original
vector.
public Vector2 set( float x, float y) {
this .x = x;
this .y = y;
return this ;
}
public Vector2 set(Vector2 other) {
this .x = other.x;
this .y = other.y;
return this ;
}
The set() methods allow us to set the x and y components of a vector, based on either two float
arguments or another vector. The methods return a reference to this vector, so we can chain
operations, as discussed previously.
public Vector2 add( float x, float y) {
this .x += x;
this .y += y;
return this ;
}
 
Search WWH ::




Custom Search