This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Object A wants calculate an angle at which to Intercept Object B along its path of movement in an x,y system. Speed of A&B is known, as well as distance between the 2 and angle of movement of A&B. (and A can rotate freely)

Although this sounds rather simple to me, i have thought hard about it and couldnt come up with a solution so far. I thought the best solution would probably be to use a system of linear equasions, but i couldnt figure out how to do it right, as well as how to implement it in java. So the second best idea i had was to treat Time as a constant and use a recursive method that increments the time factor until a solution is found or until it would take too long.

3 vectors: (angles are absolute)
- direct line from A to B: length l, angle alpha
- line from target to contact point: length vB*Time, angle beta
- line from A to contact point: length vA*Time, angle gamma

Speed vA/B is 1 or 2 pixels per frame. (along the axis of movement, so the actual movement is cos/sin*v for either x or y)
Speed for Object A can be assumed to be 2 since it wants to actually catch B, speed for B can either be 1 or 2.
So i use 2 equations that add up the x,y components of the 2 first vectors to get to the third:

Since cosine and sine switch places depending on where the angle is facing the whole thing gets a little complicated.

(A->contact) = (A->B) + (B->contact)

X/Y: 2 * t * cos/sin(gamma) = l * cos/sin(alpha) + vB * t * cos/sin(beta)

--->cos/singamma:

X/Y: cos/sin(gamma) = (l * cos/sin(alpha)) / (2 * t) - vB * cos/sin(beta) / 2

At this point i have only constants on the right side of the equation and i can solve it. The problem that remains is, for alpha and beta i can easily find out whether to use sine or cosine for x/y, but for gamma i cant. So right now my method returns a -array of sine of cosine, which i could use to find out the angle, but i dont know which is which. If either of the values is smaller than -1 or bigger than 1 the method calls itself again and increments t by 1, which i figured should work out for values of t that are too small to make contact possible.

##### Share on other sites
My first recommendation is to stop thinking in angles and distances: If you think in vectors instead, your life will be much better. Second, you should learn to search for information. There have been many threads about this in the past. See this one for example.

If you still have trouble getting it to work, post again.

##### Share on other sites
Thanks for the link, it was quite helpful. I looked at your formulas und played around a little with my calculator, and came to notice that in case v.v = s^2, the term will end up beeing 0, making a solution impossible. Although this probably wont happen too often it doesnt feel right.

t^2(s^2-(v.v))-2t(A-B).v-(A-B)(A-B)=0

or t^2-(2t(A-B).v / s^2-(v.v))-((A-B)(A-B) / s^2-(v.v)) = 0

In both cases, i will end up dividing by zero at some point. Should i try to get an approximation using 0+(terribly small number h)? Did i misunderstand something?

##### Share on other sites
If the bullet and the target are going at the same speed, you won't be able to hit it, unless the target is coming towards the attacker. What kind of game are you making, where bullets and targets might be going at the same speed?

##### Share on other sites
Nah those arent bullets, but creatures. Its not really a game, more of a simple life simulator kind of thing. Simple ai driven by the basic needs of creatures expressed in values. Anyway, the method will be used when one of the creatures gets angry and wants to attack another creature, its not even totally necessary but it would be cool if it would try to intercept instead of just following it - i also do it to learn how it is done.

The point is, depending on the direction of the movement, interception can be done if both are same speed.

---------> v=(2,0)
^
|
|
(A.B)=(0,2)

s=2

This was the example i was thinking about but thinking about it again it seems kinda logical that A cant intercept B. But if it was like you said, and Object B was moving towards A, the problem arises again.

B
x---------> v=(2,0)
.............^
............ x s=2
.............A

Also in this example, if both were to move at the same speed, A can intercept B, depending on the length of (A-B). Disregard the ".", theyre just there because the forum wont display spaces.

• ### Game Developer Survey We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 56
• 18
• 28