Solved Rotating particle circle

Discussion in 'Spigot Plugin Development' started by craterhater, May 24, 2017.

  1. EDIT: The real question here is: 'How do I create a vertical circle that I can rotate according to yaw and pitch'

    Hey there, This code:
    Code (Text):
    public void tick(){
            if(main.getConfig().get("Rings")!=null){
                for(String l : main.getConfig().getStringList("Rings")){
                    String[] parts = l.split(",");
                    Location loc = new Location(Bukkit.getWorld(parts[0]),Integer.parseInt(parts[1]),Integer.parseInt(parts[2]),Integer.parseInt(parts[3]));
                 
                    Location location1 = loc;
                    Location location2 = loc;
                    Location location3 = loc;
                 
                    int particles = 50;
                    float radius = 0.7f;
                    for (int i = 0; i < particles; i++) {
                        double angle, x, z;
                        angle = 2 * Math.PI * i / particles;
                        x = Math.cos(angle) * radius;
                        z = Math.sin(angle) * radius;
                        location1.add(0, x, z);
                        location2.add(0, x, z);
                        location3.add(0, x, z);
                     
                        for(Player p : Bukkit.getOnlinePlayers()){
                             p.spigot().playEffect(location1,Effect.FIREWORKS_SPARK, 1, 0, 0.2f, 0.2f,0.2f, 0, 20, 10);
                             p.spigot().playEffect(location2,Effect.FIREWORKS_SPARK, 1, 0, 0.2f, 0.2f,0.2f, 0, 20, 10);
                             p.spigot().playEffect(location3,Effect.FIREWORKS_SPARK, 1, 0, 0.2f, 0.2f,0.2f, 0, 20, 10);
                        }
                     
                        location1.subtract(0, x, z);
                        location2.subtract(0, x, z);
                        location3.subtract(0, x, z);
                    }
                 
                }
            }
        }
    Generates a vertical circle, I was just wondering how I could make it face a certain direction.

    I got the direction as a pitch and a yaw, how would I intergrate them?

    Thanks
     
    #1 craterhater, May 24, 2017
    Last edited: May 24, 2017
  2. If you want to rotate them There are multiple methods and @finnbon resource already did a really great tutorial on it anyway if you need help ask ;)
     
  3. Sorry but, what are you doing? You have 3 indentical location objects, to do what? Just use a single location.
     
  4. Alright, let's rephrase this then, how do I create a vertical circle that I can rotate according to yaw and pitch?
     
  5. You mean like a helix?
     
  6. No like a circle where you can fly through
     
  7. Oh a circle like with the fire thing from races?
     
  8. Yea
     
  9.  
  10. you need it to rotate from both yaw and pitch?

    just to understand the circle should rotate, so that the player which is related to the circle always see a perfect circle, never a line or an ellipse?
     
    • Creative Creative x 1
  11. The tutorial that someone else already suggested literally explains how to do that. The effect only keeps spinning if you make it so.

    Anyway, there's a different way using less resource heavy operations. Here's how you can achieve what you want.

    First, you need a for loop to create the circle, which you already have, that's good. Then you use that to calculate the current angle. Also good, keep that part, we'll need it. ;)
    After that, we're gonna change some stuff. First, you need the direction you want the circle to point in as a vector. I'm not talking about what side the flat side faces too, I'm talking about what side the side of the circle faces too. So if you want the flat side to face towards the player, you'll have to rotate 90 degrees.

    Then get that direction, clone it (so we don't modify the original), and multiply it by the radius multiplied by the cosine of the current angle. This might sound a bit confusing but don't worry, it should make sense if you think a little longer and see the code
    Code (Java):

        Vector offset = yourDirection.clone().multiply(radius * Math.cos(angle));
     
    As you can read in the first tutorial in my signature, cosine returns the X-difference between the middle of a circle and a point on the circumference at the specified angle. We'll use this to determine how 'far out' our current particle will be played.

    Now, we're gonna add a Y-value to that vector. As you can also read in the before mentioned tutorial, sine returns the Y-difference between the middle of a circle and a point on the circumference at a specified angle. We'll use this to determine how high the particle has to be played.
    Code (Java):

        offset.setY(Math.sin(angle) * radius);
     
    Now the 'offset' vector contains the values we gotta add to the center of the effect to display the particle. So go ahead and do that!
    Add the vector 'offset' to your center location (where the circle should be displayed).
    Play a particle at that location.
    Subtract the vector 'offset' from your center location to move it back to it's original position.

    I hope this made sense, and if some parts didn't, don't hesitate to ask for clarification. I'll leave a full version down below, al though I don't recommend copy pasting it, but try to understand what I wrote. Not only is it better for you, but I also hate seeing what I write go to waste, and how everyone just blindly copy pastes what I put as result. Thanks

    Code (Java):

        Vector offset = yourDirection.clone().multiply(Math.cos(angle) * radius);
        offset.setY(Math.sin(angle) * radius);
        yourLocation.add(offset);
        // play particle at yourLocation
        yourLocation.subtract(offset);
     
     
    • Informative Informative x 2
  12. EDIT: Solved it!

    Thank you so much for writing this, I am struggling to understand where to integrate it though. I currently do this and it doesn't really work:
    Code (Text):
    for(String l : main.getConfig().getStringList("Rings")){
                    String[] parts = l.split(",");
                    Location loc = new Location(Bukkit.getWorld(parts[0]),Integer.parseInt(parts[1]),Integer.parseInt(parts[2]),Integer.parseInt(parts[3]));
                 
                    loc.setYaw(Float.parseFloat(parts[4]));
                    loc.setPitch(Float.parseFloat(parts[5]));
                 
                    int particles = 50;
                    float radius = 0.7f;
                 
                    for (int i = 0; i < particles; i++) {
                        double angle;
                        angle = 2 * Math.PI * i / particles;
                        Vector offset = loc.getDirection().clone().multiply(Math.cos(angle) * radius);
                        offset.setY(Math.sin(angle) * radius);
                        loc.add(offset);
                     
                        for(Player p : Bukkit.getOnlinePlayers()){
                             p.spigot().playEffect(loc,Effect.FIREWORKS_SPARK, 1, 0, 0.2f, 0.2f,0.2f, 0, 20, 10);
                        }
                     
                        loc.subtract(offset);
                    }
                 
                }
    It is probably something terribly obvious but I am an absolute moron when it comes to Maths.
     
    #14 craterhater, May 24, 2017
    Last edited: May 24, 2017