Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (603) Games in Android Showcase (171) games submitted by our members Games in WIP (652) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Where to learn 2D-vectors  (Read 1309 times) 0 Members and 1 Guest are viewing this topic.
Mattedatten

Senior Newbie

 « Posted 2011-02-06 14:25:47 »

Hey all

I've been working on this small 2D-side scroller, after following MrJavaHelp's game tutorials on youtube.

I asked here and on another forum for how to solve my idea of a shot following the mouse-press.
I wanted to do this with angles, but I can't seem to get it right.

On the other forum where I asked, I got the answer to drop angles, and use vectors, and he linked me to R2-vectors on wikipedia, a short, non-informative text (in my view)

So, now I've been watching about 30min different youtube-videos explaining what a vector is, and I kind of get it now.

But I have no idea how to implement or use it in Java/programming.
..
So, where to find examples or tutorials or something that shows how and where to use vectors in Java-2D?
(Or, do anyone here have an personal example? )

Thanks if anyone has an answer
//M
Kieran Newland

Senior Newbie

 « Reply #1 - Posted 2011-02-06 15:05:34 »

The function, Math.atan2(y, x) can give you the angle of your mouse compared to the sprite. You could rotate your sprite to this angle or something similar to show that the angle is correct.

Im not sure if its the same with Java, but with flash, the angles are crooked. You need to -180 on the angle to get the real angle (its something like this anyway. Its easy enough to work out).

You should be able to get somewhere with that. I hope it works!

Kieran Newland
krasse
 « Reply #2 - Posted 2011-02-06 20:13:35 »

My 2D vector class:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301 `public class Vec2D {   public double x;   public double y;   public static Vec2D ZERO = new Vec2D(0, 0);   public static Vec2D X = new Vec2D(1, 0);   public static Vec2D Y = new Vec2D(0, 1);   @Override   public String toString() {      return "[" + x + ", " + y + "]";   }   public Vec2D() {   }   public Vec2D(Vec2D v) {      set(v);   }      public Vec2D(double x, double y) {      this.x = x;      this.y = y;   }   public Vec2D(double... coords) {      this.x = coords[0];      this.y = coords[1];   }   public void rotate(Vec2D center, double angle) {      double cx = center.x;      double cy = center.y;      double diffx = x - cx;      double diffy = y - cy;      x = cx + diffx * Math.cos(angle) - diffy * Math.sin(angle);      y = cy + diffx * Math.sin(angle) + diffy * Math.cos(angle);   }   public Vec2D copy() {      return new Vec2D(x, y);   }   // Returns a new Vec2D in the base b1 and b2 (which must be normalized   // first)   public static Vec2D getNewCoords(Vec2D v, Vec2D b1, Vec2D b2) {      // Project v on both b1 and b2 to get the coordinates      double c1 = v.dot(b1);      double c2 = v.dot(b2);      return new Vec2D(c1, c2);   }   public Vec2D crossWithZ() {      return new Vec2D(-y, x);   }   // Projects this Vector to v   public Vec2D projectCopy(Vec2D v) {      double dot = dot(v);      double vlength = v.length();      return v.multCopy(dot / (vlength * vlength));   }   public double dot(Vec2D v) {      return x * v.x + y * v.y;   }   public static Vec2D center(Vec2D v1, Vec2D v2) {      return new Vec2D(0.5 * (v1.x + v2.x), 0.5 * (v1.y + v2.y));   }   public static Vec2D center(ArrayList list) {      Vec2D result = new Vec2D();      if (list.size() > 0) {         for (Vec2D v : list) {            result.plus(v);         }         result.mult(1.0 / list.size());      }      return result;   }   public static Vec2D center(Vec2D v1, Vec2D v2, Vec2D v3) {      return new Vec2D((v1.x + v2.x + v3.x) / 3.0, (v1.y + v2.y + v3.y) / 3.0);   }   public Vec2D rotateCopy(double angle) {      Vec2D result = copy();      result.rotate(angle);      return result;   }   public Vec2D rotate(double angle) {      double newX = x * Math.cos(angle) - y * Math.sin(angle);      double newY = x * Math.sin(angle) + y * Math.cos(angle);      x = newX;      y = newY;      return this;   }   public Vec2D multCopy(double val) {      return new Vec2D(x * val, y * val);   }   public Vec2D mult(double val) {      x *= val;      y *= val;      return this;   }   public Vec2D minusCopy(Vec2D v) {      return new Vec2D(x - v.x, y - v.y);   }   public Vec2D plusCopy(Vec2D v) {      return new Vec2D(x + v.x, y + v.y);   }   public Vec2D plus(double x, double y) {      this.x += x;      this.y += y;      return this;   }   public Vec2D plusCopy(double x, double y) {      return copy().plus(x, y);   }   public Vec2D perpendicularCopy() {      return new Vec2D(y, -x);   }   public double angle(Vec2D v) {      return Math.atan2(v.y, v.x) - Math.atan2(y, x);      // return Math.atan2(diffy, diffx);   }   public static double angle(double x1, double y1, double x2, double y2) {      return Math.atan2(y2, x2) - Math.atan2(y1, x1);      // return Math.atan2(diffy, diffx);   }   public static double angle(double x, double y) {      return Math.atan2(y, x);   }   public double angle() {      return Math.atan2(y, x);   }   public double length() {      return Math.sqrt(x * x + y * y);   }   public double sqLength() {      return x * x + y * y;   }   public double getLength() {      return Math.sqrt(x * x + y * y);   }   public double crossValue(Vec2D v) {      return x * v.y - y * v.x;   }   public Vec2D normalizeCopy() {      double l = Math.sqrt(x * x + y * y);      return new Vec2D(x / l, y / l);   }   public Vec2D normalize() {      double l = Math.sqrt(x * x + y * y);      x /= l;      y /= l;      return this;   }   public static double distanceBetween(double x1, double y1, double x2,         double y2) {      double diffx = x1 - x2;      double diffy = y1 - y2;      return Math.sqrt(diffx * diffx + diffy * diffy);   }   public double distanceTo(Vec2D v) {      double diffx = x - v.x;      double diffy = y - v.y;      return Math.sqrt(diffx * diffx + diffy * diffy);   }   public double distanceToSq(double tx, double ty) {      double diffx = x - tx;      double diffy = y - ty;      return diffx * diffx + diffy * diffy;   }   public boolean within(int mx, int my, int width, double zoomx,         double zoomy, double offsetx, double offsety) {      int sx = (int) (zoomx * x - offsetx);      int sy = (int) (zoomy * y - offsety);      return (Math.abs(sx - mx) <= width && Math.abs(sy - my) <= width);   }   public Vec2D translate(double dx, double dy) {      x += dx;      y += dy;      return this;   }   public Vec2D scale(double scalex, double scaley, double centerx,         double centery) {      double diffx = x - centerx;      double diffy = y - centery;      x = centerx + diffx * scalex;      y = centery + diffy * scaley;      return this;   }   public Vec2D scale(double scaleX, double scaleY) {      return scale(scaleX, scaleY, 0.0, 0.0);   }   public Vec2D plus(Vec2D v) {      x += v.x;      y += v.y;      return this;   }   public double distanceTo(double x, double y) {      return distanceTo(new Vec2D(x, y));   }   public Vec2D negateCopy() {      Vec2D result = copy();      return result.negate();   }   public Vec2D negate() {      x = -x;      y = -y;      return this;   }   public void set(Vec2D v) {      this.x = v.x;      this.y = v.y;   }   public void set(double x, double y) {      this.x = x;      this.y = y;   }   public Vec2D minus(Vec2D v) {      x -= v.x;      y -= v.y;      return this;   }   public static double diffAngle(Vec2D v1, Vec2D v2) {      Vec2D v1n = v1.normalizeCopy();      Vec2D v2n = v2.normalizeCopy();      double product = v1n.dot(v2n);      double crossProductZ = v1n.crossValue(v2n);      double result = Math.acos(product);      if (crossProductZ > 0) {         result *= -1;      }      return result;   }   public Vec2D rotateTowards(Vec2D v, double maxAngle) {      double currentAngle = angle(v);      // System.out.println(this + " " + currentAngle);      if (currentAngle > maxAngle) {         currentAngle = maxAngle;      } else if (currentAngle < -maxAngle) {         currentAngle = -maxAngle;      }      rotate(currentAngle);      return this;   }}`

If you want to create a vector between two points, such as the gun position and the mouse position, for animation, you can do like this:

 1  2  3  4  5  6  7  8  9  10  11 `Vec2D gunPos = new Vec2D(gx, gy);Vec2D mousePos = new Vec2D(mx, my);Vec2D gunToMouseN = mousePos.minusCopy(gunPos).normalize();// gunToMouseN is a vector of length 1double bulletSpeed = ...;Vec2D bulletVelocity = gunToMouseN.multCopy(bulletSpeed);// Then, in animation where bulletPos is updated for each stepbulletPos.plus(bulletVelocity);`

Mattedatten

Senior Newbie

 « Reply #3 - Posted 2011-02-06 22:54:21 »

The function, Math.atan2(y, x) can give you the angle of your mouse compared to the sprite. You could rotate your sprite to this angle or something similar to show that the angle is correct.
That's what I'm trying to work with, but for some reason I can't seem to get the mathemathics right, hm, but I might have an idea to try out. (I guess what I can't see really is how to count the angle as 0,0 is top-right corner, not bottom-right. But as I said, I just got an idea what to try (if statements  ))

My 2D vector class:
[...]
If you want to create a vector between two points, such as the gun position and the mouse position, for animation, you can do like this:
[...]

Great, thanks .. but, I won't copy it, because that not learning
I will read the methodes that you'd use and rewrite them (Great that you have some comments in there)

Well, thanks again, both of you
jezek2
 « Reply #4 - Posted 2011-02-07 04:06:50 »

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 SHC (17 views) 2015-08-01 03:58:20 Jesse (18 views) 2015-07-29 04:35:27 Riven (38 views) 2015-07-27 16:38:00 Riven (20 views) 2015-07-27 15:35:20 Riven (23 views) 2015-07-27 12:26:13 Riven (13 views) 2015-07-27 12:23:39 BurntPizza (33 views) 2015-07-25 00:14:37 BurntPizza (45 views) 2015-07-24 22:06:39 BurntPizza (26 views) 2015-07-24 06:06:53 NoxInc (34 views) 2015-07-22 22:16:53
 theagentd 51x wessles 45x basil_ 35x KaiHH 26x orangepascal 26x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x princec 11x CelestialCreator 11x klaus 11x Jesse 11x Zaneris 9x pquiring 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org