Circle Velocities

Discussion in 'Plugin Development' started by Mr_toaster111, Sep 14, 2014.

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

    Mr_toaster111

    I am trying to figure out how to set mine carts in a circular path. I have tried this code which is what I have found on other threads:
    http://pastebin.com/7URTnux4
    but it doesnt give a circle shape it just looks like this:
    2014-09-14_13.36.39.png
     
  2. Offline

    mythbusterma

    Mr_toaster111

    That's pretty close to a circle, I don't understand what the issue is?
     
  3. Offline

    Mr_toaster111

    mythbusterma well my goal is to get it like this as you can see a much cleaner circle
     
  4. Offline

    Gamecube762

    Well you only have 1 minecart, see what it looks like with multiple.
     
  5. Offline

    Mr_toaster111

    Thanks for the reply i'll try that.
     
  6. Offline

    Mr_toaster111

    Gamecube762 I tried making multiple it still looks like a diamond shape, Its the shape that you would get if you made the tracks diagnol
     
  7. Offline

    blablubbabc

    It seems that the minecart does finer movement if you reduce the circles radius (vector length) and the time it takes to drive 1 round (increasing step size)). Making the circle too small or the time-per-round too small however makes it look choppy.

    This is what I was testing with and which seems to work just fine:

    Code:java
    1. @EventHandler
    2. void onEntityInteract(PlayerInteractEntityEvent event) {
    3. final Entity clicked = event.getRightClicked();
    4. new BukkitRunnable() {
    5.  
    6. int counter = 0;
    7. double angle = 0.0D;
    8. double step = ((2 * Math.PI) / 100.0D); // 1 circle in 5 seconds
    9. double speed = 0.5D; // determines radius
    10.  
    11. @Override
    12. public void run() {
    13. clicked.setVelocity(new Vector(Math.cos(angle) * speed, 0.0D, Math.sin(angle) * speed));
    14. angle += step;
    15. if (++counter > 500) {
    16. this.cancel(); // stop after 5 circles
    17. }
    18. }
    19. }.runTaskTimer(this, 1L, 1L);
    20. }
     
    shmkane likes this.
  8. Offline

    shmkane

    Circles are broken up into degrees and/or radians. If you could get the circumference and split it up into a couple segments around a fixed midpoint, you'd end up with a circle.

    [​IMG]

    Edit: my technology is inferior, use what blablubbabc posted!
     
  9. Offline

    Mr_toaster111

    blablubbabc Thanks so much your a life saver!!!!!!!
     
  10. Offline

    Mr_toaster111

    blablubbabc im having a problem where every time around the cars sccrunch up to the middle like this.

    It seems like the first quarter of a way around the car is too far outside of the wood block and by the third quarter around its too far inside of the wood
    2014-09-21_20.29.26.png
     
  11. Offline

    blablubbabc

    Looks like the readius of the movement is too small, so it's not actually rotating around the exact center yellow block.
     
  12. Offline

    Mr_toaster111

    is there any way i can use those numbers like the speed and the step to determine this exact 6 block radius?
     
  13. Offline

    blablubbabc

    Here is another attempt because I am not completely sure how the speed exactly result in the given radius: moving the minecart with teleportation instead

    Code:java
    1. @EventHandler
    2. void onPlayerInteract(PlayerInteractEvent event) {
    3. if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    4. final Player player = event.getPlayer();
    5. if (player.getItemInHand() != null && player.getItemInHand().getType() != Material.AIR) return;
    6. final Location center = event.getClickedBlock().getLocation().add(0.5D, 1.0D, 0.5D);
    7. final double radius = 5.0D;
    8. final int ticksPerCircle = 100; // 1 circle in 5 seconds
    9. final double angleStep = ((2 * Math.PI) / ticksPerCircle);
    10. final int amountOfCircles = 5; // stop after 5 circles
    11.  
    12. final Entity entity = center.getWorld().spawnEntity(center, EntityType.MINECART);
    13.  
    14. new BukkitRunnable() {
    15.  
    16. int currentCircle = 1;
    17. int currentTick = 0;
    18. double angle = 0.0D;
    19.  
    20. @Override
    21. public void run() {
    22. Location newLoc = new Location(center.getWorld(), center.getX() + (Math.cos(angle) * radius), center.getY(), center.getZ() + (Math.sin(angle) * radius));
    23. entity.teleport(newLoc);
    24.  
    25. // prepare next step:
    26. currentTick++;
    27. currentTick %= ticksPerCircle;
    28. if (currentTick == 0) {
    29. if (currentCircle == amountOfCircles) {
    30. entity.remove();
    31. this.cancel();
    32. return;
    33. }
    34. currentCircle++;
    35. angle = 0;
    36. } else {
    37. angle += angleStep;
    38. }
    39. }
    40. }.runTaskTimer(this, 2L, 1L); // delayed by 2 ticks, because there seems to be some craftbukkit/minecraft visual bug when teleporting the entity with only 1 tick delay
    41. }


    Only thing missing there now is to also set the yaw accordingly, depending on the current angle.
     
  14. Offline

    Mr_toaster111

    Thanks so much for you work on helping me, sadly you cannot move minecarts with teleportation as it doesnt work with a passenger.
     
  15. Offline

    blablubbabc

    Did you try how it looks if you eject the passenger, teleport and add back the passenger every time?

    Other than that you would currently have to experiment around with the speed variable (when using velocity instead)..

    Or you try this formula (from wikipedia):

    [​IMG]
    and see if this works accurate in minecraft as well..

    With 100 ticks per circle and radius of 5 blocks the speed would be around 0,3141592653589

    Edit: this doesn't seem to work that easy in minecraft however..
     
  16. Offline

    Mr_toaster111

    blablubbabc I got everything working fine using the velocity method you gave me except when i add more minecarts those minecarts get a little wider out of the circle.
     
  17. Offline

    blablubbabc

    Mr_toaster111 Can you give me your current code for that? Which value for the speed did you end up using?
     
  18. Offline

    iMurder

    This is why we try and NOT make circles in minecraft.
    Never the less, hope this is sorted out soon :)
     
  19. Offline

    Mr_toaster111

    Code:java
    1. Bukkit.getScheduler().runTaskTimer(p, new Runnable() {
    2.  
    3. int counter1 = 0;
    4. double angle1 = 0D;
    5. double step1 = ((2 * Math.PI) / 170.0D); // 1 circle in 5 seconds //EDIT 7.5 //100 is 5 seconds
    6. double speed1 = 0.375D; // determines radius
    7.  
    8.  
    9.  
    10. @Override
    11. public void run() {
    12.  
    13.  
    14. car1.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 1.5) * speed1, 0.0D, Math.sin(angle1 + 1.5) * speed1));
    15. car2.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 2) * speed1, 0.0D, Math.sin(angle1 + 2) * speed1));
    16. car3.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 2.5) * speed1, 0.0D, Math.sin(angle1 + 2.5) * speed1));
    17. car4.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 3) * speed1, 0.0D, Math.sin(angle1 + 3) * speed1));
    18. car5.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 3.5) * speed1, 0.0D, Math.sin(angle1 + 3.5) * speed1));
    19. car6.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 4) * speed1, 0.0D, Math.sin(angle1 + 4) * speed1));
    20.  
    21. car7.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 4.5) * speed1, 0.0D, Math.sin(angle1 + 4.5) * speed1));
    22. car8.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 5.0) * speed1, 0.0D, Math.sin(angle1 + 5.0) * speed1));
    23. car9.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 5.5) * speed1, 0.0D, Math.sin(angle1 + 5.5) * speed1));
    24. car10.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 0) * speed1, 0.0D, Math.sin(angle1 + 0) * speed1));
    25. car11.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 0.5) * speed1, 0.0D, Math.sin(angle1 + 0.5) * speed1));
    26. car12.getBukkitEntity().setVelocity(new Vector(Math.cos(angle1 + 1) * speed1, 0.0D, Math.sin(angle1 + 1) * speed1));
    27.  
    28.  
    29. angle1 += step1;
    30.  
    31. }


    The run method code ^
     
  20. Offline

    Mr_toaster111

Thread Status:
Not open for further replies.

Share This Page