Vector Rotation

Discussion in 'Plugin Development' started by Herowise, May 13, 2016.

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

    Herowise

    So I never tried this but not until recently I just realized that I could do something cool like Rotating a Particle movement such as a Butterfly moving as I am changing Direction (Eye Pitch and Yaw). I already Have a vector class for it but can't use it that efficiently as I only use it for defining a vector.


    Code:
    package me.hero.particlesenum;
    
    import net.minecraft.server.v1_8_R1.EnumParticle;
    import net.minecraft.server.v1_8_R1.PacketPlayOutWorldParticles;
    
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    
                if(cmd.getName().equalsIgnoreCase("herowise")){
                    Player player= (Player) sender;
               
                     flameEffect(player);
                }
           
            return false;
            }
        private void flameEffect(Player player) {
        
                        Location l = player.getLocation();
       
                                for (double theta = 0.0; theta < 72; theta += 0.01) {
                                    double y = Math.exp(Math.cos(theta)) - 2*Math.cos(4*theta) + Math.pow(Math.sin(theta/12), 5);
                                    double z = y * Math.cos(theta);
                                    double x = y * Math.sin(theta);
        
                     
                                PacketPlayOutWorldParticles packetParticle = new PacketPlayOutWorldParticles(EnumParticle.FLAME, true,
    
                                                (float) (l.getX() + x),
                                                ((float) (l.getY() + y)),
                                                (float) (l.getZ() + z),
                                                0, 0, 0, 1, 0);
        
                                ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetParticle);
                            }                       
                       
                        }
                }
    
    So this is a butterfly i have created and this is the vectors class I wanna use to make it rotate.

    Code:
    package me.hero.particlesenum;
    import org.bukkit.Location;
    import org.bukkit.util.Vector;
    
    public final class VectorUtils {
    
        private VectorUtils() {
        }
    
        public static final Vector rotateAroundAxisX(Vector v, double angle) {
            double y, z, cos, sin;
            cos = Math.cos(angle);
            sin = Math.sin(angle);
            y = v.getY() * cos - v.getZ() * sin;
            z = v.getY() * sin + v.getZ() * cos;
            return v.setY(y).setZ(z);
        }
    
        public static final Vector rotateAroundAxisY(Vector v, double angle) {
            double x, z, cos, sin;
            cos = Math.cos(angle);
            sin = Math.sin(angle);
            x = v.getX() * cos + v.getZ() * sin;
            z = v.getX() * -sin + v.getZ() * cos;
            return v.setX(x).setZ(z);
        }
    
        public static final Vector rotateAroundAxisZ(Vector v, double angle) {
            double x, y, cos, sin;
            cos = Math.cos(angle);
            sin = Math.sin(angle);
            x = v.getX() * cos - v.getY() * sin;
            y = v.getX() * sin + v.getY() * cos;
            return v.setX(x).setY(y);
        }
    
        public static final Vector rotateVector(Vector v, double angleX, double angleY, double angleZ) {
            // double x = v.getX(), y = v.getY(), z = v.getZ();
            // double cosX = Math.cos(angleX), sinX = Math.sin(angleX), cosY =
            // Math.cos(angleY), sinY = Math.sin(angleY), cosZ = Math.cos(angleZ),
            // sinZ = Math.sin(angleZ);
            // double nx, ny, nz;
            // nx = (x * cosY + z * sinY) * (x * cosZ - y * sinZ);
            // ny = (y * cosX - z * sinX) * (x * sinZ + y * cosZ);
            // nz = (y * sinX + z * cosX) * (-x * sinY + z * cosY);
            // return v.setX(nx).setY(ny).setZ(nz);
            // Having some strange behavior up there.. Have to look in it later. TODO
            rotateAroundAxisX(v, angleX);
            rotateAroundAxisY(v, angleY);
            rotateAroundAxisZ(v, angleZ);
            return v;
        }
    
        /**
         * Rotate a vector about a location using that location's direction
         *
         * @param v
         * @param location
         * @return
         */
        public static final Vector rotateVector(Vector v, Location location) {
            return rotateVector(v, location.getYaw(), location.getPitch());
        }
    
        /**
         * This handles non-unit vectors, with yaw and pitch instead of X,Y,Z angles.
         *
         * Thanks to SexyToad!
         *
         * @param v
         * @param yawDegrees
         * @param pitchDegrees
         * @return
         */
        public static final Vector rotateVector(Vector v, float yawDegrees, float pitchDegrees) {
            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 initialX, initialY, initialZ;
            double x, y, z;
    
            // Z_Axis rotation (Pitch)
            initialX = v.getX();
            initialY = v.getY();
            x = initialX * cosPitch - initialY * sinPitch;
            y = initialX * sinPitch + initialY * cosPitch;
    
            // Y_Axis rotation (Yaw)
            initialZ = v.getZ();
            initialX = x;
            z = initialZ * cosYaw - initialX * sinYaw;
            x = initialZ * sinYaw + initialX * cosYaw;
    
            return new Vector(x, y, z);
        }
    
        public static final double angleToXAxis(Vector vector) {
            return Math.atan2(vector.getX(), vector.getY());
        }
    }
    
    So basically how do I do it? Vectors arent my thing tbh ;-;
     
  2. Offline

    Herowise

    bumpppp
     
  3. Offline

    mythbusterma

    @Herowise

    The question isn't clear at all. What are you asking?
     
  4. Offline

    Betagear

    Well, you'll need to do this in 2 or 3 steps, depending on how much axises you want to rotate on.
    First, convert your angle in radians
    Use math.sin for the first value, which will be 1 at 0° and 360°
    Use math.cos for the second value, which will be 1 at 90° and 270°

    And repeat that as much time as axises you want to rotate on.
    By example, if you want to rotate around X, the first value would be Y and the second value would be Z.
    Around Y : X and Z
    Around Z : Y and X
    But keep a vector for your rotation and your local velocity, so that way it's simpler when you reset your rotation.


    Note that these formulas works regardless of the multiple of 360 you're using. You could use -90° as well as 400°.
     
  5. Offline

    ski23

    if your question still isnt answered, can you post a picture or something describing further of what you mean. I believe I may have something that may help you but I'm not sure if I'm understanding what you would like to do entirely
     
  6. Offline

    Herowise

    Due to some inconvenience i was't doing anything Vector related for very long so now I wanna start it. Sorry For bringing an Old thread back I am rlly so

    @ski23

    Think u have a Butterfly done with a parametric or polar coords
    Now as u face different directions
    The Axis Changes With The players Yaw and pitch
     
  7. Offline

    Herowise

    bumppp ;-;
     
Thread Status:
Not open for further replies.

Share This Page