2 * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
\r
5 * License. You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
\r
10 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
\r
11 * governing permissions and limitations under the License.
\r
14 package com.badlogic.gdx.math;
\r
16 import java.io.Serializable;
\r
18 import com.badlogic.gdx.utils.MathUtils;
\r
21 * Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
\r
22 * @author badlogicgames@gmail.com
\r
25 public class Vector2 implements Serializable {
\r
26 private static final long serialVersionUID = 913902788239530931L;
\r
28 /** static temporary vector **/
\r
29 private final static Vector2 tmp = new Vector2();
\r
31 /** the x-component of this vector **/
\r
33 /** the y-component of this vector **/
\r
37 * Constructs a new vector at (0,0)
\r
44 * Constructs a vector with the given components
\r
45 * @param x The x-component
\r
46 * @param y The y-component
\r
48 public Vector2 (float x, float y) {
\r
54 * Constructs a vector from the given vector
\r
55 * @param v The vector
\r
57 public Vector2 (Vector2 v) {
\r
62 * @return a copy of this vector
\r
64 public Vector2 cpy () {
\r
65 return new Vector2(this);
\r
69 * @return The euclidian length
\r
71 public float len () {
\r
72 return (float)Math.sqrt(x * x + y * y);
\r
76 * @return The squared euclidian length
\r
78 public float len2 () {
\r
79 return x * x + y * y;
\r
83 * Sets this vector from the given vector
\r
84 * @param v The vector
\r
85 * @return This vector for chaining
\r
87 public Vector2 set (Vector2 v) {
\r
94 * Sets the components of this vector
\r
95 * @param x The x-component
\r
96 * @param y The y-component
\r
97 * @return This vector for chaining
\r
99 public Vector2 set (float x, float y) {
\r
106 * Substracts the given vector from this vector.
\r
107 * @param v The vector
\r
108 * @return This vector for chaining
\r
110 public Vector2 sub (Vector2 v) {
\r
117 * Normalizes this vector
\r
118 * @return This vector for chaining
\r
120 public Vector2 nor () {
\r
130 * Adds the given vector to this vector
\r
131 * @param v The vector
\r
132 * @return This vector for chaining
\r
134 public Vector2 add (Vector2 v) {
\r
141 * Adds the given components to this vector
\r
142 * @param x The x-component
\r
143 * @param y The y-component
\r
144 * @return This vector for chaining
\r
146 public Vector2 add (float x, float y) {
\r
153 * @param v The other vector
\r
154 * @return The dot product between this and the other vector
\r
156 public float dot (Vector2 v) {
\r
157 return x * v.x + y * v.y;
\r
161 * Multiplies this vector by a scalar
\r
162 * @param scalar The scalar
\r
163 * @return This vector for chaining
\r
165 public Vector2 mul (float scalar) {
\r
172 * @param v The other vector
\r
173 * @return the distance between this and the other vector
\r
175 public float dst (Vector2 v) {
\r
176 final float x_d = v.x - x;
\r
177 final float y_d = v.y - y;
\r
178 return (float)Math.sqrt(x_d * x_d + y_d * y_d);
\r
182 * @param x The x-component of the other vector
\r
183 * @param y The y-component of the other vector
\r
184 * @return the distance between this and the other vector
\r
186 public float dst (float x, float y) {
\r
187 final float x_d = x - this.x;
\r
188 final float y_d = y - this.y;
\r
189 return (float)Math.sqrt(x_d * x_d + y_d * y_d);
\r
193 * @param v The other vector
\r
194 * @return the squared distance between this and the other vector
\r
196 public float dst2 (Vector2 v) {
\r
197 final float x_d = v.x - x;
\r
198 final float y_d = v.y - y;
\r
199 return x_d * x_d + y_d * y_d;
\r
202 public String toString () {
\r
203 return "[" + x + ":" + y + "]";
\r
207 * Substracts the other vector from this vector.
\r
208 * @param x The x-component of the other vector
\r
209 * @param y The y-component of the other vector
\r
210 * @return This vector for chaining
\r
212 public Vector2 sub (float x, float y) {
\r
219 * @return a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add(
\r
220 * v2.tmp() ) will not work!
\r
222 public Vector2 tmp () {
\r
223 return tmp.set(this);
\r
227 * Multiplies this vector by the given matrix
\r
228 * @param mat the matrix
\r
229 * @return this vector
\r
231 public Vector2 mul (Matrix3 mat) {
\r
232 float x = this.x * mat.vals[0] + this.y * mat.vals[3] + mat.vals[6];
\r
233 float y = this.x * mat.vals[1] + this.y * mat.vals[4] + mat.vals[7];
\r
240 * Calculates the 2D cross product between this
\r
241 * and the given vector.
\r
242 * @param v the other vector
\r
243 * @return the cross product
\r
245 public float crs(Vector2 v) {
\r
246 return this.x * v.y - this.y * v.x;
\r
250 * Calculates the 2D cross product between this
\r
251 * and the given vector.
\r
252 * @param x the x-coordinate of the other vector
\r
253 * @param y the y-coordinate of the other vector
\r
254 * @return the cross product
\r
256 public float crs(float x, float y) {
\r
257 return this.x * y - this.y * x;
\r
261 * @return the angle in degrees of this vector (point) relative to the x-axis. Angles are counter-clockwise and between 0 and 360.
\r
263 public float angle() {
\r
264 float angle = (float)Math.atan2(y, x) * MathUtils.degreesToRadians;
\r
271 * Rotates the Vector2 by the given angle, counter-clockwise.
\r
272 * @param angle the angle in degrees
\r
275 public Vector2 rotate(float angle) {
\r
276 float rad = angle * MathUtils.degreesToRadians;
\r
277 float cos = (float)Math.cos(rad);
\r
278 float sin = (float)Math.sin(rad);
\r
280 float newX = this.x * cos - this.y * sin;
\r
281 float newY = this.x * sin + this.y * cos;
\r