1.15.2 Need help getting a couple random locations in a circle around a location

Discussion in 'Spigot Plugin Development' started by VivianMusic, Mar 18, 2020.

  1. How would you get a couple (say 5) random locations around a set location? I am currently trying to use

    Code (Text):
        public static Location getRandomLocation(Location origin, double radius) {
            Random r = new Random();

            Double randomRadius = r.nextDouble() * radius;
            Double theta =  Math.toRadians(r.nextDouble() * 360);
            Double phi = Math.toRadians(r.nextDouble() * 180 - 90);

            double x = randomRadius * Math.cos(theta) * Math.sin(phi);
            double y = origin.getY();
            double z = randomRadius * Math.cos(phi);

            Location newLoc = origin.add(x, 0, z);

            return newLoc;
        }
    and this is getting a random location, but it's not in a circle around the origin
     
  2. Alright, I am doing this
    Code (Text):
    ClassUtils.spawnSkeletonRiser(ClassUtils.getLocationInCircle(playerLoc, 2));
    Code (Text):
        public static Location getLocationInCircle(Location origin, Integer radius) {
            double angle = random() * 360;
            return origin.add(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
        }

        public static void spawnSkeletonRiser(Location location) {
            ArmorStand armorStand = (ArmorStand) Bukkit.getServer().getWorld(location.getWorld().getName()).spawnEntity(location, EntityType.ARMOR_STAND);
            ItemStack skeletonHead = new ItemStack(Material.SKELETON_SKULL);
            armorStand.setHelmet(skeletonHead);
        }
    which works good, if I spawn them one by one:
    [​IMG]

    But if I try to spawn multiple, it does this:
    [​IMG]
     
  3. Show both code. Code for spawning one by one and code for spawning multiple
     
  4. Code (Text):
    public void teleportEntites(Location center, float radius, List<Entity> entites) {
        int i = 0;
        for(Entity entity : entites) {
            ++i;
            double angle 2 * Math.PI / list.size() * i;
            entity.teleport(center.clone().add(Math.cos(angle) * radius, 0.0, Math.sin(angle) * radius));
        }
    }
    that may help u.
     
  5. Code (Text):
        public static Location getLocationInCircle(Location origin, Integer radius) {
            double angle = random() * 360;
            return origin.add(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
        }

        public static void spawnSkeletonRiser(Location location) {
            ArmorStand armorStand = (ArmorStand) Bukkit.getServer().getWorld(location.getWorld().getName()).spawnEntity(location, EntityType.ARMOR_STAND);
            armorStand.setSmall(true);
            ItemStack skeletonHead = new ItemStack(Material.SKELETON_SKULL);
            armorStand.setHelmet(skeletonHead);

            double maxLength = 8;
            for(double d = 0; d <= maxLength; d += 0.1)
            {
                location.add(location.getDirection().multiply(2));
                location.getWorld().spawnParticle(Particle.CRIT, location, 10);
            }
        }
    This is my code for spawning them and such.
    I call it here:
    Code (Text):
    ClassUtils.spawnSkeletonRiser(ClassUtils.getLocationInCircle(playerLoc.clone(), 2));
    When I try to spawn multiple at once, I just put them in a "for(i=0)..." loop and they mess up. Ideally, I want them to pick 3 random points in a 360 circle, but evenly spaced out.

    Also, thanks @Olay307 for that but I'm not really wanting to teleport them after the fact. What I'm working on is I want a random number of ArmorStands to spawn around me (evenly spaced out, and half a block underground), then have them slowly rise up, then once they get to ground level, the armorstands delete and spawn skeletons in their places. I can do the rest, but I'm just trying to get them to spawn evenly spaced out
     
  6. x is x, y is z. That's all.
    So, you don't want to have the situation where two coordinates are close to each other?
     
  7. Correct