 # Solved "Cutting" a vector

Discussion in 'Plugin Development' started by colozz, Jul 10, 2013.

Not open for further replies.
1. Offline

### colozz

Hi, I'm searching for "cutting" a vector but my request is not very easy to explain so I'm gonna use pictures. I have a stone block and above this one, there is an air one.
The center of the air cube is represented with a green dot, the player is represented by a blue dot and the vector is the purple arrow.
So I have a cube that is launching arrows (don't ask me why ^^) and I set the arrow velocity with the substract method with the player's location so the player receive the arrow.
But the problem is that if the player is under the stone cube : The arrow is stuck on the stone block.
So I was searching for "cutting" the vector and get a new starting point for this one like this (the red dot) : Finally I'm just searching for cutting the beginning of the vector of two meters.
Thank you for helping !

#1
2. Offline

### xTrollxDudex

colozz
Save the vector. Then move the arrow under the block then reapply the vector

#2
3. Offline

How to ?

#3
4. Offline

### andf54

The solution depends on what you want to do for intermediate angles.

What if the projectile needs to be shot at -70 deg? Do you to do it the same as the -90 deg example or should the shoot location be still on top?

Or do you want the projectile to be shot from below if the angle is negative and above if the angle is positive?

#4
5. Offline

### colozz

I only want the arrow to be shot/spawned 2 meters further than a location.

#5
6. Offline

### andf54

Then why make it so complicated?
Code:
`Location shifted = originalLocation.clone().add(0.0, -2.0, 0.0);`

#6
7. Offline

### colozz

I want the arrow shot two meters further but on the vector like this : #7
8. Offline

### andf54

Code:
```        Location lshooter = something;
Location ltarget = something2;
Vector shift = ltarget.toVector().subtract(lshooter.toVector()).multiply(2.0); // May be other way around
```

#8
9. Offline

### colozz

Ok so using your code :

Code:java
`Location t = zone.get(str); // It's the shooter location Vector v = p.getLocation().toVector().subtract(t.toVector()).multiply(2.0); t.getWorld().spawnArrow(t.clone().add(v), v, 2.0f, 0.0f);`

But ingame I didn't see any arrow spawning.

#9
10. Offline

### andf54

Oh, wait sorry, forgot to normalise.
Code:
```    Location t = zone.get(str); // It's the shooter location

Vector v = p.getLocation().toVector().subtract(t.toVector()).normalize().multiply(2.0);

```
In the old code it shot the arrow 2x the distance of the target.

#10
colozz likes this.
11. Offline

### colozz

andf54 that works, thanks #11