Get angle between two positions

Discussion in 'Plugin Development' started by PoligamerYT, Sep 9, 2022.

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

    PoligamerYT

    Hello, I am making a plugin for electricity and I want to make realistic cables so I use an armor stand that has a chain in the head. I want that the head rotates at a specific angle between that two positions so the heads can form and straight line.

    This is the code I have for now.

    Code:
        public static void SpawnCable(Location from, Location to, Double space, Player p, ItemStack item)
        {
            from.setPitch(0);
            from.setYaw(0);
    
            to.setPitch(0);
            to.setYaw(0);
    
            World world = p.getWorld();
    
            double distance = from.distance(to);
    
            Vector p1 = from.toVector();
            Vector p2 = to.toVector();
    
            Vector vector = p2.clone().subtract(p1).normalize().multiply(space);
    
            double covered = 0;
    
            for (; covered < distance; p1.add(vector))
            {
                ArmorStand armorStand = (ArmorStand) world.spawnEntity(p1.toLocation(p.getWorld()), EntityType.ARMOR_STAND);
    
                armorStand.addEquipmentLock(EquipmentSlot.HEAD, ArmorStand.LockType.ADDING_OR_CHANGING);
                armorStand.addEquipmentLock(EquipmentSlot.CHEST, ArmorStand.LockType.ADDING_OR_CHANGING);
                armorStand.addEquipmentLock(EquipmentSlot.LEGS, ArmorStand.LockType.ADDING_OR_CHANGING);
                armorStand.addEquipmentLock(EquipmentSlot.FEET, ArmorStand.LockType.ADDING_OR_CHANGING);
                armorStand.addEquipmentLock(EquipmentSlot.HAND, ArmorStand.LockType.ADDING_OR_CHANGING);
                armorStand.addEquipmentLock(EquipmentSlot.OFF_HAND, ArmorStand.LockType.ADDING_OR_CHANGING);
    
                if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Red"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.RED);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Black"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.BLACK);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Green"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.GREEN);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Blue"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.BLUE);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Pink"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.PINK);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Purple"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.PURPLE);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Yellow"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.YELLOW);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-Brown"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.BROWN);
                }
                else if(item.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(BetterMC.plugin, "Color-White"), PersistentDataType.STRING))
                {
                    armorStand.setHelmet(CableColors.WHITE);
                }
    
                armorStand.setVisible(false);
                armorStand.setGravity(false);
                armorStand.setInvulnerable(true);
                armorStand.setSmall(true);
                armorStand.setBasePlate(false);
    
                Location armorstandLocation = armorStand.getLocation();
    
                Vector playerVec = to.toVector();
                Vector armorstandVec = armorstandLocation.toVector();
    
                Vector facingVector = playerVec.subtract(armorstandVec).normalize();
    
                armorstandLocation.setDirection(facingVector);
                armorStand.teleport(armorstandLocation);
    
                Vector inBetween = to.clone().subtract(armorstandLocation).toVector();
                Vector lookVec = armorstandLocation.getDirection();
    
                double angleDir = (Math.atan2(inBetween.getZ(),inBetween.getX()) / 2 / Math.PI * 360 + 360) % 360;
                double angleLook = (Math.atan2(lookVec.getZ(),lookVec.getX()) / 2 / Math.PI * 360 + 360) % 360;
    
                double angle = (angleDir - angleLook + 360) % 360;
    
                armorStand.setHeadPose(new EulerAngle(angle, 0, 0));
    
                covered += space;
            }
        }
    If you need more info said me!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @PoligamerYT And what is the issue that you are having then?
     
  3. Offline

    PoligamerYT

    The problem is that the system of rotation doesn't work.
     
Thread Status:
Not open for further replies.

Share This Page