# Velocity towards a certain point

Discussion in 'Plugin Development' started by arjanforgames, Dec 15, 2013.

Not open for further replies.
1. Offline

### arjanforgames

Let's say I spawn an arrow entity just above my player.
Is it possible to set it's velocity so it will land exactly at a marked spot?
So it will shoot up higher/faster if it's far away etc.

#1
2. Offline

### BeaztX

Hello, you could save the distance as a variable, then multiply the vector depending on the distance, tho i think there's a method to set exact travel route for Entities. Not sure...

#2
3. Offline

### arjanforgames

Yeah, so I'm having trouble with this that's why I posted here.
Please post a snippet of code that could help me calculating everything, I'm bad at mathematics

#3
4. Offline

### NathanWolf

I think it's very difficult to have it land at an exact spot unless you set the arrow speed so high that it basically avoids gravity.

To fire it "towards" a certain point, you want to use a little bit of geometry for that

If you have two points, A and B, the direction from one to the other is the line segment that connects them. A vector is basically a line segment with one end at the origin, so to get a direction vector from two points you subtract one from the other- making one of them the origin.

Translated to code, that would look like:
// Assume you have two Location's, playerLocation and targetLocation
Vector direction = targetLocation.toVector().subtract(playerLocation.toVector());

This will give you a direction from the first point to the second. Firing an arrow with this vector should approximate firing an arrow as if the target were under your crosshairs- but this does not account for gravity, just like you would have to if you were shooting the bow normally.

Hope that helps!

#4
5. Offline

### blablubbabc

Additionally you can also constantly reset the arrows velocity each tick to the wanted direction and speed. That way you can avoid the gravity influence.

#5