Maths.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package eu.tankernn.gameEngine.util;
  2. import org.lwjgl.util.vector.Matrix4f;
  3. import org.lwjgl.util.vector.Vector2f;
  4. import org.lwjgl.util.vector.Vector3f;
  5. import org.lwjgl.util.vector.Vector4f;
  6. public class Maths {
  7. public static float distanceBetweenPoints(Vector3f pos1, Vector3f pos2) {
  8. float baseWidth = pos1.x - pos2.x;
  9. float baseDepth = pos1.z - pos2.z;
  10. float baseDiagonal = (float) Math.sqrt(Math.pow(baseWidth, 2) + Math.pow(baseDepth, 2));
  11. float cubeHeight = pos1.y - pos2.y;
  12. float cubeDiagonal = (float) Math.sqrt(Math.pow(baseDiagonal, 2) + Math.pow(cubeHeight, 2));
  13. return cubeDiagonal;
  14. }
  15. public static Vector4f slerp(Vector4f qa, Vector4f qb, double time) {
  16. // quaternion to return
  17. Vector4f qm = new Vector4f();
  18. // Calculate angle between them.
  19. double cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  20. // if qa=qb or qa=-qb then theta = 0 and we can return qa
  21. if (Math.abs(cosHalfTheta) >= 1.0){
  22. qm.w = qa.w;qm.x = qa.x;qm.y = qa.y;qm.z = qa.z;
  23. return qm;
  24. }
  25. // Calculate temporary values.
  26. double halfTheta = Math.acos(cosHalfTheta);
  27. double sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta*cosHalfTheta);
  28. // if theta = 180 degrees then result is not fully defined
  29. // we could rotate around any axis normal to qa or qb
  30. if (Math.abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute
  31. qm.w = (float) (qa.w * 0.5 + qb.w * 0.5);
  32. qm.x = (float) (qa.x * 0.5 + qb.x * 0.5);
  33. qm.y = (float) (qa.y * 0.5 + qb.y * 0.5);
  34. qm.z = (float) (qa.z * 0.5 + qb.z * 0.5);
  35. return qm;
  36. }
  37. double ratioA = Math.sin((1 - time) * halfTheta) / sinHalfTheta;
  38. double ratioB = Math.sin(time * halfTheta) / sinHalfTheta;
  39. //calculate Quaternion.
  40. qm.w = (float) (qa.w * ratioA + qb.w * ratioB);
  41. qm.x = (float) (qa.x * ratioA + qb.x * ratioB);
  42. qm.y = (float) (qa.y * ratioA + qb.y * ratioB);
  43. qm.z = (float) (qa.z * ratioA + qb.z * ratioB);
  44. return qm;
  45. }
  46. public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale) {
  47. Matrix4f matrix = new Matrix4f();
  48. matrix.setIdentity();
  49. Matrix4f.translate(translation, matrix, matrix);
  50. Matrix4f.scale(new Vector3f(scale.x, scale.y, 1f), matrix, matrix);
  51. return matrix;
  52. }
  53. public static float barryCentric(Vector3f p1, Vector3f p2, Vector3f p3, Vector2f pos) {
  54. float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
  55. float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
  56. float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
  57. float l3 = 1.0f - l1 - l2;
  58. return l1 * p1.y + l2 * p2.y + l3 * p3.y;
  59. }
  60. public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale) {
  61. Matrix4f matrix = new Matrix4f();
  62. matrix.setIdentity();
  63. Matrix4f.translate(translation, matrix, matrix);
  64. Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix, matrix);
  65. Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix, matrix);
  66. Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix, matrix);
  67. Matrix4f.scale(new Vector3f(scale, scale, scale), matrix, matrix);
  68. return matrix;
  69. }
  70. }