Making items rotate around player

Discussion in 'Plugin Development' started by CeramicTitan, Jul 16, 2014.

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

    CeramicTitan

    I am currently trying to make items rotate around the player. If I use the .teleport() method it looks glitchy and unnatural. What can I use to constantly update the location of the items without make it look glitchy?

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. final Player p = event.getPlayer();
    4. final Item item = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.ENDER_PEARL, 1));
    5. final Item secondItem = p.getWorld().dropItem(p.getLocation(), new ItemStack(Material.ENDER_PEARL, 1));
    6. item.setPickupDelay(Integer.MAX_VALUE);
    7. secondItem.setPickupDelay(Integer.MAX_VALUE);
    8. final List<Location> list = circle(p.getLocation(), 3, 1, true, false, +4);
    9. final List<Location> second = circle(p.getLocation(), 3, 1, true, false, 0);
    10. runnable = new BukkitRunnable() {
    11. int forward = 0;
    12. int backward = second.size() - 1;
    13.  
    14. public void run() {
    15. if (forward < list.size() && backward < second.size()) {
    16. Location location = list.get(forward);
    17. Location inverse = second.get(backward);
    18. forward++;
    19. backward--;
    20. for (int i = 0; i < 100; i++) {
    21. //item.teleport(location);
    22. //secondItem.teleport(inverse);
    23. }
    24. } else {
    25. forward = 0;
    26. backward = second.size() - 1;
    27. }
    28.  
    29. }
    30.  
    31. }.runTaskTimer(this, 0L, 1L);
    32.  
    33. }
    34. /**
    35.   * Found on the Bukkit Forums
    36.   * @author Rprrr
    37.   * [url]https://forums.bukkit.org/threads/creating-a-circle.93632/[/url]
    38.   *
    39.   */
    40.  
    41. public List<Location> circle(Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
    42. List<Location> circleblocks = new ArrayList<Location>();
    43. int cx = loc.getBlockX();
    44. int cy = loc.getBlockY();
    45. int cz = loc.getBlockZ();
    46. for (int x = cx - r; x <= cx + r; x++)
    47. for (int z = cz - r; z <= cz + r; z++)
    48. for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
    49. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    50. if (dist < r * r && !(hollow && dist < (r - 1) * (r - 1))) {
    51. Location l = new Location(loc.getWorld(), x, y + plus_y, z);
    52. circleblocks.add(l);
    53. }
    54. }
    55.  
    56. return circleblocks;
    57. }
     
  2. Offline

    Traks

    I think setting the entity's velocity using Entity#setVelocity(Vector) every x ticks would not look glitchy. It would require quite some calculations when the player moves, while keeping the circular orbit, though...
     
  3. Offline

    CeramicTitan

    I have tried item.setVelocity(location.toVector()); where to my surprise, nothing happens when I join. I may have made a mistake. I'll test again tomorrow.
     
  4. Offline

    Flamedek


    That might be (though I'm no expert) because you are making new Location objects without any pitch/yaw information. So the vector would only have a starting position but no direction.

    Getting the correct direction information for the circular motion might turn out quite complicated
     
  5. Offline

    Deleted user

  6. Offline

    CeramicTitan

    Hahah, I can't say I was thinking about mario kart when I thought about this idea.
     
  7. Offline

    1Rogue

    Pre-calculate the locations relative to the player (as in the difference to where you can use Location#add), then launch the block towards that location. The more locations you use, the better the animation will be.

    As for an equal distance, math!:

    Code:java
    1. public static class SpawnCircle {
    2.  
    3. private final double x;
    4. private final double z;
    5.  
    6. public SpawnCircle(double radius) {
    7. double angle = Math.random() * Math.PI * 2;
    8. this.x = Math.cos(angle) * radius;
    9. this.z = Math.sin(angle) * radius;
    10. }
    11.  
    12. public double getX() {
    13. return this.x;
    14. }
    15.  
    16. public double getZ() {
    17. return this.z;
    18. }
    19.  
    20. }
    21.  
    22. //Elsewheres...
    23. int initialRadius = /* your amount */;
    24. double rad = Math.sqrt(2 * (initialRadius * initialRadius)) / 2;
    25. SpawnCircle circle;
    26. //To get your random location...
    27. circle = new SpawnCircle(rad);
    28. Location rand = new Location(circle.getX(), /* your Y */, circle.getZ());


    Of course instead of random you can easily just splice the amounts into something like 12 equidistant locations.
     
    CeramicTitan and Garris0n like this.
  8. Offline

    pluginsbyjason

    Did you try using trigonometry? (sine waves)
     
  9. Offline

    CeramicTitan

    I'll give this ago :)
     
  10. Offline

    HeavyMine13

    How to split them? I'm curious !
     
  11. Offline

    1Rogue

    Math. Simple radian works like x/2 for 90 degree angles etc
     
Thread Status:
Not open for further replies.

Share This Page