Solved Sphere with particles

Discussion in 'Plugin Development' started by Panda_Crafter, Jul 22, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Panda_Crafter

    Hello, I am trying to create a sphere with particles but every found thread about this is kinda useless.

    I have made a frame of the sphere but as you see in the image, it is just a cross, if possible i want to rotate 1 of the 2 sides to create a full sphere.

    [​IMG]

    Code:
               int r = 2;
               
               for (double y = 7; y >= 0; y -= 0.005) {
                   double x = r * Math.cos(y);
                   double z = r * Math.sin(y);
             
                   Location loc2 = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + 1, loc.getZ() + z);
                   loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
               }
               
               for (double y = 7; y >= 0; y -= 0.005) {
                   double x = r * Math.cos(y);
    //               double z = -(0.r * Math.sin(y));
                  double y2 = r * Math.sin(y);
             
                   Location loc2 = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + y2 + 1, loc.getZ());
                   loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
               }
               
               for (double y = 7; y >= 0; y -= 0.005) {
    //               double x = -(0.r * Math.cos(y));
                   double z = r * Math.sin(y);
                  double y2 = r * Math.cos(y);
             
                   Location loc2 = new Location(loc.getWorld(), loc.getX(), loc.getY() + y2 + 1, loc.getZ() + z);
                   loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
               }
               
               for (double y = 7; y >= 0; y -= 0.005) {
                   double x = r * Math.cos(y);
    //               double z = -(0.r * Math.sin(y));
                  double y2 = r * Math.sin(y);
             
                   Location loc2 = new Location(loc.getWorld(), loc.getX() - x, loc.getY() - y2 + 1, loc.getZ());
                   loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
               }
    
               for (double y = 7; y >= 0; y -= 0.005) {
    //               double x = Math.sin(i/y);
    //               double x2 = i * Math.cos(y);
                   double z = r * Math.sin(y);
                  double y2 = r * Math.cos(y);
             
                   Location loc2 = new Location(loc.getWorld(), loc.getX(), loc.getY() - y2 + 1, loc.getZ() - z);
                   loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
               }
    
     
  2. Offline

    Zombie_Striker

    @Panda_Crafter
    This is overthinking it.
    1. Create a location which will be the center of the sphere.
    2. Create three for int loops. This will loop through the area you want to create the sphere in.
    3. If the distance between the center and the location is equal to the radius, spawn the particle.
     
  3. Offline

    Panda_Crafter

    This will only results in 6 dots.

    [​IMG]

    Code:
                      double r = 2;
                    
                      for(double x = -r; x <= r; x++) {
                          for(double y = -r; y <= r; y++) {
                              for(double z = -r; z <= r; z++) {
                                      double dist = Math.sqrt(x*x + y*y + z*z);
                                    
                                      if(dist == r){
                                        Location loc2 = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + 1, loc.getZ() + z);
                                        loc.getWorld().playEffect(loc2, Effect.COLOURED_DUST, 1);
                                      }
                              }
                          }
                      }
    
    EDIT:

    I a friend gave this to me and it worked.
    https://www.spigotmc.org/threads/tutorial-creating-a-sphere.146338/
     
    Last edited: Jul 22, 2016
  4. Offline

    ChipDev

    I've been waiting for someone to ask for this. Since you seem already experienced with the equations, how it works, etc... I'll show you the way I make spheres using Cartesian coordinates:
    Code:
    x = cos(i) * sin(j)
    y = cos(i) * cos(j)
    z = sin(i)
    which i = -π/2 ... π/2
    and j = 0 ... 2π
    Also, you have to switch around the coordinates to match minecraft coordinates. But is is a sphere, so you shouldn't have many problems.
    EDIT: Well, you seem to have gotten it. If you need any more help, ask me!
     
  5. Offline

    Herowise

    Ok ok So Here we are, I was planning on making a tutorial of it in the resource page but for lack of time i couldnt do it.
    So To do this we will use Particle Effect class n Reflection utils class by DarkBlade12
    but ofc NMS packets or other ways of spawning particles can also be used:

    So the code is here:

    Code:
    private void sphere(final Player player){
    
              
            
             final Location loc = player.getLocation();
              new BukkitRunnable(){
                  double p = 0;
                public void run(){
                p+= Math.PI /10;
                for(double t = 0; t<=360; t+=Math.PI/40){
                   double r =1.5;
                    double x = r*cos(t)*sin(p);
                    double y = r*cos(p)+1.5;
                    double z = r*sin(t)*sin(p);
                    loc.add(x,y,z);
                    ParticleEffect.FIREWORKS_SPARK.display(0,0,0,0,1,loc, 100D);
                    loc.subtract(x, y, z);
                }     
             if(p > 2*Math.PI){
                 this.cancel();                
                }
                }
                          
              }.runTaskTimer(this,0,1);
            }
    PS: If you dun like da Flashiness ;p remove the Bukkit runnable
    This is basicall what @ChipDev was saying, but I just added the Flash xd
     
  6. Offline

    Panda_Crafter


    Is there is a Particle Effect class n Reflection Utils class for 1.10.2? I tried to use the one from DarkBlade12 but this is my error and it says that the current bukkit version is not supported.

    Code:
    [13:41:06 ERROR]: Could not pass event AsyncPlayerChatEvent to NonsenseCraft v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.chat(PlayerConnection.
    java:1273) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.jav
    a:1211) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat$1.run(PacketPlayInChat
    .java:39) [spigot.jar:git-Spigot-9797151-301db84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_66]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: me.pandacrafter1.nonsensecraft.effects.ParticleEffects$ParticlePacket
    $VersionIncompatibleException: Your current bukkit version seems to be incompati
    ble with this library
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects$ParticlePacket
    .initialize(ParticleEffects.java:1418) ~[?:?]
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects$ParticlePacket
    .<init>(ParticleEffects.java:1349) ~[?:?]
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects.display(Partic
    leEffects.java:575) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.runHelix(Cha
    tRecieve.java:288) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:274) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_10_R1.Packe
    t63WorldParticles
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:91) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:86) ~[spigot.jar:git-Spigot-9797151-301db84]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_66]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_66]
            at java.lang.Class.forName0(Native Method) ~[?:1.8.0_66]
            at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_66]
    
    tClass(ReflectionUtils.java:414) ~[?:?]
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects$ParticlePacket
    .initialize(ParticleEffects.java:1412) ~[?:?]
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects$ParticlePacket
    .<init>(ParticleEffects.java:1349) ~[?:?]
            at me.pandacrafter1.nonsensecraft.effects.ParticleEffects.display(Partic
    leEffects.java:575) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.runHelix(Cha
    tRecieve.java:288) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:274) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    
     
  7. Offline

    Herowise

    I never tried but i am sure it works for 1.9 And the the packets are messed up thats wat it states in the log xd
     
  8. Offline

    Esophose

    You need to add in the three fixes for it to work in 1.10. They are found on the last page of comments near the bottom on the library thread.
     
  9. Offline

    Panda_Crafter

    Thanks i found it!
     
Thread Status:
Not open for further replies.

Share This Page