diff options
| author | 2024-06-14 20:47:14 -0500 | |
|---|---|---|
| committer | 2024-06-14 20:47:14 -0500 | |
| commit | 0dfcb09376007cded49a1d2d54bf2c197a2dbe44 (patch) | |
| tree | 6becc57575b232099298fc4821f9c859e8db1b07 /src/main/java/dev/figboot/cuberender/math | |
| parent | initial commit (diff) | |
allow translation using 4x4 matrices
Diffstat (limited to 'src/main/java/dev/figboot/cuberender/math')
3 files changed, 38 insertions, 8 deletions
diff --git a/src/main/java/dev/figboot/cuberender/math/Matrix4f.java b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java index e86db41..8f98b3b 100644 --- a/src/main/java/dev/figboot/cuberender/math/Matrix4f.java +++ b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java @@ -80,7 +80,7 @@ public class Matrix4f { } public Vector4f transform(Vector4f in) { - return transform(in, in); + return transform(in, new Vector4f()); } public Vector4f transform(Vector4f in, Vector4f target) { diff --git a/src/main/java/dev/figboot/cuberender/math/Vector3f.java b/src/main/java/dev/figboot/cuberender/math/Vector3f.java index 40720e8..a2608f1 100644 --- a/src/main/java/dev/figboot/cuberender/math/Vector3f.java +++ b/src/main/java/dev/figboot/cuberender/math/Vector3f.java @@ -34,9 +34,9 @@ public class Vector3f { public Vector3f normalize(Vector3f target) { float len = length(); - target.x /= len; - target.y /= len; - target.z /= len; + target.x = this.x / len; + target.y = this.y / len; + target.z = this.z / len; return target; } } diff --git a/src/main/java/dev/figboot/cuberender/math/Vector4f.java b/src/main/java/dev/figboot/cuberender/math/Vector4f.java index 7a29010..402b5ed 100644 --- a/src/main/java/dev/figboot/cuberender/math/Vector4f.java +++ b/src/main/java/dev/figboot/cuberender/math/Vector4f.java @@ -10,6 +10,10 @@ public class Vector4f { this(0, 0, 0, 0); } + public Vector4f(float x, float y, float z) { + this(x, y, z, 1f); + } + public Vector4f(Vector4f vec) { this(vec.x, vec.y, vec.z, vec.w); } @@ -38,10 +42,36 @@ public class Vector4f { } public Vector4f times(float fact, Vector4f target) { - target.x *= fact; - target.y *= fact; - target.z *= fact; - target.w *= fact; + target.x = x * fact; + target.y = y * fact; + target.z = z * fact; + target.w = w * fact; + return target; + } + + public float dot(Vector4f that) { + return this.x * that.x + this.y * that.y + this.z * that.z + this.w * that.w; + } + + public float lengthSquared() { + // dot(this, this) + return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; + } + + public float length() { + return (float)Math.sqrt(lengthSquared()); + } + + public Vector4f normalize() { + return normalize(this); + } + + public Vector4f normalize(Vector4f target) { + float len = length(); + target.x = this.x / len; + target.y = this.y / len; + target.z = this.z / len; + target.w = this.w / len; return target; } } |
