Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:32:13+00:00 2026-05-26T00:32:13+00:00

I have an image inside the panel and it moves in a clockwise direction.

  • 0

I have an image inside the panel and it moves in a clockwise direction. Now, I want it to move in a random direction and that is my problem.

Could someone give me an idea how to do it?

Here’s what I’ve tried :

private int xVelocity = 1;
private int yVelocity = 1;
private int x, y;
private static final int RIGHT_WALL = 400;
private static final int UP_WALL = 1;
private static final int DOWN_WALL = 400;
private static final int LEFT_WALL = 1;

public void cycle()
{       

    x += xVelocity;
    if (x >= RIGHT_WALL)
    {
        x = RIGHT_WALL;
        if (y >= UP_WALL)
        {
            y += yVelocity;
        }

    }

   if (y > DOWN_WALL)
    {
        y = DOWN_WALL;
        if (x >= LEFT_WALL)
       {
            xVelocity *= -1;
        }
    }


    if (x <= LEFT_WALL)
    {

        x = LEFT_WALL;
        if (y <= DOWN_WALL)
        {
            y -= yVelocity;
        }

    }

    if (y < UP_WALL)
    {
        y = UP_WALL;
        if (x <= RIGHT_WALL)
        {
            xVelocity *= -1;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:32:14+00:00Added an answer on May 26, 2026 at 12:32 am

    Call a method like this to set a random direction:

    public void setRandomDirection() {
      double direction = Math.random()*2.0*Math.PI;
      double speed = 10.0;
      xVelocity = (int) (speed*Math.cos(direction));
      yVelocity = (int) (speed*Math.sin(direction));
    }
    

    Just noticed that you’re cycle method will need a little fixing for this to work.

    public void cycle() {
      x += xVelocity;
      y += yVelocity; //added
      if (x >= RIGHT_WALL) {
        x = RIGHT_WALL;
        setRandomDirection();//bounce off in a random direction
      }
      if (x <= LEFT_WALL) {
        x = LEFT_WALL;
        setRandomDirection();
      }
      if (y >= DOWN_WALL) {
        y = DOWN_WALL;
        setRandomDirection();
      }
      if (y <= UP_WALL) {
        y = UP_WALL;
        setRandomDirection();
      }
    }
    

    (It works, but this is not the most efficient/elegant way to do it)

    And if you want some sort of ‘random walk’ try something like this:

    public void cycle() {
      //move forward
      x += xVelocity;
      y += yVelocity;
    
      if (Math.random() < 0.1) {//sometimes..
        setRandomDirection();   //..change course to a random direction
      }
    }
    

    You can increase 0.1 (max 1.0) to make it move more shaky.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that displays an image inside of a Windows Forms PictureBox
I have an image gallery that I created by reading the contents inside a
I have a set of buttons inside a stack panel. I want them all
Inside the update panel I have update progess I want to set stylesheet inside
I have an image that is generated automatically inside an Ajax UpdatePanel. This image
I have a image inside a div which is 100*100 , i want to
I have a thumbnail image inside a div. I use another image as the
I have a button and image inside it in my silverlight user control. When
I have a table with an image inside: <table style=border: 3px solid rgb(0, 0,
I have the following XAML code, which displays a picture (image inside borders) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.