Sunday, January 20, 2019

Math for Game Developers - Character Movement 6 (Adding Vectors)

Math for Game Developers - Character Movement 6 (Adding Vectors)

Math Class:
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;
}
}

Character Movement 5 (Unit-Length Vectors) - Math for Game Developers

Character Movement 5 (Unit-Length Vectors)

Main Class:

public class Main {
public static void main(String[] args) {
Point p = new Point(3, 4);
Point i = new Point(1, 2);
Vector ip = Vector.subtractPoints(p, i);
Vector unit = Vector.normalized(ip);
System.out.println("Normalized Unit: "+unit);
System.out.println("Normalized length: "+Vector.length(unit));
}
}

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 double x, y;
public Vector(double d, double e) {

this.x = d;
this.y = e;
}
public static Vector subtractPoints(Point p, Point i) {
return new Vector(p.getX() - i.getX(), p.getY()-i.getY());
}
public static double length(Vector v) {
return Math.sqrt(((v.x *v.x) + (v.y*v.y)));
}
public static Vector normalized(Vector v) {
return new Vector(v.x / length(v), v.y / length(v));
}
@Override
public String toString() {
return x+", "+y;
}
}

Math for Game Developers - Character Movement 4 (Vector Scaling)

Math for Game Developers - Character Movement 4 (Vector Scaling)

Main class:

public class Main {
public static void main(String[] args) {
Vector v= new Vector(3,4);
System.out.println("Initial Speed: "+v.length());
System.out.println("Double Speed: "+v.scalingSpeed(2));
System.out.println("Halved Speed: "+v.scalingSpeed(0.5f));
}
}

Vector Class:

public class Vector {
private float x, y;

public Vector() {
}

public Vector(float x, float y) {

this.x = x;
this.y = y;
}

public float length() {
return (float) Math.sqrt((x*x) + (y*y));
}

public float scalingSpeed(float times) {
return times*length();
}
}

Saturday, January 19, 2019

Math for Game Developers - Distance Comparison

Math for Game Developers - Distance Comparison

Main class:

package learn.mathforgame.DistanceComparison;

public class Main {
public static void main(String[] args) {
Point p = new Point(0, -1);
Point i = new Point(1, 1);
Point c = new Point(2, -1);
Vector ip = new Vector().subtractPoints(p, i);
Vector cp = new Vector().subtractPoints(p, c);
System.out.println("Result IP: "+new Vector().length(ip));
System.out.println("Result CP: "+new Vector().length(cp));
}
}

Point class:

package learn.mathforgame.DistanceComparison;

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.VectorLength;

public class Vector {
private int x, y;
public Vector() {
}
public Vector(int x, int y) {

this.x = x;
this.y = y;
}
public Vector subtractPoints(Point p, Point i) {
return new Vector(p.getX() - i.getX(), p.getY()-i.getY());
}
public int length(Vector v) {
return ((v.x *v.x) + (v.y*v.y));
}
}

Math for Game Developers - Character Movement 3 (Vector Length)

Math for Game Developers - Character Movement 3 (Vector Length)


Main class:

package learn.mathforgame.VectorLength;

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().subtractPoints(p, i);
System.out.println("Result: "+v.length(v));
}
}

Point class:

package learn.mathforgame.VectorLength;

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.VectorLength;

public class Vector {
private int x, y;
public Vector() {
}
public Vector(int x, int y) {

this.x = x;
this.y = y;
}
public Vector subtractPoints(Point p, Point i) {
return new Vector(p.getX() - i.getX(), p.getY()-i.getY());
}
public float length(Vector v) {
return (float) Math.sqrt((v.x *v.x) + (v.y*v.y));
}
}

Math for Game Developers - Character Movement (Points and Vectors)

Math for Game Developers - Character Movement (Points and Vectors)

Main class:

package learn.mathforgame.PointsandVectors;

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

Point class:

package learn.mathforgame.PointsandVectors;

public class Point {
private int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point addVector(Vector v) {
return new Point(x + v.getX(), y+v.getY());
}
@Override
public String toString() {
return x +", "+y;
}
}

Vector class:

package learn.mathforgame.PointsandVectors;

public class Vector {
private int x, y;
public Vector(int x, int y) {

this.x = x;
this.y = y;
}


public int getX() {
return x;
}

public int getY() {
return y;
}
}



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;
}
}