Jump to content
  • Advertisement
Sign in to follow this  
dries123

Steering Behaviors: Wandering problem

This topic is 3794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a problem with my wandering function here. Does anyone see what's wrong with it?
public TV_2DVECTOR Wander()
{
    int Sign = m_Random.Next(0, 1);
    float Number = (float)m_Random.NextDouble();

    if (Sign == 0)
        Number = -Number;

    m_Settings.WanderTarget += new TV_2DVECTOR(Number * m_Settings.WanderJitter, Number * m_Settings.WanderJitter);
    m_Math.TVVec2Normalize(ref m_Settings.WanderTarget, m_Settings.WanderTarget);
    m_Settings.WanderTarget *= m_Settings.WanderRadius;

    TV_2DVECTOR TargetLocal = m_Settings.WanderTarget + new TV_2DVECTOR(m_Settings.WanderDistance, 0);

    return TargetLocal - m_Position;
}

Share this post


Link to post
Share on other sites
Advertisement
The wandering should appear like it is random, but it seems my bot is always going in the same direction: the origin (0, 0). This is not how it is supposed to work.

Share this post


Link to post
Share on other sites

m_Settings.WanderTarget += new TV_2DVECTOR(Number * m_Settings.WanderJitter, Number * m_Settings.WanderJitter);


According to this line and the ones following it, it seems to be that you will always jiiter between either (WanderRadius, WanderRadius) or (-WanderRadius, -WanderRadius).


TV_2DVECTOR TargetLocal = m_Settings.WanderTarget + new TV_2DVECTOR(m_Settings.WanderDistance, 0);


I can't make sense of this line. Why are you adding an offset to what I assume is the x coordinate?


return TargetLocal - m_Position;


so what is this method actually supposed to return? That isnt clear, and is probably your main problem.

Share this post


Link to post
Share on other sites
I don't mean to sound rude, but have you actually tried to debug your code? Step through the instructions and see what happens to your variables?

You normalize WanderTarget, which means that it will be at exactly 1 unit from origin, you then scale it using a radius and add an offset to its X value (as Steadtler pointed out).

If you read your return value line, you're substracting a LOCAL offset with a WORLD position. Seems like you should only be returning TargetLocal.

Edit: Also you're normalizing a vector that could be of length 0 if Number or WanderJitter is 0...

Share this post


Link to post
Share on other sites
I did try to debug it, but I couldn't find what I was doing wrong. I'll take a look at the things you said.

Thnx for all the good replies.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!