Sign in to follow this  

Steering Behaviors: Wandering problem

This topic is 3492 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
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

This topic is 3492 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.

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