Making a particle line from point 1 to point 2

Discussion in 'Plugin Development' started by 360_, Oct 21, 2017.

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

    360_

    So all I'm trying to do is make a particle line go from the orange wool to the white wool [​IMG]
    The only bit of particles i know how to do is "player.spawnParticle(Particle.FLAME, player.getLocation().add(0, 2, 0.500), 1);" but nothing else. (and yes I know that that's not how its supposed to be but that's how I've always done it) . Hopefully, someone can help me out with this
     
  2. Offline

    nullYoung

    Hi,

    method "Player#spawnParticle(Particle, Location)" doesn't exists but you can use "World#playEffect(Effect, Location, Integer)" where third argument is effect data.

    Bye,
    - nullYoung
     
  3. @nullYoung
    The player#spawnParticle(Particle, Location, int) method does indeed exist, you must be doing something wrong.

    @360_
    I use the below method to draw a line between two points. It works when the points are diagonal, straight, on-axis, off-axis, really anywhere. It is however a bit of a hard thing if you're not familiar with how to work with vectors, so if you don't understand my explanation, I will post the code.

    Basically what we do is we is subtract the two points, which will give us a vector pointing from one location to the other, and then we spawn a particle every so often. This is done by starting with the first location point, and then adding the normalized version of the vector we obtained by subtracting the two points. This way we get a location which is only part of the way to the other point. We then repeat the process again adding the normalized vector (you can also multiply this vector with whatever you like to change the spacing between the particles) until we arrive at the second point.
    Code:java
    1. public void drawLine(Location point1, Location point2, double space) {
    2. World world = point1.getWorld();
    3. Validate.isTrue(point2.getWorld().equals(world), "Lines cannot be in different worlds!");
    4. double distance = point1.distance(point2);
    5. Vector p1 = point1.toVector();
    6. Vector p2 = point2.toVector();
    7. Vector vector = p2.clone().subtract(p1).normalize().multiply(space);
    8. double length = 0;
    9. for (; length < distance; p1.add(vector)) {
    10. world.spawnParticle(Particle.REDSTONE, p1.getX(), p1.getY(), p1.getZ(), 1);
    11. length += space;
    12. }
    13. }
     
  4. Offline

    MightyOne

    @AlvinB why do you spoonfeed it? Doing it yourself is the best way to learn it :/
     
  5. Offline

    Zombie_Striker

    @nullYoung @AlvinB
    spawnParticle was craeted roughly in 1.9-1.10. If you are not seeing it, then you are most likely using one of the older updates that should no longer be used.
     
  6. @MightyOne
    Well, I think with complex enough subjects such as this one it's okay to give some code, because if you're trying to follow along an explanation of a subject you have no experience in, it will most likely just result in inefficient code. Also, for some maths-heavy methods, it might just be better to go ahead and use it for now, and try and read up on the maths another day.
     
  7. Offline

    MightyOne

    @AlvinB
    I call this basic geometry idk maybe my expectations of peoples intelligence in this forum is just too high. I really dont know. You found it out, I found it out, why not he? And if you really understood something you should be able to explain it to anyone. I mean the first part of your answer was already not so bad I would have just left it at that.
    People here ask for help and not others to do their work. Gbye
     
  8. @MightyOne
    Well basic geometry or not, if you haven't learned it in school (I can tell you I certainly didn't), you won't know it. And really, I consider that part of code a piece of the explanation. It's always easier to have a reference implementation to go off of, especially if it's an unfamiliar topic.
     
  9. Offline

    360_

    Thanks for tring to help me out by giving me the code but i want to learn how to do it not get spoonfed
     
  10. @360_
    Well, you don't have to use my code, you can try to code it yourself using the explanation. The code is just there for you to look at if you get stuck :)
     
  11. Offline

    Horsey

    @360_ Well I can explain the code for you, but you should use that method:

    Code:java
    1.  
    2. public void drawLine(
    3. /* Would be your orange wool */Location point1,
    4. /* Your white wool */ Location point2,
    5. /*Space between each particle*/double space
    6. ) {
    7.  
    8. World world = point1.getWorld();
    9.  
    10. /*Throw an error if the points are in different worlds*/
    11. Validate.isTrue(point2.getWorld().equals(world), "Lines cannot be in different worlds!");
    12.  
    13. /*Distance between the two particles*/
    14. double distance = point1.distance(point2);
    15.  
    16. /* The points as vectors */
    17. Vector p1 = point1.toVector();
    18. Vector p2 = point2.toVector();
    19.  
    20. /* Subtract gives you a vector between the points, we multiply by the space*/
    21. Vector vector = p2.clone().subtract(p1).normalize().multiply(space);
    22.  
    23. /*The distance covered*/
    24. double covered = 0;
    25.  
    26. /* We run this code while we haven't covered the distance, we increase the point by the space every time*/
    27. for (; covered < distance; p1.add(vector)) {
    28. /*Spawn the particle at the point*/
    29. world.spawnParticle(Particle.REDSTONE, p1.getX(), p1.getY(), p1.getZ(), 1);
    30.  
    31. /* We add the space covered */
    32. covered += space;
    33. }
    34. }
    35.  
     
Thread Status:
Not open for further replies.

Share This Page