# Get and set vector magnitude.

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

Not open for further replies.
1. Offline

### srspore

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!

#1
2. Offline

### mythbusterma

@srspore

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

#2
3. Offline

### mcdorli

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);
arrow2.setShooter(player);
arrow.remove();

The "arrow" variable is the original arrow, the arrow2 is the spawned one

#3
4. Offline

### mythbusterma

@mcdorli

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

Code:
```double magnitude = incoming.length();
output = output.normalize().multiply(magnitude);```

#4
mcdorli likes this.
5. Offline

### mcdorli

Oh...yes...That's a good way too...

#5
mythbusterma likes this.