1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
\r
16 package com.badlogic.gdx.math;
\r
18 import java.io.Serializable;
\r
22 * Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
\r
23 * @author badlogicgames@gmail.com
\r
26 public class Vector2 implements Serializable {
\r
27 private static final long serialVersionUID = 913902788239530931L;
\r
29 /** static temporary vector **/
\r
30 private final static Vector2 tmp = new Vector2();
\r
32 /** the x-component of this vector **/
\r
34 /** the y-component of this vector **/
\r
38 * Constructs a new vector at (0,0)
\r
45 * Constructs a vector with the given components
\r
46 * @param x The x-component
\r
47 * @param y The y-component
\r
49 public Vector2 (float x, float y) {
\r
55 * Constructs a vector from the given vector
\r
56 * @param v The vector
\r
58 public Vector2 (Vector2 v) {
\r
63 * @return a copy of this vector
\r
65 public Vector2 cpy () {
\r
66 return new Vector2(this);
\r
70 * @return The euclidian length
\r
72 public float len () {
\r
73 return (float)Math.sqrt(x * x + y * y);
\r
77 * @return The squared euclidian length
\r
79 public float len2 () {
\r
80 return x * x + y * y;
\r
84 * Sets this vector from the given vector
\r
85 * @param v The vector
\r
86 * @return This vector for chaining
\r
88 public Vector2 set (Vector2 v) {
\r
95 * Sets the components of this vector
\r
96 * @param x The x-component
\r
97 * @param y The y-component
\r
98 * @return This vector for chaining
\r
100 public Vector2 set (float x, float y) {
\r
107 * Substracts the given vector from this vector.
\r
108 * @param v The vector
\r
109 * @return This vector for chaining
\r
111 public Vector2 sub (Vector2 v) {
\r
118 * Normalizes this vector
\r
119 * @return This vector for chaining
\r
121 public Vector2 nor () {
\r
131 * Adds the given vector to this vector
\r
132 * @param v The vector
\r
133 * @return This vector for chaining
\r
135 public Vector2 add (Vector2 v) {
\r
142 * Adds the given components to this vector
\r
143 * @param x The x-component
\r
144 * @param y The y-component
\r
145 * @return This vector for chaining
\r
147 public Vector2 add (float x, float y) {
\r
154 * @param v The other vector
\r
155 * @return The dot product between this and the other vector
\r
157 public float dot (Vector2 v) {
\r
158 return x * v.x + y * v.y;
\r
162 * Multiplies this vector by a scalar
\r
163 * @param scalar The scalar
\r
164 * @return This vector for chaining
\r
166 public Vector2 mul (float scalar) {
\r
173 * @param v The other vector
\r
174 * @return the distance between this and the other vector
\r
176 public float dst (Vector2 v) {
\r
177 final float x_d = v.x - x;
\r
178 final float y_d = v.y - y;
\r
179 return (float)Math.sqrt(x_d * x_d + y_d * y_d);
\r
183 * @param x The x-component of the other vector
\r
184 * @param y The y-component of the other vector
\r
185 * @return the distance between this and the other vector
\r
187 public float dst (float x, float y) {
\r
188 final float x_d = x - this.x;
\r
189 final float y_d = y - this.y;
\r
190 return (float)Math.sqrt(x_d * x_d + y_d * y_d);
\r
194 * @param v The other vector
\r
195 * @return the squared distance between this and the other vector
\r
197 public float dst2 (Vector2 v) {
\r
198 final float x_d = v.x - x;
\r
199 final float y_d = v.y - y;
\r
200 return x_d * x_d + y_d * y_d;
\r
203 public String toString () {
\r
204 return "[" + x + ":" + y + "]";
\r
208 * Substracts the other vector from this vector.
\r
209 * @param x The x-component of the other vector
\r
210 * @param y The y-component of the other vector
\r
211 * @return This vector for chaining
\r
213 public Vector2 sub (float x, float y) {
\r
220 * @return a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add(
\r
221 * v2.tmp() ) will not work!
\r
223 public Vector2 tmp () {
\r
224 return tmp.set(this);
\r
228 * Multiplies this vector by the given matrix
\r
229 * @param mat the matrix
\r
230 * @return this vector
\r
232 public Vector2 mul (Matrix3 mat) {
\r
233 float x = this.x * mat.vals[0] + this.y * mat.vals[3] + mat.vals[6];
\r
234 float y = this.x * mat.vals[1] + this.y * mat.vals[4] + mat.vals[7];
\r
241 * Calculates the 2D cross product between this
\r
242 * and the given vector.
\r
243 * @param v the other vector
\r
244 * @return the cross product
\r
246 public float crs(Vector2 v) {
\r
247 return this.x * v.y - this.y * v.x;
\r
251 * Calculates the 2D cross product between this
\r
252 * and the given vector.
\r
253 * @param x the x-coordinate of the other vector
\r
254 * @param y the y-coordinate of the other vector
\r
255 * @return the cross product
\r
257 public float crs(float x, float y) {
\r
258 return this.x * y - this.y * x;
\r
262 * @return the angle in degrees of this vector (point) relative to the x-axis. Angles are counter-clockwise and between 0 and 360.
\r
264 public float angle() {
\r
265 float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;
\r
272 * Rotates the Vector2 by the given angle, counter-clockwise.
\r
273 * @param angle the angle in degrees
\r
276 public Vector2 rotate(float angle) {
\r
277 float rad = angle * MathUtils.degreesToRadians;
\r
278 float cos = (float)Math.cos(rad);
\r
279 float sin = (float)Math.sin(rad);
\r
281 float newX = this.x * cos - this.y * sin;
\r
282 float newY = this.x * sin + this.y * cos;
\r