Sunday, January 20, 2019

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

No comments:

Post a Comment