Get and set vector magnitude.

Discussion in 'Plugin Development' started by srspore, Sep 29, 2015.

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


    I basically want to make an arrow bounce off of something at the same speed it came in at. So I have one vector that is the arrows velocity and I have another vector that is the direction I want the arrow to go in, however the magnitude is not the same as the arrows velocity, so I need to get the arrows magnitude and set it to the magnitude of the vector the arrow should follow when it bounces. Thank you in advance!
  2. Offline



    Well, you can normalise the output Vector and then multiply by the incoming velocity.
  3. Offline


    Getting the magnitude is just a mathematic equation.

    Vector vec = arrow.getLocation().getVelocity();
    return Math.sqrt(vec.getX()*vec.getX() + vec.getY()*vec.getY() + vec.getZ()*vec.getZ());

    Setting the magnitude the negative of the original is easier, you need to use the magnitude you got above (named mag)

    Arrow arrow2 = world.spawnArrow(arrow.getLocation(), arrow.getLocation().getDirection(), -mag, 0);

    The "arrow" variable is the original arrow, the arrow2 is the spawned one
  4. Offline



    *coughs loudly*
    Reinventing the wheel like always, I see.

    double magnitude = incoming.length();
    output = output.normalize().multiply(magnitude);
    mcdorli likes this.
  5. Offline


    Oh...yes...That's a good way too...
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page