Results 1 to 6 of 6
  1. #1
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default random direction

    Hello i have to get my image of the cock roach to move in a random direction. so far i can nly get it to go in a predifined direction, any help that you can give me will be greatfully accepted...

    thank you very much for all your help.




    import javax.swing.*;
    import java.awt.*;
    public class Cockroach extends JFrame implements Runnable {
    private int roachy=100, roachx=0, bsize=50, y=1, x=1;
    JPanel bug = new JPanel();
    private Thread thethread=new Thread(this);
    public Cockroach() {
    setTitle("Cockroach");

    add(bug);
    setSize(700,500);
    setVisible(true);
    thethread.start();

    }
    Image roach = Toolkit.getDefaultToolkit().getImage(getClass().ge tResource("roach.jpg"));
    public void paint(Graphics g) {
    super.paint(g);
    setVisible(true);
    g.setColor(Color.blue);
    g.drawImage(roach, roachx, roachy, this);


    }

    public void run() {

    while(true) {
    if(y==1) roachy=roachy+20;
    else roachy=roachy-10;
    if(y==1 && roachy>=getHeight()-bsize) y=0;
    else if(y==0 && roachy<=0) y=1;
    if(x==1) roachx=roachx+20;
    else roachx=roachx-10;
    if(x==1 && roachx>=getWidth()-bsize) x=0;
    else if(x==0 && roachx<=0) x=1;

    repaint();

    try{
    thethread.sleep(100);
    }catch(InterruptedException e) {}
    }}





    public static void main(String[] args) {
    Cockroach bb=new Cockroach();
    }

    private void sleep(int i) {
    // TODO Auto-generated method stub

    }
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    // do this only once: 
    // pick a random direction
    double direction = (new Random()).nextDouble() * Math.PI * 2;
    // or whatever velocity you want, play with this number
    int velocity = 20;
    // calculate dx and dy
    int dx = (int)(velocity * Math.cos(direction));
    int dy = (int)(velocity * Math.sin(direction));
    now in your thread, instead of doing

    roachx=roachx+20
    or
    roachx=roachx-10

    do


    roachx=roachx+dx
    and
    roachx=roachx-dx

  3. #3
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default random direction

    thank you very much for your help working A1 now thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This way may make your sprite jump all over the place a la Brownian motion. If this is your goal, fine, but if you want it to be less jumpy, you could keep track of the current direction and then use random to add or subtract a "delta" direction to the current direction so that the changes in direction are more gradual. Whatever works best.

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    This way may make your sprite jump all over the place a la Brownian motion
    not the case - you do not I suggest calculating dx and dy only ONCE, right?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Can't get graphics to move opposite direction
    By Nimblyjacks in forum Java Applets
    Replies: 8
    Last Post: 12-22-2009, 02:46 AM
  2. Looking for direction...
    By ewomack in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 12:00 AM
  3. Replies: 3
    Last Post: 12-31-2008, 06:57 AM
  4. [SOLVED] Need direction...
    By hotice1027 in forum New To Java
    Replies: 5
    Last Post: 11-28-2008, 10:03 AM
  5. I need direction too. output control
    By bornwithnoname in forum New To Java
    Replies: 0
    Last Post: 11-23-2008, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •