Advanced Vector Help Needed

Discussion in 'Plugin Development' started by MasterGberry, Aug 17, 2014.

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

    MasterGberry

    So I am trying to do some vector math, and I'm pretty awful with them. I would say this is a lot more challenging than simple vector math.

    http://prntscr.com/4dph2b

    Circle - Player
    Blue Box - Corner of rectangle
    Red Square - Center point
    Black Rectangle - Area of effect for a player's ability

    Axis is labeled in the corner. That's the X and Z axis for the regular Minecraft block system. I want to try and get the direction the player is facing, and shove all the players back who is in front of him in a certain area (the rectangle).

    I am having issues because of the rotations and this needing to be based on the player's direction (re-orientating the axis). I managed to get the red square by using this:

    Code:java
    1. Vector vector = attacker.getLocation().getDirection().add(attacker.getLocation().getDirection()).add(attacker.getLocation().getDirection());


    That information might be entirely useless. But anyways yeah I am trying to create that rectangle and then iterate through the players online and determine if a player is in that rectangle or not. Once again the rectangle is based on the player's direction.

    Thanks for any guidance/help.
     
  2. Offline

    AoH_Ruthless

    MasterGberry
    How do you plan on getting the center of the rectangle? Is it target block?
     
  3. Offline

    St3venAU

    I know this isn't exactly an answer to your question, but maybe it will work for your purposes. If you are happy with using a wedge shape instead of a rectangle, then this might be an easier approach:

    - Get all players within a certain range of your player using getNearbyEntities (this will include players behind your player for now but that's ok).
    - For each of those players, calculate the vector that connects your player to the other player (subtract the two player's position vectors).
    - Measure the angle between that vector and your main player's direction vector.
    - If that angle is less than a certain threshold then apply the effect you wish (the larger this threshold, the wider the area of effect would be).
     
  4. Offline

    MasterGberry

    AoH_Ruthless thats what im trying to figure out
    St3venAU not a bad idea...could do that too..
    Anyone else got any ideas?
     
  5. Offline

    JBoss925

    Matrices to the rescue!!!
    Basically, create a rectangle that is in line with the coordinates.
    Then, get the points within the rectangle (x,y,z coords) now because it'll be much harder later on.
    Make sure to store these locations somewhere.
    Now, loop through the locations rotating them using a matrices with the yaw(note: make sure yaw isn't a float by doing Math.toRadians(yaw)) like so:
    Code:java
    1. List<Location> locs = //locations you got from the coordinate-aligned rectangle;
    2. List<Location> newlocs = new ArrayList<Location>();
    3. Location loca = p.getLocation();
    4. Float yaw1 = loca.getYaw();
    5. double yaw = Math.toRadians(yaw1);
    6. for(Location loc : locs){
    7. double rotatedX = Math.cos(yaw) * (loc.getX() - loca.getX()) - Math.sin(yaw) * (loc.getZ()-loca.getZ()) + loca.getX();
    8. double rotatedZ = Math.sin(yaw) * (loc.getX() - loca.getX()) + Math.cos(yaw) * (loc.getZ() - loca.getZ()) + loca.getZ();
    9. newlocs.add(new Location(p.getWorld(), rotatedX, loc.getY(), rotatedZ));
    10. }

    after rotation is done add them to a new location list.
    Then loop through the new location list and teleport players to them.
     
Thread Status:
Not open for further replies.

Share This Page