Vectors, these strangers.

Discussion in 'Plugin Development' started by Flegyas, Jul 20, 2014.

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

    Flegyas

    Hi all.
    My problem is about the creation of a circular movement (I've read other thread about that but I didn't find a solution working for me/they had no answer).
    So, basically, I tried the implementation of the "uniform circular motion" only changing the velocity and in fact it worked "a bit" in that way.
    Strangely, the entity moving was on another circle than the one described by me, the command sender should be on the center of the circle, but he seemed to be on the circumference.
    I thought it was my fault (the implementation was very weird xD) so I tryed with an easier one:
    Calculating the positions where the entity should move (in a sequence) basing them on vertices of multiside polygons and at every update teleporting it to the next vertex.
    Again, the entity seems to be on another path, with the command sender in one vertex.
    This is very weird for me, also because I've done some debug like this:
    Code:java
    1. world.getBlockAt((nextLoc = locations.next()).subtract(0, 1, 0)).setType(
    2. Material.LAPIS_BLOCK);
    3. spawned.teleport(nextLoc);

    And the lapis blocks were placed in the right positions, but the entity still was moving on his own path.

    As you can see, it's between my legs xD
    [​IMG]

    Has someone an idea of what is happening?
    Thanks to all.

    OK, I managed to find the solution but the doubt about vectors/teleport is still here.
    I fixed the problem spawning the entity directly on the first vertex, but why the teleport doesn't work?
    If I teleport it to a location, it doesn't go to the right location, I think is like a "relative teleport" to something I don't know :confused:

    Another question:
    What is the right way to calculate a "vector between two Location"? Reading on the forum I've read that I should do:
    Code:java
    1. endLoc.toVector().subtract(startLoc.toVector());

    But using it the entity goes "to infinity and beyond" xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    xTrollxDudex

    Vector represents magnitude and direction, you want the distance between two points, not two magnitude/directions.

    In that case, use Location#distanceSquared(Location). Or you can do it the hard way and use Pythagorean theorem.
     
  3. Offline

    Flegyas

    First, thank you for the reply.
    Uhm, I think you're right, but partly.
    I still need the direction because I want the "velocity vector", is there an easy method to calculate it? (I'm doing setVelocity(Vector vector) to change the movement)
     
  4. Offline

    xTrollxDudex

    Flegyas
    You can always convert the locations back as a vector when you're done.

    Or is your question to make a single vector from those 2 points?
     
  5. Offline

    Flegyas

    xTrollxDudex Yes, I'd like to know what is the right way to calculate vectors between two points because I need directional velocity.
     
  6. Offline

    xTrollxDudex

    Flegyas
    Try switching the endLoc and startLocs on your vector subtraction?
     
  7. Offline

    Flegyas

    xTrollxDudex
    I tried all possible combinations/transformations , no way to get the right one xD
     
  8. Offline

    xTrollxDudex

    Did a bit of research and I was wrong :p

    How are you getting the entity to move with the vector?
     
  9. Offline

    Flegyas

    xTrollxDudex
    Simply with world.dropItem method.
     
  10. Offline

    Traks

    To get the vector between two points in 3-dimensional space, just treat the points as vectors starting at (0, 0, 0) and subtract the first one from the second one. So if you would want to obtain the vector from (x1, y1, z1) to (x2, y2, z2), that would be (x2 - x1, y2 - y1, z2 - z1).
     
  11. Offline

    Flegyas

    Traks I think that some code will explain better than me. This is the method I'm using to set the position at every update (I've scheduled un updater for the class):
    Code:java
    1. public void update() {
    2. Location actualLoc = spawned.getLocation();
    3. if (actualLoc.distance(nextLoc) <= RANGE) {
    4. Location next = locations.next();
    5. spawned.setVelocity(velocity = next.subtract(nextLoc).toVector());
    6. nextLoc = next;
    7. } else spawned.setVelocity(velocity);
    8. }

    Where "spawned" is the item I've spawned on the object creation and "locations" is an iterator of a Location List (the locations represent the vertices of the polygon) and "nextLoc" is the current location to move to.
     
  12. Offline

    Flegyas

    A little bump, I'd like to find the method to get the velocity between two point, for example:
    If A is at (10, 70, 10) and B is at (50, 100, 30) how can I move A to B setting its velocity to something?
    I've tryed with destination.subtract(start) where destination and subtract are both Vector/Location but without results :l
    Thanks.
    xTrollxDudex , Traks
     
  13. Offline

    Traks

    What method did you utilise to move, i'm assuming, an entity from A to B? You would need Entity#setVelocity(org.bukkit.util.Vector) either way.
     
  14. Offline

    Flegyas

    Traks
    Yes, I'm using that, I've also tryed with a simple test:
    1) Spawn Item in A.
    2) At every update, calculate B-A and set the velocity of the Item to that amount.
    3) Yell at the item which remains there.:mad:
     
  15. Offline

    Traks

    Could you post the code you used?
     
  16. Offline

    Flegyas

    Traks in onCommand:
    Code:java
    1. // Identify the command
    2. final World world = player.getWorld();
    3. final Location dest = new Location(world, 0, 10, 50);
    4. final Item item = world.dropItem(new Location(world, 0, 10, 0), new ItemStack(
    5. Material.APPLE));
    6. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    7.  
    8. @Override
    9. public void run() {
    10. item.setVelocity(dest.subtract(item.getLocation()).toVector());
    11. }
    12.  
    13. }, 0, 20);
    14. // Something else
     
Thread Status:
Not open for further replies.

Share This Page