title: Smooth movement in Java
body: <p>I'm making a simulation in a 3D environment. So far, I have the movements of all the creatures, but it is not "smooth". I've tried quite a few things but was horribly wrong. Now I just have no idea what to do. I was thinking of implementing a vector (not vector class) but don't really know how.</p>
  
  <pre><code>import env3d.EnvObject;
  import java.util.ArrayList;
  
  abstract public class Creature extends EnvObject
  {
  
  /**
   * Constructor for objects of class Creature
   */
  public Creature(double x, double y, double z)
  {
      setX(x);
      setY(y);
      setZ(z);
      setScale(1);
  }
  
  public void move(ArrayList&lt;Creature&gt; creatures, ArrayList&lt;Creature&gt; dead_creatures)
  {                
      double rand = Math.random();
      if (rand &lt; 0.25) {
          setX(getX()+getScale());
          setRotateY(90);
      } else if (rand &lt; 0.5) {
          setX(getX()-getScale());
          setRotateY(270);
      } else if (rand &lt; 0.75) {
          setZ(getZ()+getScale());
          setRotateY(0);
      } else if (rand &lt; 1) {
          setZ(getZ()-getScale());
          setRotateY(180);
      }                
  
      if (getX() &lt; getScale()) setX(getScale());
      if (getX() &gt; 50-getScale()) setX(50 - getScale());
      if (getZ() &lt; getScale()) setZ(getScale());
      if (getZ() &gt; 50-getScale()) setZ(50 - getScale());
  
      // collision detection
      if (this instanceof Fox) {
          for (Creature c : creatures) {
              if (c.distance(this) &lt; c.getScale()+this.getScale() &amp;&amp; c instanceof Tux) {
                  dead_creatures.add(c);
              }
          }
      }
  }        
  </code></pre>
  
  <p>}</p>
  
  <pre><code>import env3d.Env;
  import java.util.ArrayList;
  
  /**
   * A predator and prey simulation.  Fox is the predator and Tux is the prey.
   */
  public class Game
  {
  private Env env;    
  private boolean finished;
  
  private ArrayList&lt;Creature&gt; creatures;
  
  /**
   * Constructor for the Game class. It sets up the foxes and tuxes.
   */
  public Game()
  {
      // we use a separate ArrayList to keep track of each animal. 
      // our room is 50 x 50.
      creatures = new ArrayList&lt;Creature&gt;();
      for (int i = 0; i &lt; 55; i++) {
          if (i &lt; 5) {
              creatures.add(new Fox((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));        
          } else {
              creatures.add(new Tux((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
          }
      }
  }
  
  /**
   * Play the game
   */
  public void play()
  {
  
      finished = false;
  
      // Create the new environment.  Must be done in the same
      // method as the game loop
      env = new Env();
  
      // Make the room 50 x 50.
      env.setRoom(new Room());
  
      // Add all the animals into to the environment for display
      for (Creature c : creatures) {
          env.addObject(c);
      }
  
      // Sets up the camera
      env.setCameraXYZ(25, 50, 55);
      env.setCameraPitch(-63);
  
      // Turn off the default controls
      env.setDefaultControl(false);
  
      // A list to keep track of dead tuxes.
      ArrayList&lt;Creature&gt; dead_creatures = new ArrayList&lt;Creature&gt;();
  
      // The main game loop
      while (!finished) {            
  
          if (env.getKey() == 1)  {
              finished = true;
           }
  
          // Move each fox and tux.
          for (Creature c : creatures) {
              c.move(creatures, dead_creatures);
          }
  
          // Clean up of the dead tuxes.
          for (Creature c : dead_creatures) {
              env.removeObject(c);
              creatures.remove(c);
          }
          // we clear the ArrayList for the next loop.  We could create a new one 
          // every loop but that would be very inefficient.
          dead_creatures.clear();
  
          // Update display
          env.advanceOneFrame();
      }
  
      // Just a little clean up
      env.exit();
  }
  
  
  /**
   * Main method to launch the program.
   */
  public static void main(String args[]) {
      (new Game()).play();
  }
  </code></pre>
  
  <p>}</p>