Getting location relative to an entity's yaw

Discussion in 'Spigot Plugin Development' started by _Ug, Jun 23, 2016.

  1. _Ug

    _Ug

    Hey, was working on a project and was wondering if anyone knew their way around vectors and could help me out here. Essentially what I have is a list of relative locations (to the center entity) when the yaw is at 0 and need those locations to be altered as the entity in the center looks in different directions to maintain the same shape in all directions. In case this is confusing I'll draw a quick example:

    http://i.imgur.com/06jV4NQ.png

    With the circle being the center and the arrow being the current direction. Essentially keeping all the locations ("L") in the same position relative to the center entity's yaw.

    What I have tried:
    I have tried to get the location of the core entity and add a new vector to it that is a normalized vector of the core entity's direction multiplied by a new vector of the distance between location on all 3 axes. That did not seem to work whatsoever, making the locations just go farther / closer as the center entity looked around.
     
  2. Could you not just save where you want each entity to be placed relative to the "core" entity, then, with a repeating task, set the locations of your outer entities to the location of the "core" entity +/- the relative location to it?


    You would just have to work with locations then, and not vectors. Or would that not exactly work for what you are doing?
     
  3. _Ug

    _Ug

    As I said in the initial post, I want it to be relative to the entity's yaw, not just their location. If you look at the picture you should hopefully get a better understanding for what I want to be done.
     
  4. _Ug

    _Ug

    Just tried to use that. I managed to get it working in that the same block is in front when looking +-x and the same block is in front when looking +-z. However, when looking +-z the block in front/back is different than that when looking +-x. Also, this makes it so that blocks are super close when not looking in their direction and super far out when looking in their direction.
     
  5. Let me get some stuff clear first.
    You want to get the locations of points on a circle around the player?
    Have you tried to Google on how to do this with particles? And then you can use that code. I've seen those pieces of code on the forums.
    If this is not what you want then please day what you want.
    And give the code that doest work
     
  6. _Ug

    _Ug

    The circle is a simple example. What I would like is to have a list of locations relative to a core location when the core location entity's yaw is 0. I then want all of those locations to never change in distance from the central entity as the central entity looks in all directions but I do want them to be at the same spot relative to the central entities point of view. An example of this would be (and this is not what I am doing but an example that should make it clear enough) that I have a player and I want to have a surfboard for the player made out of particles but always have them be in the same direction relative to the player's yaw. So if the player is looking straight it would be in front and behind no matter where they are looking and would never go sideways.

    Here is my current (not working) code:
    Code (Text):
                        double x,y,z;
                        Location a = blocks.get(mb);
                        Location b = blocks.get(core);
                        x= a.getX()-b.getX();
                        y= a.getY()-b.getY();
                        z= a.getZ()-b.getZ();
                       
                        double yaw = Math.atan2(z, x);
                       
                        double pitch = Math.atan2(Math.sqrt(Math.pow(z, 2) + Math.pow(x, 2)), y) + Math.PI;
                       
                        double X = Math.sin(pitch) * Math.cos(yaw);
                        double Y = Math.cos(pitch);
                        double Z = Math.sin(pitch) * Math.sin(yaw);
                       
                        Vector vector = new Vector(X,Y,Z).multiply(bottom.getLocation().getDirection().normalize());
                        Location f = location.clone().add(vector);
                        mb.getArmorStand().teleport(f);
    With a and b being the original relative locations, x, y, and z being the original distance between the two locations, and "location" being the location that the core is currently at. This gives a result that is not desired in two ways. First of all, it will keep the same location in front of the player when looking the same or opposite direction (+ or - x and + or - z). The other problem with this, however, is that the locations grow in distance from the center as the entity looks towards their direction or the opposite of it but when looking on z if the location is centered on x the locations will be inside of the center entity (crushed down in distance). I know this is probably hard to understand so I will try to get a gif of it in action.
     
  7. If this is for an entity, then you can get the yaw and pitch from the entity.
    So you would get this:
    Code (Text):
    double yaw = player.getLocation().getYaw();
    double pitch = player.getLocation().getPitch();
    Then we need to specify the location around a person.
    We do this by having a yaw and pitch and a distance variable.
    Code (Text):
    double locYaw = 10;
    double locPitch = 0;
    double locDistance = 2;
    So, the location is going to be a bit to the right, 2 blocks away and in the center of the screen.

    Now we need to calculate the delta vector. This vector is basically the offset.
    I'm now just taking the piece of code you took.
    Code (Text):
    double X = Math.sin(locPitch + pitch) * Math.cos(locYaw + yaw) * locDistance;
    double Y = Math.cos(locPitch + pitch) * locDistance;
    double Z = Math.sin(locPitch + pitch) * Math.sin(locYaw + yaw) * locDistance;
    The X,Y,Z variables are now just the offset that you need to add to the player's location.
    So you can add the variables to a clone of the player's location and you got the location relative to where the player is looking.
    Now I do want to say that when you look up, the position goes up as well. So if you don't want that, then you need to just set the pitch variable to a constant value like 0.

    I am doing this out of my head without testing, but it should work.