user843916
user843916

Reputation:

How to make a random direction?

I have an image that moves in a random direction when it starts to hit the wall. The image always starts or appear at the upper left corner during execution, you can see that in my code. Now I want the image to appear in a random position during execution and that is my problem, could someone give me an idea about this? Thanks in advance.

public class Ball extends JPanel implements Runnable
{

private Image ball;
private Thread animator;
private int x, y;
private final int DELAY = 20;
private int speedX = 1;
private int speedY = 1;
private static final int RIGHT_WALL = 200;
private static final int LEFT_WALL = 1;
private static final int DOWN_WALL = 200;
private static final int UP_WALL = 1;

public Ball()
{
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    ImageIcon ii = new ImageIcon(this.getClass().getResource("ball.gif"));
    ball = ii.getImage();

    x = y = 10;
}

public void addNotify()
{
    super.addNotify();
    animator = new Thread(this);
    animator.start();
}

public void paint(Graphics g)
{
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(ball, x, y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void move()
{
    x += speedX;
    y += speedY;
    if (x >= RIGHT_WALL)
    {
        x = RIGHT_WALL;
        moveRandomDirection();
    }
    if (y > DOWN_WALL)
    {
        y = DOWN_WALL;
        moveRandomDirection();
    }
    if (x <= LEFT_WALL)
    {
        x = LEFT_WALL;
        moveRandomDirection();
    }
    if (y < UP_WALL)
    {
        y = UP_WALL;
        moveRandomDirection();
    }
}

public void moveRandomDirection()
{
    double direction = Math.random() * 2.0 * Math.PI;
    double speed = 10.0;
    speedX = (int) (speed * Math.cos(direction));
    speedY = (int) (speed * Math.sin(direction));
}

public void run()
{
    long beforeTime, timeDiff, sleep;
    beforeTime = System.currentTimeMillis();
    while (true)
    {
        move();
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep > 2)
        {
            sleep = 1;
        }
        try
        {
            Thread.sleep(sleep);
        }
        catch (InterruptedException e)
        {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
}
}

Upvotes: 1

Views: 7962

Answers (2)

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

This covers a random location:

Compute the area in which the ball is allowed to appear:

int x = RIGHT_WALL - LEFT_WALL;
int y = DOWN_WALL - UP_WALL;

Subtract the ball size:

x -= ball.getWidth(null);
y -= ball.getHeight(null);

Choose a random location:

Random r = new Random(); // java.util.Random
x = r.nextInt(x);
y = r.nextInt(y);

Move the coordinates to the beginning of the upper-left corner of the wall:

x += LEFT_WALL;
y += UP_WALL;

Now x and y are valid positions for the ball to appear.

Note: Be aware of that the x and y I used here are NOT your class members. These are supposed to be local variables.


It seems like you solved the algorithm yourself for a random direction.


There is a mistake in your move() method: You don't care about the ball size when you check for collision. The ifs should look like this:

if (x + ball.getWidth(null) >= RIGHT_WALL)
{
    x = RIGHT_WALL - ball.getWidth(null);
    moveRandomDirection();
}
if (y + ball.getHeight(null) >= DOWN_WALL)
{
    y = DOWN_WALL - ball.getHeight(null);
    moveRandomDirection();
}
if (x <= LEFT_WALL)
{
    x = LEFT_WALL;
    moveRandomDirection();
}
if (y <= UP_WALL)
{
    y = UP_WALL;
    moveRandomDirection();
}

Upvotes: 1

Michael Schmidt
Michael Schmidt

Reputation: 391

Instead of x=y=10 in your constructor you can generate random values as well. I would use the java.util.Random class to do this. Have a look at the nextInt Method. You can set a maximum value there using the parameter.

Upvotes: 0

Related Questions