Steering Behavior Seeking code malfunction

Started by
2 comments, last by Khatharr 6 years, 9 months ago

I got some code that im tweaking. Ive almost got it to work. It doesn't seem to work right though. First when it first starts in my INIT function that I set to target 50,0,0, it jets off way to fast. Here is what im doing.

 


seek(IvVector3 target)
{

    IvVector3 desired = target - position;
    desired.Normalize();
    desired = desired * maxspeed;
    IvVector3 steer = desired - velocity;


    if(steer.LengthSquared() > maxforce * maxforce)
    {
        steer.Normalize();
        steer *= maxforce;
    }

    applyForce(steer);


}



   void applyForce(IvVector3 Force)
{

    acceleration = Force;


}

In my update Function

 


velocity += acceleration;

if(GetKeyState(0x31))
    {
        seek(IvVector3(0,0,50));
    }
 if(GetKeyState(0x32))
    {
        seek(IvVector3(-50,0,0));
    }



position += (velocity * mTimer.DeltaTime());
TESTS = XMMatrixTranslation(position.x,0,position.z);
// Reset acceleration to 0 each cycle
acceleration.IsZero();  

Also when it reaches the target, it completey stops instead of going back and forth

 

 

Advertisement

This might not be precisely the problem, but it looks you're basing your homing/seeking on the current velocity of the object.  I think what you're really aiming to do (pun intended) is to separate the object's velocity vector from its heading.  In other words, I think your update should:

  • Look at the direction the object "wants" to go -- this is the desired direction you've calculated (it should be a normalized vector)
  • Look at the object's (missile or something?) current heading -- which direction is it presently facing?  (should also be a normalized vector)
  • Determine the angle between the two vectors; i.e., determine how to rotate the object to match the missile's heading to the desired vector (you'll probably need an angle and axis, as an angle of rotation is meaningless, if not also given an axis to rotate around)
  • Determine force/impulse to apply to the missile, to accomplish the desired rotation of its heading

Note:  The missile should always be thrusting -- traveling "forward" along its heading vector.  The goal of this algorithm is to determine how to change that heading.  Of course, in every frame, because the missile is moving, the "desired" vector will change, which will cause the rotational force/impulse to change, which will cause the missile's heading to change.  You'll probably be able (read: "need") to tweak a few parameters to get the seeking to behave the way you want.

A caveat: I've never written a homing/seeking missile before, so I could be somewhat off - but at a high level, I think your steps should look something like what I've described.

Another caveat: I gave my answer assuming you want to avoid the "dreaded quaternion".  Quats would probably make easy work of this problem, but I'm not sure what your comfort level is with that.

Have any experts in the community done something like this?

 

You're not multiplying your acceleration value by dt.

 

Also, you may want to change applyForce to use += instead of = since you're probably going to want to apply more than one force at a time.

 

Are you reading Millington?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement