Solved I need a location iterator that uses exact decimal locations

Discussion in 'Plugin Development' started by guangong4, Jun 30, 2014.

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

    guangong4

    Solved.
     
  2. Offline

    AoH_Ruthless

    guangong4

    Do you even understand what the class does? It's pretty clear you copied the BlockIterator class verbatim ...
     
  3. Offline

    guangong4

    AoH_Ruthless
    I understand what it does as a whole and how to use it, but not exactly in each line / method. If you could provide a brief explanation and point me in the right way, I could accomplish what I'm trying to do on my own.

    I've been using BlockIterator to make lines of particle/block break effects, but these don't appear in a perfect line all the time. That's what I'm trying to change.
     
  4. Offline

    AoH_Ruthless

    guangong4
    Why not use the normal BlockIterator? Then, while the BlockIterator#hasNext(), you could get the location of the next block and send some sort of particle effect to players at that location. I would try that anyways, to see if it provides a relatively straight line.

    Wouldn't a straight line only be present if the yaw and pitch were multiples of 90? I am not sure though, haven't really delved too deep into the actual meaning of the BlockIterator class.
     
  5. Offline

    afistofirony

    guangong4 So basically, you want a way to iterate along a line with a custom distance? That's pretty easy.

    Code:
    import org.bukkit.util.Vector;
    import org.bukkit.Location;
     
    public class RayTrace {
     
        Vector direction, currentPos;
     
        public RayTrace(Location origin) {
            this.currentPos = new Vector(origin.getX(), origin.getY(), origin.getZ());
            this.direction = RayTrace.toVector(origin.getYaw(), origin.getPitch());
        }
     
        public Vector iterate(double distance) {
            return (this.currentPos = this.currentPos.add(direction));
        }
     
        public static Vector toVector(float yaw, float pitch) {
            float rotX = yaw, rotY = pitch, cosY = Math.cos(Math.toRadians(rotY));
     
            float x = (-cosY * Math.sin(Math.toRadians(rotX))),
                    y = (-Math.sin(Math.toRadians(rotY))),
                    z = (cosY * Math.cos(Math.toRadians(rotX)));
     
            return new Vector(x, y, z); // you may have to cast to double here
        }       
     
    }
    Now you can just use this class whenever necessary:
    Code:
    RayTrace trace = new RayTrace(player.getLocation());
    for(int i = 0; i < maxDistance / distancePerIteration; ++i) {
        Vector target = trace.iterate(distancePerIteration);
        // do stuff with the new target
    }
    EDIT: in the above example, maxDistance * distancePerIteration should be maxDistance / distancePerIteration.
     
  6. Offline

    guangong4

    afistofirony
    Thanks! It works perfectly.
    I just want to know, where is the distance argument for RayTrace.iterate used?
     
  7. Offline

    afistofirony

    guangong4 oops, I should probably fix that. :p

    I also didn't realize Bukkit's Vector class is mutable, so this should account for that as well.

    Code:
        public Vector iterate(double distance) {
            return  this.currentPos.add(direction.clone().multiply(distance));
        }
    
     
Thread Status:
Not open for further replies.

Share This Page