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 6203927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:00:02+00:00 2026-05-24T05:00:02+00:00

I want to be able to assign a shape a random direction to go

  • 0

I want to be able to assign a shape a random direction to go in at a regular speed.

I tried assigning a random number to the x and y values of a translation but that caused the shape to sometimes move way too fast or just blip off the screen.

Is there a way to choose a random direction for a shape to move in without rotating the shape (at least as far as the user can tell)?

Also is there a way of re-genereating the random number when an event is called (ie: button click) which would allow a change in direction?

EDIT: Just checked. Using the rotate function with a random angle will shoot the square off in a random direction, but still is there a way to do this without altering the orientation of the shape?

  • 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-24T05:00:04+00:00Added an answer on May 24, 2026 at 5:00 am

    Ok, so you want a shape to move in either the postive or negative x, y or z directions?

    If you want this to be random then use the Random class. From this you can use Random.nextInt(1) which will either be 0 or 1, if the result is 0 then set the value to be -1.

    The +1 or -1 is now used for your direction.

    Now you know which way the object travels you can simply multiply this by the speed of your object

    Here is some rough code

    Random rand = new Random(); int direction = rand.nextInt(1);

    if(direction == 0) direction = -1;

    Shape theShape.position.x += direction * theShape.speed

    This may not be how your interface is setup (such as theShape.position or theShape.speed) but the idea is the same

    After reading this again I think you are trying to rotate your shape lol, rotation is not just adding to the x and y values. Do you use matrices in your application? If not your should use trig to figure out what the x and y values will be after a rotation of x degrees

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

Sidebar

Related Questions

What I want to do here is to be able to assign the values
In a database I want to be able to assign values of a varying
I want to be able to capture the exception that is thrown when a
I have an object with a number of properties. I want to be able
I want to be able to assign different images to my tabs in the
I want to be able to do: For Each thing In things End For
I want to be able to play sound files in my program. Where should
I want to be able to make an HTTP call updating some select boxes
I want to be able to view a SQL Server 2005 Reporting Services report
I want to be able to do this. MyInterface interface = new ServiceProxyHelper<ProxyType>(); Here's

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.