Advertisement

Steering Behaviors: Wandering problem

Started by May 24, 2008 06:11 AM
6 comments, last by dries123 16 years, 6 months ago
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;
}

It would be easier if you were to tell us what sort of problem you are having.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

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.
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.
I'll take a look at it. By the way the function should return the total steering force.
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...
Advertisement
Good call. When in doubt, step through with a ton of watches open. Debugging 101.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

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.

This topic is closed to new replies.

Advertisement