Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 200 | chris | 1 | package com.gebauz.Bauzoid.math; |
| 2 | |||
| 3 | public class Vector2 |
||
| 4 | { |
||
| 5 | |||
| 6 | public float x; |
||
| 7 | public float y; |
||
| 8 | |||
| 9 | public Vector2() |
||
| 10 | { |
||
| 11 | } |
||
| 12 | |||
| 13 | public Vector2(float _x, float _y) |
||
| 14 | { |
||
| 15 | x = _x; |
||
| 16 | y = _y; |
||
| 17 | } |
||
| 18 | |||
| 19 | public void normalize() |
||
| 20 | { |
||
| 21 | float len = length(); |
||
| 22 | if (len != 0.0f) |
||
| 23 | { |
||
| 24 | x /= len; |
||
| 25 | y /= len; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public float length() |
||
| 30 | { |
||
| 31 | float squared = squaredLength(); |
||
| 32 | return (float)Math.sqrt(squared); |
||
| 33 | } |
||
| 34 | |||
| 35 | public void setLength(float newLength) |
||
| 36 | { |
||
| 37 | normalize(); |
||
| 38 | x *= newLength; |
||
| 39 | y *= newLength; |
||
| 40 | } |
||
| 41 | |||
| 42 | public boolean isZero() |
||
| 43 | { |
||
| 44 | return ((x == 0.0f) && (y == 0.0f)); |
||
| 45 | } |
||
| 46 | |||
| 47 | public float squaredLength() |
||
| 48 | { |
||
| 49 | return (x*x + y*y); |
||
| 50 | } |
||
| 51 | |||
| 52 | public void addVector(Vector2 v) |
||
| 53 | { |
||
| 54 | x += v.x; |
||
| 55 | y += v.y; |
||
| 56 | } |
||
| 57 | |||
| 58 | public void subtractVector(Vector2 v) |
||
| 59 | { |
||
| 60 | x -= v.x; |
||
| 61 | y -= v.y; |
||
| 62 | } |
||
| 63 | |||
| 64 | public float getAngle() |
||
| 65 | { |
||
| 66 | float angle = (float)Math.atan2(x, y); |
||
| 67 | return MathUtil.stayInDegrees0to360(MathUtil.radToDeg(angle)); |
||
| 68 | } |
||
| 69 | |||
| 70 | public float dotProduct(Vector2 v) |
||
| 71 | { |
||
| 72 | return dotProduct(this, v); |
||
| 73 | } |
||
| 74 | |||
| 75 | static public float dotProduct(Vector2 a, Vector2 b) |
||
| 76 | { |
||
| 77 | return (a.x*b.x + a.y*b.y); |
||
| 78 | } |
||
| 79 | |||
| 80 | static public float distance(Vector2 a, Vector2 b) |
||
| 81 | { |
||
| 82 | Vector2 c = new Vector2(b.x - a.x, b.y - a.y); |
||
| 83 | return c.length(); |
||
| 84 | } |
||
| 85 | |||
| 86 | static public Vector2 fromRotation(float angle, float length) |
||
| 87 | { |
||
| 88 | Vector2 result = fromRotation(angle); |
||
| 89 | result.setLength(length); |
||
| 90 | return result; |
||
| 91 | } |
||
| 92 | |||
| 93 | static public Vector2 fromRotation(float angle) |
||
| 94 | { |
||
| 95 | return new Vector2((float)Math.cos(MathUtil.degToRad(angle)), (float)Math.sin(MathUtil.degToRad(angle))); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 |