Get random location in cuboid

Discussion in 'Plugin Development' started by Vincent1468, Sep 3, 2013.

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

    Vincent1468

    Hello,

    If I have a cuboid, so two locations at each side, how would I get a random location in that cuboid?

    Thanks.
     
  2. Offline

    metalhedd

    It helps to think of the cuboid differently... it's not 2 locations per side. its 2 locations total. a minimum point, and a maximum point. when you look at it this way, your problem is to simply find 3 random numbers within 3 clearly defined ranges.

    int deltaX = maxX - minX;
    int deltaY = maxY - minY;
    int deltaZ = maxZ - minZ;
    int rx = random.nextInt(deltaX) + minX;
    int ry = random.nextInt(deltaY) + minY;
    int rz = random.nextInt(deltaZ) + minZ;
     
    Vincent1468 likes this.
  3. Offline

    FurmigaHumana

    I'm not sure if this will work, but give it a try:

    Code:java
    1.  
    2. Location start... // the first point
    3. Location end... // the second point
    4.  
    5. public Location getRandomLocation() {
    6.  
    7. /* the the highest X, Y, Z value */
    8. int maxX = Math.max(start.getBlockX(), end.getBlockX());
    9. int maxY = Math.max(start.getBlockY(), end.getBlockY());
    10. int maxZ = Math.max(start.getBlockZ(), end.getBlockZ());
    11.  
    12. /* the the lowest X, Y, Z value */
    13. int minX = Math.min(start.getBlockX(), end.getBlockX());
    14. int minY = Math.min(start.getBlockY(), end.getBlockY());
    15. int minZ = Math.min(start.getBlockZ(), end.getBlockZ());
    16.  
    17. // create a new random location inside the limits
    18. return new Location(start.getWorld(), nextInt(minX, maxX), nextInt(minY, maxY), nextint(minY, maxZ));
    19. }
    20.  
    21. public int nextInt(int min, int max) {
    22. Random rnd = new Random(); // the random thingy
    23. return rnd.nextInt(max - min) + max; // get a new random number inside the limits
    24. }
    25.  
     
    Vincent1468 likes this.
  4. Offline

    Vincent1468

    FurmigaHumana
    Thanks you, I'm trying this but I'm getting this error:
    Code:
    java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at nl.imesiacraft.plugins.SpawnManager.nextInt(SpawnManager.java:83)
    at nl.imesiacraft.plugins.SpawnManager.getRandomLocation(SpawnManager.java:77)
    at nl.imesiacraft.plugins.SpawnManager.spawn(SpawnManager.java:31)
    at nl.imesiacraft.plugins.Arena.start(Arena.java:152)
    at nl.imesiacraft.plugins.Arena$1.run(Arena.java:142)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftTask.run(CraftTask.java:53)
    at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:522)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    
    Line 83:
    Code:java
    1.  
    2. return rnd.nextInt(max - min) + max; // get a new random number inside the limits
    3.  

    Line 77:
    Code:java
    1.  
    2. int z = nextInt(minZ, maxZ);
    3.  

    And line 31:
    Code:java
    1.  
    2. Entity ent = world.spawnEntity(getRandomLocation(a.getL1(), a.getL2()), EntityType.ZOMBIE);
    3.  
     
  5. Offline

    sharp237

    try:

    return rnd.nextInt(Math.abs(max - min))+ max;
     
    Vincent1468 likes this.
  6. Offline

    FurmigaHumana

    maxZ should always be bigger, anyway... Change this:

    To this:

    Code:java
    1.  
    2. return rnd.nextInt(Math.abs(max - min)) + max; // get a new random number inside the limits
    3.  
     
  7. Offline

    sharp237

    Also, shouldn't it be:

    Code:
    return rnd.nextInt(Math.abs(max - min) + 1) + min;
     
  8. Offline

    Vincent1468

    sharp237 FurmigaHumana
    Tried both, same error.
    Code:
    2013-09-03 21:44:17 [SEVERE] java.lang.IllegalArgumentException: n must be positive
    2013-09-03 21:44:17 [SEVERE]at java.util.Random.nextInt(Unknown Source)
    
     
  9. Offline

    sharp237


    try this:

    Code:
    Bukkit.getLogger().info("n=" + (Math.abs(max - min) + 1));
    return rnd.nextInt(Math.abs(max - min) + 1) + min;
    See what the console says.
     
    Vincent1468 likes this.
  10. Offline

    Vincent1468

    sharp237
    Console says this:
    Code:
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    2013-09-03 22:22:51 [INFO] n=3
    2013-09-03 22:22:51 [INFO] n=1
    
    And no error!
    But for some reason all the zombies spawned on the same location.
     
Thread Status:
Not open for further replies.

Share This Page