Vector calculation problem

Discussion in 'Plugin Development' started by connor stone, Oct 28, 2013.

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

    connor stone

    I'm trying to take a living entity (zombie for example) that is near a player, and give it a velocity that would land it at the exact location of the player. Heres what i have now

    1. public boolean execute(){
    2. if (!mayExecute())
    3. return false;
    5. Location pLoc = p.getLocation();
    6. int i = 0;
    7. for (Entity it : p.getNearbyEntities(range, range, range)){
    8. if (i++ >= maxTargets) break;
    9. if (!(it instanceof Creature)) return false;
    10. if (!(p.hasLineOfSight(it))) return false;
    12. Location eLoc = it.getLocation();
    14. Vector from = new Vector(eLoc.getX(), eLoc.getY(), eLoc.getZ());
    15. Vector to = new Vector(pLoc.getX(), pLoc.getY(), pLoc.getZ());
    17. Vector vector = to.subtract(from);
    18. it.setVelocity(vector);
    19. }

    What it ends up doing is exactly what i want for the most part, the entity shoots directly at the player from where ever it is....but it goes about twice as far or more past the player...any help?

    After scouring the internet for hours upon hours, I've realized that this is a extremely complex college-level physics problem that takes into the account many things

    minecraft's -0.02 velocity per tick drag
    the distance of the two entitys

    and many other things...Plus adding the fact that i want to velocity to go in the direction of the other brain is fried. Any pros who know the answer? I want to take a nearby entity, and give it the velocity required to move directly at the player, and stop at the player aswell. Comphenix LucasEmanuel .Link to minecraft motion stuff:

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


    connor stone
    It's not college-level physics, but it's definitely harder than your average vector math. Here's some food for thought:
    - There are actually infinite possible velocities that would cause the mob to land on the player, because two points can be passed through by infinite curves.
    - Assuming the moving entity will start on the ground, you'll probably always want it to arc upwards. Hopefully that narrows down your possible solutions.
    - The important input is the Y velocity. X and Z aren't so important; all you need to do to find those is find the direction vector between the two entities (pos2 - pos1). The only hard part is finding the correct Y value.

    If you need more in-depth help, when I get home tomorrow I'll draw you some pictures to illustrate what I'm talking about.
    Jake6177 likes this.
  3. Offline

    connor stone

    Any suggestions on what to try? a formula example?

    Bump. Anybody good enough at physics for this? I've asked a calculus professor and my friend who takes college physics and they say because the drag is always changing based on the velocity of the entity (which is unknown), its too complex for them

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


    connor stone
    I'm no college student, never fear, a 12 year old is here!

    Remembering from my notes: "See, the total distance of a projectile excluding air resistance and yaw when given:
    a-angle (lol can't find the theta or whatever symbol)
    g-gravitational acceleration: 9.81 m/s^2
    d-distance traveled
    y-distance off the ground

    d=((v*cos(a))/g)*(v*sin(a)+sqrt((v*sin(a))^2 + 2*g*y))
    If I remember correctly. Look it up.
  5. Offline

    connor stone

    @xTrollxDudex what? im looking for the velocity something needs when i only have the distance it has to travel
  6. Offline


    connor stone
    Oh whoops! Lol.

    Try using .muliply(0.5) on the vector
  7. Offline

    connor stone

    xTrollxDudex I'll try that. Where does it get its speed from when it subtracts like that? is it always the same?
  8. Offline


    connor stone
    I don't get your questions, but if I explain it:
    So, your entity moves twice as far right? Well if you multiply by 0.5, the velocity it will move at is half the magnitude of the vector, and half the distance.

    If you don't get it, then, a person older than 12 will have to explain :p
  9. Offline

    connor stone

    I asked why it is that when you subtract the velocity of the entity from the player it suddenly has a large speed, i know what multiplying by .5 is, I'm not stupid :), i didn't even ask about that. I said id try it.

    Problem is its not always exactly twice as far, sometimes its close to correct and sometimes it over shoots by 50 blocks. I need an actual fix for the problem by calculating the velocity its going to need each time depending on its distance, but i don't have a clue how
Thread Status:
Not open for further replies.

Share This Page