1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Teleportation

Discussion in 'Development' started by Daniel23, Oct 17, 2017.

  1. Daniel23

    Daniel23 Witch

    Messages:
    65
    GitHub:
    daniel23
    When a command is pressed it teleports a player in a particular place
    [​IMG] [https://postimg.org/image/72a26gjpln/]
    As you can see at the image a above when the command is executed the player in the red area (1000x1000-500x500) will be teleported to the 500x500 green area (safespawn) but if the person. Is inside the 500x500 area and 100x100 they're not gonna be teleported. And so on
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    [​IMG]
    That's how you use the img tag...
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    First, let's make a function which checks whether a Vector3 lies in between two Vector2 points.
    PHP:
    public function inVector2(Vector3 $vec3Vector2 $vec2aVector2 $vec2b) : bool{
        
    $v2x = [$vec2a->x$vec2b->x];//x-value of the Vector2s
        
    $v2y = [$vec2a->y$vec2b->y];//y-value of the Vector2s (corresponds to vec3->z)
        
    return $vec3->min($v2x) && $vec3->max($v2x) && $vec3->min($v2y) && $vec3->max($v2y);
    }
    Basic math, pretty easy to understand.

    Now, lets make functions for checking if the Vector3 lies in the three regions.

    100x100 would be:
    PHP:
    public function in100x100(Vector3 $object) : bool{
        return 
    $this->inVector2($object, new Vector2(-50, -50), new Vector2(5050));
    }
    500x500 would be:
    PHP:
    public function in500x500(Vector3 $object) : bool{
        return !
    $this->in100x100($object//check if object is not in 100x100 because 100x100 lies in 500x500
            
    && $this->inVector2($object, new Vector2(-250, -250), new Vector2(250250));
    }
    Similarly, you can guess what 1000x1000 would be.

    You just need to connect the dots now. You have got everything.
    PHP:
    $canBeTeleported $this->in1000x1000($player) && !$this->in500x500($player);
    if(!
    $canBeTeleported){
        if(
    $this->in500x500($player)){
            echo 
    "Could not teleport $player because they are in the green region.";
        }
        if(
    $this->in100x100($player)){
            echo 
    "Could not teleport $player because they are in the blue region.";
        }
    }
    Yeah, it's somewhat annoying to call in100x100() function multiple times in some instances. Maybe have a second argument for in{!=100}x{!=100}() functions that would control checking areas inside an area.
     
    Daniel23 and jasonwynn10 like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.