Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. package de.craftstuebchen.particles;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.entity.Entity;
  5. import org.bukkit.util.Vector;
  6.  
  7. import java.util.Random;
  8.  
  9. public class MathUtils {
  10.  
  11. public static Random random = new Random();
  12.  
  13. public static final Vector rotateAroundAxisX(Vector v, double angle) {
  14. double y, z, cos, sin;
  15. cos = Math.cos(angle);
  16. sin = Math.sin(angle);
  17. y = v.getY() * cos - v.getZ() * sin;
  18. z = v.getY() * sin + v.getZ() * cos;
  19. return v.setY(y).setZ(z);
  20. }
  21.  
  22. public static final Vector rotateAroundAxisY(Vector v, double angle) {
  23. double x, z, cos, sin;
  24. cos = Math.cos(angle);
  25. sin = Math.sin(angle);
  26. x = v.getX() * cos + v.getZ() * sin;
  27. z = v.getX() * -sin + v.getZ() * cos;
  28. return v.setX(x).setZ(z);
  29. }
  30.  
  31. public static final Vector rotateAroundAxisZ(Vector v, double angle) {
  32. double x, y, cos, sin;
  33. cos = Math.cos(angle);
  34. sin = Math.sin(angle);
  35. x = v.getX() * cos - v.getY() * sin;
  36. y = v.getX() * sin + v.getY() * cos;
  37. return v.setX(x).setY(y);
  38. }
  39.  
  40. public static final Vector rotateVector(Vector v, double angleX, double angleY, double angleZ) {
  41. rotateAroundAxisX(v, angleX);
  42. rotateAroundAxisY(v, angleY);
  43. rotateAroundAxisZ(v, angleZ);
  44. return v;
  45. }
  46.  
  47. public static final double angleToXAxis(Vector vector) {
  48. return Math.atan2(vector.getX(), vector.getY());
  49. }
  50.  
  51. public static Vector getRandomVector() {
  52. double x = random.nextDouble() * 2.0D - 1.0D;
  53. double y = random.nextDouble() * 2.0D - 1.0D;
  54. double z = random.nextDouble() * 2.0D - 1.0D;
  55.  
  56. return new Vector(x, y, z).normalize();
  57. }
  58.  
  59. public static Vector getRandomCircleVector() {
  60. double rnd = random.nextDouble() * 2.0D * 3.141592653589793D;
  61. double x = Math.cos(rnd);
  62. double z = Math.sin(rnd);
  63.  
  64. return new Vector(x, 0.0D, z);
  65. }
  66.  
  67. public static double randomDouble(double min, double max) {
  68. return Math.random() < 0.5 ? ((1 - Math.random()) * (max - min) + min) : (Math.random() * (max - min) + min);
  69. }
  70.  
  71. public static float randomRangeFloat(float min, float max) {
  72. return (float) (Math.random() < 0.5 ? ((1 - Math.random()) * (max - min) + min) : (Math.random() * (max - min) + min));
  73. }
  74.  
  75. /**
  76. * Returns a random integer between the value min and the value max.
  77. * @param min the minimum integer value.
  78. * @param max the maximum integer value.
  79. * @return a random integer between two values.
  80. */
  81. public static int randomRangeInt(int min, int max) {
  82. return (int) (Math.random() < 0.5 ? ((1 - Math.random()) * (max - min) + min) : (Math.random() * (max - min) + min));
  83. }
  84.  
  85. public static double offset(Entity a, Entity b) {
  86. return offset(a.getLocation().toVector(), b.getLocation().toVector());
  87. }
  88.  
  89. public static double offset(Location a, Location b) {
  90. return offset(a.toVector(), b.toVector());
  91. }
  92.  
  93. public static double offset(Vector a, Vector b) {
  94. return a.subtract(b).length();
  95. }
  96.  
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement