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
 float x
          the x-component of this vector
 float y
          the y-component of this vector
 
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.
 float dot(Vector2 v)
           
 float dst(float x, float y)
           
 float dst(Vector2 v)
           
 float dst2(Vector2 v)
           
 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(Matrix3 mat)
          Multiplies this vector by the given matrix
 Vector2 nor()
          Normalizes this vector
 Vector2 rotate(float angle)
          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
 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()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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

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

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()
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.

rotate

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

Parameters:
angle - the angle in degrees
Returns:
the

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.

libgdx API

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