Advertisement
Cool_boy21

Circle particle

Jul 5th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package ua.prettydude.testplugin;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.Particle;
  5. import org.bukkit.World;
  6. import org.bukkit.entity.Projectile;
  7. import org.bukkit.scheduler.BukkitRunnable;
  8. import org.bukkit.util.Vector;
  9.  
  10. public class ParticleScheduler extends BukkitRunnable {
  11.  
  12.     private Projectile proj;
  13.     private World world;
  14.     private int particleCount;
  15.  
  16.     public static double RADIUS = 1;
  17.  
  18.     public ParticleScheduler(Projectile proj, int particleCount) {
  19.         this.proj = proj;
  20.         this.particleCount = particleCount;
  21.  
  22.         this.world = proj.getWorld();
  23.     }
  24.  
  25.     @Override
  26.     public void run() {
  27.         if (proj.isDead() || proj.isOnGround()) {
  28.             this.cancel();
  29.             return;
  30.         }
  31.         for (int i = 0; i < particleCount; i++) {
  32.             double angle = (2 * Math.PI) / particleCount * i;
  33.             Location loc = preparePoint(proj, RADIUS, angle);
  34.             world.spawnParticle(Particle.VILLAGER_HAPPY, loc, 1, 0.1, 0.1, 0.1, 0.1, null,true);
  35.         }
  36.     }
  37.  
  38.     private Location preparePoint(Projectile proj, double radius, double angle) {
  39.         Location center = proj.getLocation();
  40.         Vector v = proj.getVelocity().normalize();
  41.         Vector per = getPerpendicular(v);
  42.         Vector rotated = rotateVectorCC(per, v, angle).normalize();
  43.         return center.add(rotated.multiply(radius));
  44.     }
  45.  
  46.     private Vector getPerpendicular(Vector base) {
  47.         return Math.abs(base.getZ()) < Math.abs(base.getX())
  48.                 ? new Vector(base.getY(), -base.getX(), 0)
  49.                 : new Vector(0, -base.getZ(), base.getY());
  50.     }
  51.  
  52.     private Vector rotateVectorCC(Vector vec, Vector axis, double theta) {
  53.         double x = vec.getX(), y = vec.getY(), z = vec.getZ();
  54.         double u = axis.getX(), v = axis.getY(), w = axis.getZ();
  55.         double xPrime = u * (u * x + v * y + w * z) * (1d - Math.cos(theta))
  56.                 + x * Math.cos(theta)
  57.                 + (-w * y + v * z) * Math.sin(theta);
  58.         double yPrime = v * (u * x + v * y + w * z) * (1d - Math.cos(theta))
  59.                 + y * Math.cos(theta)
  60.                 + (w * x - u * z) * Math.sin(theta);
  61.         double zPrime = w * (u * x + v * y + w * z) * (1d - Math.cos(theta))
  62.                 + z * Math.cos(theta)
  63.                 + (-v * x + u * y) * Math.sin(theta);
  64.         return new Vector(xPrime, yPrime, zPrime);
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement