# Implementing wander into my game

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

## 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

// 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

// Move the cricle in front of the player
var targetLocalPos = new vector2D(0,0);

targetLocalPos.assign(this.m_wanderTarget);

// Move the point into world space
this.itSelf.m_side, this.itSelf.m_pos);

targetFinalPos.sub(this.itSelf.m_pos);
return targetFinalPos;
}

{
// 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
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 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 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 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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• ### Who's Online (See full list)

There are no registered users currently online

×