v8forlyfe
asked on
Making one object seek another, JOGL
Hey, I'm trying to figure out the math for having one object "seek" another object. I've done things that kind of almost do it, one in particular hit the object if it wasn't moving. But what I need specifically is how to calculate the change in direction for an "artificially intelligent" enemy to follow my "hero" object on the x,z plane, and ultimately make contact with it.
This may sound confusing but any help would be appreciated and I can explain more if need be!
This may sound confusing but any help would be appreciated and I can explain more if need be!
ASKER
Thanks for the tip! I've looked into the steering behavior, my only problem is that it's a little overwhelming for me... It's all in C++ (as far as I could find) which I have no background in, and I couldn't really find any useful math on a simple "seek" or "pursue" algorithm for me to implement. If you know where in that large library I could find just a simpler solution, that would be great, otherwise I think I'll have to look into other options.
Here's the two main functions of a simple seek steering behavior implemented in C# using XNA which should read pretty much the same as pseudo-code.
The key to these steering behaviors is simply manipulating vectors. In this example Vector2 is basically just a struct with an x and y value. If you don't know vector math, mainly just addition/subtraction/multi plication you should review http://www.physicsclassroom.com/Class/vectors/index.cfm.
The seek behavior basically just gets the vector from your current position to the target position, sets the length of the vector equal to your max speed, and adds that vector to your current position.
To get a pursuit force, instead of calling seek and passing in the position of your target you look at your target's current heading, project out in front of the target, and then pass the target's position + projection vector to getSeekForce.
The key to these steering behaviors is simply manipulating vectors. In this example Vector2 is basically just a struct with an x and y value. If you don't know vector math, mainly just addition/subtraction/multi
The seek behavior basically just gets the vector from your current position to the target position, sets the length of the vector equal to your max speed, and adds that vector to your current position.
To get a pursuit force, instead of calling seek and passing in the position of your target you look at your target's current heading, project out in front of the target, and then pass the target's position + projection vector to getSeekForce.
public void Update(GameTime gameTime){
Vector2 steeringForce = getSeekForce(Variables.board.mouse.Position);
Vector2 acceleration = steeringForce / mass;
Vector2 velocityDelta = acceleration;
velocity = velocity + velocityDelta;
if (velocity.Length() > maxSpeed) {
velocity.Normalize();
velocity = velocity * maxSpeed;
}
setMyPosition(position + velocity);
heading = velocity;
heading.Normalize();
}
Vector2 getSeekForce(Vector2 target) {
Vector2 offset = target - position;
offset.Normalize();
offset *= maxSpeed;
return offset;
}
ASKER
Ok, you explained that pretty well, I think I pretty much get it. A few questions for implementation though, what is the equivelent of "vectory 2" in java, because I've never used them. I understand (I think) that it is just a value that hold the x,y (or x,z like I need?) coords. Also, would I need this "GameTime" class to implement this? Because I don't know what that is either.
As far as I know, there is not a native equivalent class. You will either need to write your own or try looking into third party or graphical APIs to find a suitable vector class.
The GameTime class is just part of the XNA framework's Update function, it's not necessary for a Java implementation.
The GameTime class is just part of the XNA framework's Update function, it's not necessary for a Java implementation.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey thanks for your help, I actually wrote my own method that gets the pursuit job done pretty nicely! It fits in with my overall program framework a lot better and pretty efficient at catching my hero.
Some good general information can be found at http://www.red3d.com/cwr/steer/ the top has steering descriptions, the bottom has a math review.
For some actual code you could check out http://opensteer.sourceforge.net/ and download their source code which has implementations of a lot of the steering behaviors.