Solved Moving an entity around a point

Discussion in 'Plugin Development' started by Yeowza, Mar 7, 2020.

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

    Yeowza

  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    bowlerguy66

    @Yeowza Couldn't you use
    Code:
    entity.teleport(loc)
     
  4. Offline

    Yeowza

    Thanks guys. I could use teleport but the movement is very blocky and not smooth like the setVelocity.

    Code:
                final Entity t14t = t14;
                final Location start = t14t.getLocation();
                new BukkitRunnable() {
                    double angle = 0;
                    double x = 0;
                    double z = 0;
                    double y = 0;
                    public void run() {
                        angle++; if (angle > 90) { angle = 0; }
                        if (!t14t.isValid()) { this.cancel(); }
                        else {
                            angle = (angle) * (Math.PI/180); // Convert to radians
    
                            x = Math.cos(angle) * (t14t.getLocation().getX() - start.getX()) - Math.sin(angle) * (t14t.getLocation().getZ() - start.getZ()) + start.getX();
                            z = Math.sin(angle) * (t14t.getLocation().getX() - start.getX()) + Math.cos(angle) * (t14t.getLocation().getZ() - start.getZ()) + start.getZ();
                        }
                        t14t.teleport(new Location(Main.world, x, y, z));
                    }
                }.runTaskTimer(Main.plugin, 0, 1);
     
  5. Offline

    bowlerguy66

    @Yeowza A few notes on your code:
    - I'd recommend using Math.toRadians() to clean up a little bit
    - The math for getting your x and z coordinates seems excessive, this is what I do when calculating circles:
    Code:
    double radius;
    double angle;
    x = originX + Math.cos(angle) * radius;
    z = originZ + Math.sin(angle) * radius;
    As for using velocities, the velocity of an object going in a circle will always be tangent to the circle it's going around. That might be a way for you to set up your velocities for the entity.
     
  6. Offline

    Yeowza

    Thanks bowlerguy66, that pushed me in the right direction. I think I'm almost done with it now.

    Code:
                final Entity t14t = t14;
                new BukkitRunnable() {
                    double angle = 0;
                    double x = 0;
                    double z = 1;
                    public void run() {                   
                        if (!t14t.isValid()) { this.cancel(); }
                        else {
                            angle++;
                            x = Math.toRadians(angle);
                            x = Math.sin(x);
                            z = Math.toRadians(angle);
                            z = Math.cos(z);
                            if (angle > 360) { angle = 0; }
                           
                            t14t.setVelocity(new Vector(x, 0, z).multiply(0.7));
                        }
                    }
                }.runTaskTimer(Main.plugin, 0, 1);
    It's a minigame where the player shoots TNT that rotates around stuff. Thanks again man.
     
    bowlerguy66 likes this.
  7. Offline

    KarimAKL

    What do you need to do?

    Couldn't you just use this?:
    Code:Java
    1. x = Math.sin(Math.toRadians(angle));
    2. z = Math.cos(Math.toRadians(angle));
     
Thread Status:
Not open for further replies.

Share This Page