Saturday, January 19, 2019

Math for Game Developers - Character Movement 2 (Subtracting Vectors)

Math for Game Developers - Character Movement 2 (Subtracting Vectors)

Math class:
package learn.mathforgame.subtractVector;

public class Main {
public static void main(String[] args) {
Point p = new Point(0, -1);
Point i = new Point(1, 1);
Vector v = new Vector().subtractPoint(p, i);
System.out.println("Result: "+v);
}
}

Point class:

package learn.mathforgame.subtractVector;

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:

package learn.mathforgame.subtractVector;

public class Vector {
private int x, y;

public Vector() {
}

public Vector(int x, int y) {

this.x = x;
this.y = y;
}
public Vector subtractPoint(Point p, Point i) {
return new Vector(p.getX() - i.getX(), p.getY() - i.getY());
}
@Override
public String toString() {
return x+", "+y;
}
}


No comments:

Post a Comment