This site requires JavaScript, please enable it in your browser!
Greenfoot back
LugNut29
LugNut29 wrote ...

2013/11/17

Random velocity help

LugNut29 LugNut29

2013/11/17

#
Hi, I am working on a project and I need to make each of the 5 orbs I create in the OrbWorld have random velocities. With what I have so far, it seems that all the orbs move at the same velocity. Where did I go wrong? Thanks!
public class OrbWorld extends World
{
    /**
     * Constructor for objects of class OrbWorld.
     * 
     */
    public OrbWorld()
    {
        super(600, 400, 1); // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        populateWorld();
    }    
    public void populateWorld()
    {
        Random rn = new Random();
        int x,y;
        for(int i=1;i<=5;i++)
        {
            x = rn.nextInt() % 500;
            y = rn.nextInt() % 300;
            addObject(new Orb(x,y),x,y);
        }
    }
}
danpost danpost

2013/11/17

#
Please post the code for your Orb class.
LugNut29 LugNut29

2013/11/17

#
Here's the orb class:
public class Orb extends Actor
{
    /**
     * Act - do whatever the Orb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
       move();// Move method
       turnIfNeeded();
   }

   private Velocity vel;
   Orb(int xSpeed, int ySpeed)
   {
       vel = new Velocity(xSpeed, ySpeed);
        
   }
    
   public void move()
   {
       int currentx, currenty,xspeed,yspeed;
       currentx = getX();
       currenty = getY();
       xspeed = vel.getXSpeed();
       yspeed = vel.getYSpeed();
       setLocation((currentx + xspeed),(currenty + yspeed));
   }
   
       public void turnAtLeftWall()
    {
        if (getX() - getImage().getWidth()/2 <= 0)
        {
            Greenfoot.playSound("2c.wav");
            vel.reverseX();
        }
    }
    public void turnAtRoof()
    {
        if (getY() - getImage().getHeight()/2 <= 0)
        {
            Greenfoot.playSound("2c.wav");
            vel.reverseY();
        }
    }
    public void turnAtFloor()
    {
        if (getY() + getImage().getHeight()/2 >= getWorld().getHeight())
        {
            Greenfoot.playSound("2c.wav");
            vel.reverseY();
        }
    }
    public void turnAtRightWall()
    {
        if (getX() + getImage().getWidth()/2 >= getWorld().getWidth())
        {
            Greenfoot.playSound("2c.wav");
            vel.reverseX();
        }
    }

    public void turnIfNeeded()
    {
        turnAtLeftWall();
        turnAtRightWall();
        turnAtFloor();
        turnAtRoof();
    }
danpost danpost

2013/11/17

#
Well, it is not that your orbs are getting the same velocity. You are using the same values that you use ('x' and 'y') for its initial location in the world to set the velocity to. You need to get two more random values within the maximum velocity values to set the velocity to in your 'populateWorld' method in your OrbWorld class.
LugNut29 LugNut29

2013/11/17

#
Thanks danpost! I'll look at it and give it a go.
You need to login to post a reply.