so I am creating a program that has a human bicycle car and truck and they all run on a street but they can collide. so I am confused on how I should write it, the car should be able to collide with the other three objects. I have more code but it seems that it will be to confusing to put all of it up but if it is needed to solve my problem i will post it.

thanks in advanced

/*
 * TCSS 305
 * assignment 2 easystreet
 */
import java.util.Map;

/**
* This program creates a subclass of the vehicle object.
*
* @author Goyofoyo
* @version 1
*/

public class Car extends Vehicle
{
  /**
   * the death time.
   */
  private static final int DEATH_TIME = 0;
  
  /**
   * 
   * @param the_x the x-coordinate.
   * @param the_y the y-coordinate.
   * @param the_terrain the terrain.
   * @param the_dir the direction.
   */
  public Car(final int the_x, final int the_y,
               final Direction the_dir, final Terrain the_terrain)
  {
    super(the_x, the_y, the_dir, the_terrain, DEATH_TIME);
  }
  
  /**
   * @return the direction of the car.
   * @param the_neighbors the_neighbors provides the direction and the terrain. 
   * @param the_light the light terrain.
   */
  public Direction chooseDirection(final Map<Direction, Terrain> the_neighbors,
                                   final Light the_light)
  {
    Direction direction = Direction.random();
    
    if (moveTo(the_neighbors, the_light))
    {
      
      while (direction == getDirection().reverse() || 
          
          (the_neighbors.get(direction) != Terrain.STREET &&

          the_neighbors.get(direction) != Terrain.LIGHT))
      {
        direction = Direction.random();
      }
    }
    
    else
    {
      setDirection(direction.reverse());
    }
    
    return direction;
  }
  /**
   * 
   * @param the_neighbors the_neighbors provides the direction and the terrain.
   * @param the_light the light terrain.
   * @return the terrain.
   */
  public boolean moveTo(final Map<Direction, Terrain> the_neighbors,
                        final Light the_light)
  {
    return the_neighbors.get(getDirection()) == Terrain.STREET ||
    the_neighbors.get(getDirection()) == Terrain.LIGHT ||
    the_neighbors.get(getDirection().left()) == Terrain.STREET ||
    the_neighbors.get(getDirection().left()) == Terrain.LIGHT ||
    the_neighbors.get(getDirection().right()) == Terrain.STREET ||
    the_neighbors.get(getDirection().right()) == Terrain.LIGHT;
  }
  
  /**
   * @param the_terrain the type of terrain the truck can move in.
   * @param the_light the lights it can go threw
   * @return Where the truck can move to, street and threw lights.
   */
  public boolean canPass(final Terrain the_terrain, final Light the_light)
  {
    boolean can_pass = false;
    if (the_terrain == Terrain.STREET)
    {
      can_pass = true;
    }
    else if (the_terrain == Terrain.LIGHT)
    {
      if (the_light == Light.GREEN)
      {
        can_pass = true;
      }
      else if (the_light == Light.YELLOW)
      {
        can_pass = true;
      }
    }
    return can_pass;
    

  }
  public void collide(final Movable the_other)
  {
    
  }
  



}

Recommended Answers

All 2 Replies

What does the rest of your code look like?

Before coding, you need to decide what a "collision" is. Generally a "collision" would be the two objects having the same coordinates at the same time. From a Venn Diagram point of view, if you draw A and B on a piece of paper, A and B collide if they intersect/overlap each other. That would be the most basic scenario, but you could have more complex collisions. Maybe they "collide" if they are within 2 inches of each other or whatever.

But it can get more complicated than that. If you have a timer where you keep track of things and constantly update positions, you could have a situation where they collided BETWEEN the calculations. Think of a train going from L.A. to New York and a train going from New York to L.A. on the same train tracks. They HAVE to collide since there is no way for them to pass each other, but if you simply calculate new coordinates every hour on the hour or whatever, they could collide at the HALF HOUR and you'd never know it if all you ever did is compare coordinates.

If the objects are just considered dots, then that's a whole different situation. Again, you'll have to define collision somehow before you can code.

Once you have that defined, then you can move forward on the actual coding. Since you are using plain old coordinates, might be easiest to have something like "two objects collide if they are within one foot of each other". Take two (x,y) coordinates, calculate the distance between, compare to one foot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.