Reputation: 300
I've seen vectors quite a bit in my time learning LibGDX and coding in general. But I've never been able to grasp what they are.
I know:
In programming, do they also represent magnitude and direction? Is the array aspect to say that they represent all points between two x and y coordinates?
Or else do they represent a single point that has a direction, ie when that point moves it will go in that direction? Does it signify movement?
Example:
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
What is happening here? Why would we want to set the ground to a vector?
Upvotes: 1
Views: 1726
Reputation: 20140
std::vector
,Vector2
andVector3
andvector math
std::vector, is a sequence container in c++ that encapsulates dynamic size arrays.
Vector2 and Vector3 are classes in Libgdx API that contains 2 and 3 float value respectively also having number of method that help in practical implementation of vector math.
vector math is an amazing tool that makes programming of complex behaviors much simpler. vector math is very helpful in 2D/3D game development.
vector as magnitude and direction
Typically, we define coordinates as an (x,y) pair, x representing the horizontal offset and y the vertical one. This makes sense given the screen is just a rectangle in two dimensions. As an example, here is a position in 2D space:
A position can be anywhere in space. The position (0,0) has a name, it’s called the origin. Remember this term well because it has more implicit uses later. The (0,0) of a n-dimensions coordinate system is the origin.
In vector math, coordinates have two different uses, both equally important. They are used to represent a position but also a vector. The same position as before, when imagined as a vector, has a different meaning.
When imagined as a vector, two properties can be inferred, the direction and the magnitude. Every position in space can be a vector, with the exception of the origin. This is because coordinates (0,0) can’t represent direction (magnitude 0).
Upvotes: 3