2002-08-16 23:52:26 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002 Light Weight Java Game Library Project
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
|
* met:
|
2002-08-09 12:56:30 +02:00
|
|
|
*
|
2002-08-16 23:52:26 +02:00
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
|
|
|
|
package org.lwjgl.vector;
|
|
|
|
|
|
2002-08-24 23:12:31 +02:00
|
|
|
import org.lwjgl.Math;
|
|
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
/**
|
2002-08-16 23:52:26 +02:00
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Holds a 2-tuple vector.
|
2002-08-09 12:56:30 +02:00
|
|
|
*
|
2002-08-16 23:52:26 +02:00
|
|
|
* @author cix_foo <cix_foo@users.sourceforge.net>
|
|
|
|
|
* @version $Revision$
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
2002-08-16 23:52:26 +02:00
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
public class Vector2f {
|
|
|
|
|
|
|
|
|
|
public float x, y;
|
|
|
|
|
|
|
|
|
|
/**
|
2002-08-24 23:12:31 +02:00
|
|
|
* Constructor for Vector3f.
|
2002-08-09 12:56:30 +02:00
|
|
|
*/
|
|
|
|
|
public Vector2f() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
2002-08-24 23:12:31 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f(Vector2f src) {
|
|
|
|
|
set(src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f(float x, float y) {
|
|
|
|
|
set(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set values
|
|
|
|
|
* @return this
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f set(float x, float y) {
|
|
|
|
|
this.x = x;
|
|
|
|
|
this.y = y;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2002-08-09 12:56:30 +02:00
|
|
|
|
2002-08-24 23:12:31 +02:00
|
|
|
/**
|
|
|
|
|
* Load from another Vector2f
|
|
|
|
|
* @param src The source vector
|
|
|
|
|
* @return this
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f set(Vector2f src) {
|
|
|
|
|
x = src.x;
|
|
|
|
|
y = src.y;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return the length of the vector
|
|
|
|
|
*/
|
|
|
|
|
public float length() {
|
|
|
|
|
return Math.sqrt(lengthSquared());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return the length squared of the vector
|
|
|
|
|
*/
|
|
|
|
|
public float lengthSquared() {
|
|
|
|
|
return x * x + y * y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Translate a vector
|
|
|
|
|
* @param x The translation in x
|
|
|
|
|
* @param y the translation in y
|
|
|
|
|
* @return this
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f translate(float x, float y) {
|
|
|
|
|
this.x += x;
|
|
|
|
|
this.y += y;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Negate a vector
|
|
|
|
|
* @return this
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f negate() {
|
|
|
|
|
x = -x;
|
|
|
|
|
y = -y;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalise this vector
|
|
|
|
|
* @return this
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f normalise() {
|
|
|
|
|
float l = 1.0f / length();
|
|
|
|
|
x *= l;
|
|
|
|
|
y *= l;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalise this vector and place the result in another vector.
|
|
|
|
|
* @param dest The destination vector, or null if a new vector is to be created
|
|
|
|
|
* @return the normalised vector
|
|
|
|
|
*/
|
|
|
|
|
public Vector2f normalise(Vector2f dest) {
|
|
|
|
|
float l = length();
|
|
|
|
|
|
|
|
|
|
if (dest == null)
|
|
|
|
|
dest = new Vector2f(x / l, y / l);
|
|
|
|
|
else
|
|
|
|
|
dest.set(x / l, y / l);
|
|
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The dot product of two vectors is calculated as
|
|
|
|
|
* v1.x * v2.x + v1.y * v2.y
|
|
|
|
|
* @param left The LHS vector
|
|
|
|
|
* @param right The RHS vector
|
|
|
|
|
* @return left dot right
|
|
|
|
|
*/
|
|
|
|
|
public static float dot(Vector2f left, Vector2f right) {
|
|
|
|
|
return left.x * right.x + left.y * right.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the angle between two vectors, in degrees
|
|
|
|
|
* @param a A vector
|
|
|
|
|
* @param b The other vector
|
|
|
|
|
* @return the angle between the two vectors, in degrees
|
|
|
|
|
*/
|
|
|
|
|
public static float angle(Vector2f a, Vector2f b) {
|
|
|
|
|
float dls = dot(a, b) / (a.length() * b.length());
|
|
|
|
|
if (dls < -1f)
|
|
|
|
|
dls = -1f;
|
|
|
|
|
else if (dls > 1.0f)
|
|
|
|
|
dls = 1.0f;
|
|
|
|
|
return Math.acos(dls);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-09 12:56:30 +02:00
|
|
|
}
|