A Math Problem...
by Anthony Ayers · in Torque X 2D · 05/23/2008 (3:31 pm) · 8 replies
I am currently working on a piece of AI where I'm trying to determine where the player should move, to be at a maximun weapon range (15.0f) from the target. I have currently determined the distance between player and target - returning a Float, and can obviously subtract that value from my weapons range to get the proper distance in a float value - but that is as far as I get. Now this confuses me as well, so I'll do my best to give an example:
We have player at Vector 10,0 and a weapon with a maximum range of 15.0f.
We have a target at Vector -10,0.
Visual Example - where X is target, Y is player, and Z is something like the vector I am trying to achieve:
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . X . . . . . . . . . Z . . . Y .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Any help would be much appreciated!
We have player at Vector 10,0 and a weapon with a maximum range of 15.0f.
We have a target at Vector -10,0.
Visual Example - where X is target, Y is player, and Z is something like the vector I am trying to achieve:
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . X . . . . . . . . . Z . . . Y .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Any help would be much appreciated!
#2
05/24/2008 (3:02 pm)
Will this give me the vector of Z??? Again, I'm essentially trying to move my player(Y) to vector Z based upon the maximum range(15.0f) of the players weapon. So, lets just say that the distance between X and Y is 20 and my weapons range is 15, so I'd just want to move forward 5 to point Z(to be at my maximum weapon range). Is this what your solution will give me? Again,thanks alot.
#3
05/24/2008 (3:12 pm)
It will give you the vector/position z.
#4
05/25/2008 (1:07 pm)
Great, I'll give it a shot!
#5
Is this the correct interpretation? Is the radius supposed to be the 15.0f value I mentioned? If not, what should that be? That is the only part that isn't completly clear to me.
thanks again
05/27/2008 (7:07 am)
Please pardon my slowness on this one, but is this what I should be doing???Vector2 diff = Vector2.Subtract(SceneObject.Position, ai.CurrentTarget[0].Position); //my Y and X positions diff.Normalize(); diff *= selectedItem.Range; //Range is the 15.0f value. Is this my radius??????
Is this the correct interpretation? Is the radius supposed to be the 15.0f value I mentioned? If not, what should that be? That is the only part that isn't completly clear to me.
thanks again
#6
If 15.0f is your maximum weapon range and the distance you want to be away, then multiplying the normalized vector by 15.0 will give you a point that far away from the center.
However, you want to flip the order you are subracting the points. The general rule I remember is "destinationPoint - sourcePoint" gives you the vector from the source to the destination. You are calculating the position the ai wants to be at right? So the vector should point towards the ai.
05/27/2008 (11:50 am)
Yes, it looks like you do want to multiply it by 15.0f.If 15.0f is your maximum weapon range and the distance you want to be away, then multiplying the normalized vector by 15.0 will give you a point that far away from the center.
However, you want to flip the order you are subracting the points. The general rule I remember is "destinationPoint - sourcePoint" gives you the vector from the source to the destination. You are calculating the position the ai wants to be at right? So the vector should point towards the ai.
#7
Looks like I got it, however as the AI pc hovers around its target raduis, sometimes it moves VERY quickly!?
My game recognizes the pc speed in the MovementComponent, so why would it randomly decide to speed up the player? Any thoughts?
05/28/2008 (9:51 am)
James,Looks like I got it, however as the AI pc hovers around its target raduis, sometimes it moves VERY quickly!?
My game recognizes the pc speed in the MovementComponent, so why would it randomly decide to speed up the player? Any thoughts?
#8
I'd recommend looking at the part of your code where you use that target/destination position to calculate a speed or velocity.
You might already have heard this, but the term for this area of ai is "steering behaviors" and I would recommend reading up all you can because some general knowledge on it would help you out.
05/28/2008 (10:29 am)
That calculation just gives you a target/destination position, how you move there and at what speed has to be determined somehow else, and I haven't seen that part of your code so I'm not sure.I'd recommend looking at the part of your code where you use that target/destination position to calculate a speed or velocity.
You might already have heard this, but the term for this area of ai is "steering behaviors" and I would recommend reading up all you can because some general knowledge on it would help you out.
Associate James Ford
Sickhead Games
Get the vector from the centerpoint (x) to the um ... problem point (y).
Normalize, and then multiple by the radius, done!
vec = y - x;
vec.normalize();
vec *= radius;
Converting this from a float to an int could require some rounding.