Math for Game Developers - Character Movement 6 (Adding Vectors)
public class Main {
public static void main(String[] args) {
Point d = new Point(4, 0);
Point r = new Point(0, -5);
Vector v= Vector.AddVector(d, r);
System.out.println("Diagonal Result: "+v);
}
}
Point class:
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Vector Class:
public class Vector {
private int x, y;
public Vector() {
}
public Vector(int x, int y) {
this.x = x;
this.y = y;
}
public static Vector AddVector(Point d, Point r) {
return new Vector(d.getX() + r.getX(), d.getY() + r.getY());
}
@Override
public String toString() {
return x+", "+y;
}
}