Sign in to follow this  
h3ro

Implementing wander into my game

Recommended Posts

Hallo, I am trying to make some of my player object wander around in my world. But I am having some troubles with it. For some reasons my player is just spinning around in the top left corner (0,0 in my world) This is because my wanderTarget is located there, even though it should not. If anyone have time, I would appreciate some help.
  // Wander behavior
  // The player is moving randomly around
  // -------------------- 
  this.wander = function()
  {
    var rand_1 = (Math.random() *  2) - 1
    var rand_2 = (Math.random() *  2) - 1   

    var moveVector = new vector2D(rand_1 * this.m_wanderJitter,
                                  rand_2 * this.m_wanderJitter);
    
    // m_wanderTarget is stored in the class                 
    this.m_wanderTarget.add(moveVector);
    
    // Reproject the vector back into the unit circle
    this.m_wanderTarget.normalize();
    
    // Increase the size of the vector to match the radius of the
    // Circle
    this.m_wanderTarget.mult(this.m_wanderRadius);
    
    // Move the cricle in front of the player
    var targetLocalPos = new vector2D(0,0);
    var movementToAdd = new vector2D(this.m_wanderDistance,0);
    
    targetLocalPos.assign(this.m_wanderTarget);   
    targetLocalPos.add(movementToAdd);
    
    // Move the point into world space
    targetFinalPos = pointToWorldSpace(targetLocalPos   , this.itSelf.m_heading, 
                                       this.itSelf.m_side, this.itSelf.m_pos);
          
    targetFinalPos.sub(this.itSelf.m_pos);
    return targetFinalPos;  
  }

function pointToWorldSpace(vectorPoint, agentHeading, agentSide, agentPos)
{
  // Move the point we want to move into a 1x3 matrix
  var transformPoint = newMatrix(1,3, list(vectorPoint.x,vectorPoint.y,0));               
  
  // Create a  rotation and translation matrix
  transformMatrix = newMatrix(3, 3, list( agentHeading.x, agentHeading.y , 0,
                                          agentSide.x   , agentSide.y    , 0,
                                          agentPos.x    , agentPos.y     , 1 ));

  // Move the point
  transformPoint = matrixMultiply(transformPoint, transformMatrix)
  
  // Return it as a vector
  tempX = transformPoint.getVal(1,1);
  tempY = transformPoint.getVal(1,2);
  
  return (new vector2D(tempX, tempY));                                     
}


thanks

Share this post


Link to post
Share on other sites
Looks like you've got a fancy version of the "drunken sailor" random walk algorithm. The net result of the algorithm is random wandering around a small area. The speed of wandering depends on the distance taken with each step.

You might improve your existing routine by using a much larger travel distance.

Or, if you are trying to cover a map rather than a radius around the actor, you should pick a random final destination from within your world that is sufficiently far from the actor. Have the actor walk all the way to that destination. After they have reached it, repeat.

Share this post


Link to post
Share on other sites
Thank you for your reply frob, but I am not sure you understood my problem. Please let my try again.

What I am trying to do is make one of my players move around the map in a random order. Using a steering behavior called Wander is one way of doing this. It works by creating a circle in front of the player and then create a vector between the player and a point on the circle. That vector is then used as the steering force.
By having a circle in front of the player you will get a very smooth movement (depending on settings)

Here is a picture of how it works (ignore the wall feelers)
http://www.red3d.com/cwr/steer/gdc99/figure8.gif
http://www.red3d.com/cwr/steer/gdc99/ (scroll down half the page for info)

My problem is that the circle is never in front of player and there is never a fixed distance between the circle and the player (even though it should be). My target circle is just moving around 0,0 in world space and I can not make out why.
I do suspect that the problem might be in the pointToWorldSpace() function, but I am not sure. Any help on this would be appreciated.

Thanks

Share this post


Link to post
Share on other sites
I'm not sure what to make of your "newMatrix" and "list" functions, so I can't verify your math. What I can tell you, however, is what you ought to be doing.

1. Generate your random offset vector, which's magnitude should be strictly less than one.
2. Add it to the state.
3. Re-normalize the state.
4. Make a copy of the state, and scale it up to desired broadness (read: set maximum lateral steering and linear acceleration)
5. Translate the copy of the state out to desired lead (read: set minimum linear acceleration)
6. Use this copy of state as your steering force.

I suspect 5 and/or 6 is where your code is going wrong, given your description of the result, but I can't be certain. 6 heavily depends on whether your actor does his acceleration and heading calculations in world space or a local space.

Assuming your actor works in world space only, 6 involves taking the steering vector and translating it around the (still local-space) origin by his own heading, so that the steering is relative to his current heading, and then doing the acceleration and new-heading calculation in world-space.

You may find it more trivial to store your actor's heading as an angle relative to the +X axis, and generate his heading vector from that whenever the angle changes. Then, in step 6 you will use the Y-axis steering to change his heading, and the X-axis steering to change his current velocity, instead of translating to world coordinates and back for no reason.

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