Sign in to follow this  

Steering Behavior Seeking code malfunction

Recommended Posts

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 = desired * maxspeed;
    IvVector3 steer = desired - velocity;

    if(steer.LengthSquared() > maxforce * maxforce)
        steer *= maxforce;



   void applyForce(IvVector3 Force)

    acceleration = Force;


In my update Function


velocity += acceleration;


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

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



Share this post

Link to post
Share on other sites
masskonfuzion    150

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?


Share this post

Link to post
Share on other sites
Khatharr    8812

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?

Edited by Khatharr

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By MarcusAseth
      Hi guys, I'm not quite sure how to achieve this
      Here what I got so far, my GameMode class own an object of type Physics:
      class Physics { private://variables GameMode* Game; public://constructors Physics(GameMode* gameRef); ~Physics(); public://methods bool IsColliding(Entity* current, ECollisionTest collisionTestType); private://methods bool BoxCollisionTest(const Rect& current, const Rect& other); }; And since all my entities have a pointer to the GameMode they can retrieve a pointer to the Physics object, so when they move they can query the Physics object to see if they collided with something, passing "this" as the object to control against all the Entities inside GameMode, like this:
      //Collision Check if (PhysicsManager->IsColliding(this, ECollisionTest::EBoxCollisionTest)) { LogConsole("Collision"); } and IsColliding() goes like this:
      bool Physics::IsColliding(Entity* current, ECollisionTest collisionTestType) { for (auto& E : Game->Entities) { if (current != E.get())//avoid self testing { switch (collisionTestType) { case ECollisionTest::EBoxCollisionTest: { if (BoxCollisionTest(current->GetCollisionBox(), E->GetCollisionBox())) { return true; } } break; } } } return false; } Now, there are two things I don't like about my current setup.
      1) I am testing against all the entities. The paddle of the Breakout game can only move left and right, and hit either a ball or the left/right boundary, makes no sense to test it against 50 bricks at every update.
      2)From inside the Paddle class I am calling "IsColliding(this, ECollisionTest::EBoxCollisionTest)" so there is the assumption that all the entities that can collide with this one require a box test. May not be the case, maybe I have a "worm" entity running on the screen and it requires another type of collision test, so the choosen function to run shouldn't relying on me explicitly saying which one is, but it should be called at runtime trough overload, based on the type of Entity being compared. So maybe the Paddle<->Ball comparison would do a BoxCollisionTest, while Paddle<->Worm comparison would do something else.
      So how do I get it to work the way I want?
      This is what I want:
      1)Physics object is working with Entity*, and yet I need it to being able to distinguish between a Paddle* and or a Brick* or a Boundary* or a Ball* (all of this inherit from entity), a bit kind of like Unreal Engine BlueprintEditor Cast node, which allow me to say "Cast to Door" and if the thing was a door it return success, otherwise fails. Can you show me an example of how such mechanism is built, in code(C++)?
      2)Physics object should just call a generic TestCollision() function between the "this" passed (the current colliding object) and all the other entities, and then the overload resolution calls the appropriate function based on the types. And yet this shouldn't fail to compile for pairs for which I don't explicitly overload, for instance I wouldn't overload for Paddle<->Brick because such comparison will never happen, so when Paddle<->Brick comparison is performed, it should just return false even though I never declared a TestCollision(Paddle* p, Brick* b). 
      How can this be achieved?
    • By cozzie
      During the journey of creating my 2nd 3D engine, I'm basing quite some of the approaches on the Game Engine Architecture book (Jason Gregory). I've now arrived on the topic: IO.
      In short, the target platforms for me are PC, XBone and PS4.
      With this main assumption I thought of the following logics/ guidelines to follow:
      - I can assume file/folder structures will work on all 3 platforms, when I use '/' as a folder separator
      -- I will define 1 global with the base folder for the application, the rest will 'inherit' from there
      (which can be anything, independent of the 'mount' or drive)
      -- for now I'll create 1 subfolder with data/files that might need write access, so later on I only have to worry about 1 subfolder (settings, configs etc.).
      - file extensions can be longer than 3 characters (in Linux based FreeBSD on PS4)
      - all class members functions needing to load a file, shouldn't have to now about the file structure of the logical application, so they will all take a full filename string including path
      (combining root + subfolder + filename and separators is then the responsibility of the caller/ calling code)
      - some functions will need to be passed a folder, because contents in that folder need to be read OR I can simply define a small list of defined subfolders (under root/ base), because it won't be more then 5 to 10 folders in total (data/shaders, data/textures, data/objects, data/sound etc.)
      My questions:
      - what do you think about this approach?
      - regarding the last point, which of the 2 options would you apply?
      -- option 2 might work fine but feels a bit 'static', not very flexible (on the other hand, would you actually need flexibility here?)
      Any input is appreciated, as always.
    • By RubenRS
      How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
      I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
    • By MarcusAseth
      this is super strange...
      I have this function here:
      template<typename TFirst, typename... TArgs> bool util::LogConsole(const TFirst& first, const TArgs&... rest) { std::cout << first << " "; return LogConsole(rest...); } bool util::LogConsole() { std::cout << std::endl; return false; } it's recursive, keep printing "first" and recursively sending the rest, until rest is empty and the overload taking no argument is called, which just print a newline. Ignore the bool return.
      Anyway, now I have this code somewhere else:
      for (auto& E : Entities) { float valx = E->GetPosition().x;//return 751 float valy = E->GetPosition().y;//return 838 LogConsole(valx, valy); } Entities is a vector<unique_ptr<Entity>> which currently only contains the derived from Entity player Paddle, so I access the Base class Entity method called GetPosition, which is the center pivot coordinates of the object.
      I pass the variable to LogConsole which predictably prints:
      But not I try to call LogConsole without those 2 variables inbetween, like this:
      for (auto& E : Entities) { LogConsole(E->GetPosition().x, E->GetPosition().y); } and watch what kind of output I get! :
      ... the y value is an unreasonable value o_o
      How is this even possible?! Since only the second value is being affected, my assumption is that there is some weird interplay with the second template parameter, the variadic argument...but I wasn't able to debug it even stepping trough it line by line...can anyone come with an explanation for this really strange behaviour?
    • By MarcusAseth
      Maybe today I'm confused and I'm missing the obvious...
      I've made this simple struct inside my Utility.h 
      template<typename TFirst, typename TSecond> struct Pair { Pair(TFirst f, TSecond s):First{f},Second{s}{} TFirst First; TSecond Second; }; And I'm using it from App.h (which #include "Utility.h") like so:
      Pair<Uint32, Uint32> GetWindowSize(); App.cpp
      Pair<Uint32, Uint32> App::GetWindowSize() { return Pair<Uint32, Uint32>(Width,Height); } And I'm getting a cascade of errors ALL on line 39 (image below).
      Any idea what I am doing wrong this time? x_x

      EDIT: please mod delete this topic, I just remembered Utility.h put everything inside namespace util, sorry x_x
  • Popular Now