Reputation: 77
Problem: I have two vectors. I know the starting point of one vector, its direction, its magnitude. I know the starting point of the other vector and its magnitude. I need to find the direction of second vector as well as the position of intersection.
Vector A: Vector B:
Position = Known Position = Known
Direction= Known Direction= UNKNOWN
Magnitude= Known Magnitude= Known
To Find: Point of intersection.
Is it possible to find the point of intersection, with the given parameters? If so then how?
Application: I want to find the position where a player would be found based on the velocity he is moving, and shoot a bullet at him at the moment he would be found, taking into account the time taken for the bullet to reach that virtual target position.
Upvotes: 0
Views: 2511
Reputation: 15035
Following on from the comments I'm going to take a leap here and answer your ultimate question directly.
Say the player is, at the initial time, at a point p
and travelling with velocity v
; your gun is at position q
and shoots a bullet in any direction at speed s
:
The length of OP is vΔt
and that of Q sΔt
. The angle a
is given by the dot product:
We can then use the cosine rule to solve for Δt
:
Written in this form, we can easily see that it is a quadratic equation, and thus directly solve for Δt
using the Quadratic formula:
There are a few cases we need to consider here:
v < s
: need to take the positive root only as otherwise we would get negative time.v > s
and dot(PQ, s) < 0
: the bullet will never catch the player.v > s
and dot(PQ, s) > 0
: take the negative root this time, as the positive root is for a backwards travelling player (longer time; this is also the case presented in the diagram above).Having the correct value for Δt
from above will then enable us to find the intersection point o
, and therefore the intended direction d
:
Note that d
is not normalized. Also, this solution works for 3D too, unlike an approach with angles.
Upvotes: 5
Reputation: 984
Let subscript 1 mark the player, and subscript 2 mark the AI:
The positions as a function of time t are :
You have 2 uknowns:
The collision happens when Xs and Ys match. i.e.:
So,
and also
substitue your values and equate these to expressions of alpha_2 to obtain t, then you can substitue t in either expression to obtain the angle alpha_2.
Upvotes: 1