enter image description here

I want objects in my game to move randomly but always from right side of the screen to the left side. The movement must be as smooth and natural as possible, like they are following a path. Can i obtain this with the wander steering behavior without having objects going in every direction randomly? (that's what i have right now)


2 Answers 2


Do you want the objects to move along the X axis (right to left) at the same speed or should this vary?

If it should be the same speed, simply apply some sort of random modification to their Y velocity every X frames while keeping the X velocity at a constant.

If they should slightly vary in speed from left to right, do the same as above making sure to keep the X velocity above some minimum value.

Here is a code example for objects having the same X speed:

public class MovingCircle(){

    // vectors for position and velocity
    Vector2 position = new Vector2();
    Vector2 velocity = new Vector2();

    // the circles will modify their direction every 3 seconds
    // changing this to an instance variable (remove static) would allow different circles to 
    // change directions at different frequencies, making some more erratic than others
    private static float directionChangeFrequency = 3f;

    // these determine how much of a direction change can occur per directionChangeFrequency
    private static float minDirectionChangeAmount = 0.1f;
    private static float maxDirectionChangeAmount = 0.5f;

    private static Random randomNumberGen = new Random();

    // timer to keep track of when this moving circle should change direction
    float directionChangetimer = 0;

    public void update(float delta){
        position.x += velocity.x * delta;
        position.y += velocity.y * delta;

        directionChangetimer  += delta;

        if(directionChangetimer >= directionChangeFrequency){
            directionChangetimer -= directionChangeFrequency;
            float directionChangeRange = maxDirectionChangeAmount - minDirectionChangeAmount;
            // calculate a random change amount between the minimum and max
            float directionChangeAmount = randomNumberGen.nextFloat() * directionChangeRange + minDirectionChangeAmount;
            // flip the sign half the time so that the velocity increases and decreases
                directionChangeAmount  = -directionChangeAmount;
            // apply the change amount to the velocity;
            velocity.y += directionChangeAmount;

Here is a pseudo theory on how to write it. Base on the lines drawn the max value for the Y-axis when generating random values is based off of the most extreme points from the object's center. You will also need a timer to set check distance. a more advanced version would be to apply random time to the below pseudo code to trigger more random variations based also on distance:

Start from current position of object Move object to the left and calculate object's new position from old position. If object's new position moves 'n' amount of distance from the old position in 'N' amount of milliseconds, randomly move object in the y-direction between nY and -nY. Store new position as old position. Repeat process.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .