Create circle around player

Discussion in 'Plugin Development' started by Woef2001, Aug 1, 2014.

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

    Woef2001

    i tried to create a particle circle around a player for like a spell but it kinda doesn't work.
    also i find this code a little bit weird, there has to be a different and easier way to do it.

    Code:java
    1. /*
    2.   * - - - - x x x - - - -
    3.   * - - x x - - - x x - -
    4.   * - x - - - - - - - x -
    5.   * - x - - - - - - - x -
    6.   * x - - - - - - - - - x
    7.   * x - - - - O - - - - x
    8.   * x - - - - - - - - - x
    9.   * - x - - - - - - - x -
    10.   * - x - - - - - - - x -
    11.   * - - x x - - - x x - -
    12.   * - - - - x x x - - - -
    13.   */
    14.  
    15. public void run() {
    16. if (amDone >= 75) done = true;
    17. if (player.isDead() || player == null || done) cancel();
    18. ParticleEffect.MOB_SPELL.display(player.getLocation().add(0, 1, 5), 0, 0, 0, 3, parAm);
    19. ParticleEffect.MOB_SPELL.display(player.getLocation().add(1, 1, 5), 0, 0, 0, 3, parAm);
    20. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-1, 1, 5), 0, 0, 0, 3, parAm);
    21. ParticleEffect.MOB_SPELL.display(player.getLocation().add(0, 1, -5), 0, 0, 0, 3, parAm);
    22. ParticleEffect.MOB_SPELL.display(player.getLocation().add(1, 1, -5), 0, 0, 0, 3, parAm);
    23. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-1, 1, -5), 0, 0, 0, 3, parAm);
    24. ParticleEffect.MOB_SPELL.display(player.getLocation().add(5, 1, 0), 0, 0, 0, 3, parAm);
    25. ParticleEffect.MOB_SPELL.display(player.getLocation().add(5, 1, 1), 0, 0, 0, 3, parAm);
    26. ParticleEffect.MOB_SPELL.display(player.getLocation().add(5, 1, -1), 0, 0, 0, 3, parAm);
    27. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-5, 1, 0), 0, 0, 0, 3, parAm);
    28. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-5, 1, 1), 0, 0, 0, 3, parAm);
    29. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-5, 1, -1), 0, 0, 0, 3, parAm);
    30. ParticleEffect.MOB_SPELL.display(player.getLocation().add(4, 1, 2), 0, 0, 0, 3, parAm);
    31. ParticleEffect.MOB_SPELL.display(player.getLocation().add(4, 1, 3), 0, 0, 0, 3, parAm);
    32. ParticleEffect.MOB_SPELL.display(player.getLocation().add(3, 1, 4), 0, 0, 0, 3, parAm);
    33. ParticleEffect.MOB_SPELL.display(player.getLocation().add(2, 1, 4), 0, 0, 0, 3, parAm);
    34. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-4, 1, 2), 0, 0, 0, 3, parAm);
    35. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-4, 1, 3), 0, 0, 0, 3, parAm);
    36. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-3, 1, 4), 0, 0, 0, 3, parAm);
    37. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-2, 1, 4), 0, 0, 0, 3, parAm);
    38. ParticleEffect.MOB_SPELL.display(player.getLocation().add(4, 1, -2), 0, 0, 0, 3, parAm);
    39. ParticleEffect.MOB_SPELL.display(player.getLocation().add(4, 1, -3), 0, 0, 0, 3, parAm);
    40. ParticleEffect.MOB_SPELL.display(player.getLocation().add(3, 1, -4), 0, 0, 0, 3, parAm);
    41. ParticleEffect.MOB_SPELL.display(player.getLocation().add(2, 1, -4), 0, 0, 0, 3, parAm);
    42. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-4, 1, -2), 0, 0, 0, 3, parAm);
    43. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-4, 1, -3), 0, 0, 0, 3, parAm);
    44. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-3, 1, -4), 0, 0, 0, 3, parAm);
    45. ParticleEffect.MOB_SPELL.display(player.getLocation().add(-2, 1, -4), 0, 0, 0, 3, parAm);
    46. amDone++;
    47. }


    2014-08-01_12.06.59.png

    i want to close those gaps between the particles and make the circle more round but in the way that i dont have to use the same line of code with decimal numbers over and over again. seems a bit unnecessary.
     
  2. Offline

    NKloco

    You can try this little code I wrote now, it's based on a circle definition of a distance from the circle center, so it should work just fine. basicly what it does, is checking all the blocks in the square around the player where the circle should be and if the block is in the right distance it makes the particles apear there.

    Code:java
    1. /*
    2. * - - - - x x x - - - -
    3. * - - x x - - - x x - -
    4. * - x - - - - - - - x -
    5. * - x - - - - - - - x -
    6. * x - - - - - - - - - x
    7. * x - - - - O - - - - x
    8. * x - - - - - - - - - x
    9. * - x - - - - - - - x -
    10. * - x - - - - - - - x -
    11. * - - x x - - - x x - -
    12. * - - - - x x x - - - -
    13. */
    14.  
    15. public void run() {
    16. if (amDone >= 75) done = true;
    17. if (player.isDead() || player == null || done) cancel();
    18. int radius = RAD;
    19. for(int x = player.getLocation().getBlockX() - radius ; x <= player.getLocation().getBlockX() + radius ; x++)
    20. for(int z = player.getLocation().getBlockZ() - radius ; x <= player.getLocation().getBlockZ() + radius ; z++)
    21. if(Math.round(Math.sqrt(Math.pow(player.getLocation().getBlockX() - x, 2) +
    22. Math.pow(player.getLocation().getBlockZ() - z, 2))) == radius)
    23. ParticleEffect.MOB_SPELL.display(new Location(player.getWorld(), x, player.getLocation().getBlockY() + 1, z), 0, 0, 0, 3, parAm);
    24.  
    25. amDone++;
    26. }
     
  3. Offline

    xize

    Woef2001

    why do you not loop to create a circle?

    int size = 30;
    for(int i = 0.0; i < 360.0;i++) {

    double angle = (i*Math.PI - 180);
    int x = (int) size*Math.cos(angle);
    int z = (int) size*Math.sin(angle);
    Block block = p.getLocation().getWorld().getBlockAt(x, p.getLocation().getY() , z);
    }
     
    DigitalSniperz likes this.
  4. Offline

    Woef2001

    hmm i implemented your code but it doesnt seems to work:

    2014-08-01_14.49.09.png

    Code:java
    1. private void createCircle() {
    2. double size = 5;
    3. for(int i = 0; i < 360; i++) {
    4. double angle = (i*Math.PI - 180);
    5. double x = size*Math.cos(angle);
    6. double z = size*Math.sin(angle);
    7. Location loc = player.getLocation().add(x, 1, z);
    8. ParticleEffect.MOB_SPELL.display(loc, 0, 0, 0, 3, parAm);
    9. }
    10. }
     
  5. Offline

    Traks

    xize Woef2001 Degree to radian conversion can be done with degrees * Math.PI / 180 (xise misspelled?), or Math#toRadians(double). You could even create a constant for Math.PI / 180 as an attempt for micro-optimisation or create a cache for the sine and cosine results.
     
  6. Offline

    xize

    Traks
    yea I see ive misspelled alot more D: its indeed / 180 and I forgot to use loc.add() to:p
     
  7. Offline

    nickpowns

    can som1 plss give the code for this? i'm trying to find the right code for weeks now...
     
  8. Offline

    Woef2001

    okay i've been working with this code for a while but one thing bothers me a lot.
    when i start the thread that will display the particles every time it displays new particles it removes the other particles spawned in earlier!

    code:
    Code:java
    1.  
    2. private Player player;
    3. private boolean done = false;
    4. private int amDone = 0;
    5. private int parAm = 5;
    6. private double size = 7;
    7.  
    8. public MageSpecial(Mage kit) {
    9. player = kit.getPlayer();
    10. runTaskTimer(Main.plugin, 0l, 4l);
    11. }
    12.  
    13. public void run() {
    14. if (amDone >= 75) done = true;
    15. if (player.isDead() || player == null || done) cancel();
    16.  
    17. for (int i = 0; i < 360; i++) {
    18. double angle = (i * Math.PI / 180);
    19. double x = size * Math.cos(angle);
    20. double z = size * Math.sin(angle);
    21. Location loc = player.getLocation().add(x, 1, z);
    22. ParticleEffect.MOB_SPELL.display(loc, 0, 0, 0, 1, parAm);
    23. ParticleEffect.WITCH_MAGIC.display(loc, 0, 0, 0, 1, parAm);
    24. }
    25.  
    26. amDone++;
    27. }
    28.  


    it looks like this all the time:
    View attachment 21430

    you dont get those particles above the particles that display the ring:
    View attachment 21432

    they don't fade away they just disappear.

    srr but my browser wont let me edit the replies.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    nickpowns

    I tried your code with hearts but its way too fast...
     
  10. Offline

    Woef2001

  11. Offline

    Vorqe

    With this code how would you detect players within the circle created?
     
  12. Offline

    fireblast709

    Vorqe loop over players, check if the distanceSquared is smaller than the radius squared
     
  13. Offline

    nickpowns

    can't som1 just give me the code?
     
  14. Offline

    nzkiwi.5000

    Woef2001 nickpowns
    Code:java
    1. int points = 12 //amount of points to be generated
    2. for (int i = 0; i < 360; i += 360/points) {
    3. double angle = (i * Math.PI / 180);
    4. double x = size * Math.cos(angle);
    5. double z = size * Math.sin(angle);
    6. Location loc = player.getLocation().add(x, 1, z);
    7. ParticleEffect.MOB_SPELL.display(loc, 0, 0, 0, 1, parAm);
    8. ParticleEffect.WITCH_MAGIC.display(loc, 0, 0, 0, 1, parAm);
    9. }

    This is a bit modified code by Woef2001
     
  15. Offline

    nickpowns

    can you fit it in an playerjoin event plss
     
  16. Offline

    fireblast709

    nickpowns try writing your own, instead of asking to be spoonfed... Then you might actually learn something new :p
     
    EpicPie11, JoaoBM, kaif21 and 2 others like this.
Thread Status:
Not open for further replies.

Share This Page