[MATH] create a circular location

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

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

#1
2. Offline

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!

#3
HeavyMine13 likes this.
4. Offline

HeavyMine13

That would be amazing, thanks!

#4
biel likes this.
5. Offline

Chiller

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

#5
6. Offline

HeavyMine13

And how do I iterate through a box xD

#6
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. } }

#7
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
#8
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) {
}
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.

#9
10. Offline

HeavyMine13

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

#10
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

#11
12. Offline

HeavyMine13

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

#12
13. Offline

DevilMental

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

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

#13
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) {
}
return temp;
}```

#14
15. Offline

HeavyMine13

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

#15
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...

~DevilMental

#16
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

#17
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.

#18
19. Offline

-cut-

#19
20. Offline

Commander9292

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.

#20
21. Offline

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());
last = v;
}
return ret;
}```
Just loop through the result of that method, and every tick set the entity velocity to the vector

#21
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!

#22
23. Offline

#23
24. Offline

HeavyMine13

Of course, I contribute to it always And Slikey is my friend

#24
25. Offline

HeavyMine13

Bump

#25
26. Offline

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) {