Rotate entities arround circle

Discussion in 'Help' started by felony13twelve, Nov 28, 2022.

  1. felony13twelve

    felony13twelve Creeper

    Messages:
    2
    I want to make such an animation as in the video - I know that I need cosine, sine, etc. but my knowledge does not allow me to write it, because I don't know geometry well. Please help me to do this.
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    747
    Start by spawning them in circle, then move by an angle offset.
     
  3. LearXD

    LearXD Creeper

    Messages:
    3
    GitHub:
    learxd
    I believe you already know how to generate entities, so let's get down to business, the Mathematics behind it.

    To make a circle we're going to use the sine and cosine of an angle that will show us a position on the circle, then we're going to multiply both by the radius, since the default "radius" of sine and cosine is 1.

    As the amount of items can vary, I made a calculation that will correctly distribute them in the circle...

    Code: (I haven't programmed in pocketmine for a while, so I know the API works correctly, so please follow this code as a guide, not a solution...)

    Code:
    <?php
    
    use pocketmine\item\Item;
    use pocketmine\math\Vector3;
    
    const RADIUS = 3;
    protected $items = [];
    protected $center;
    
    protected $turns = 0;
    
    //Your constructor function
    public function __construct(array $items) {
        $thos->items = $items;
        $this->center = new Vector3(0,0,0);
    }
    
    //Your loop can be a ScheduleTask
    public function loop() {
        $pi = 2 * M_PI;
        $cicles = 1 / sizeof($items);
       
        foreach ( $items as $i => $item ) {
            $x = sin($pi * ($i * $cicles)) * RADIUS;
            $y = cos($pi* ($i * $cicles)) * RADIUS;
            // X, Y, Z (using only Y,Z)
            $item->teleport($this->center->add(0, $x, $y));
        }
       
        $this->turns += $cicles;
        $this->items[] = array_shift($this->items);
       
        if ($this->turns >= 1) { //1 = 100%
            // 1 turn completed, you can stop here
        }
    }
     
    Primus likes this.

Share This Page

  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.