[MATH] create a circular location

Discussion in 'Plugin Development' started by HeavyMine13, Aug 9, 2014.

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

    HeavyMine13

    Hello! I want to make a orbital path around a location, do any of you know an algorithm for creating the path? Thanks! :3
     
  2. Offline

    OHQCraft

  3. Offline

    biel

    I have a function that does that in my plugin. When I get to home, I'll post it here. Basically it uses trigonometry to get locations in every direction.

    Sorry, but now I'm on my mobile!
     
    HeavyMine13 likes this.
  4. Offline

    HeavyMine13

    That would be amazing, thanks!
     
    biel likes this.
  5. Offline

    Chiller

    HeavyMine13 You just have to iterate through a square and check if x^2 + z^2 ~= radius.
     
  6. Offline

    HeavyMine13

    And how do I iterate through a box xD
     
  7. Offline

    Chiller

    HeavyMine13 Two nested for loops

    for (int x = 0; x < radius; x++) { for (int z = 0; z < radius; z++) { Check if x^2 + z^2 ~= radius. } }
     
  8. Offline

    HeavyMine13

    Do I then set the locations x and z to var x and var z? Or do i add var x and z to the location i have?

    biel are you home yet?

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

    mythbusterma

    HeavyMine13 Chiller

    Or, you could not waste computing power and calculate vectors in the circle instead via simple trigonometry.

    Code:
    public Set<Vector> getLocationsSurrounding (Location loc, float radius, float precision) {
      HashSet<Vector> temp = new HashSet<Vector>();
      for(float i = 0; i < 2*Math.Pi; i += precision) {
        temp.add(new Vector(Math.Cos(i))*radius + loc.getX(), loc.getY(), Math.Sin(i))*radius      +loc.getZ());
      }
      return temp;
    }
    Where the value of precision determines how many locations you're going to get, I would start with 0.1 and see where that gets you.
     
  10. Offline

    HeavyMine13

    This doesn't work:
    Code:java
    1. Location orbitalL = (Location) getLocationsSurrounding(location.add(0, YOffset, 0), 0.5f, 0.1f);
     
  11. Offline

    DevilMental

    getLocationsSurrounding doesn't return Location, it returns Set<Vector>, so to get all the locations, you iterate into everyone and you get the location by casting Vector to Location. Easy ;)
     
  12. Offline

    HeavyMine13

    Like this?
    Code:java
    1. Vector orbitalL = getLocationsSurrounding(location.add(0, YOffset, 0), 0.5f, 0.1f).iterator().next();
    2. Location orLocation = orbitalL.toLocation(location.getWorld());
     
  13. Offline

    DevilMental

    HeavyMine13 Nope, getLocationsSurronding returns a Set of Vectors so you would :
    Code:java
    1. Set<Vector> vectors = getLocationsSurrounding... // Do your thing
    2. World w = your world;
    3. List<Location> locations = new List<Location>();
    4. for (Vector v : vectors){
    5. locations.add(v.toLocation(w));
    6. }

    Then, in the list "locations" there would be all the locations of the circle.
     
  14. Offline

    mythbusterma

    HeavyMine13

    If you really want Locations you just do this instead:

    Code:
    public Set<Location> getLocationsSurrounding (Location loc, float radius, float precision) {
      HashSet<Location> temp = new HashSet<Location>();
      for(float i = 0; i < 2*Math.Pi; i += precision) {
        temp.add(new Location(loc.getWorld(), Math.Cos(i))*radius + loc.getX(), loc.getY(), Math.Sin(i))*radius +loc.getZ());
      }
      return temp;
    }
     
  15. Offline

    HeavyMine13

    Well, it is only staying in one location and not going to the next one:
    Code:java
    1. Set<Vector> vectors = getLocationsSurrounding(
    2. location.add(0, YOffset, 0), 4f, 0.3f);
    3. World w = location.getWorld();
    4. List<Location> locations = new ArrayList<Location>();
    5. for (Vector vex : vectors) {
    6. locations.add(vex.toLocation(w));
    7. }
    8.  
    9. Location orLocation = locations.listIterator().next();
     
  16. Offline

    DevilMental

    Look HeavyMine13 before learning Bukkit API, learn basic Java. I'm not saying that your knowledge isn't good, I'm just saying that you should have more looked at some basic Java code. A set/list/etc... are all kind of the same thing : they contain data, in this case the Set<Vector> contains a lot of Vectors. My code basically iterates into every vectors into the Set, then add them to a list of locations. All you got to do is to make a for loop to, for example, set all blocks from all the locations and set them to diamond : simple example. Sorry if I offended you, it was not my pupose...

    Hope that solves your problem,
    ~DevilMental
     
  17. Offline

    HeavyMine13

    Hello, I would like u to know, it is kind of the opposite. I know full java, but not advanced in bukkit. Thanks for your advice, even though i felt offended. You don't understand me. I DONT want a full circle at one time, i want an orbital path for example a diamond to orbit an entity.

    Thanks-
    Kevin
     
  18. Offline

    Commander9292

    Maybe you should have taken a look at the return type of the method then, as that had nothing to do with the Bukkit API.
     
  19. Offline

    bobacadodl

    -cut-
    Sorry, I completely misread your last post. My mistake
     
  20. Offline

    Commander9292

    bobacadodl
    I think the OP wants an item to orbit around an entity. Not entirely sure, that's just what I got from their last post.
     
  21. Offline

    bobacadodl

    This is assuming you basically want an entity to "orbit" around a location.

    Code:
    public static List<Vector> getCircularVector(Location center, int radius, float precision) {
            List<Vector> ret = new ArrayList<>();
            Vector last = new Vector(radius + center.getX(), center.getY(), center.getZ());
            for(float i=precision; i<2*Math.PI; i+=precision) {
                Vector v = new Vector(Math.cos(i) * radius + center.getX(), center.getY(), Math.sin(i) * radius + center.getZ());
                ret.add(v.subtract(last));
                last = v;
            }
            return ret;
        }
    Just loop through the result of that method, and every tick set the entity velocity to the vector
     
  22. Offline

    HeavyMine13

    Thanks for your post, first of all. Second, its not an entity. I have a custom made particle shape and I want it to rotate around a specified location. For example: The particle will act like a satellite and the location will act like a planet. So I can't use that method, since I can't (I think) set a particle velocity! Thanks!
     
  23. Offline

    chaseoes

    Have you seen this? http://forums.bukkit.org/threads/ef...-the-nice-way-text-image-in-particles.259879/
     
  24. Offline

    HeavyMine13

  25. Offline

    HeavyMine13

  26. Offline

    bobacadodl

    HeavyMine13
    I still have no clue what you're having trouble with... Just get a circle of locations around the player and using a runnable, spawn a new particle in the next location every few ticks.

    Code:
    public static List<Location> getCircle(Location center, int radius, float precision) {
            List<Location> ret = new ArrayList<>();
            for(float i=precision; i<2*Math.PI; i+=precision) {
                ret.add(new Location(center.getWorld(), Math.cos(i) * radius + center.getX(), center.getY(), Math.sin(i) * radius + center.getZ()));
            }
            return ret;
        }
     
Thread Status:
Not open for further replies.

Share This Page