Solved Circular particle beams help in 1.15.x

Discussion in 'Spigot Plugin Development' started by ZizzyZizzy, Feb 14, 2020.

  1. So there are a bunch of topics around here on this subject. I've seriously tried every single one of them and I still cannot get this to work properly in 1.15.x.

    The problem with the below code is that the circles in the beam sometimes flatten out and sometimes they are a perfect circle, depending on the pitch and yaw. Supposedly the rotateAroundAxisX and rotateAroundAxisY should fix this, but it does not. Clearly I'm missing something here.

    Code (Text):
     public static void circleBeamTest(Player p){
            int amount = 20;
            float radius = 1.5F;
            Location center = p.getEyeLocation();
            World world = center.getWorld();
            final Vector dir = p.getLocation().getDirection().normalize().multiply(0.5);
            final double pitch = center.getPitch() +90.0F;
            final double yaw = -center.getYaw();
            double increment = (2 * Math.PI) / amount;
            for(int duration = 0; duration < 40; duration++) {
                for (int i = 0; i < amount; i++) {
                    double angle = i * increment;
                    double x = radius * Math.cos(angle);
                    double z = radius * Math.sin(angle);
                    Vector vec = new Vector(x, 0, z);
                    VectorUtils.rotateAroundAxisX(vec, pitch);
                    VectorUtils.rotateAroundAxisY(vec, yaw);
                    center.add(vec);
                    world.spawnParticle(Particle.REDSTONE,center,1,new Particle.DustOptions(Color.fromRGB(0, 101, 255),0.5F));
                    center.subtract(vec);
                }
                center.add(dir);
            }
        }
    ..and the VectorUtils functions:

    Code (Text):
        public static final Vector rotateAroundAxisX(Vector v, double angle) {
            double cos = Math.cos(angle);
            double sin = Math.sin(angle);
            double y = v.getY() * cos - v.getZ() * sin;
            double z = v.getY() * sin + v.getZ() * cos;
            return v.setY(y).setZ(z);
        }

        public static final Vector rotateAroundAxisY(Vector v, double angle) {
            double cos = Math.cos(angle);
            double sin = Math.sin(angle);
            double x = v.getX() * cos + v.getZ() * sin;
            double z = v.getX() * -sin + v.getZ() * cos;
            return v.setX(x).setZ(z);
        }
     
    I've had a similarly frustrating experience with yaw calculations. Something to do with the numbers changing depending on the turning direction. (Is it 0-360 this time? Maybe, maybe not! Schrodinger would be proud.)
     
    #1 ZizzyZizzy, Feb 14, 2020
    Last edited: Feb 14, 2020
  2. Maybe you will find out if the cat is alive or dead by this:
    yaw is defined from -180° to +180°
    pitch is defined from -90° to 90°
    If both yaw and pitch are 0°, that represents the player staring exactly into the positive z-direction. (I would have defined this to be x-Axis, but that's just how way it is)

    Math#sin(double) resp. Math#cos(double) wants an angle in radians, not degrees. While degrees are normally defined to go from 0-360, radians go from 0-2*pi in one period of the sin/cos-wave. Therefore, you'll have to first convert the degrees to a radians-value. You can do this for example by calling Math#toRadians(degrees)

    Now, let's dive into what 'rotate around x'/'rotate around y' actually does. This will, unfortunately, include somewhat advanced math.
    Suppose you have a 2-Dimensional Vector v = (x,y)' in the standard cartesian-coordinate system. You now want to rotate that vector w.r.t. the mathematical positive direction of rotation and a certain angle alpha. Let's take the very simple example; v = (1,0)', rotated by 90°, resp. pi/2. This would mean that the resulting vector is v(rot) = (0,1)'. If you plug in cos(pi/2) * 1, you'll get 0. If you plug in sin(pi/2), you'll get 1.
    It can be shown, that the cos/sin computes the correct angle for any applicable vector. In general, what you did when you defined your rotation-methods was to apply this matrix:

    [cos(a) sin(a) ]
    [-sin(a) cos(a)]

    This matrix is called a rotation matrix and it rotates a Vector a certain amount of degrees to the mathematical positive direction of rotation. The only thing that you missed here is thus the conversion of degrees to radians, which is probably what solves your problem.
     
  3. Bahh, I SWEAR I tried that already and it completely flattened my circles. I must have been messing with it in one of the other 5 functions I created trying to figure this out.

    It is working now, thanks! Just have to put it in a runnable and I'm set.

    Code (Text):

            final double pitch = (center.getPitch() +90.0F) * 0.017453292F;
            final double yaw = -center.getYaw() * 0.017453292F;