0
votes

I want to create a "spiral effect" with particles (or any entities) in Java.

I'm new to objective programming (and also Java), so I started with something easier. I firstly created a Path object that has a value of Locations[] signed to it, it gets from the user a: Start location, End location, and double value, that tells him, how much space between each location in the path he has.

    private void setLocations() {

    //initialize vars
    Location start = getStart();
    World world = start.getWorld();
    Location[] locations = new Location[amount];
    double x = start.getX();
    double y = start.getY();
    double z = start.getZ();

    //loop that will set values for locations
    for (int i = 0; i < amount; i++) {
        locations[i] = new Location(
                world,
                x + dividedDistanceX * (i + 1),
                y + dividedDistanceY * (i + 1),
                z + dividedDistanceZ * (i + 1)
        );
    }

    this.locations = locations;
}

Now you might be asking what is the amount? So simply it's the number of points that are created when the object is initialized. It's simple math like getting the longest distance from point to point, and then dividing it by the value of space between each point.

Now the situation gets a little more complicated, so I prepared graphics for you:)

3D graph

I want to rotate points around the longest axis to form some form of a spiral, and I want from user to set the maximum distance between the starting point and the new one.

Something like this:

Blue points are an example of new points that belong to spiral

And another graph of the sinusoid around one vector (x, y)

Sinusoid around vector(x, y)

Honestly, I need some help.

Here's GitHub object link

Things I know I need to do:

  1. Get the axis around which I will rotate point (it's the longest distance between points)

  2. Add some value to the rest values (x+something, y+something)

  3. Add angle, that point will rotate with, (for example each point will be rotated by 22,5).

1

1 Answers

0
votes

Okay, so i did it, it wasn't even that hard:

public Location[] createSpiral(double radius, float angle, Location[] path) {
final int length = path.length;
Location[] result = path.clone();
Location start = path[0];
Location end = path[length - 1];

double startX = start.getX();
double startY = start.getY();
double startZ = start.getZ();
double endX = end.getX();
double endY = end.getY();
double endZ = end.getZ();
double distanceX = setDistance(startX, endX);
double distanceY = setDistance(startY, endY);
double distanceZ = setDistance(startZ, endZ);
double highestOffset = getHighestOffset(new double[]{distanceX, distanceY, distanceZ});


if (highestOffset == abs(distanceX)) {

    for (int i = 0; i < length; i++) {
        double sin = radius * sin(angle * i / length);
        double cos = radius * cos(angle * i / length);
        result[i].setY(result[i].getY() + cos);
        result[i].setZ(result[i].getZ() + sin);
    }

} else if (highestOffset == abs(distanceY)) {
    for (int i = 0; i < length; i++) {
        double sin = radius * sin(angle * i / length);
        double cos = radius * cos(angle * i / length);
        result[i].setX(result[i].getX() + cos);
        result[i].setZ(result[i].getZ() + sin);

    }

} else if (highestOffset == abs(distanceZ)) {
    for (int i = 0; i < length; i++) {
        double sin = radius * sin(angle * i / length);
        double cos = radius * cos(angle * i / length);
        result[i].setX(result[i].getX() + cos);
        result[i].setY(result[i].getY() + sin);

    }

} else {
    return path;
}

return result;

}

It's just

double sin = radius * sin(angle * i / length);
double cos = radius * cos(angle * i / length);

and adding those values to corresponding X, Y if Z has the highest distance from a location, etc.

The rest of the code and methods are located in the GitHub link above.