Circular Regions. How?

Discussion in 'Plugin Development' started by shadrxninga, Jun 11, 2011.

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

    shadrxninga

    How would check if a player was in a certain radius from a point? So in other words how would I make a circular region. I can make a cuboid one with no trouble. But a circular one...


    Any help would be appreciated :D
     
  2. Offline

    CaiusTSM

    make a center point and put it to a vector, then use the vector's distance() function to get the distance between the center and the player, then if the distance <= radius do stuff
     
    shadrxninga likes this.
  3. Offline

    shadrxninga

    @CaiusTSM
    Is there anyway not to check to y value, so it is a cylinder from sky to bedrock?
     
  4. Offline

    Sethcran

    Take the 2d coordinates of the player (x,z) and the 2d coordinates of the point you are interested in (x,z) and use the distance formula on them. This could be easily coded yourself, or I am sure there is some java library somewhere that supports it.

    sqrt( (x2 - x1)^2 + (y2 - y1)^2)
     
  5. Offline

    shadrxninga

    @Sethcran
    So that will give you the distance between The player and the point you have given, on a 2d plane. So it doesn't matter how high or low you are? You will still be in that circular region, and not a sphere?

    Thanks for the help guys, nearly got it...
     
  6. Offline

    matter123

    @shadrxninga yes but you need to use z values not y so it would look like

    sqrt( (x2 - x1)^2 + (z2 - z1)^2)
     
  7. Offline

    shadrxninga

    @matter123

    Yep, I got it working, one problem though... - I've set this up so when you are in the zone you can't break blocks. But the thing is - if you stand outside of that zone, then you can break the blocks inside it...

    Heres the code:
    Code:
        public void onBlockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();
            Location loc = new Location(player.getWorld(), 0 , 0 , 0);
    
            double x2 = player.getLocation().getX();
            double x1 = loc.getX();
            double z2 = player.getLocation().getZ();
            double z1 = loc.getZ();
            double radius = 10;
    
            double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(z2 - z1, 2));
            if(distance <= radius){
                player.sendMessage("test");
                player.sendMessage("Your in the zone");
                event.setCancelled(true);
                        }else{
                            player.sendMessage("Your not it a Zone");
                        }
    
                }
     
  8. Offline

    matter123

    use would hook onto onPlayer interact check if the action is leftclick block
    if so use player.getTargetblock(null,120) and check if that block is in the xone if so cancel the event

    @shadrxninga
     
    shadrxninga likes this.
  9. Offline

    Sethcran


    To fix this, simple use the block locations instead of the player locations.

    Block block = event.getBlock();
    int x2 = block.getX();
    int z2 = block.getZ();

    You will need to deal with some integer/double conversions, but that is trivial.
     
    shadrxninga likes this.
  10. Offline

    shadrxninga

Thread Status:
Not open for further replies.

Share This Page