Velocity based intersections
I'm stumbling on this one..
I have two 2D rays,
Ray 1, has a start location, a direction and a velocity
Ray 2, has a start location and a velocity.
The velocities are different from each other.
How do i find Ray 2's angle such that the two rays will intersect at the earliest time. I realize they may not intersect, either because they are parallel, or because based on time, they miss each other.
Put another way, where is the collision point?
[490 byte] By [
dmbdmba] at [2007-9-29 9:40:59]

This article should help you :-'Collision between two moving circles' http://www.gamasutra.com/features/20020118/vandenhuevel_02.htm
Abusea at 2007-7-14 23:12:53 >

It seems like it would be a much simpler problem if you tried to solve it by adopting a moving frame of reference. In the frame, the 'particle' whose direction and velocity are known comes to rest (at the origin) and the other 'particle' is now moving in the opposite direction and velocity. Now the problem becomes, can I find a direction with given velocity which when added to the given vector will intersect my new frames origin.
Don't have time to go through the math on this, but hope it helps.
> This article should help you :-
>
> 'Collision between two moving circles'
>
> http://www.gamasutra.com/features/20020118/vandenhuevel
> 02.htm
Nice article, and a good start, actually makes sense:)
Altho they don't tackle the thorny issue that the second vector is not known. I will attempt to muddle threw a half baked extension to the article...
Rots of ruck, to me
ahhhh, I just re-read your original question.What you need to solve, is the intersection points of a line (Ray 1) and a circle (all possible end points of Ray 2)Circle<->Line intersection is fairly simple. http://mathworld.wolfram.com/Circle-LineIntersection.html
Abusea at 2007-7-14 23:12:53 >

doing that will give you the 2nd coordinate for Ray2.
From that, you can simply use Math.atan2(ray2.x2-ray2.x1,ray2.y2-ray2.y1) to determine the Rays angle.
(if thats what you actually after)
What do you need this for, is it for target prediction for AI players?
(thats the 1st use of it I can think of off the top of my head ;)
Abusea at 2007-7-14 23:12:53 >

Yes targeting prediction system
> ahhhh, I just re-read your original question.
>
> What you need to solve, is the intersection points of
> a line (Ray 1) and a circle (all possible end points
> of Ray 2)
>
> Circle<->Line intersection is fairly simple.
Well except that that circle expands over time, it's not a static circle
You have a target A at coordinates (ax, ay)
The target is moving in time with a speed of (adx, ady) per time unit n.
At any given point of time A will be found at the coordinates (ax + adx*n, ay +ady*n).
You then fire a bullet B from (bx, by)
B is moving and is always found at coordinates (bx + bdx*n, by+bdy*n)
At a given time (as soon as possible) A hits B right in the center giving us this equation.
(ax + adx*n, ay +ady*n) = (bx + bdx*n, by+bdy*n)
Lets denote the speed of B as SB. B moves at the speed of SB giving us this equation:
bdx*bdx + bdy*bdy = SB*SB
We now have two equations with three unknown variables: (bdx, bdy and n).
We need to put the first equation on the form
n = ...
And then we need to set the derived to zero to calculate n for the earliest possible time of intersection.
When n is calculated you can fill inn n and calculate bdx and bdy from the equations.
The principle is the same if you introduce a third coordinate z.
I was with you until you got toAnd then we need to set the derived to zero to calculate n for the earliest possible time of intersection.When n is calculated you can fill inn n and calculate bdx and bdy from the equations.
If you are trying to get a good targeting system, I worked this out for a Robocode project. It has the same concepts with a moving target and bullet velocity. My goal was to find the angle to shoot at. I have the whole derivation if you want it:
given: distance = D, enemy heading = ThetaH, enemy velocity = Ve,
bullet velocity = Vb, enemy direction ThetaE
find: intersection location
enemy x pos at time t : Ex(t) = D sin ThetaE + Ve * t sin ThetaH
enemy y pos at time t : Ey(t) = D cos ThetaE + Ve * t cos ThetaH
quadradic formula:
A = (Ve^2 - Vb^2), B = (2D Ve cos(ThetaE - ThetaH)), C = D^2
t = (-B +/- sqrt(B^2 - 4*A*C))/2A
t > 0 && A < 0, so use t = (-B - sqrt(B^2 - 4*A*C))/2A
solve for Ex(t) and Ey(t)
I hope this helps!
-JBoeing
Oh, and the A < 0 is off the assumption that the bullet always moves faster then the target. Also, all velocities are assumed positive, so for a negative velocity, you would need to reverse the direction for the heading and use a positive velocity.
> given: distance = D, enemy heading = ThetaH, enemy
> velocity = Ve,
>bullet velocity = Vb, enemy direction ThetaE
>
> find: intersection location
> enemy x pos at time t : Ex(t) = D sin ThetaE +
> etaE + Ve * t sin ThetaH
> enemy y pos at time t : Ey(t) = D cos ThetaE +
> etaE + Ve * t cos ThetaH
>
>quadradic formula:
> A = (Ve^2 - Vb^2), B = (2D Ve cos(ThetaE -
> etaE - ThetaH)), C = D^2
>t = (-B +/- sqrt(B^2 - 4*A*C))/2A
>
> t > 0 && A < 0, so use t = (-B - sqrt(B^2 -
> (B^2 - 4*A*C))/2A
>
>solve for Ex(t) and Ey(t)
>
> I hope this helps!
>
> -JBoeing
My head is starting to spin....
what's the 'distance', and what is the difference between 'enemy heading' and 'enemy direction'?
"Ray Collisions for idiots" is what i need
distance is the distance between the shooter and the target.The next two use absolute coordinates on a plane:enemy heading is direction the enemy velocity is in.enemy direction is the angle from the shooter to the target.
Sorry, I forgot to mention, in my equation, the velocities are the magnitudes without direction. The heading is used to determine direction.
For more clarification, if the enemy direction uses compass degrees (0 degees is north, 90 is east, etc.) so a target south of you would be at about at 180 degrees. Likewise, if the target was moving west, its heading would be 270 degrees.
> distance is the distance between the shooter and the> target.ok, so the initial distance, i assume, at n = 0
Yes, the distance variable is the distance at t=0.
what's the 'distance'The distance between to points A and B at coordinates (ax, ay) and (bx, by) or (ax, ay, az) and (bx, by, bz) is:sqrt((ax-bx)^2+(ay-by)^2)orsqrt((ax-bx)^2+(ay-by)^2+(az-bz)^2)
Imagine a coordinate system where dmbdmb is in Origo...-Then his head starts spinning.Now, we have to introduce a new variable to the equation. The spinning speed of Dmbs head (Vdmb). Multiply by Pi and extract the square root.
Throwing in a HeadSpinningTooFastException.
Got an idea.
Simplify.
Lets change how to make them intersect to how to make them intersect or almost intersect...
Imagine the following scenario:
A is the target. B is the bullet.
The movement of A has an angle of 90 degrees to the line from the origin of A to the origin of B.
The distance from A to the origin of B remains constant (i.e.) A moves in a circle around B.
Now apply some math and solve the problem.
Now apply this solution to the original problem (modify the variable denoting the speed of A according to the angle (it might differ from 90 degrees))
My guess is that this solution should give you a hit or a close by hit, and it is far easier to implement.