How to get the angle and speed to reach a movable point

Started by
30 comments, last by Staryon 17 years, 10 months ago
Quote:Original post by Staryon
Could you give me more details about how to calculate RadiusA(T)?
The max speed should be included there, right?


Radius is just the possible distance A can travel, with no information about the direction it is pointed.
So maxSpeedA*Time = radius.

In reality, since this is a car, and cars cannot make sharp turns instantly, rather than a circle of possible locations A can reach, it is probably really a irregular shape like a Cardiod (heart shape) or some kind of oval.
But since this calculation is going to be repeated each gameloop, the approximation should be good enough. If you want to be safer about it, you can Under-Estimate the radius of A's interception circle... and multiply it by .9 or something to give a saftey margin.


Regarding Vectors
generally, a Vector does not include a 'heading' value, but if you want to save that information too, it shouldn't hurt. Just make sure the heading really matches up with the x,y information when they change.
Advertisement
haphazardlynamed, thanks for the explanation. I think I'm getting closer.

PositionB(T) = Vb * T
RadiusA(T) = Vmaxa * T
DistB(T) = sqrt ( (Xa-Xb)^2 + (Ya-Yb)^2 )

Now I need to combine RadiusA(T) and DistB(T) but I cannot find the way of getting T from DistB(T)
Do you know what I'm missing?

Thanks. I really really appreciate all your help.




You need to break up the PositionB function into X and Y components and plug its parts into your Distance forumula (subsititute Xb and Yb), then Distance will have a T variable.

From there, if you equate DistB and RadiusA... you should be able to simplfy it to Quadratic Forumla.
*Edited*

See next post.
Is this correct?

PositionB(T) = ( Xb+V*T , Yb+V*T )

So

DistB(T) = sqrt ( (Xa-(Xb+V*T))^2 + (Ya-(Yb+V*T) )^2 )
Quote:Original post by Staryon
Is this correct?

PositionB(T) = ( Xb+V*T , Yb+V*T )

So

DistB(T) = sqrt ( (Xa-(Xb+V*T))^2 + (Ya-(Yb+V*T) )^2 )


Assuming that 'V' is a vector like the one you asked about earlier.
what you want is the individual x and y coord pieces, not the entire vector in that formula

so
PositionB(t)= (Xb+V.x*t , Yb+V.y*t)

DistB(T) = sqrt ( (Xa-(Xb+V.x*T))^2 + (Ya-(Yb+V.y*T) )^2 )



RadiusA(T)= maxSpdA*T

equate:
RadiusA(T)=DistB(T)maxSpdA*T=sqrt ( (Xa-(Xb+V.x*T))^2 + (Ya-(Yb+V.y*T) )^2 )maxSpdA^2*T^2 = (Xa-(Xb+V.x*T))^2 + (Ya-(Yb+V.y*T) )^2 maxSpdA^2*T^2 = Xa^2 -2(Xb+V.x*T) + (Xb+V.x*T)^2 + Ya^2 -2(Yb+V.y*T) +(Yb+V.y*T)^2maxSpdA^2*T^2 = -2V.x*T + (Xb+V.x*T)^2 -2V.y*T +(Yb+V.y*T)^2 + Xa^2 -2Xb + Ya^2 -2Yb maxSpdA^2*T^2 = -2V.x*T + Xb^2 + 2(V.x*T) + V.x^2*T^2 -2V.y*T + Yb^2 +2V.y*T +V.y^2*T^2 + Xa^2 -2Xb + Ya^2 -2Yb maxSpdA^2*T^2 = + V.x^2*T^2 +V.y^2*T^2 -2V.x*T  + 2(V.x*T)  -2V.y*T  +2V.y*T  + Xa^2 -2Xb + Ya^2 -2Yb + Xb^2 + Yb^20 = +V.x^2*T^2 +V.y^2*T^2 -maxSpdA^2*T^2 +Xa^2 +Ya^2 +Xb^2 +Yb^2 -2Xb -2Yb This is Quadratic, solve using Quadratic Formula


You might want to work all that out separatly to doublecheck, since I Probably made mistakes...
oh... what I meant with ´V´ is velocity and V.x and V.y should both be the same, right?
Quote:Original post by Staryon
oh... what I meant with ´V´ is velocity and V.x and V.y should both be the same, right?


Yes
Quote:Orignal post by Staryon
nilkn, I'm still studying your algorithm. I'll get back to you later.


My algorithm is similar to what is being illustrated in this image from the article JBourrie linked you to:



We are, of course, interested in the Pursuit path, not the Evade path. My method is to move A not towards B's current position, but towards where we predict B will be if it continues moving in a straight line until we hit it.

This is a difficult problem to solve analytically. Although I'm sure it's possible, I instead opted to use a simple numerical algorithm to compute how far forward in time we should project B's position. We then use this time value to actually compute the projected future position of B, and move A towards this point.

Quote:Orignal post by Staryon
1. Is Vec2 something like this?


Yes, Vec2 simply referred to any valid class representing a 2-dimensional vector. In most cases, however, it would not contain a heading member. I also assumed the class had overloaded operators appropriate for a vector.

Quote:2. In the predictPosition function, when you do

return b.position + b.velocity * dt;

Should I calculate that for x and y ? I'm not sure how to make that operation if 'position' has 2 coordinates (x,y)


Yes. b.position and b.velocity are vectors, so that code could be expanded into:


Vec2 predictedPosition;
predictedPosition.x = b.position.x + b.velocity.x * dt;
predictedPosition.y = b.position.y + b.velocity.y * dt;

return predictedPosition;

The value dt represents how far forward in time to project B's position.

Quote:3. Is mag(A) = sqrt (x*x + y*y ) ?


Yes.

Keep in mind that the code snippet I posted was pseudo-code. It was intended to illustrate the concepts using code, not as a piece of code ready to copy-and-paste into your project.
The simplest chase algorithm that looks good and works most of the time is:

for each frame    move towards where the target is now


That looks like this:

http://www.red3d.com/cwr/steer/PursueEvade.html

-me

This topic is closed to new replies.

Advertisement