libgdx API

com.badlogic.gdx.math
Class Vector2

java.lang.Object
  extended by com.badlogic.gdx.math.Vector2
All Implemented Interfaces:
java.io.Serializable

public class Vector2
extends java.lang.Object
implements java.io.Serializable

Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself

Author:
badlogicgames@gmail.com
See Also:
Serialized Form

Field Summary
static Vector2 tmp
          Static temporary vector.
static Vector2 tmp2
          Static temporary vector.
static Vector2 tmp3
          Static temporary vector.
 float x
          the x-component of this vector
static Vector2 X
           
 float y
          the y-component of this vector
static Vector2 Y
           
static Vector2 Zero
           
 
Constructor Summary
Vector2()
          Constructs a new vector at (0,0)
Vector2(float x, float y)
          Constructs a vector with the given components
Vector2(Vector2 v)
          Constructs a vector from the given vector
 
Method Summary
 Vector2 add(float x, float y)
          Adds the given components to this vector
 Vector2 add(Vector2 v)
          Adds the given vector to this vector
 float angle()
           
 Vector2 cpy()
           
 float crs(float x, float y)
          Calculates the 2D cross product between this and the given vector.
 float crs(Vector2 v)
          Calculates the 2D cross product between this and the given vector.
 Vector2 div(float value)
           
 Vector2 div(float vx, float vy)
           
 Vector2 div(Vector2 other)
           
 float dot(Vector2 v)
           
 float dst(float x, float y)
           
 float dst(Vector2 v)
           
 float dst2(float x, float y)
           
 float dst2(Vector2 v)
           
 boolean epsilonEquals(Vector2 obj, float epsilon)
          Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
 boolean equals(java.lang.Object obj)
           
 int hashCode()
           
 float len()
           
 float len2()
           
 Vector2 lerp(Vector2 target, float alpha)
          Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
 Vector2 mul(float scalar)
          Multiplies this vector by a scalar
 Vector2 mul(float x, float y)
          Multiplies this vector by a scalar
 Vector2 mul(Matrix3 mat)
          Multiplies this vector by the given matrix
 Vector2 nor()
          Normalizes this vector
 Vector2 rotate(float degrees)
          Rotates the Vector2 by the given angle, counter-clockwise.
 Vector2 set(float x, float y)
          Sets the components of this vector
 Vector2 set(Vector2 v)
          Sets this vector from the given vector
 void setAngle(float angle)
          Sets the angle of the vector.
 Vector2 sub(float x, float y)
          Substracts the other vector from this vector.
 Vector2 sub(Vector2 v)
          Substracts the given vector from this vector.
 Vector2 tmp()
          NEVER EVER SAVE THIS REFERENCE! Do not use this unless you are aware of the side-effects, e.g.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

tmp

public static final Vector2 tmp
Static temporary vector. Use with care! Use only when sure other code will not also use this.

See Also:
tmp()

tmp2

public static final Vector2 tmp2
Static temporary vector. Use with care! Use only when sure other code will not also use this.

See Also:
tmp()

tmp3

public static final Vector2 tmp3
Static temporary vector. Use with care! Use only when sure other code will not also use this.

See Also:
tmp()

X

public static final Vector2 X

Y

public static final Vector2 Y

Zero

public static final Vector2 Zero

x

public float x
the x-component of this vector


y

public float y
the y-component of this vector

Constructor Detail

Vector2

public Vector2()
Constructs a new vector at (0,0)


Vector2

public Vector2(float x,
               float y)
Constructs a vector with the given components

Parameters:
x - The x-component
y - The y-component

Vector2

public Vector2(Vector2 v)
Constructs a vector from the given vector

Parameters:
v - The vector
Method Detail

cpy

public Vector2 cpy()
Returns:
a copy of this vector

len

public float len()
Returns:
The euclidian length

len2

public float len2()
Returns:
The squared euclidian length

set

public Vector2 set(Vector2 v)
Sets this vector from the given vector

Parameters:
v - The vector
Returns:
This vector for chaining

set

public Vector2 set(float x,
                   float y)
Sets the components of this vector

Parameters:
x - The x-component
y - The y-component
Returns:
This vector for chaining

sub

public Vector2 sub(Vector2 v)
Substracts the given vector from this vector.

Parameters:
v - The vector
Returns:
This vector for chaining

nor

public Vector2 nor()
Normalizes this vector

Returns:
This vector for chaining

add

public Vector2 add(Vector2 v)
Adds the given vector to this vector

Parameters:
v - The vector
Returns:
This vector for chaining

add

public Vector2 add(float x,
                   float y)
Adds the given components to this vector

Parameters:
x - The x-component
y - The y-component
Returns:
This vector for chaining

dot

public float dot(Vector2 v)
Parameters:
v - The other vector
Returns:
The dot product between this and the other vector

mul

public Vector2 mul(float scalar)
Multiplies this vector by a scalar

Parameters:
scalar - The scalar
Returns:
This vector for chaining

mul

public Vector2 mul(float x,
                   float y)
Multiplies this vector by a scalar

Returns:
This vector for chaining

div

public Vector2 div(float value)

div

public Vector2 div(float vx,
                   float vy)

div

public Vector2 div(Vector2 other)

dst

public float dst(Vector2 v)
Parameters:
v - The other vector
Returns:
the distance between this and the other vector

dst

public float dst(float x,
                 float y)
Parameters:
x - The x-component of the other vector
y - The y-component of the other vector
Returns:
the distance between this and the other vector

dst2

public float dst2(Vector2 v)
Parameters:
v - The other vector
Returns:
the squared distance between this and the other vector

dst2

public float dst2(float x,
                  float y)
Parameters:
x - The x-component of the other vector
y - The y-component of the other vector
Returns:
the squared distance between this and the other vector

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

sub

public Vector2 sub(float x,
                   float y)
Substracts the other vector from this vector.

Parameters:
x - The x-component of the other vector
y - The y-component of the other vector
Returns:
This vector for chaining

tmp

public Vector2 tmp()
NEVER EVER SAVE THIS REFERENCE! Do not use this unless you are aware of the side-effects, e.g. other methods might call this as well.

Returns:
a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add( v2.tmp() ) will not work!

mul

public Vector2 mul(Matrix3 mat)
Multiplies this vector by the given matrix

Parameters:
mat - the matrix
Returns:
this vector

crs

public float crs(Vector2 v)
Calculates the 2D cross product between this and the given vector.

Parameters:
v - the other vector
Returns:
the cross product

crs

public float crs(float x,
                 float y)
Calculates the 2D cross product between this and the given vector.

Parameters:
x - the x-coordinate of the other vector
y - the y-coordinate of the other vector
Returns:
the cross product

angle

public float angle()
Returns:
the angle in degrees of this vector (point) relative to the x-axis. Angles are counter-clockwise and between 0 and 360.

setAngle

public void setAngle(float angle)
Sets the angle of the vector.

Parameters:
angle - The angle to set.

rotate

public Vector2 rotate(float degrees)
Rotates the Vector2 by the given angle, counter-clockwise.

Parameters:
degrees - the angle in degrees

lerp

public Vector2 lerp(Vector2 target,
                    float alpha)
Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.

Parameters:
target - The target vector
alpha - The interpolation coefficient
Returns:
This vector for chaining.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

epsilonEquals

public boolean epsilonEquals(Vector2 obj,
                             float epsilon)
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.

Parameters:
obj -
epsilon -
Returns:
whether the vectors are the same.

libgdx API

Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)