From 5a808dbc617e03066e94557a60ec7b90ec1582bf Mon Sep 17 00:00:00 2001 From: Esophose Date: Tue, 14 Jan 2020 13:47:01 -0700 Subject: [PATCH] Now using a lookup table for sin/cos operations for styles --- .../styles/ParticleStyleBeam.java | 5 +- .../styles/ParticleStyleCelebration.java | 11 +++-- .../styles/ParticleStyleCompanion.java | 9 ++-- .../styles/ParticleStyleHalo.java | 5 +- .../styles/ParticleStyleInvocation.java | 21 +++++---- .../styles/ParticleStyleOrbit.java | 5 +- .../styles/ParticleStylePopper.java | 3 +- .../styles/ParticleStylePulse.java | 9 ++-- .../styles/ParticleStyleQuadhelix.java | 5 +- .../styles/ParticleStyleRings.java | 9 ++-- .../styles/ParticleStyleSphere.java | 7 +-- .../styles/ParticleStyleSpin.java | 20 ++------ .../styles/ParticleStyleSpiral.java | 5 +- .../styles/ParticleStyleTwins.java | 5 +- .../styles/ParticleStyleVortex.java | 3 +- .../styles/ParticleStyleWhirl.java | 9 ++-- .../styles/ParticleStyleWhirlwind.java | 9 ++-- .../styles/ParticleStyleWings.java | 7 +-- .../esophose/playerparticles/util/MathL.java | 46 +++++++++++++++++++ .../playerparticles/util/VectorUtils.java | 20 ++++---- 20 files changed, 133 insertions(+), 80 deletions(-) create mode 100644 src/main/java/dev/esophose/playerparticles/util/MathL.java diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBeam.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBeam.java index 32a73c2..511e166 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBeam.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleBeam.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -25,9 +26,9 @@ public class ParticleStyleBeam extends DefaultParticleStyle { List particles = new ArrayList<>(); for (int i = 0; i < points; i++) { double angle = slice * i; - double newX = location.getX() + radius * Math.cos(angle); + double newX = location.getX() + radius * MathL.cos(angle); double newY = location.getY() + (this.step / 10D) - 1; - double newZ = location.getZ() + radius * Math.sin(angle); + double newZ = location.getZ() + radius * MathL.sin(angle); particles.add(new PParticle(new Location(location.getWorld(), newX, newY, newZ))); } return particles; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java index 48a8909..050f264 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCelebration.java @@ -9,6 +9,7 @@ import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.PPlayer; import dev.esophose.playerparticles.particles.ParticleEffect; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -73,8 +74,8 @@ public class ParticleStyleCelebration extends DefaultParticleStyle { private void spawnFirework(final Location location, final PPlayer pplayer, final ParticlePair particle, final Random random) { double angle = random.nextDouble() * Math.PI * 2; double distanceFrom = 1.25 + random.nextDouble() * 1.5; - double dx = Math.sin(angle) * distanceFrom; - double dz = Math.cos(angle) * distanceFrom; + double dx = MathL.sin(angle) * distanceFrom; + double dz = MathL.cos(angle) * distanceFrom; final Location loc = location.clone().add(dx, 1, dz); final int fuse = 3 + random.nextInt(3); Player player = pplayer.getPlayer(); @@ -102,9 +103,9 @@ public class ParticleStyleCelebration extends DefaultParticleStyle { double v = random.nextDouble(); double theta = 2 * Math.PI * u; double phi = Math.acos(2 * v - 1); - double dx = radius * Math.sin(phi) * Math.cos(theta); - double dy = radius * Math.sin(phi) * Math.sin(theta); - double dz = radius * Math.cos(phi); + double dx = radius * MathL.sin(phi) * MathL.cos(theta); + double dy = radius * MathL.sin(phi) * MathL.sin(theta); + double dz = radius * MathL.cos(phi); particles.add(new PParticle(this.location.clone().add(dx, dy, dz))); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCompanion.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCompanion.java index 0902e91..97e4e75 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCompanion.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleCompanion.java @@ -26,6 +26,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -51,12 +52,12 @@ public class ParticleStyleCompanion extends DefaultParticleStyle { Vector vector = new Vector(); double t = (Math.PI / this.numParticles) * this.step; - double r = Math.sin(t) * this.size; + double r = MathL.sin(t) * this.size; double s = 2 * Math.PI * t; - vector.setX(this.xFactor * r * Math.cos(s) + this.xOffset); - vector.setZ(this.zFactor * r * Math.sin(s) + this.zOffset); - vector.setY(this.yFactor * this.size * Math.cos(t) + this.yOffset); + vector.setX(this.xFactor * r * MathL.cos(s) + this.xOffset); + vector.setZ(this.zFactor * r * MathL.sin(s) + this.zOffset); + vector.setY(this.yFactor * this.size * MathL.cos(t) + this.yOffset); for (int i = 0; i < this.particlesPerIteration; i++) { particles.add(new PParticle(location.clone().subtract(vector))); diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHalo.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHalo.java index fad256a..18529c8 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHalo.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleHalo.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -27,9 +28,9 @@ public class ParticleStyleHalo extends DefaultParticleStyle { for (int i = 0; i < points; i++) { double angle = slice * i; - double dx = radius * Math.cos(angle); + double dx = radius * MathL.cos(angle); double dy = 1.5; - double dz = radius * Math.sin(angle); + double dz = radius * MathL.sin(angle); particles.add(new PParticle(location.clone().add(dx, dy, dz))); } return particles; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleInvocation.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleInvocation.java index ebb1126..3a88bfe 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleInvocation.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleInvocation.java @@ -4,6 +4,7 @@ import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticleEffect; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -27,32 +28,32 @@ public class ParticleStyleInvocation extends DefaultParticleStyle { // Circle around everything, spawn less often if (this.circleStep % 5 == 0) { for (int i = 0; i < this.numSteps; i++) { - double dx = Math.cos(Math.PI * 2 * ((double) i / this.numSteps)) * this.radius; + double dx = MathL.cos(Math.PI * 2 * ((double) i / this.numSteps)) * this.radius; double dy = -0.9; - double dz = Math.sin(Math.PI * 2 * ((double) i / this.numSteps)) * this.radius; + double dz = MathL.sin(Math.PI * 2 * ((double) i / this.numSteps)) * this.radius; particles.add(new PParticle(location.clone().add(dx, dy, dz))); } } // Orbit going clockwise for (int i = 0; i < this.points; i++) { - double dx = Math.cos(this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; + double dx = MathL.cos(this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; double dy = -0.9; - double dz = Math.sin(this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; + double dz = MathL.sin(this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; double angle = Math.atan2(dz, dx); - double xAng = -Math.cos(angle); - double zAng = -Math.sin(angle); + double xAng = -MathL.cos(angle); + double zAng = -MathL.sin(angle); particles.add(new PParticle(location.clone().add(dx, dy, dz), xAng, 0, zAng, speed, true)); } // Orbit going counter-clockwise for (int i = 0; i > -this.points; i--) { - double dx = Math.cos(-this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; + double dx = MathL.cos(-this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; double dy = -0.9; - double dz = Math.sin(-this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; + double dz = MathL.sin(-this.step + (Math.PI * 2 * ((double) i / this.points))) * this.radius; double angle = Math.atan2(dz, dx); - double xAng = -Math.cos(angle); - double zAng = -Math.sin(angle); + double xAng = -MathL.cos(angle); + double zAng = -MathL.sin(angle); particles.add(new PParticle(location.clone().add(dx, dy, dz), xAng, 0, zAng, speed, true)); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleOrbit.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleOrbit.java index 442cad2..be66f56 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleOrbit.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleOrbit.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -21,8 +22,8 @@ public class ParticleStyleOrbit extends DefaultParticleStyle { public List getParticles(ParticlePair particle, Location location) { List particles = new ArrayList<>(); for (int i = 0; i < orbs; i++) { - double dx = -(Math.cos((this.step / (double) numSteps) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))); - double dz = -(Math.sin((this.step / (double) numSteps) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))); + double dx = -(MathL.cos((this.step / (double) numSteps) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))); + double dz = -(MathL.sin((this.step / (double) numSteps) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))); particles.add(new PParticle(location.clone().add(dx, 0, dz))); } return particles; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePopper.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePopper.java index 90a6ae8..8961d25 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePopper.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePopper.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -27,7 +28,7 @@ public class ParticleStylePopper extends DefaultParticleStyle { double radius = (1 - (double) this.step / this.maxStep); for (int i = 0; i < this.helices; i++) { double angle = this.step * this.radials + (2 * Math.PI * i / this.helices); - Vector v = new Vector(Math.cos(angle) * radius, this.step * this.grow - 1, Math.sin(angle) * radius); + Vector v = new Vector(MathL.cos(angle) * radius, this.step * this.grow - 1, MathL.sin(angle) * radius); particles.add(new PParticle(location.clone().add(v))); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePulse.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePulse.java index 7fd77a8..e7578b3 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePulse.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStylePulse.java @@ -4,6 +4,7 @@ import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticleEffect; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -26,12 +27,12 @@ public class ParticleStylePulse extends DefaultParticleStyle { if (this.step == 0) { for (int i = 0; i < this.points; i++) { - double dx = Math.cos(Math.PI * 2 * ((double) i / this.points)) * this.radius; + double dx = MathL.cos(Math.PI * 2 * ((double) i / this.points)) * this.radius; double dy = -0.9; - double dz = Math.sin(Math.PI * 2 * ((double) i / this.points)) * this.radius; + double dz = MathL.sin(Math.PI * 2 * ((double) i / this.points)) * this.radius; double angle = Math.atan2(dz, dx); - double xAng = Math.cos(angle); - double zAng = Math.sin(angle); + double xAng = MathL.cos(angle); + double zAng = MathL.sin(angle); particles.add(new PParticle(location.clone().add(dx, dy, dz), xAng, 0, zAng, speed, true)); } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleQuadhelix.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleQuadhelix.java index 8ffbaff..a6c0bcf 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleQuadhelix.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleQuadhelix.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -24,9 +25,9 @@ public class ParticleStyleQuadhelix extends DefaultParticleStyle { public List getParticles(ParticlePair particle, Location location) { List particles = new ArrayList<>(); for (int i = 0; i < orbs; i++) { - double dx = -(Math.cos((this.stepX / (double) maxStepX) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))) * ((maxStepY - Math.abs(this.stepY)) / (double) maxStepY); + double dx = -(MathL.cos((this.stepX / (double) maxStepX) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))) * ((maxStepY - Math.abs(this.stepY)) / (double) maxStepY); double dy = (this.stepY / (double) maxStepY) * 1.5; - double dz = -(Math.sin((this.stepX / (double) maxStepX) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))) * ((maxStepY - Math.abs(this.stepY)) / (double) maxStepY); + double dz = -(MathL.sin((this.stepX / (double) maxStepX) * (Math.PI * 2) + (((Math.PI * 2) / orbs) * i))) * ((maxStepY - Math.abs(this.stepY)) / (double) maxStepY); particles.add(new PParticle(location.clone().add(dx, dy, dz))); } return particles; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleRings.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleRings.java index e313f4a..4b22cce 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleRings.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleRings.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -23,10 +24,10 @@ public class ParticleStyleRings extends DefaultParticleStyle { double ring1 = Math.PI / (maxStep / 2D) * this.step; double ring2 = Math.PI / (maxStep / 2D) * (((this.step + maxStep / 2D) % maxStep)); - particles.add(new PParticle(location.clone().add(Math.cos(ring1), Math.sin(ring1), Math.sin(ring1)))); - particles.add(new PParticle(location.clone().add(Math.cos(ring1 + Math.PI), Math.sin(ring1), Math.sin(ring1 + Math.PI)))); - particles.add(new PParticle(location.clone().add(Math.cos(ring2), Math.sin(ring2), Math.sin(ring2)))); - particles.add(new PParticle(location.clone().add(Math.cos(ring2 + Math.PI), Math.sin(ring2), Math.sin(ring2 + Math.PI)))); + particles.add(new PParticle(location.clone().add(MathL.cos(ring1), MathL.sin(ring1), MathL.sin(ring1)))); + particles.add(new PParticle(location.clone().add(MathL.cos(ring1 + Math.PI), MathL.sin(ring1), MathL.sin(ring1 + Math.PI)))); + particles.add(new PParticle(location.clone().add(MathL.cos(ring2), MathL.sin(ring2), MathL.sin(ring2)))); + particles.add(new PParticle(location.clone().add(MathL.cos(ring2 + Math.PI), MathL.sin(ring2), MathL.sin(ring2 + Math.PI)))); return particles; } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSphere.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSphere.java index 31138a6..2446e13 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSphere.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSphere.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -24,9 +25,9 @@ public class ParticleStyleSphere extends DefaultParticleStyle { double v = Math.random(); double theta = 2 * Math.PI * u; double phi = Math.acos(2 * v - 1); - double dx = radius * Math.sin(phi) * Math.cos(theta); - double dy = radius * Math.sin(phi) * Math.sin(theta); - double dz = radius * Math.cos(phi); + double dx = radius * MathL.sin(phi) * MathL.cos(theta); + double dy = radius * MathL.sin(phi) * MathL.sin(theta); + double dz = radius * MathL.cos(phi); particles.add(new PParticle(location.clone().add(dx, dy, dz))); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpin.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpin.java index 36362f4..7094f4c 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpin.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpin.java @@ -3,27 +3,15 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.Collections; import java.util.List; import org.bukkit.Location; public class ParticleStyleSpin extends DefaultParticleStyle { - private static double[] cos, sin; private static final int maxSteps = 30; private int step = 0; - - static { - cos = new double[maxSteps]; - sin = new double[maxSteps]; - - int i = 0; - for (double n = 0; n < Math.PI * 2; n += Math.PI * 2 / maxSteps) { - cos[i] = Math.cos(n); - sin[i] = Math.sin(n); - i++; - } - } public ParticleStyleSpin() { super("spin", true, true, -0.5); @@ -31,10 +19,12 @@ public class ParticleStyleSpin extends DefaultParticleStyle { @Override public List getParticles(ParticlePair particle, Location location) { + double slice = (Math.PI * 2 / maxSteps) * this.step; + double radius = .5; - double newX = location.getX() + radius * cos[this.step]; + double newX = location.getX() + radius * MathL.cos(slice); double newY = location.getY() + 1.5; - double newZ = location.getZ() + radius * sin[this.step]; + double newZ = location.getZ() + radius * MathL.sin(slice); return Collections.singletonList(new PParticle(new Location(location.getWorld(), newX, newY, newZ))); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpiral.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpiral.java index 0f87f85..0260f58 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpiral.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleSpiral.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -19,9 +20,9 @@ public class ParticleStyleSpiral extends DefaultParticleStyle { public List getParticles(ParticlePair particle, Location location) { List particles = new ArrayList<>(); for (int stepY = -60; stepY < 60; stepY += 10) { - double dx = -(Math.cos(((this.stepX + stepY) / 90D) * Math.PI * 2)) * 0.8; + double dx = -(MathL.cos(((this.stepX + stepY) / 90D) * Math.PI * 2)) * 0.8; double dy = stepY / 45D; - double dz = -(Math.sin(((this.stepX + stepY) / 90D) * Math.PI * 2)) * 0.8; + double dz = -(MathL.sin(((this.stepX + stepY) / 90D) * Math.PI * 2)) * 0.8; particles.add(new PParticle(location.clone().add(dx, dy, dz))); } return particles; diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTwins.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTwins.java index e9a46d8..0689189 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTwins.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleTwins.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -23,8 +24,8 @@ public class ParticleStyleTwins extends DefaultParticleStyle { int i = 0; for (double n = 0; n < numSteps; n++) { - cos[i] = -Math.cos(n / numSteps * Math.PI * 2); - sin[i] = -Math.sin(n / numSteps * Math.PI * 2); + cos[i] = -MathL.cos(n / numSteps * Math.PI * 2); + sin[i] = -MathL.sin(n / numSteps * Math.PI * 2); i++; } } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleVortex.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleVortex.java index af3db2a..98d0bd2 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleVortex.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleVortex.java @@ -26,6 +26,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -50,7 +51,7 @@ public class ParticleStyleVortex extends DefaultParticleStyle { double radius = 2 * (1 - (double) this.step / this.maxStep); for (int i = 0; i < this.helices; i++) { double angle = this.step * this.radials + (2 * Math.PI * i / this.helices); - Vector v = new Vector(Math.cos(angle) * radius, this.step * this.grow - 1, Math.sin(angle) * radius); + Vector v = new Vector(MathL.cos(angle) * radius, this.step * this.grow - 1, MathL.sin(angle) * radius); particles.add(new PParticle(location.clone().add(v))); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirl.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirl.java index 1aa487d..ce38a19 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirl.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirl.java @@ -4,6 +4,7 @@ import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticleEffect; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -24,12 +25,12 @@ public class ParticleStyleWhirl extends DefaultParticleStyle { double speed = this.getSpeedByEffect(particle.getEffect()); for (int i = 0; i < this.points; i++) { - double dx = Math.cos(this.step + (Math.PI * 2 * ((double) i / this.points))); + double dx = MathL.cos(this.step + (Math.PI * 2 * ((double) i / this.points))); double dy = -0.9; - double dz = Math.sin(this.step + (Math.PI * 2 * ((double) i / this.points))); + double dz = MathL.sin(this.step + (Math.PI * 2 * ((double) i / this.points))); double angle = Math.atan2(dz, dx); - double xAng = Math.cos(angle); - double zAng = Math.sin(angle); + double xAng = MathL.cos(angle); + double zAng = MathL.sin(angle); particles.add(new PParticle(location.clone().add(0, dy, 0), xAng, 0, zAng, speed, true)); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirlwind.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirlwind.java index 6b43b75..315b768 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirlwind.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWhirlwind.java @@ -4,6 +4,7 @@ import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticleEffect; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import java.util.ArrayList; import java.util.List; import org.bukkit.Location; @@ -25,12 +26,12 @@ public class ParticleStyleWhirlwind extends DefaultParticleStyle { // Orbit going clockwise for (int i = 0; i < this.points; i++) { - double dx = Math.cos(this.step + (Math.PI * 2 * ((double) i / this.points))); + double dx = MathL.cos(this.step + (Math.PI * 2 * ((double) i / this.points))); double dy = -0.9; - double dz = Math.sin(this.step + (Math.PI * 2 * ((double) i / this.points))); + double dz = MathL.sin(this.step + (Math.PI * 2 * ((double) i / this.points))); double angle = Math.atan2(dz, dx); - double xAng = Math.cos(angle); - double zAng = Math.sin(angle); + double xAng = MathL.cos(angle); + double zAng = MathL.sin(angle); particles.add(new PParticle(location.clone().add(0, dy, 0), xAng, 0, zAng, speed, true)); } diff --git a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWings.java b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWings.java index 49e500f..50a5b6d 100644 --- a/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWings.java +++ b/src/main/java/dev/esophose/playerparticles/styles/ParticleStyleWings.java @@ -3,6 +3,7 @@ package dev.esophose.playerparticles.styles; import dev.esophose.playerparticles.config.CommentedFileConfiguration; import dev.esophose.playerparticles.particles.PParticle; import dev.esophose.playerparticles.particles.ParticlePair; +import dev.esophose.playerparticles.util.MathL; import dev.esophose.playerparticles.util.VectorUtils; import java.util.ArrayList; import java.util.List; @@ -22,9 +23,9 @@ public class ParticleStyleWings extends DefaultParticleStyle { List particles = new ArrayList<>(); if (this.spawnTimer == 0) { for (double t = 0; t < Math.PI * 2; t += Math.PI / 48) { - double offset = (Math.pow(Math.E, Math.cos(t)) - 2 * Math.cos(t * 4) - Math.pow(Math.sin(t / 12), 5)) / 2; - double x = Math.sin(t) * offset; - double y = Math.cos(t) * offset; + double offset = (Math.pow(Math.E, MathL.cos(t)) - 2 * MathL.cos(t * 4) - Math.pow(MathL.sin(t / 12), 5)) / 2; + double x = MathL.sin(t) * offset; + double y = MathL.cos(t) * offset; Vector v = VectorUtils.rotateAroundAxisY(new Vector(x, y, -0.3), -Math.toRadians(location.getYaw())); particles.add(new PParticle(location.clone().add(v.getX(), v.getY(), v.getZ()))); } diff --git a/src/main/java/dev/esophose/playerparticles/util/MathL.java b/src/main/java/dev/esophose/playerparticles/util/MathL.java new file mode 100644 index 0000000..169161d --- /dev/null +++ b/src/main/java/dev/esophose/playerparticles/util/MathL.java @@ -0,0 +1,46 @@ +package dev.esophose.playerparticles.util; + +/** + * Credit goes to Riven from this thread: + * http://www.java-gaming.org/topics/extremely-fast-sine-cosine/36469/view.html + */ +public final class MathL { + + private static final int SIN_BITS, SIN_MASK, SIN_COUNT; + private static final float radFull, radToIndex; + private static final float degFull, degToIndex; + private static final float[] sin, cos; + + static { + SIN_BITS = 12; + SIN_MASK = ~(-1 << SIN_BITS); + SIN_COUNT = SIN_MASK + 1; + + radFull = (float) (Math.PI * 2.0); + degFull = (float) (360.0); + radToIndex = SIN_COUNT / radFull; + degToIndex = SIN_COUNT / degFull; + + sin = new float[SIN_COUNT]; + cos = new float[SIN_COUNT]; + + for (int i = 0; i < SIN_COUNT; i++) { + sin[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * radFull); + cos[i] = (float) Math.cos((i + 0.5f) / SIN_COUNT * radFull); + } + + for (int i = 0; i < 360; i += 90) { + sin[(int) (i * degToIndex) & SIN_MASK] = (float) Math.sin(i * Math.PI / 180.0); + cos[(int) (i * degToIndex) & SIN_MASK] = (float) Math.cos(i * Math.PI / 180.0); + } + } + + public static float sin(double rad) { + return sin[(int) (rad * radToIndex) & SIN_MASK]; + } + + public static float cos(double rad) { + return cos[(int) (rad * radToIndex) & SIN_MASK]; + } + +} diff --git a/src/main/java/dev/esophose/playerparticles/util/VectorUtils.java b/src/main/java/dev/esophose/playerparticles/util/VectorUtils.java index 7b48d7f..7db58ff 100644 --- a/src/main/java/dev/esophose/playerparticles/util/VectorUtils.java +++ b/src/main/java/dev/esophose/playerparticles/util/VectorUtils.java @@ -44,8 +44,8 @@ public final class VectorUtils { */ public static Vector rotateAroundAxisX(Vector v, double angle) { double y, z, cos, sin; - cos = Math.cos(angle); - sin = Math.sin(angle); + cos = MathL.cos(angle); + sin = MathL.sin(angle); y = v.getY() * cos - v.getZ() * sin; z = v.getY() * sin + v.getZ() * cos; return v.setY(y).setZ(z); @@ -60,8 +60,8 @@ public final class VectorUtils { */ public static Vector rotateAroundAxisY(Vector v, double angle) { double x, z, cos, sin; - cos = Math.cos(angle); - sin = Math.sin(angle); + cos = MathL.cos(angle); + sin = MathL.sin(angle); x = v.getX() * cos + v.getZ() * sin; z = v.getX() * -sin + v.getZ() * cos; return v.setX(x).setZ(z); @@ -76,8 +76,8 @@ public final class VectorUtils { */ public static Vector rotateAroundAxisZ(Vector v, double angle) { double x, y, cos, sin; - cos = Math.cos(angle); - sin = Math.sin(angle); + cos = MathL.cos(angle); + sin = MathL.sin(angle); x = v.getX() * cos - v.getY() * sin; y = v.getX() * sin + v.getY() * cos; return v.setX(x).setY(y); @@ -124,10 +124,10 @@ public final class VectorUtils { double yaw = Math.toRadians(-1 * (yawDegrees + 90)); double pitch = Math.toRadians(-pitchDegrees); - double cosYaw = Math.cos(yaw); - double cosPitch = Math.cos(pitch); - double sinYaw = Math.sin(yaw); - double sinPitch = Math.sin(pitch); + double cosYaw = MathL.cos(yaw); + double cosPitch = MathL.cos(pitch); + double sinYaw = MathL.sin(yaw); + double sinPitch = MathL.sin(pitch); double initialX, initialY, initialZ; double x, y, z;