Advertisement
ASasseCreations

ASCL Maths Class

Mar 13th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.33 KB | None | 0 0
  1. package org.asassecreations.engine.tools.math;
  2.  
  3. import org.asassecreations.engine.component.Transform;
  4. import org.asassecreations.engine.render.Camera;
  5. import org.asassecreations.engine.render.Color;
  6. import org.lwjgl.util.vector.Matrix4f;
  7. import org.lwjgl.util.vector.Vector2f;
  8. import org.lwjgl.util.vector.Vector3f;
  9. import org.lwjgl.util.vector.Vector4f;
  10.  
  11. public final class Maths {
  12.  
  13.     private Maths() {
  14.  
  15.     }
  16.  
  17.     public static final float clampMin(final float value, final float min) {
  18.  
  19.         if (value <= min) return min;
  20.         return value;
  21.  
  22.     }
  23.  
  24.     public static final float clampMax(final float value, final float max) {
  25.  
  26.         if (value >= max) return max;
  27.         return value;
  28.  
  29.     }
  30.  
  31.     public static final float clamp(final float value, final float min, final float max) {
  32.  
  33.         return clampMin(clampMax(value, max), min);
  34.  
  35.     }
  36.  
  37.     public static final int clamp(final int value, final int min, final int max) {
  38.  
  39.         if (value >= max) return max;
  40.         if (value <= min) return min;
  41.         return value;
  42.  
  43.     }
  44.  
  45.     public static final float mix(final float a, final float b, final float percent) {
  46.  
  47.         return a * (1f - percent) + b * percent;
  48.  
  49.     }
  50.  
  51.     public static final Vector2f mix(final Vector2f a, final Vector2f b, final float percent) {
  52.  
  53.         return new Vector2f(mix(a.x, b.x, percent), mix(a.y, b.y, percent));
  54.  
  55.     }
  56.  
  57.     public static final Vector3f mix(final Vector3f a, final Vector3f b, final float percent) {
  58.  
  59.         return new Vector3f(mix(a.x, b.x, percent), mix(a.y, b.y, percent), mix(a.z, b.z, percent));
  60.  
  61.     }
  62.  
  63.     public static final Vector4f mix(final Vector4f a, final Vector4f b, final float percent) {
  64.  
  65.         return new Vector4f(mix(a.x, b.x, percent), mix(a.y, b.y, percent), mix(a.z, b.z, percent), mix(a.w, b.w, percent));
  66.  
  67.     }
  68.  
  69.     public static final Color mix(final Color a, final Color b, final float percent) {
  70.  
  71.         return new Color(mix(a.asVector4(), b.asVector4(), percent));
  72.  
  73.     }
  74.  
  75.     public static final float approach(final float current, final float goal, final float delta) {
  76.  
  77.         final float difference = goal - current;
  78.  
  79.         if (difference > delta) return current + delta;
  80.         if (difference < -delta) return current - delta;
  81.  
  82.         return goal;
  83.  
  84.     }
  85.  
  86.     public static final float approachSmooth(final float current, final float goal, final float delta) {
  87.  
  88.         return approach(current, goal, positive(current - goal) * delta);
  89.  
  90.     }
  91.  
  92.     public static final float minMaxConvert(final float input, final float inputLow, final float inputHigh, final float outputLow, final float outputHigh) {
  93.  
  94.         return (input - inputLow) / (inputHigh - inputLow) * (outputHigh - outputLow) + outputLow;
  95.  
  96.     }
  97.  
  98.     public static final Matrix4f createTransformationMatrix(final Vector3f translation, final Vector3f rotation, final Vector3f scale) {
  99.  
  100.         final Matrix4f matrix = new Matrix4f();
  101.         matrix.setIdentity();
  102.  
  103.         Matrix4f.translate(translation, matrix, matrix);
  104.         Matrix4f.rotate((float) Math.toRadians(rotation.x), new Vector3f(1, 0, 0), matrix, matrix);
  105.         Matrix4f.rotate((float) Math.toRadians(rotation.y), new Vector3f(0, 1, 0), matrix, matrix);
  106.         Matrix4f.rotate((float) Math.toRadians(rotation.z), new Vector3f(0, 0, 1), matrix, matrix);
  107.         Matrix4f.scale(scale, matrix, matrix);
  108.  
  109.         return matrix;
  110.  
  111.     }
  112.  
  113.     public static final Matrix4f createTransformationMatrix(final Transform transform) {
  114.  
  115.         return createTransformationMatrix(transform.position, transform.rotation, transform.scale);
  116.  
  117.     }
  118.  
  119.     public static final Matrix4f createViewMatrix(final Camera camera) {
  120.  
  121.         final Matrix4f matrix = new Matrix4f();
  122.         matrix.setIdentity();
  123.  
  124.         Matrix4f.rotate((float) Math.toRadians(camera.rotation.x), new Vector3f(1, 0, 0), matrix, matrix);
  125.         Matrix4f.rotate((float) Math.toRadians(camera.rotation.y), new Vector3f(0, 1, 0), matrix, matrix);
  126.         Matrix4f.rotate((float) Math.toRadians(camera.rotation.z), new Vector3f(0, 0, 1), matrix, matrix);
  127.  
  128.         final Vector3f negative = new Vector3f(-camera.position.x, -camera.position.y, -camera.position.z);
  129.  
  130.         Matrix4f.translate(negative, matrix, matrix);
  131.  
  132.         return matrix;
  133.  
  134.     }
  135.  
  136.     public static final float barryCentric(final Vector3f p1, final Vector3f p2, final Vector3f p3, final Vector2f pos) {
  137.         final float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
  138.         final float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
  139.         final float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
  140.         final float l3 = 1.0f - l1 - l2;
  141.         return l1 * p1.y + l2 * p2.y + l3 * p3.y;
  142.     }
  143.  
  144.     public static final Matrix4f createProjectionMatrix(final float fov, final float nearPlane, final float farPlane, final int width, final int height) {
  145.  
  146.         final Matrix4f matrix = new Matrix4f();
  147.         final float aspectRatio = (float) width / (float) height;
  148.         final float y_scale = (float) (1f / Math.tan(Math.toRadians(fov / 2f)));
  149.         final float x_scale = y_scale / aspectRatio;
  150.         final float frustum_length = farPlane - nearPlane;
  151.  
  152.         matrix.m00 = x_scale;
  153.         matrix.m11 = y_scale;
  154.         matrix.m22 = -((farPlane + nearPlane) / frustum_length);
  155.         matrix.m23 = -1;
  156.         matrix.m32 = -(2 * nearPlane * farPlane / frustum_length);
  157.         matrix.m33 = 0;
  158.  
  159.         return matrix;
  160.  
  161.     }
  162.  
  163.     public static final float positive(final float value) {
  164.  
  165.         return abs(value);
  166.  
  167.     }
  168.  
  169.     public static final float negative(final float value) {
  170.  
  171.         return -abs(value);
  172.  
  173.     }
  174.  
  175.     public static final float abs(final float value) {
  176.  
  177.         return value < 0 ? -value : value;
  178.  
  179.     }
  180.  
  181.     public static final float min(final float a, final float b) {
  182.  
  183.         return Math.min(a, b);
  184.  
  185.     }
  186.  
  187.     public static final float max(final float a, final float b) {
  188.  
  189.         return Math.max(a, b);
  190.  
  191.     }
  192.  
  193.     public static final Matrix4f createTransformationMatrix(final Vector2f translation, final Vector2f scale) {
  194.         final Matrix4f matrix = new Matrix4f();
  195.         matrix.setIdentity();
  196.         Matrix4f.translate(translation, matrix, matrix);
  197.         Matrix4f.scale(new Vector3f(scale.x, scale.y, 1f), matrix, matrix);
  198.         return matrix;
  199.     }
  200.  
  201.     public static final float hnsin(final float input) {
  202.  
  203.         return (float) Math.sin(Math.PI * input);
  204.  
  205.     }
  206.  
  207.     public static final float hncos(final float input) {
  208.  
  209.         return (float) Math.cos(Math.PI * input);
  210.  
  211.     }
  212.  
  213.     public static final float fnsin(final float input) {
  214.  
  215.         return (float) Math.sin(2f * Math.PI * input);
  216.  
  217.     }
  218.  
  219.     public static final float fncos(final float input) {
  220.  
  221.         return (float) Math.cos(2f * Math.PI * input);
  222.  
  223.     }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement